<p>Friendly Automation <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/18525">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span></span><br></pre><div style="white-space:pre-wrap">Approvals:
George Joseph: Looks good to me, approved
Friendly Automation: Approved for Submit
</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">test: Add ability to capture child process output<br><br>ASTERISK-30037<br><br>Change-Id: Icbf84ce05addb197a458361c35d784e460d8d6c2<br>---<br>M include/asterisk/test.h<br>M main/Makefile<br>M main/test.c<br>3 files changed, 318 insertions(+), 0 deletions(-)<br><br></pre>
<pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/include/asterisk/test.h b/include/asterisk/test.h</span><br><span>index 78d9788..12aed65 100644</span><br><span>--- a/include/asterisk/test.h</span><br><span>+++ b/include/asterisk/test.h</span><br><span>@@ -209,6 +209,27 @@</span><br><span> struct ast_test;</span><br><span> </span><br><span> /*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief A capture of running an external process.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This contains a buffer holding stdout, another containing stderr,</span><br><span style="color: hsl(120, 100%, 40%);">+ * the process id of the child, and its exit code.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+struct ast_test_capture {</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! \brief buffer holding stdout */</span><br><span style="color: hsl(120, 100%, 40%);">+ char *outbuf;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! \brief length of buffer holding stdout */</span><br><span style="color: hsl(120, 100%, 40%);">+ size_t outlen;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! \brief buffer holding stderr */</span><br><span style="color: hsl(120, 100%, 40%);">+ char *errbuf;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! \brief length of buffer holding stderr */</span><br><span style="color: hsl(120, 100%, 40%);">+ size_t errlen;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! \brief process id of child */</span><br><span style="color: hsl(120, 100%, 40%);">+ pid_t pid;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! \brief exit code of child */</span><br><span style="color: hsl(120, 100%, 40%);">+ int exitcode;</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span> * \brief Contains all the initialization information required to store a new test definition</span><br><span> */</span><br><span> struct ast_test_info {</span><br><span>@@ -417,5 +438,40 @@</span><br><span> } \</span><br><span> })</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Release the storage (buffers) associated with capturing</span><br><span style="color: hsl(120, 100%, 40%);">+ * the output of an external child process.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \since 19.4.0</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param capture The structure describing the child process and its</span><br><span style="color: hsl(120, 100%, 40%);">+ * associated output.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void ast_test_capture_free(struct ast_test_capture *capture);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Run a child process and capture its output and exit code.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \!since 19.4.0</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param capture The structure describing the child process and its</span><br><span style="color: hsl(120, 100%, 40%);">+ * associated output.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param file The name of the file to execute (uses $PATH to locate).</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param argv The NULL-terminated array of arguments to pass to the</span><br><span style="color: hsl(120, 100%, 40%);">+ * child process, starting with the command name itself.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param data The buffer of input to be sent to child process's stdin;</span><br><span style="color: hsl(120, 100%, 40%);">+ * optional and may be NULL.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param datalen The length of the buffer, if not NULL, otherwise zero.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval 1 for success</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval other failure</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+int ast_test_capture_command(struct ast_test_capture *capture, const char *file, char *const argv[], const char *data, unsigned datalen);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> #endif /* TEST_FRAMEWORK */</span><br><span> #endif /* _AST_TEST_H */</span><br><span>diff --git a/main/Makefile b/main/Makefile</span><br><span>index 9f31a3a..ac47423 100644</span><br><span>--- a/main/Makefile</span><br><span>+++ b/main/Makefile</span><br><span>@@ -167,6 +167,9 @@</span><br><span> options.o: _ASTCFLAGS+=$(call get_menuselect_cflags,REF_DEBUG)</span><br><span> sched.o: _ASTCFLAGS+=$(call get_menuselect_cflags,DEBUG_SCHEDULER DUMP_SCHEDULER)</span><br><span> tcptls.o: _ASTCFLAGS+=$(OPENSSL_INCLUDE) -Wno-deprecated-declarations</span><br><span style="color: hsl(120, 100%, 40%);">+# since we're using open_memstream(), we need to release the buffer with</span><br><span style="color: hsl(120, 100%, 40%);">+# the native free() function or we might get unexpected behavior.</span><br><span style="color: hsl(120, 100%, 40%);">+test.o: _ASTCFLAGS+=-DASTMM_LIBC=ASTMM_IGNORE</span><br><span> uuid.o: _ASTCFLAGS+=$(UUID_INCLUDE)</span><br><span> stasis.o: _ASTCFLAGS+=$(call get_menuselect_cflags,AO2_DEBUG)</span><br><span> time.o: _ASTCFLAGS+=-D_XOPEN_SOURCE=700</span><br><span>diff --git a/main/test.c b/main/test.c</span><br><span>index 5135803..747262c 100644</span><br><span>--- a/main/test.c</span><br><span>+++ b/main/test.c</span><br><span>@@ -48,6 +48,16 @@</span><br><span> #include "asterisk/astobj2.h"</span><br><span> #include "asterisk/stasis.h"</span><br><span> #include "asterisk/json.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/app.h" /* for ast_replace_sigchld(), etc. */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#include <stdio.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <errno.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <fcntl.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <string.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <sys/types.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <sys/stat.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <sys/wait.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <signal.h></span><br><span> </span><br><span> /*! \since 12</span><br><span> * \brief The topic for test suite messages</span><br><span>@@ -100,6 +110,42 @@</span><br><span> TEST_NAME_CATEGORY = 2,</span><br><span> };</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+#define zfclose(fp) \</span><br><span style="color: hsl(120, 100%, 40%);">+ ({ if (fp != NULL) { \</span><br><span style="color: hsl(120, 100%, 40%);">+ fclose(fp); \</span><br><span style="color: hsl(120, 100%, 40%);">+ fp = NULL; \</span><br><span style="color: hsl(120, 100%, 40%);">+ } \</span><br><span style="color: hsl(120, 100%, 40%);">+ (void)0; \</span><br><span style="color: hsl(120, 100%, 40%);">+ })</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define zclose(fd) \</span><br><span style="color: hsl(120, 100%, 40%);">+ ({ if (fd != -1) { \</span><br><span style="color: hsl(120, 100%, 40%);">+ close(fd); \</span><br><span style="color: hsl(120, 100%, 40%);">+ fd = -1; \</span><br><span style="color: hsl(120, 100%, 40%);">+ } \</span><br><span style="color: hsl(120, 100%, 40%);">+ (void)0; \</span><br><span style="color: hsl(120, 100%, 40%);">+ })</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define movefd(oldfd, newfd) \</span><br><span style="color: hsl(120, 100%, 40%);">+ ({ if (oldfd != newfd) { \</span><br><span style="color: hsl(120, 100%, 40%);">+ dup2(oldfd, newfd); \</span><br><span style="color: hsl(120, 100%, 40%);">+ close(oldfd); \</span><br><span style="color: hsl(120, 100%, 40%);">+ oldfd = -1; \</span><br><span style="color: hsl(120, 100%, 40%);">+ } \</span><br><span style="color: hsl(120, 100%, 40%);">+ (void)0; \</span><br><span style="color: hsl(120, 100%, 40%);">+ })</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define lowerfd(oldfd) \</span><br><span style="color: hsl(120, 100%, 40%);">+ ({ int newfd = dup(oldfd); \</span><br><span style="color: hsl(120, 100%, 40%);">+ if (newfd > oldfd) \</span><br><span style="color: hsl(120, 100%, 40%);">+ close(newfd); \</span><br><span style="color: hsl(120, 100%, 40%);">+ else { \</span><br><span style="color: hsl(120, 100%, 40%);">+ close(oldfd); \</span><br><span style="color: hsl(120, 100%, 40%);">+ oldfd = newfd; \</span><br><span style="color: hsl(120, 100%, 40%);">+ } \</span><br><span style="color: hsl(120, 100%, 40%);">+ (void)0; \</span><br><span style="color: hsl(120, 100%, 40%);">+ })</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /*! List of registered test definitions */</span><br><span> static AST_LIST_HEAD_STATIC(tests, ast_test);</span><br><span> </span><br><span>@@ -267,6 +313,207 @@</span><br><span> test->state = state;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+void ast_test_capture_free(struct ast_test_capture *capture)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ if (capture) {</span><br><span style="color: hsl(120, 100%, 40%);">+ free(capture->outbuf);</span><br><span style="color: hsl(120, 100%, 40%);">+ capture->outbuf = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ free(capture->errbuf);</span><br><span style="color: hsl(120, 100%, 40%);">+ capture->errbuf = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ capture->pid = -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ capture->exitcode = -1;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+int ast_test_capture_command(struct ast_test_capture *capture, const char *file, char *const argv[], const char *data, unsigned datalen)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ int fd0[2] = { -1, -1 }, fd1[2] = { -1, -1 }, fd2[2] = { -1, -1 };</span><br><span style="color: hsl(120, 100%, 40%);">+ pid_t pid = -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ int status = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ memset(capture, 0, sizeof(*capture));</span><br><span style="color: hsl(120, 100%, 40%);">+ capture->pid = capture->exitcode = -1;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (data != NULL && datalen > 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (pipe(fd0) == -1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Couldn't open stdin pipe: %s\n", strerror(errno));</span><br><span style="color: hsl(120, 100%, 40%);">+ goto cleanup;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ fcntl(fd0[1], F_SETFL, fcntl(fd0[1], F_GETFL, 0) | O_NONBLOCK);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ if ((fd0[0] = open("/dev/null", O_RDONLY)) == -1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Couldn't open /dev/null: %s\n", strerror(errno));</span><br><span style="color: hsl(120, 100%, 40%);">+ goto cleanup;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (pipe(fd1) == -1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Couldn't open stdout pipe: %s\n", strerror(errno));</span><br><span style="color: hsl(120, 100%, 40%);">+ goto cleanup;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (pipe(fd2) == -1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Couldn't open stdout pipe: %s\n", strerror(errno));</span><br><span style="color: hsl(120, 100%, 40%);">+ goto cleanup;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* we don't want anyone else reaping our children */</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_replace_sigchld();</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if ((pid = fork()) == -1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Failed to fork(): %s\n", strerror(errno));</span><br><span style="color: hsl(120, 100%, 40%);">+ goto cleanup;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (pid == 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ fclose(stdin);</span><br><span style="color: hsl(120, 100%, 40%);">+ zclose(fd0[1]);</span><br><span style="color: hsl(120, 100%, 40%);">+ zclose(fd1[0]);</span><br><span style="color: hsl(120, 100%, 40%);">+ zclose(fd2[0]);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ movefd(fd0[0], 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ movefd(fd1[1], 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ movefd(fd2[1], 2);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ execvp(file, argv);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Failed to execv(): %s\n", strerror(errno));</span><br><span style="color: hsl(120, 100%, 40%);">+ exit(1);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ FILE *cmd = NULL, *out = NULL, *err = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ char buf[BUFSIZ];</span><br><span style="color: hsl(120, 100%, 40%);">+ int wstatus, n, nfds;</span><br><span style="color: hsl(120, 100%, 40%);">+ fd_set readfds, writefds;</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned i;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ zclose(fd0[0]);</span><br><span style="color: hsl(120, 100%, 40%);">+ zclose(fd1[1]);</span><br><span style="color: hsl(120, 100%, 40%);">+ zclose(fd2[1]);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ lowerfd(fd0[1]);</span><br><span style="color: hsl(120, 100%, 40%);">+ lowerfd(fd1[0]);</span><br><span style="color: hsl(120, 100%, 40%);">+ lowerfd(fd2[0]);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if ((cmd = fmemopen(buf, sizeof(buf), "w")) == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Failed to open memory buffer: %s\n", strerror(errno));</span><br><span style="color: hsl(120, 100%, 40%);">+ kill(pid, SIGKILL);</span><br><span style="color: hsl(120, 100%, 40%);">+ goto cleanup;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ for (i = 0; argv[i] != NULL; ++i) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (i > 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ fputc(' ', cmd);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ fputs(argv[i], cmd);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ zfclose(cmd);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_TRACE, "run: %.*s\n", (int)sizeof(buf), buf);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if ((out = open_memstream(&capture->outbuf, &capture->outlen)) == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Failed to open output buffer: %s\n", strerror(errno));</span><br><span style="color: hsl(120, 100%, 40%);">+ kill(pid, SIGKILL);</span><br><span style="color: hsl(120, 100%, 40%);">+ goto cleanup;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if ((err = open_memstream(&capture->errbuf, &capture->errlen)) == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Failed to open error buffer: %s\n", strerror(errno));</span><br><span style="color: hsl(120, 100%, 40%);">+ kill(pid, SIGKILL);</span><br><span style="color: hsl(120, 100%, 40%);">+ goto cleanup;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ while (1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ n = waitpid(pid, &wstatus, WNOHANG);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (n == pid && WIFEXITED(wstatus)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ zclose(fd0[1]);</span><br><span style="color: hsl(120, 100%, 40%);">+ zclose(fd1[0]);</span><br><span style="color: hsl(120, 100%, 40%);">+ zclose(fd2[0]);</span><br><span style="color: hsl(120, 100%, 40%);">+ zfclose(out);</span><br><span style="color: hsl(120, 100%, 40%);">+ zfclose(err);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ capture->pid = pid;</span><br><span style="color: hsl(120, 100%, 40%);">+ capture->exitcode = WEXITSTATUS(wstatus);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_TRACE, "run: pid %d exits %d\n", capture->pid, capture->exitcode);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* a function that does the opposite of ffs()</span><br><span style="color: hsl(120, 100%, 40%);">+ * would be handy here for finding the highest</span><br><span style="color: hsl(120, 100%, 40%);">+ * descriptor number.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+ nfds = MAX(fd0[1], MAX(fd1[0], fd2[0])) + 1;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ FD_ZERO(&readfds);</span><br><span style="color: hsl(120, 100%, 40%);">+ FD_ZERO(&writefds);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (fd0[1] != -1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (data != NULL && datalen > 0)</span><br><span style="color: hsl(120, 100%, 40%);">+ FD_SET(fd0[1], &writefds);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (fd1[0] != -1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ FD_SET(fd1[0], &readfds);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (fd2[0] != -1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ FD_SET(fd2[0], &readfds);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* not clear that exception fds are meaningful</span><br><span style="color: hsl(120, 100%, 40%);">+ * with non-network descriptors.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+ n = select(nfds, &readfds, &writefds, NULL, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (FD_ISSET(fd0[1], &writefds)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ n = write(fd0[1], data, datalen);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (n > 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ data += n;</span><br><span style="color: hsl(120, 100%, 40%);">+ datalen -= MIN(datalen, n);</span><br><span style="color: hsl(120, 100%, 40%);">+ /* out of data, so close stdin */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (datalen == 0)</span><br><span style="color: hsl(120, 100%, 40%);">+ zclose(fd0[1]);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ zclose(fd0[1]);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (FD_ISSET(fd1[0], &readfds)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ n = read(fd1[0], buf, sizeof(buf));</span><br><span style="color: hsl(120, 100%, 40%);">+ if (n > 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ fwrite(buf, sizeof(char), n, out);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ zclose(fd1[0]);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (FD_ISSET(fd2[0], &readfds)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ n = read(fd2[0], buf, sizeof(buf));</span><br><span style="color: hsl(120, 100%, 40%);">+ if (n > 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ fwrite(buf, sizeof(char), n, err);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ zclose(fd2[0]);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ status = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+cleanup:</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_unreplace_sigchld();</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ zfclose(cmd);</span><br><span style="color: hsl(120, 100%, 40%);">+ zfclose(out);</span><br><span style="color: hsl(120, 100%, 40%);">+ zfclose(err);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ zclose(fd0[1]);</span><br><span style="color: hsl(120, 100%, 40%);">+ zclose(fd1[0]);</span><br><span style="color: hsl(120, 100%, 40%);">+ zclose(fd1[1]);</span><br><span style="color: hsl(120, 100%, 40%);">+ zclose(fd2[0]);</span><br><span style="color: hsl(120, 100%, 40%);">+ zclose(fd2[1]);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return status;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /*</span><br><span> * These are the Java reserved words we need to munge so Jenkins</span><br><span> * doesn't barf on them.</span><br><span>@@ -1242,3 +1489,4 @@</span><br><span> </span><br><span> return 0;</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/18525">change 18525</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/18525"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: Icbf84ce05addb197a458361c35d784e460d8d6c2 </div>
<div style="display:none"> Gerrit-Change-Number: 18525 </div>
<div style="display:none"> Gerrit-PatchSet: 15 </div>
<div style="display:none"> Gerrit-Owner: Philip Prindeville <philipp@redfish-solutions.com> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@sangoma.com> </div>
<div style="display:none"> Gerrit-Reviewer: N A <mail@interlinked.x10host.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>