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