samba-bgqd: Convert closeall_*() to closefrom_*()
authorVolker Lendecke <vl@samba.org>
Mon, 20 Sep 2021 09:59:13 +0000 (11:59 +0200)
committerJeremy Allison <jra@samba.org>
Tue, 21 Sep 2021 00:13:32 +0000 (00:13 +0000)
Align it with closefrom() in preparation for use elsewhere

Signed-off-by: Volker Lendecke <vl@samba.org>
source3/printing/samba-bgqd.c

index 5a5fc488f5a61b095b74d4d9fd943c747d2c6e7c..11b364afea7a90c4aa3f29990897040f424dd0cc 100644 (file)
@@ -155,7 +155,7 @@ static int samba_bgqd_pidfile_create(
        return EAGAIN;
 }
 
-static int closeall_except(int *fds, size_t num_fds)
+static int closefrom_except(int lower, int *fds, size_t num_fds)
 {
        size_t i;
        int max_keep = -1;
@@ -168,7 +168,7 @@ static int closeall_except(int *fds, size_t num_fds)
                return 0;
        }
 
-       for (fd = 0; fd < max_keep; fd++) {
+       for (fd = lower; fd < max_keep; fd++) {
                bool keep = false;
 
                /*
@@ -191,17 +191,18 @@ static int closeall_except(int *fds, size_t num_fds)
                }
        }
 
-       closefrom(max_keep+1);
+       closefrom(MAX(lower, max_keep+1));
        return 0;
 }
 
-static int closeall_except_fd_params(
+static int closefrom_except_fd_params(
+       int lower,
        size_t num_fd_params,
        const char *fd_params[],
        int argc,
        const char *argv[])
 {
-       int fds[num_fd_params+3];
+       int fds[num_fd_params];
        size_t i;
        struct poptOption long_options[num_fd_params + 1];
        poptContext pc;
@@ -217,10 +218,6 @@ static int closeall_except_fd_params(
        }
        long_options[num_fd_params] = (struct poptOption) { .longName=NULL, };
 
-       fds[num_fd_params] = 0;
-       fds[num_fd_params+1] = 1;
-       fds[num_fd_params+2] = 2;
-
        pc = poptGetContext(argv[0], argc, argv, long_options, 0);
 
        while ((ret = poptGetNextOpt(pc)) != -1) {
@@ -229,7 +226,7 @@ static int closeall_except_fd_params(
 
        poptFreeContext(pc);
 
-       ret = closeall_except(fds, ARRAY_SIZE(fds));
+       ret = closefrom_except(lower, fds, ARRAY_SIZE(fds));
        return ret;
 }
 
@@ -304,8 +301,8 @@ int main(int argc, const char *argv[])
                        "ready-signal-fd", "parent-watch-fd",
                };
 
-               closeall_except_fd_params(
-                       ARRAY_SIZE(fd_params), fd_params, argc, argv);
+               closefrom_except_fd_params(
+                       3, ARRAY_SIZE(fd_params), fd_params, argc, argv);
        }
 
        talloc_enable_null_tracking();