Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/shared/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

#define ERRX do { fprintf (stderr, "out of memory\n"); exit (1); } while (0)

Expand Down Expand Up @@ -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
14 changes: 7 additions & 7 deletions src/start-stop-daemon/start-stop-daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
13 changes: 6 additions & 7 deletions src/supervise-daemon/supervise-daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Loading