| 1 | /* Capture output from a subprocess. |
| 2 | Copyright (C) 2017 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <support/capture_subprocess.h> |
| 20 | |
| 21 | #include <errno.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <support/check.h> |
| 24 | #include <support/xunistd.h> |
| 25 | #include <support/xsocket.h> |
| 26 | |
| 27 | static void |
| 28 | transfer (const char *what, struct pollfd *pfd, struct xmemstream *stream) |
| 29 | { |
| 30 | if (pfd->revents != 0) |
| 31 | { |
| 32 | char buf[1024]; |
| 33 | ssize_t ret = TEMP_FAILURE_RETRY (read (pfd->fd, buf, sizeof (buf))); |
| 34 | if (ret < 0) |
| 35 | { |
| 36 | support_record_failure (); |
| 37 | printf ("error: reading from subprocess %s: %m" , what); |
| 38 | pfd->events = 0; |
| 39 | pfd->revents = 0; |
| 40 | } |
| 41 | else if (ret == 0) |
| 42 | { |
| 43 | /* EOF reached. Stop listening. */ |
| 44 | pfd->events = 0; |
| 45 | pfd->revents = 0; |
| 46 | } |
| 47 | else |
| 48 | /* Store the data just read. */ |
| 49 | TEST_VERIFY (fwrite (buf, ret, 1, stream->out) == 1); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | struct support_capture_subprocess |
| 54 | support_capture_subprocess (void (*callback) (void *), void *closure) |
| 55 | { |
| 56 | struct support_capture_subprocess result; |
| 57 | xopen_memstream (&result.out); |
| 58 | xopen_memstream (&result.err); |
| 59 | |
| 60 | int stdout_pipe[2]; |
| 61 | xpipe (stdout_pipe); |
| 62 | int stderr_pipe[2]; |
| 63 | xpipe (stderr_pipe); |
| 64 | |
| 65 | TEST_VERIFY (fflush (stdout) == 0); |
| 66 | TEST_VERIFY (fflush (stderr) == 0); |
| 67 | |
| 68 | pid_t pid = xfork (); |
| 69 | if (pid == 0) |
| 70 | { |
| 71 | xclose (stdout_pipe[0]); |
| 72 | xclose (stderr_pipe[0]); |
| 73 | xdup2 (stdout_pipe[1], STDOUT_FILENO); |
| 74 | xdup2 (stderr_pipe[1], STDERR_FILENO); |
| 75 | callback (closure); |
| 76 | _exit (0); |
| 77 | } |
| 78 | xclose (stdout_pipe[1]); |
| 79 | xclose (stderr_pipe[1]); |
| 80 | |
| 81 | struct pollfd fds[2] = |
| 82 | { |
| 83 | { .fd = stdout_pipe[0], .events = POLLIN }, |
| 84 | { .fd = stderr_pipe[0], .events = POLLIN }, |
| 85 | }; |
| 86 | |
| 87 | do |
| 88 | { |
| 89 | xpoll (fds, 2, -1); |
| 90 | transfer ("stdout" , &fds[0], &result.out); |
| 91 | transfer ("stderr" , &fds[1], &result.err); |
| 92 | } |
| 93 | while (fds[0].events != 0 || fds[1].events != 0); |
| 94 | xclose (stdout_pipe[0]); |
| 95 | xclose (stderr_pipe[0]); |
| 96 | |
| 97 | xfclose_memstream (&result.out); |
| 98 | xfclose_memstream (&result.err); |
| 99 | xwaitpid (pid, &result.status, 0); |
| 100 | return result; |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | support_capture_subprocess_free (struct support_capture_subprocess *p) |
| 105 | { |
| 106 | free (p->out.buffer); |
| 107 | free (p->err.buffer); |
| 108 | } |
| 109 | |