From 64a8018cb760aac27cc48bbff1ce6d67a61a18f9 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Fri, 7 Jan 2022 17:54:11 +0100 Subject: simplify CLOEXEC handling --- rvnit.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/rvnit.c b/rvnit.c index 77cd4bd..f8f634f 100644 --- a/rvnit.c +++ b/rvnit.c @@ -98,6 +98,8 @@ restart(int i) if (services[i].logfd[0] == -1) { pipe(services[i].logfd); fcntl(services[i].logfd[0], F_SETFL, O_NONBLOCK); + fcntl(services[i].logfd[0], F_SETFD, FD_CLOEXEC); + fcntl(services[i].logfd[1], F_SETFD, FD_CLOEXEC); write(newlogfd[1], "!", 1); } @@ -109,6 +111,8 @@ restart(int i) if (services[j].logfd[0] == -1) { // hook up logger pipe(loggerpipe); + fcntl(loggerpipe[0], F_SETFD, FD_CLOEXEC); + fcntl(loggerpipe[1], F_SETFD, FD_CLOEXEC); services[j].logfd[0] = loggerpipe[0]; services[j].logfd[1] = loggerpipe[1]; services[j].logged = 1; @@ -129,22 +133,22 @@ restart(int i) pid_t child = fork(); if (child == 0) { - dup2(services[i].logfd[1], 1); - if (loggerpipe[0] != -1) { // loggers get read end of loggerpipe + // note that all dup2 below will remove CLOEXEC + + if (services[i].name[2] == 'L') { + // loggers get read end of loggerpipe dup2(loggerpipe[0], 0); - close(loggerpipe[0]); - close(loggerpipe[1]); } if (services[i].name[2] == 'G') { // global loggers get read end of global log pipe dup2(globallogfd[0], 0); - // global loggers write to stderr, to avoid lopos + // global loggers write to stderr, to avoid loops dup2(2, 1); + } else { + // pass write end of logger pipe to capture stdout + dup2(services[i].logfd[1], 1); } - - close(services[i].logfd[0]); - close(services[i].logfd[1]); sleep(delay); setsid(); execl(services[i].name, @@ -152,12 +156,6 @@ restart(int i) (char *)0); exit(-1); } else if (child > 0) { - fcntl(services[i].logfd[0], F_SETFD, FD_CLOEXEC); - fcntl(services[i].logfd[1], F_SETFD, FD_CLOEXEC); - if (loggerpipe[0] != -1) { - fcntl(loggerpipe[0], F_SETFD, FD_CLOEXEC); - fcntl(loggerpipe[1], F_SETFD, FD_CLOEXEC); - } services[i].pid = services[i].display_pid = child; services[i].start = now; } else { -- cgit 1.4.1