spawn-pipe.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* Creation of subprocesses, communicating via pipes.
  2. Copyright (C) 2001-2003, 2006, 2008-2013 Free Software Foundation, Inc.
  3. Written by Bruno Haible <haible@clisp.cons.org>, 2001.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program 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
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef _SPAWN_PIPE_H
  15. #define _SPAWN_PIPE_H
  16. /* Get pid_t. */
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #include <sys/types.h>
  20. #include <stdbool.h>
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /* All these functions create a subprocess and don't wait for its termination.
  25. They return the process id of the subprocess. They also return in fd[]
  26. one or two file descriptors for communication with the subprocess.
  27. If the subprocess creation fails: if exit_on_error is true, the main
  28. process exits with an error message; otherwise, an error message is given
  29. if null_stderr is false, then -1 is returned, with errno set, and fd[]
  30. remain uninitialized.
  31. After finishing communication, the caller should call wait_subprocess()
  32. to get rid of the subprocess in the process table.
  33. If slave_process is true, the child process will be terminated when its
  34. creator receives a catchable fatal signal or exits normally. If
  35. slave_process is false, the child process will continue running in this
  36. case, until it is lucky enough to attempt to communicate with its creator
  37. and thus get a SIGPIPE signal.
  38. If exit_on_error is false, a child process id of -1 should be treated the
  39. same way as a subprocess which accepts no input, produces no output and
  40. terminates with exit code 127. Why? Some errors during posix_spawnp()
  41. cause the function posix_spawnp() to return an error code; some other
  42. errors cause the subprocess to exit with return code 127. It is
  43. implementation dependent which error is reported which way. The caller
  44. must treat both cases as equivalent.
  45. It is recommended that no signal is blocked or ignored (i.e. have a
  46. signal handler with value SIG_IGN) while any of these functions is called.
  47. The reason is that child processes inherit the mask of blocked signals
  48. from their parent (both through posix_spawn() and fork()/exec());
  49. likewise, signals ignored in the parent are also ignored in the child
  50. (except possibly for SIGCHLD). And POSIX:2001 says [in the description
  51. of exec()]:
  52. "it should be noted that many existing applications wrongly
  53. assume that they start with certain signals set to the default
  54. action and/or unblocked. In particular, applications written
  55. with a simpler signal model that does not include blocking of
  56. signals, such as the one in the ISO C standard, may not behave
  57. properly if invoked with some signals blocked. Therefore, it is
  58. best not to block or ignore signals across execs without explicit
  59. reason to do so, and especially not to block signals across execs
  60. of arbitrary (not closely co-operating) programs." */
  61. /* Open a pipe for output to a child process.
  62. * The child's stdout goes to a file.
  63. *
  64. * write system read
  65. * parent -> fd[0] -> STDIN_FILENO -> child
  66. *
  67. * Note: When writing to a child process, it is useful to ignore the SIGPIPE
  68. * signal and the EPIPE error code.
  69. */
  70. extern pid_t create_pipe_out (const char *progname,
  71. const char *prog_path, char **prog_argv,
  72. const char *prog_stdout, bool null_stderr,
  73. bool slave_process, bool exit_on_error,
  74. int fd[1]);
  75. /* Open a pipe for input from a child process.
  76. * The child's stdin comes from a file.
  77. *
  78. * read system write
  79. * parent <- fd[0] <- STDOUT_FILENO <- child
  80. *
  81. */
  82. extern pid_t create_pipe_in (const char *progname,
  83. const char *prog_path, char **prog_argv,
  84. const char *prog_stdin, bool null_stderr,
  85. bool slave_process, bool exit_on_error,
  86. int fd[1]);
  87. /* Open a bidirectional pipe.
  88. *
  89. * write system read
  90. * parent -> fd[1] -> STDIN_FILENO -> child
  91. * parent <- fd[0] <- STDOUT_FILENO <- child
  92. * read system write
  93. *
  94. * Note: When writing to a child process, it is useful to ignore the SIGPIPE
  95. * signal and the EPIPE error code.
  96. *
  97. * Note: The parent process must be careful to avoid deadlock.
  98. * 1) If you write more than PIPE_MAX bytes or, more generally, if you write
  99. * more bytes than the subprocess can handle at once, the subprocess
  100. * may write its data and wait on you to read it, but you are currently
  101. * busy writing.
  102. * 2) When you don't know ahead of time how many bytes the subprocess
  103. * will produce, the usual technique of calling read (fd, buf, BUFSIZ)
  104. * with a fixed BUFSIZ will, on Linux 2.2.17 and on BSD systems, cause
  105. * the read() call to block until *all* of the buffer has been filled.
  106. * But the subprocess cannot produce more data until you gave it more
  107. * input. But you are currently busy reading from it.
  108. */
  109. extern pid_t create_pipe_bidi (const char *progname,
  110. const char *prog_path, char **prog_argv,
  111. bool null_stderr,
  112. bool slave_process, bool exit_on_error,
  113. int fd[2]);
  114. /* The name of the "always silent" device. */
  115. #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  116. /* Native Windows API. */
  117. # define DEV_NULL "NUL"
  118. #else
  119. /* Unix API. */
  120. # define DEV_NULL "/dev/null"
  121. #endif
  122. #ifdef __cplusplus
  123. }
  124. #endif
  125. #endif /* _SPAWN_PIPE_H */