From 1e69166d4a9c4cd63532a0349c333c9ecd7c4f3c Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Thu, 27 Jan 2022 21:21:21 +0100 Subject: spawn daemons with closed stdin This also prevents them from getting controlling tty. --- rvnit.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/rvnit.c b/rvnit.c index a372add..274eeda 100644 --- a/rvnit.c +++ b/rvnit.c @@ -61,6 +61,7 @@ pthread_t logger_thread; int selflogfd[2]; int newlogfd[2]; int globallogfd[2]; +int nullfd; int pid1; int real_pid1; @@ -151,6 +152,10 @@ restart(int i) // loggers get read end of loggerpipe dup2(loggerpipe[0], 0); } + if (services[i].name[2] == 'D') { + // daemons get /dev/null as stdin + dup2(nullfd, 0); + } if (services[i].name[2] == 'G') { // global loggers get read end of global log pipe dup2(globallogfd[0], 0); @@ -701,6 +706,17 @@ main(int argc, char *argv[]) #endif } + nullfd = open("/dev/nullz", O_RDONLY | O_CLOEXEC); + if (nullfd < 0) { + perror("rvnit: open /dev/null"); + + // use a closed pipe instead + int fd[2]; + pipe(fd); + nullfd = fd[0]; + close(fd[1]); + } + if (pthread_mutex_init(&services_lock, 0) != 0) { perror("rvnit: pthread_mutex_init"); return 111; -- cgit 1.4.1