diff --git a/src/shared/helpers.h b/src/shared/helpers.h index 00150d117..ba95b916a 100644 --- a/src/shared/helpers.h +++ b/src/shared/helpers.h @@ -29,6 +29,7 @@ #include #include #include +#include #define ERRX do { fprintf (stderr, "out of memory\n"); exit (1); } while (0) @@ -284,4 +285,16 @@ RC_UNUSED static char *env_findvar(const char *name, char **envp) return NULL; } +RC_UNUSED static int xopen_nonblock(const char *filename, int flags, mode_t mode) +{ + int fd = open(filename, flags | O_NONBLOCK, mode); + if (fd >= 0 && fcntl(F_SETFL, fd, flags & ~O_NONBLOCK) < 0) { + int saved_errno = errno; + close(fd); + fd = -1; + errno = saved_errno; + } + return fd; +} + #endif diff --git a/src/start-stop-daemon/start-stop-daemon.c b/src/start-stop-daemon/start-stop-daemon.c index fdcd90326..ca846657e 100644 --- a/src/start-stop-daemon/start-stop-daemon.c +++ b/src/start-stop-daemon/start-stop-daemon.c @@ -1086,17 +1086,17 @@ int main(int argc, char **argv) stdout_fd = devnull_fd; stderr_fd = devnull_fd; if (redirect_stdin) { - if ((stdin_fd = open(redirect_stdin, - O_RDONLY)) == -1) - eerrorx("%s: unable to open the input file" + if ((stdin_fd = xopen_nonblock(redirect_stdin, + O_RDONLY, 0)) == -1) + eerrorx("%s: unable to redirect the input file" " for stdin `%s': %s", applet, redirect_stdin, strerror(errno)); } if (redirect_stdout) { - if ((stdout_fd = open(redirect_stdout, + if ((stdout_fd = xopen_nonblock(redirect_stdout, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)) == -1) - eerrorx("%s: unable to open the logfile" + eerrorx("%s: unable to redirect the logfile" " for stdout `%s': %s", applet, redirect_stdout, strerror(errno)); }else if (stdout_process) { @@ -1107,10 +1107,10 @@ int main(int argc, char **argv) applet, stdout_process, strerror(errno)); } if (redirect_stderr) { - if ((stderr_fd = open(redirect_stderr, + if ((stderr_fd = xopen_nonblock(redirect_stderr, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)) == -1) - eerrorx("%s: unable to open the logfile" + eerrorx("%s: unable to redirect the logfile" " for stderr `%s': %s", applet, redirect_stderr, strerror(errno)); }else if (stderr_process) { diff --git a/src/supervise-daemon/supervise-daemon.c b/src/supervise-daemon/supervise-daemon.c index d896a6953..15b18930b 100644 --- a/src/supervise-daemon/supervise-daemon.c +++ b/src/supervise-daemon/supervise-daemon.c @@ -567,17 +567,16 @@ RC_NORETURN static void child_process(char *exec, char **argv) stdout_fd = devnull_fd; stderr_fd = devnull_fd; if (redirect_stdin) { - if ((stdin_fd = open(redirect_stdin, - O_RDONLY)) == -1) - eerrorx("%s: unable to open the input file" + if ((stdin_fd = xopen_nonblock(redirect_stdin, O_RDONLY, 0)) == -1) + eerrorx("%s: unable to redirect the input file" " for stdin `%s': %s", applet, redirect_stdin, strerror(errno)); } if (redirect_stdout) { - if ((stdout_fd = open(redirect_stdout, + if ((stdout_fd = xopen_nonblock(redirect_stdout, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR)) == -1) - eerrorx("%s: unable to open the logfile" + eerrorx("%s: unable to redirect the logfile" " for stdout `%s': %s", applet, redirect_stdout, strerror(errno)); } else if (stdout_process) { @@ -588,10 +587,10 @@ RC_NORETURN static void child_process(char *exec, char **argv) applet, stdout_process, strerror(errno)); } if (redirect_stderr) { - if ((stderr_fd = open(redirect_stderr, + if ((stderr_fd = xopen_nonblock(redirect_stderr, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR)) == -1) - eerrorx("%s: unable to open the logfile" + eerrorx("%s: unable to redirect the logfile" " for stderr `%s': %s", applet, redirect_stderr, strerror(errno)); } else if (stderr_process) {