Skip to content

Commit

Permalink
Fix #120: Redirect stdin to /dev/null for services by default
Browse files Browse the repository at this point in the history
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
  • Loading branch information
troglobit committed Apr 11, 2020
1 parent ff24ee3 commit 770a79f
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/service.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,23 @@ static int service_timeout_cancel(svc_t *svc)
return err;
}

/*
* Redirect stdin to /dev/null => all reads by process = EOF
* https://www.freedesktop.org/software/systemd/man/systemd.exec.html#Logging%20and%20Standard%20Input/Output
*/
static int stdin_redirect(void)
{
int fd;

fd = open("/dev/null", O_RDONLY | O_APPEND);
if (-1 != fd) {
dup2(fd, STDIN_FILENO);
return close(fd);
}

return -1;
}

/*
* Redirect output to a file, e.g., /dev/null, or /dev/console
*/
Expand Down Expand Up @@ -238,8 +255,11 @@ static int redirect(svc_t *svc)
close(svc->stdin_fd);
dup2(STDIN_FILENO, STDOUT_FILENO);
dup2(STDIN_FILENO, STDERR_FILENO);
} else

return 0;
}
#endif
stdin_redirect();

if (svc->log.enabled) {
if (svc->log.null)
Expand Down

0 comments on commit 770a79f

Please sign in to comment.