- if (devnull < 0) {
- err("open /dev/null failed");
- exit(1);
- }
- dup2(devnull, STDIN_FILENO);
-
- if (outpipe[1] > 0)
- dup2(outpipe[1], STDOUT_FILENO);
- else
- dup2(devnull, STDOUT_FILENO);
-
- if (errpipe[1] > 0)
- dup2(errpipe[1], STDERR_FILENO);
- else
- dup2(devnull, STDERR_FILENO);
-
- close(devnull);
- execv(arg, argv);
+ if (devnull > 0) {
+ dup2(devnull, STDIN_FILENO);
+ if (outpipe[WRITE_END] < 0)
+ dup2(devnull, STDOUT_FILENO);
+ if (errpipe[WRITE_END] < 0)
+ dup2(devnull, STDERR_FILENO);
+ close(devnull);
+ } else
+ err("open /dev/null failed: %s", strerror(errno));
+ if (outpipe[WRITE_END] > 0)
+ dup2(outpipe[WRITE_END], STDOUT_FILENO);
+ if (errpipe[WRITE_END] > 0)
+ dup2(errpipe[WRITE_END], STDERR_FILENO);
+ execv(argv[0], argv);