Simply closing the stdout and/or stderr file descriptors for
the child process can have bad side effects if for example
the child writes to stdout/stderr after open()ing a file.
The open() call might have returned the same file descriptor
one would usually expect for stdout/stderr (1 and 2), thereby
causing mis-directed writes.
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Issue #190
libzfs_run_process(const char *path, char *argv[], int flags)
{
pid_t pid;
- int rc;
+ int rc, devnull_fd;
pid = vfork();
if (pid == 0) {
+ devnull_fd = open("/dev/null", O_WRONLY);
+
+ if (devnull_fd < 0)
+ _exit(-1);
+
if (!(flags & STDOUT_VERBOSE))
- close(STDOUT_FILENO);
+ (void) dup2(devnull_fd, STDOUT_FILENO);
if (!(flags & STDERR_VERBOSE))
- close(STDERR_FILENO);
+ (void) dup2(devnull_fd, STDERR_FILENO);
+
+ close(devnull_fd);
(void) execvp(path, argv);
_exit(-1);