]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: init: Make devnullfd global and create it earlier in init
authorRemi Tricot-Le Breton <rlebreton@haproxy.com>
Tue, 28 Oct 2025 17:00:43 +0000 (18:00 +0100)
committerWilliam Lallemand <wlallemand@haproxy.com>
Wed, 29 Oct 2025 09:54:17 +0000 (10:54 +0100)
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.

include/haproxy/global.h
src/haproxy.c

index 26cba4ac4fd31c01009e54a30c01cc7caebdbb86..b4a7a406bc29c41c8c2f1d1ebd87ef220f6cc670 100644 (file)
@@ -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;
index 1a43ae6e0fb76e38a065b43023a6f7046310cbe7..28e9c1dea43a3ec1a0140bc0ea9cb79f59dbae6f 100644 (file)
@@ -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 */