version 1.336, 2020/10/03 08:30:47 |
version 1.337, 2020/10/03 09:22:26 |
|
|
{ |
{ |
char *shell; |
char *shell; |
pid_t pid; |
pid_t pid; |
int devnull, status; |
int status; |
|
|
if ((shell = getenv("SHELL")) == NULL) |
if ((shell = getenv("SHELL")) == NULL) |
shell = _PATH_BSHELL; |
shell = _PATH_BSHELL; |
|
|
shell, strerror(errno)); |
shell, strerror(errno)); |
} |
} |
|
|
/* Need this to redirect subprocess stdin/out */ |
|
if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) |
|
fatal("open(/dev/null): %s", strerror(errno)); |
|
|
|
debug("Executing command: '%.500s'", cmd); |
debug("Executing command: '%.500s'", cmd); |
|
|
/* Fork and execute the command. */ |
/* Fork and execute the command. */ |
if ((pid = fork()) == 0) { |
if ((pid = fork()) == 0) { |
char *argv[4]; |
char *argv[4]; |
|
|
/* Redirect child stdin and stdout. Leave stderr */ |
if (stdfd_devnull(1, 1, 0) == -1) |
if (dup2(devnull, STDIN_FILENO) == -1) |
fatal("%s: stdfd_devnull failed", __func__); |
fatal("dup2: %s", strerror(errno)); |
|
if (dup2(devnull, STDOUT_FILENO) == -1) |
|
fatal("dup2: %s", strerror(errno)); |
|
if (devnull > STDERR_FILENO) |
|
close(devnull); |
|
closefrom(STDERR_FILENO + 1); |
closefrom(STDERR_FILENO + 1); |
|
|
argv[0] = shell; |
argv[0] = shell; |
|
|
/* Parent. */ |
/* Parent. */ |
if (pid == -1) |
if (pid == -1) |
fatal("%s: fork: %.100s", __func__, strerror(errno)); |
fatal("%s: fork: %.100s", __func__, strerror(errno)); |
|
|
close(devnull); |
|
|
|
while (waitpid(pid, &status, 0) == -1) { |
while (waitpid(pid, &status, 0) == -1) { |
if (errno != EINTR && errno != EAGAIN) |
if (errno != EINTR && errno != EAGAIN) |