support_capture_subprocess.c 3.2 KB

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