lib: Move closefrom_except*() to a separate file
authorVolker Lendecke <vl@samba.org>
Mon, 20 Sep 2021 10:09:55 +0000 (12:09 +0200)
committerJeremy Allison <jra@samba.org>
Tue, 21 Sep 2021 01:12:12 +0000 (01:12 +0000)
Enable use in other daemons

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
Autobuild-User(master): Jeremy Allison <jra@samba.org>
Autobuild-Date(master): Tue Sep 21 01:12:12 UTC 2021 on sn-devel-184

lib/cmdline/closefrom_except.c [new file with mode: 0644]
lib/cmdline/closefrom_except.h [new file with mode: 0644]
lib/cmdline/wscript
source3/printing/samba-bgqd.c

diff --git a/lib/cmdline/closefrom_except.c b/lib/cmdline/closefrom_except.c
new file mode 100644 (file)
index 0000000..fe4e0cc
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "replace.h"
+#include "closefrom_except.h"
+#include <popt.h>
+
+int closefrom_except(int lower, int *fds, size_t num_fds)
+{
+       size_t i;
+       int max_keep = -1;
+       int fd, ret;
+
+       for (i=0; i<num_fds; i++) {
+               max_keep = MAX(max_keep, fds[i]);
+       }
+       if (max_keep == -1) {
+               return 0;
+       }
+
+       for (fd = lower; fd < max_keep; fd++) {
+               bool keep = false;
+
+               /*
+                * O(num_fds*max_keep), but we expect the number of
+                * fds to keep to be very small, typically 0,1,2 and
+                * very few more.
+                */
+               for (i=0; i<num_fds; i++) {
+                       if (fd == fds[i]) {
+                               keep = true;
+                               break;
+                       }
+               }
+               if (keep) {
+                       continue;
+               }
+               ret = close(fd);
+               if ((ret == -1) && (errno != EBADF)) {
+                       return errno;
+               }
+       }
+
+       closefrom(MAX(lower, max_keep+1));
+       return 0;
+}
+
+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];
+       size_t i;
+       struct poptOption long_options[num_fd_params + 1];
+       poptContext pc;
+       int ret;
+
+       for (i=0; i<num_fd_params; i++) {
+               fds[i] = -1;
+               long_options[i] = (struct poptOption) {
+                       .longName = fd_params[i],
+                       .argInfo = POPT_ARG_INT,
+                       .arg = &fds[i],
+               };
+       }
+       long_options[num_fd_params] = (struct poptOption) { .longName=NULL, };
+
+       pc = poptGetContext(argv[0], argc, argv, long_options, 0);
+
+       while ((ret = poptGetNextOpt(pc)) != -1) {
+               /* do nothing */
+       }
+
+       poptFreeContext(pc);
+
+       ret = closefrom_except(lower, fds, ARRAY_SIZE(fds));
+       return ret;
+}
diff --git a/lib/cmdline/closefrom_except.h b/lib/cmdline/closefrom_except.h
new file mode 100644 (file)
index 0000000..d696ebf
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __LIB_CLOSEFROM_EXCEPT_H__
+#define __LIB_CLOSEFROM_EXCEPT_H__
+
+#include "replace.h"
+
+int closefrom_except(int lower, int *fds, size_t num_fds);
+int closefrom_except_fd_params(
+       int lower,
+       size_t num_fd_params,
+       const char *fd_params[],
+       int argc,
+       const char *argv[]);
+
+#endif
index 9c50b47a41c94cc34fd8cb69329a0e352056d854..1e13561ddf64cf57b43c850c581a4fda64304f89 100644 (file)
@@ -6,7 +6,10 @@ from waflib import Logs
 
 def build(bld):
     bld.SAMBA_LIBRARY('cmdline',
-                      source='cmdline.c',
+                      source='''
+                          cmdline.c
+                          closefrom_except.c
+                      ''',
                       deps='''
                            talloc
                            cli_smb_common
index 11b364afea7a90c4aa3f29990897040f424dd0cc..336a007ab9aa19111667bb46504f056c8584baf4 100644 (file)
@@ -22,6 +22,7 @@
 #include "source3/param/loadparm.h"
 #include "source3/param/param_proto.h"
 #include "lib/cmdline/cmdline.h"
+#include "lib/cmdline/closefrom_except.h"
 #include "lib/util/talloc_stack.h"
 #include "lib/util/debug.h"
 #include "lib/util/signal.h"
@@ -155,81 +156,6 @@ static int samba_bgqd_pidfile_create(
        return EAGAIN;
 }
 
-static int closefrom_except(int lower, int *fds, size_t num_fds)
-{
-       size_t i;
-       int max_keep = -1;
-       int fd, ret;
-
-       for (i=0; i<num_fds; i++) {
-               max_keep = MAX(max_keep, fds[i]);
-       }
-       if (max_keep == -1) {
-               return 0;
-       }
-
-       for (fd = lower; fd < max_keep; fd++) {
-               bool keep = false;
-
-               /*
-                * O(num_fds*max_keep), but we expect the number of
-                * fds to keep to be very small, typically 0,1,2 and
-                * very few more.
-                */
-               for (i=0; i<num_fds; i++) {
-                       if (fd == fds[i]) {
-                               keep = true;
-                               break;
-                       }
-               }
-               if (keep) {
-                       continue;
-               }
-               ret = close(fd);
-               if ((ret == -1) && (errno != EBADF)) {
-                       return errno;
-               }
-       }
-
-       closefrom(MAX(lower, max_keep+1));
-       return 0;
-}
-
-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];
-       size_t i;
-       struct poptOption long_options[num_fd_params + 1];
-       poptContext pc;
-       int ret;
-
-       for (i=0; i<num_fd_params; i++) {
-               fds[i] = -1;
-               long_options[i] = (struct poptOption) {
-                       .longName = fd_params[i],
-                       .argInfo = POPT_ARG_INT,
-                       .arg = &fds[i],
-               };
-       }
-       long_options[num_fd_params] = (struct poptOption) { .longName=NULL, };
-
-       pc = poptGetContext(argv[0], argc, argv, long_options, 0);
-
-       while ((ret = poptGetNextOpt(pc)) != -1) {
-               /* do nothing */
-       }
-
-       poptFreeContext(pc);
-
-       ret = closefrom_except(lower, fds, ARRAY_SIZE(fds));
-       return ret;
-}
-
 int main(int argc, const char *argv[])
 {
        const struct loadparm_substitution *lp_sub =