/* Open a pipe connected to a child process. * * write system read * parent -> fd[1] -> STDIN_FILENO -> child if pipe_stdin * parent <- fd[0] <- STDOUT_FILENO <- child if pipe_stdout * read system write * * At least one of pipe_stdin, pipe_stdout must be true. * pipe_stdin and prog_stdin together determine the child's standard input. * pipe_stdout and prog_stdout together determine the child's standard output. * If pipe_stdin is true, prog_stdin is ignored. * If pipe_stdout is true, prog_stdout is ignored. */ static pid_t create_pipe (const char *progname, const char *prog_path, char **prog_argv, bool pipe_stdin, bool pipe_stdout, const char *prog_stdin, const char *prog_stdout, bool null_stderr, bool slave_process, bool exit_on_error, int fd[2]) { #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ /* Native Windows API. This uses _pipe(), dup2(), and spawnv(). It could also be implemented using the low-level functions CreatePipe(), DuplicateHandle(), CreateProcess() and _open_osfhandle(); see the GNU make and GNU clisp and cvs source code. */ int ifd[2]; int ofd[2]; int orig_stdin; int orig_stdout; int orig_stderr; int child; int nulloutfd; int stdinfd; int stdoutfd; int saved_errno; /* FIXME: Need to free memory allocated by prepare_spawn. */ prog_argv = prepare_spawn (prog_argv); if (pipe_stdout) if (pipe2_safer (ifd, O_BINARY | O_CLOEXEC) < 0) error (EXIT_FAILURE, errno, _("cannot create pipe")); if (pipe_stdin) if (pipe2_safer (ofd, O_BINARY | O_CLOEXEC) < 0) error (EXIT_FAILURE, errno, _("cannot create pipe")); /* Data flow diagram: * * write system read * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout * read system write * */ /* Save standard file handles of parent process. */ if (pipe_stdin || prog_stdin != NULL) orig_stdin = dup_safer_noinherit (STDIN_FILENO); if (pipe_stdout || prog_stdout != NULL) orig_stdout = dup_safer_noinherit (STDOUT_FILENO); if (null_stderr) orig_stderr = dup_safer_noinherit (STDERR_FILENO); child = -1; /* Create standard file handles of child process. */ nulloutfd = -1; stdinfd = -1; stdoutfd = -1; if ((!pipe_stdin || dup2 (ofd[0], STDIN_FILENO) >= 0) && (!pipe_stdout || dup2 (ifd[1], STDOUT_FILENO) >= 0) && (!null_stderr || ((nulloutfd = open ("NUL", O_RDWR, 0)) >= 0 && (nulloutfd == STDERR_FILENO || (dup2 (nulloutfd, STDERR_FILENO) >= 0 && close (nulloutfd) >= 0)))) && (pipe_stdin || prog_stdin == NULL || ((stdinfd = open (prog_stdin, O_RDONLY, 0)) >= 0 && (stdinfd == STDIN_FILENO || (dup2 (stdinfd, STDIN_FILENO) >= 0 && close (stdinfd) >= 0)))) && (pipe_stdout || prog_stdout == NULL || ((stdoutfd = open (prog_stdout, O_WRONLY, 0)) >= 0 && (stdoutfd == STDOUT_FILENO || (dup2 (stdoutfd, STDOUT_FILENO) >= 0 && close (stdoutfd) >= 0))))) /* The child process doesn't inherit ifd[0], ifd[1], ofd[0], ofd[1], but it inherits all open()ed or dup2()ed file handles (which is what we want in the case of STD*_FILENO). */ /* Use spawnvpe and pass the environment explicitly. This is needed if the program has modified the environment using putenv() or [un]setenv(). On Windows, programs have two environments, one in the "environment block" of the process and managed through SetEnvironmentVariable(), and one inside the process, in the location retrieved by the 'environ' macro. When using spawnvp() without 'e', the child process inherits a copy of the environment block - ignoring the effects of putenv() and [un]setenv(). */ { child = spawnvpe (P_NOWAIT, prog_path, (const char **) prog_argv, (const char **) environ); if (child < 0 && errno == ENOEXEC) { /* prog is not a native executable. Try to execute it as a shell script. Note that prepare_spawn() has already prepended a hidden element "sh.exe" to prog_argv. */ --prog_argv; child = spawnvpe (P_NOWAIT, prog_argv[0], (const char **) prog_argv, (const char **) environ); } } if (child == -1) saved_errno = errno; if (stdinfd >= 0) close (stdinfd); if (stdoutfd >= 0) close (stdoutfd); if (nulloutfd >= 0) close (nulloutfd); /* Restore standard file handles of parent process. */ if (null_stderr) undup_safer_noinherit (orig_stderr, STDERR_FILENO); if (pipe_stdout || prog_stdout != NULL) undup_safer_noinherit (orig_stdout, STDOUT_FILENO); if (pipe_stdin || prog_stdin != NULL) undup_safer_noinherit (orig_stdin, STDIN_FILENO); if (pipe_stdin) close (ofd[0]); if (pipe_stdout) close (ifd[1]); if (child == -1) { if (exit_on_error || !null_stderr) error (exit_on_error ? EXIT_FAILURE : 0, saved_errno, _("%s subprocess failed"), progname); if (pipe_stdout) close (ifd[0]); if (pipe_stdin) close (ofd[1]); errno = saved_errno; return -1; } if (pipe_stdout) fd[0] = ifd[0]; if (pipe_stdin) fd[1] = ofd[1]; return child; #else /* Unix API. */ int ifd[2]; int ofd[2]; sigset_t blocked_signals; posix_spawn_file_actions_t actions; bool actions_allocated; posix_spawnattr_t attrs; bool attrs_allocated; int err; pid_t child; if (pipe_stdout) if (pipe_safer (ifd) < 0) error (EXIT_FAILURE, errno, _("cannot create pipe")); if (pipe_stdin) if (pipe_safer (ofd) < 0) error (EXIT_FAILURE, errno, _("cannot create pipe")); /* Data flow diagram: * * write system read * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout * read system write * */ if (slave_process) { sigprocmask (SIG_SETMASK, NULL, &blocked_signals); block_fatal_signals (); } actions_allocated = false; attrs_allocated = false; if ((err = posix_spawn_file_actions_init (&actions)) != 0 || (actions_allocated = true, (pipe_stdin && (err = posix_spawn_file_actions_adddup2 (&actions, ofd[0], STDIN_FILENO)) != 0) || (pipe_stdout && (err = posix_spawn_file_actions_adddup2 (&actions, ifd[1], STDOUT_FILENO)) != 0) || (pipe_stdin && (err = posix_spawn_file_actions_addclose (&actions, ofd[0])) != 0) || (pipe_stdout && (err = posix_spawn_file_actions_addclose (&actions, ifd[1])) != 0) || (pipe_stdin && (err = posix_spawn_file_actions_addclose (&actions, ofd[1])) != 0) || (pipe_stdout && (err = posix_spawn_file_actions_addclose (&actions, ifd[0])) != 0) || (null_stderr && (err = posix_spawn_file_actions_addopen (&actions, STDERR_FILENO, "/dev/null", O_RDWR, 0)) != 0) || (!pipe_stdin && prog_stdin != NULL && (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, prog_stdin, O_RDONLY, 0)) != 0) || (!pipe_stdout && prog_stdout != NULL && (err = posix_spawn_file_actions_addopen (&actions, STDOUT_FILENO, prog_stdout, O_WRONLY, 0)) != 0) || (slave_process && ((err = posix_spawnattr_init (&attrs)) != 0 || (attrs_allocated = true, (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0 || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0))) )) { if (actions_allocated) posix_spawn_file_actions_destroy (&actions); if (attrs_allocated) posix_spawnattr_destroy (&attrs); if (slave_process) unblock_fatal_signals (); if (exit_on_error || !null_stderr) error (exit_on_error ? EXIT_FAILURE : 0, err, _("%s subprocess failed"), progname); if (pipe_stdout) { close (ifd[0]); close (ifd[1]); } if (pipe_stdin) { close (ofd[0]); close (ofd[1]); } errno = err; return -1; } posix_spawn_file_actions_destroy (&actions); if (attrs_allocated) posix_spawnattr_destroy (&attrs); if (slave_process) { register_slave_subprocess (child); unblock_fatal_signals (); } if (pipe_stdin) close (ofd[0]); if (pipe_stdout) close (ifd[1]); if (pipe_stdout) fd[0] = ifd[0]; if (pipe_stdin) fd[1] = ofd[1]; return child; #endif }
/* Execute a command, optionally redirecting any of the three standard file descriptors to /dev/null. Return its exit code. If it didn't terminate correctly, exit if exit_on_error is true, otherwise return 127. If slave_process is true, the child process will be terminated when its creator receives a catchable fatal signal. */ int execute (const char *progname, const char *prog_path, char **prog_argv, bool ignore_sigpipe, bool null_stdin, bool null_stdout, bool null_stderr, bool slave_process, bool exit_on_error, int *termsigp) { #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ /* Native Windows API. */ int orig_stdin; int orig_stdout; int orig_stderr; int exitcode; int nullinfd; int nulloutfd; /* FIXME: Need to free memory allocated by prepare_spawn. */ prog_argv = prepare_spawn (prog_argv); /* Save standard file handles of parent process. */ if (null_stdin) orig_stdin = dup_safer_noinherit (STDIN_FILENO); if (null_stdout) orig_stdout = dup_safer_noinherit (STDOUT_FILENO); if (null_stderr) orig_stderr = dup_safer_noinherit (STDERR_FILENO); exitcode = -1; /* Create standard file handles of child process. */ nullinfd = -1; nulloutfd = -1; if ((!null_stdin || ((nullinfd = open ("NUL", O_RDONLY, 0)) >= 0 && (nullinfd == STDIN_FILENO || (dup2 (nullinfd, STDIN_FILENO) >= 0 && close (nullinfd) >= 0)))) && (!(null_stdout || null_stderr) || ((nulloutfd = open ("NUL", O_RDWR, 0)) >= 0 && (!null_stdout || nulloutfd == STDOUT_FILENO || dup2 (nulloutfd, STDOUT_FILENO) >= 0) && (!null_stderr || nulloutfd == STDERR_FILENO || dup2 (nulloutfd, STDERR_FILENO) >= 0) && ((null_stdout && nulloutfd == STDOUT_FILENO) || (null_stderr && nulloutfd == STDERR_FILENO) || close (nulloutfd) >= 0)))) /* Use spawnvpe and pass the environment explicitly. This is needed if the program has modified the environment using putenv() or [un]setenv(). On Windows, programs have two environments, one in the "environment block" of the process and managed through SetEnvironmentVariable(), and one inside the process, in the location retrieved by the 'environ' macro. When using spawnvp() without 'e', the child process inherits a copy of the environment block - ignoring the effects of putenv() and [un]setenv(). */ { exitcode = spawnvpe (P_WAIT, prog_path, (const char **) prog_argv, (const char **) environ); if (exitcode < 0 && errno == ENOEXEC) { /* prog is not a native executable. Try to execute it as a shell script. Note that prepare_spawn() has already prepended a hidden element "sh.exe" to prog_argv. */ --prog_argv; exitcode = spawnvpe (P_WAIT, prog_argv[0], (const char **) prog_argv, (const char **) environ); } } if (nulloutfd >= 0) close (nulloutfd); if (nullinfd >= 0) close (nullinfd); /* Restore standard file handles of parent process. */ if (null_stderr) undup_safer_noinherit (orig_stderr, STDERR_FILENO); if (null_stdout) undup_safer_noinherit (orig_stdout, STDOUT_FILENO); if (null_stdin) undup_safer_noinherit (orig_stdin, STDIN_FILENO); if (termsigp != NULL) *termsigp = 0; if (exitcode == -1) { if (exit_on_error || !null_stderr) error (exit_on_error ? EXIT_FAILURE : 0, errno, _("%s subprocess failed"), progname); return 127; } return exitcode; #else /* Unix API. */ /* Note about 127: Some errors during posix_spawnp() cause the function posix_spawnp() to return an error code; some other errors cause the subprocess to exit with return code 127. It is implementation dependent which error is reported which way. We treat both cases as equivalent. */ sigset_t blocked_signals; posix_spawn_file_actions_t actions; bool actions_allocated; posix_spawnattr_t attrs; bool attrs_allocated; int err; pid_t child; if (slave_process) { sigprocmask (SIG_SETMASK, NULL, &blocked_signals); block_fatal_signals (); } actions_allocated = false; attrs_allocated = false; if ((err = posix_spawn_file_actions_init (&actions)) != 0 || (actions_allocated = true, (null_stdin && (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, "/dev/null", O_RDONLY, 0)) != 0) || (null_stdout && (err = posix_spawn_file_actions_addopen (&actions, STDOUT_FILENO, "/dev/null", O_RDWR, 0)) != 0) || (null_stderr && (err = posix_spawn_file_actions_addopen (&actions, STDERR_FILENO, "/dev/null", O_RDWR, 0)) != 0) || (slave_process && ((err = posix_spawnattr_init (&attrs)) != 0 || (attrs_allocated = true, (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0 || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0))) || (err = posix_spawnp (&child, prog_path, &actions, attrs_allocated ? &attrs : NULL, prog_argv, environ)) != 0)) { if (actions_allocated) posix_spawn_file_actions_destroy (&actions); if (attrs_allocated) posix_spawnattr_destroy (&attrs); if (slave_process) unblock_fatal_signals (); if (termsigp != NULL) *termsigp = 0; if (exit_on_error || !null_stderr) error (exit_on_error ? EXIT_FAILURE : 0, err, _("%s subprocess failed"), progname); return 127; } posix_spawn_file_actions_destroy (&actions); if (attrs_allocated) posix_spawnattr_destroy (&attrs); if (slave_process) { register_slave_subprocess (child); unblock_fatal_signals (); } return wait_subprocess (child, progname, ignore_sigpipe, null_stderr, slave_process, exit_on_error, termsigp); #endif }
/* Open a pipe for input from a child process. * The child's stdin comes from a file. * * read system write * parent <- fd[0] <- STDOUT_FILENO <- child * */ pid_t create_pipe_in (const char *progname, const char *prog_path, char **prog_argv, const char *prog_stdin, bool null_stderr, bool slave_process, bool exit_on_error, int fd[1]) { #if defined _MSC_VER || defined __MINGW32__ /* Native Woe32 API. This uses _pipe(), dup2(), and spawnv(). It could also be implemented using the low-level functions CreatePipe(), DuplicateHandle(), CreateProcess() and _open_osfhandle(); see the GNU make and GNU clisp and cvs source code. */ int ifd[2]; int orig_stdin; int orig_stdout; int orig_stderr; int child; int nulloutfd; int stdinfd; prog_argv = prepare_spawn (prog_argv); if (_pipe (ifd, 4096, O_BINARY | O_NOINHERIT) < 0) error (EXIT_FAILURE, errno, _("cannot create pipe")); /* Data flow diagram: * * read system write * parent <- ifd[0] <- ifd[1] <- child */ /* Save standard file handles of parent process. */ if (prog_stdin != NULL) orig_stdin = dup_noinherit (STDIN_FILENO); orig_stdout = dup_noinherit (STDOUT_FILENO); if (null_stderr) orig_stderr = dup_noinherit (STDERR_FILENO); child = -1; /* Create standard file handles of child process. */ nulloutfd = -1; stdinfd = -1; if (dup2 (ifd[1], STDOUT_FILENO) >= 0 && (!null_stderr || ((nulloutfd = open ("NUL", O_RDWR, 0)) >= 0 && (nulloutfd == STDERR_FILENO || (dup2 (nulloutfd, STDERR_FILENO) >= 0 && close (nulloutfd) >= 0)))) && (prog_stdin == NULL || ((stdinfd = open (prog_stdin, O_RDONLY, 0)) >= 0 && (stdinfd == STDIN_FILENO || (dup2 (stdinfd, STDIN_FILENO) >= 0 && close (stdinfd) >= 0))))) /* The child process doesn't inherit ifd[0] and ifd[1], but it inherits all open()ed or dup2()ed file handles (which is what we want in the case of STD*_FILENO) and also orig_stdin, orig_stdout, orig_stderr (which is not explicitly wanted but harmless). */ child = spawnvp (P_NOWAIT, prog_path, prog_argv); if (stdinfd >= 0) close (stdinfd); if (nulloutfd >= 0) close (nulloutfd); /* Restore standard file handles of parent process. */ if (null_stderr) dup2 (orig_stderr, STDERR_FILENO), close (orig_stderr); dup2 (orig_stdout, STDOUT_FILENO), close (orig_stdout); if (prog_stdin != NULL) dup2 (orig_stdin, STDIN_FILENO), close (orig_stdin); close (ifd[1]); if (child == -1) { if (exit_on_error || !null_stderr) error (exit_on_error ? EXIT_FAILURE : 0, errno, _("%s subprocess failed"), progname); close (ifd[0]); return -1; } fd[0] = ifd[0]; return child; #else /* Unix API. */ int ifd[2]; #if HAVE_POSIX_SPAWN sigset_t blocked_signals; posix_spawn_file_actions_t actions; bool actions_allocated; posix_spawnattr_t attrs; bool attrs_allocated; int err; pid_t child; #else int child; #endif if (pipe (ifd) < 0) error (EXIT_FAILURE, errno, _("cannot create pipe")); /* Data flow diagram: * * read system write * parent <- ifd[0] <- ifd[1] <- child */ #if HAVE_POSIX_SPAWN if (slave_process) { sigprocmask (SIG_SETMASK, NULL, &blocked_signals); block_fatal_signals (); } actions_allocated = false; attrs_allocated = false; if ((err = posix_spawn_file_actions_init (&actions)) != 0 || (actions_allocated = true, (err = posix_spawn_file_actions_adddup2 (&actions, ifd[1], STDOUT_FILENO)) != 0 || (err = posix_spawn_file_actions_addclose (&actions, ifd[1])) != 0 || (err = posix_spawn_file_actions_addclose (&actions, ifd[0])) != 0 || (null_stderr && (err = posix_spawn_file_actions_addopen (&actions, STDERR_FILENO, "/dev/null", O_RDWR, 0)) != 0) || (prog_stdin != NULL && (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, prog_stdin, O_RDONLY, 0)) != 0) || (slave_process && ((err = posix_spawnattr_init (&attrs)) != 0 || (attrs_allocated = true, (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0 || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0))) || (err = posix_spawnp (&child, prog_path, &actions, attrs_allocated ? &attrs : NULL, prog_argv, environ)) != 0)) { if (actions_allocated) posix_spawn_file_actions_destroy (&actions); if (attrs_allocated) posix_spawnattr_destroy (&attrs); if (slave_process) unblock_fatal_signals (); if (exit_on_error || !null_stderr) error (exit_on_error ? EXIT_FAILURE : 0, err, _("%s subprocess failed"), progname); close (ifd[0]); close (ifd[1]); return -1; } posix_spawn_file_actions_destroy (&actions); if (attrs_allocated) posix_spawnattr_destroy (&attrs); #else if (slave_process) block_fatal_signals (); /* Use vfork() instead of fork() for efficiency. */ if ((child = vfork ()) == 0) { /* Child process code. */ int nulloutfd; int stdinfd; if (dup2 (ifd[1], STDOUT_FILENO) >= 0 && close (ifd[1]) >= 0 && close (ifd[0]) >= 0 && (!null_stderr || ((nulloutfd = open ("/dev/null", O_RDWR, 0)) >= 0 && (nulloutfd == STDERR_FILENO || (dup2 (nulloutfd, STDERR_FILENO) >= 0 && close (nulloutfd) >= 0)))) && (prog_stdin == NULL || ((stdinfd = open (prog_stdin, O_RDONLY, 0)) >= 0 && (stdinfd == STDIN_FILENO || (dup2 (stdinfd, STDIN_FILENO) >= 0 && close (stdinfd) >= 0)))) && (!slave_process || (unblock_fatal_signals (), true))) execvp (prog_path, prog_argv); _exit (127); } if (child == -1) { if (slave_process) unblock_fatal_signals (); if (exit_on_error || !null_stderr) error (exit_on_error ? EXIT_FAILURE : 0, errno, _("%s subprocess failed"), progname); close (ifd[0]); close (ifd[1]); return -1; } #endif if (slave_process) { register_slave_subprocess (child); unblock_fatal_signals (); } close (ifd[1]); fd[0] = ifd[0]; return child; #endif }
/* Execute a command, optionally redirecting any of the three standard file descriptors to /dev/null. Return its exit code. If it didn't terminate correctly, exit if exit_on_error is true, otherwise return 127. If slave_process is true, the child process will be terminated when its creator receives a catchable fatal signal. */ int execute (const char *progname, const char *prog_path, char **prog_argv, bool ignore_sigpipe, bool null_stdin, bool null_stdout, bool null_stderr, bool slave_process, bool exit_on_error) { #if defined _MSC_VER || defined __MINGW32__ /* Native Woe32 API. */ int orig_stdin; int orig_stdout; int orig_stderr; int exitcode; int nullinfd; int nulloutfd; prog_argv = prepare_spawn (prog_argv); /* Save standard file handles of parent process. */ if (null_stdin) orig_stdin = dup_noinherit (STDIN_FILENO); if (null_stdout) orig_stdout = dup_noinherit (STDOUT_FILENO); if (null_stderr) orig_stderr = dup_noinherit (STDERR_FILENO); exitcode = -1; /* Create standard file handles of child process. */ nullinfd = -1; nulloutfd = -1; if ((!null_stdin || ((nullinfd = open ("NUL", O_RDONLY, 0)) >= 0 && (nullinfd == STDIN_FILENO || (dup2 (nullinfd, STDIN_FILENO) >= 0 && close (nullinfd) >= 0)))) && (!(null_stdout || null_stderr) || ((nulloutfd = open ("NUL", O_RDWR, 0)) >= 0 && (!null_stdout || nulloutfd == STDOUT_FILENO || dup2 (nulloutfd, STDOUT_FILENO) >= 0) && (!null_stderr || nulloutfd == STDERR_FILENO || dup2 (nulloutfd, STDERR_FILENO) >= 0) && ((null_stdout && nulloutfd == STDOUT_FILENO) || (null_stderr && nulloutfd == STDERR_FILENO) || close (nulloutfd) >= 0)))) exitcode = spawnvp (P_WAIT, prog_path, prog_argv); if (nulloutfd >= 0) close (nulloutfd); if (nullinfd >= 0) close (nullinfd); /* Restore standard file handles of parent process. */ if (null_stderr) dup2 (orig_stderr, STDERR_FILENO), close (orig_stderr); if (null_stdout) dup2 (orig_stdout, STDOUT_FILENO), close (orig_stdout); if (null_stdin) dup2 (orig_stdin, STDIN_FILENO), close (orig_stdin); if (exitcode == -1) { if (exit_on_error || !null_stderr) error (exit_on_error ? EXIT_FAILURE : 0, errno, _("%s subprocess failed"), progname); return 127; } return exitcode; #else /* Unix API. */ /* Note about 127: Some errors during posix_spawnp() cause the function posix_spawnp() to return an error code; some other errors cause the subprocess to exit with return code 127. It is implementation dependent which error is reported which way. We treat both cases as equivalent. */ #if HAVE_POSIX_SPAWN sigset_t blocked_signals; posix_spawn_file_actions_t actions; bool actions_allocated; posix_spawnattr_t attrs; bool attrs_allocated; int err; pid_t child; #else int child; #endif #if HAVE_POSIX_SPAWN if (slave_process) { sigprocmask (SIG_SETMASK, NULL, &blocked_signals); block_fatal_signals (); } actions_allocated = false; attrs_allocated = false; if ((err = posix_spawn_file_actions_init (&actions)) != 0 || (actions_allocated = true, (null_stdin && (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, "/dev/null", O_RDONLY, 0)) != 0) || (null_stdout && (err = posix_spawn_file_actions_addopen (&actions, STDOUT_FILENO, "/dev/null", O_RDWR, 0)) != 0) || (null_stderr && (err = posix_spawn_file_actions_addopen (&actions, STDERR_FILENO, "/dev/null", O_RDWR, 0)) != 0) || (slave_process && ((err = posix_spawnattr_init (&attrs)) != 0 || (attrs_allocated = true, (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0 || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0))) || (err = posix_spawnp (&child, prog_path, &actions, attrs_allocated ? &attrs : NULL, prog_argv, environ)) != 0)) { if (actions_allocated) posix_spawn_file_actions_destroy (&actions); if (attrs_allocated) posix_spawnattr_destroy (&attrs); if (slave_process) unblock_fatal_signals (); if (exit_on_error || !null_stderr) error (exit_on_error ? EXIT_FAILURE : 0, err, _("%s subprocess failed"), progname); return 127; } posix_spawn_file_actions_destroy (&actions); if (attrs_allocated) posix_spawnattr_destroy (&attrs); #else if (slave_process) block_fatal_signals (); /* Use vfork() instead of fork() for efficiency. */ if ((child = vfork ()) == 0) { /* Child process code. */ int nullinfd; int nulloutfd; if ((!null_stdin || ((nullinfd = open ("/dev/null", O_RDONLY, 0)) >= 0 && (nullinfd == STDIN_FILENO || (dup2 (nullinfd, STDIN_FILENO) >= 0 && close (nullinfd) >= 0)))) && (!(null_stdout || null_stderr) || ((nulloutfd = open ("/dev/null", O_RDWR, 0)) >= 0 && (!null_stdout || nulloutfd == STDOUT_FILENO || dup2 (nulloutfd, STDOUT_FILENO) >= 0) && (!null_stderr || nulloutfd == STDERR_FILENO || dup2 (nulloutfd, STDERR_FILENO) >= 0) && ((null_stdout && nulloutfd == STDOUT_FILENO) || (null_stderr && nulloutfd == STDERR_FILENO) || close (nulloutfd) >= 0))) && (!slave_process || (unblock_fatal_signals (), true))) execvp (prog_path, prog_argv); _exit (127); } if (child == -1) { if (slave_process) unblock_fatal_signals (); if (exit_on_error || !null_stderr) error (exit_on_error ? EXIT_FAILURE : 0, errno, _("%s subprocess failed"), progname); return 127; } #endif if (slave_process) { register_slave_subprocess (child); unblock_fatal_signals (); } return wait_subprocess (child, progname, ignore_sigpipe, null_stderr, slave_process, exit_on_error); #endif }