From 064d4024e4126c2ae791f55586de5ff12552bf54 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Fri, 17 Aug 2018 14:25:33 -0600 Subject: [PATCH] apps/examples/popen: Add a test of popen/pclose. apps/system/popen: Fix some errors found using the new popen/pclose test. --- examples/popen/.gitignore | 11 ++ examples/popen/Kconfig | 31 ++++++ examples/popen/Make.defs | 39 +++++++ examples/popen/Makefile | 56 ++++++++++ examples/popen/popen_main.c | 199 ++++++++++++++++++++++++++++++++++++ system/popen/popen.c | 8 +- 6 files changed, 343 insertions(+), 1 deletion(-) create mode 100644 examples/popen/.gitignore create mode 100644 examples/popen/Kconfig create mode 100644 examples/popen/Make.defs create mode 100644 examples/popen/Makefile create mode 100644 examples/popen/popen_main.c diff --git a/examples/popen/.gitignore b/examples/popen/.gitignore new file mode 100644 index 000000000..fa1ec7579 --- /dev/null +++ b/examples/popen/.gitignore @@ -0,0 +1,11 @@ +/Make.dep +/.depend +/.built +/*.asm +/*.obj +/*.rel +/*.lst +/*.sym +/*.adb +/*.lib +/*.src diff --git a/examples/popen/Kconfig b/examples/popen/Kconfig new file mode 100644 index 000000000..5551c5a84 --- /dev/null +++ b/examples/popen/Kconfig @@ -0,0 +1,31 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config EXAMPLES_POPEN + bool "popen() example" + default n + depends on SYSTEM_POPEN + ---help--- + Enable the popen() example + +if EXAMPLES_POPEN + +config EXAMPLES_POPEN_PROGNAME + string "Program name" + default "popen" + depends on BUILD_KERNEL + ---help--- + This is the name of the program that will be use when the NSH ELF + program is installed. + +config EXAMPLES_POPEN_PRIORITY + int "Popen task priority" + default 100 + +config EXAMPLES_POPEN_STACKSIZE + int "Popen stack size" + default 2048 + +endif diff --git a/examples/popen/Make.defs b/examples/popen/Make.defs new file mode 100644 index 000000000..cdb6a0fc9 --- /dev/null +++ b/examples/popen/Make.defs @@ -0,0 +1,39 @@ +############################################################################ +# apps/examples/popen/Make.defs +# Adds selected applications to apps/ build +# +# Copyright (C) 2018 Gregory Nutt. All rights reserved. +# Author: Gregory Nutt +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name NuttX nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +ifeq ($(CONFIG_EXAMPLES_POPEN),y) +CONFIGURED_APPS += examples/popen +endif diff --git a/examples/popen/Makefile b/examples/popen/Makefile new file mode 100644 index 000000000..b8265ecd3 --- /dev/null +++ b/examples/popen/Makefile @@ -0,0 +1,56 @@ +############################################################################ +# apps/examples/popen/Makefile +# +# Copyright (C) 2018 Gregory Nutt. All rights reserved. +# Author: Gregory Nutt +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name NuttX nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +-include $(TOPDIR)/Make.defs + +# popen() built-in application info + +CONFIG_EXAMPLES_POPEN_PRIORITY ?= SCHED_PRIORITY_DEFAULT +CONFIG_EXAMPLES_POPEN_STACKSIZE ?= 2048 + +APPNAME = popen +PRIORITY = $(CONFIG_EXAMPLES_POPEN_PRIORITY) +STACKSIZE = $(CONFIG_EXAMPLES_POPEN_STACKSIZE) + +# popen() Example + +ASRCS = +CSRCS = +MAINSRC = popen_main.c + +CONFIG_EXAMPLES_POPEN_PROGNAME ?= popen$(EXEEXT) +PROGNAME = $(CONFIG_EXAMPLES_POPEN_PROGNAME) + +include $(APPDIR)/Application.mk diff --git a/examples/popen/popen_main.c b/examples/popen/popen_main.c new file mode 100644 index 000000000..baf1ab348 --- /dev/null +++ b/examples/popen/popen_main.c @@ -0,0 +1,199 @@ +/**************************************************************************** + * examples/popen/popen_main.c + * + * Copyright (C) 2018 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: popen_main + ****************************************************************************/ + +#ifdef CONFIG_BUILD_KERNEL +int main(int argc, FAR char *argv[]) +#else +int popen_main(int argc, char *argv[]) +#endif +{ + struct itimerspec value; + struct sigevent ev; + FILE *stream; + timer_t timerid; + char buffer[256]; + size_t nread; + int exitcode; + int ret; + + /* Create a POSIX timer */ + + ev.sigev_notify = SIGEV_SIGNAL; + ev.sigev_signo = SIGALRM; + ev.sigev_value.sival_int = 0; +#ifdef CONFIG_SIG_EVTHREAD + ev.sigev_notify_function = NULL; + ev.sigev_notify_attributes = NULL; +#endif + + ret = timer_create(CLOCK_REALTIME, &ev, &timerid); + if (ret < 0) + { + fprintf(stderr, "ERROR: timer_create() failed: %d\n", errno); + return EXIT_FAILURE; + } + + /* Ask for help from the NSH shell */ + + printf("Calling popen(\"help\")\n"); + + stream = popen("help", "r"); + if (stream == NULL) + { + fprintf(stderr, "ERROR: popen() failed: %d\n", errno); + (void)timer_delete(timerid); + return EXIT_FAILURE; + } + + /* Loop until the shell stops sending data */ + + for (; ; ) + { + /* Set up a signal to wake-up the fread if there is no data */ + + value.it_value.tv_sec = 2; + value.it_value.tv_nsec = 0; + value.it_interval.tv_sec = 2; + value.it_interval.tv_nsec = 0; + + ret = timer_settime(timerid, 0, &value, NULL); + if (ret < 0) + { + fprintf(stderr, "ERROR: timer_settime() failed: %d\n", errno); + exitcode = EXIT_FAILURE; + break; + } + + /* Read data from the shell */ + + nread = fread(buffer, 1, 256, stream); + + /* Cancel the timer by setting the timeout to zero. */ + + value.it_value.tv_sec = 0; + value.it_value.tv_nsec = 0; + value.it_interval.tv_sec = 0; + value.it_interval.tv_nsec = 0; + + ret = timer_settime(timerid, 0, &value, NULL); + if (ret < 0) + { + fprintf(stderr, "ERROR: timer_settime() failed: %d\n", errno); + exitcode = EXIT_FAILURE; + break; + } + + /* Check for read errors */ + + if (nread == 0) + { + /* Did an error occur? */ + + if (ferror(stream)) + { + int errcode = errno; + + if (errcode == EINTR) + { + printf("Timeout... end of data\n"); + exitcode = EXIT_SUCCESS; + } + else + { + fprintf(stderr, "ERROR: fread() failed: %d\n", errcode); + exitcode = EXIT_FAILURE; + } + + break; + } + else + { + /* No.. must be EOF */ + + printf("End of data\n"); + exitcode = EXIT_SUCCESS; + break; + } + } + + /* Dump what we read from the shell */ + + fwrite(buffer, 1, nread, stdout); + } + + printf("Calling pclose()\n"); + + ret = pclose(stream); + if (ret < 0) + { + int errcode = errno; + + /* ECHILD is not really an error. It simply means that the child + * thread has already exited and exit status is not available. + */ + + if (errcode == ECHILD) + { + printf("The shell has already exited (and exit status is not available)\n"); + } + else + { + fprintf(stderr, "ERROR: pclose() failed: %d\n", errcode); + exitcode = EXIT_FAILURE; + } + } + + (void)timer_delete(timerid); + return exitcode; +} diff --git a/system/popen/popen.c b/system/popen/popen.c index 4dd8cff16..07af5acec 100644 --- a/system/popen/popen.c +++ b/system/popen/popen.c @@ -247,7 +247,7 @@ FILE *popen(FAR const char *command, FAR const char *mode) /* Redirect input or output as determined by the mode parameter */ - errcode = posix_spawn_file_actions_adddup2(&file_actions, oldfd, newfd); + errcode = posix_spawn_file_actions_adddup2(&file_actions, newfd, oldfd); if (errcode != 0) { goto errout_with_actions; @@ -267,6 +267,12 @@ FILE *popen(FAR const char *command, FAR const char *mode) goto errout_with_actions; } + /* We can close the 'newfd' now. It is no longer useful on this side of + * the interface. + */ + + (void)close(newfd); + /* Free attributes and file actions. Ignoring return values in the case * of an error. */