From: Remi Tricot-Le Breton Date: Tue, 28 Oct 2025 17:00:43 +0000 (+0100) Subject: MINOR: init: Make devnullfd global and create it earlier in init X-Git-Tag: v3.3-dev11~20 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1ec59d3426f1bab9924aae940406f9f6faeb9290;p=thirdparty%2Fhaproxy.git MINOR: init: Make devnullfd global and create it earlier in init The devnull fd might be needed during configuration parsing, if some options require to fork/exec for instance. So we now create it much earlier in the init process and without depending on the '-q' or '-d' parameters. --- diff --git a/include/haproxy/global.h b/include/haproxy/global.h index 26cba4ac4..b4a7a406b 100644 --- a/include/haproxy/global.h +++ b/include/haproxy/global.h @@ -53,6 +53,7 @@ extern char *progname; extern char **old_argv; extern const char *old_unixsocket; extern int daemon_fd[2]; +extern int devnullfd; struct proxy; struct server; diff --git a/src/haproxy.c b/src/haproxy.c index 1a43ae6e0..28e9c1dea 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -149,6 +149,7 @@ int pid; /* current process id */ char **init_env; /* to keep current process env variables backup */ int pidfd = -1; /* FD to keep PID */ int daemon_fd[2] = {-1, -1}; /* pipe to communicate with parent process */ +int devnullfd = -1; static unsigned long stopping_tgroup_mask; /* Thread groups acknowledging stopping */ @@ -3159,7 +3160,6 @@ static void set_identity(const char *program_name) int main(int argc, char **argv) { - int devnullfd = -1; struct rlimit limit; int intovf = (unsigned char)argc + 1; /* let the compiler know it's strictly positive */ struct cfgfile *cfg, *cfg_tmp; @@ -3289,6 +3289,18 @@ int main(int argc, char **argv) */ set_verbosity(); + /* We might need to use this devnullfd during configuration parsing. */ + devnullfd = open("/dev/null", O_RDWR, 0); + if (devnullfd < 0) { + ha_alert("Cannot open /dev/null\n"); + exit(EXIT_FAILURE); + } + if (fcntl(devnullfd, FD_CLOEXEC) != 0) { + ha_alert("Cannot make /dev/null CLOEXEC\n"); + close(devnullfd); + exit(EXIT_FAILURE); + } + /* Add entries for master and worker in proc_list, create sockpair, * that will be copied to both processes after master-worker fork to * enable the master CLI at worker side (worker can send messages to master), @@ -3427,18 +3439,6 @@ int main(int argc, char **argv) } /* End of initialization for standalone and worker modes */ - if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { - devnullfd = open("/dev/null", O_RDWR, 0); - if (devnullfd < 0) { - ha_alert("Cannot open /dev/null\n"); - exit(EXIT_FAILURE); - } - if (fcntl(devnullfd, FD_CLOEXEC) != 0) { - ha_alert("Cannot make /dev/null CLOEXEC\n"); - close(devnullfd); - exit(EXIT_FAILURE); - } - } /* applies the renice value in the worker or standalone after configuration parsing * but before changing identity */