lib: Make close_low_fd() independently linkable
authorVolker Lendecke <vl@samba.org>
Thu, 31 Jul 2014 09:40:04 +0000 (09:40 +0000)
committerJeremy Allison <jra@samba.org>
Fri, 1 Aug 2014 20:11:46 +0000 (22:11 +0200)
Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/util/become_daemon.c
lib/util/close_low_fd.c [new file with mode: 0644]
lib/util/close_low_fd.h [new file with mode: 0644]
lib/util/debug.c
lib/util/samba_util.h
lib/util/wscript_build

index d940cd7d33eb7021ef8cf65a0ad77c75dd927100..17e0bafe160beb11487561670bb04cd803365a46 100644 (file)
 #if HAVE_SYSTEMD
 #include <systemd/sd-daemon.h>
 #endif
+#include "lib/util/close_low_fd.h"
 
 /*******************************************************************
  Close the low 3 fd's and open dev/null in their place.
 ********************************************************************/
 
-_PUBLIC_ int close_low_fd(int fd)
-{
-#ifndef VALGRIND
-       int ret, dev_null;
-
-       dev_null = open("/dev/null", O_RDWR, 0);
-
-       if ((dev_null == -1) && (errno = ENFILE)) {
-               /*
-                * Try to free up an fd
-                */
-               ret = close(fd);
-               if (ret != 0) {
-                       return errno;
-               }
-       }
-
-       dev_null = open("/dev/null", O_RDWR, 0);
-       if (dev_null == -1) {
-               dev_null = open("/dev/null", O_WRONLY, 0);
-       }
-       if (dev_null == -1) {
-               return errno;
-       }
-
-       if (dev_null == fd) {
-               /*
-                * This can happen in the ENFILE case above
-                */
-               return 0;
-       }
-
-       ret = dup2(dev_null, fd);
-       if (ret == -1) {
-               int err = errno;
-               close(dev_null);
-               return err;
-       }
-       close(dev_null);
-#endif
-       return 0;
-}
-
 _PUBLIC_ void close_low_fds(bool stdin_too, bool stdout_too, bool stderr_too)
 {
 
diff --git a/lib/util/close_low_fd.c b/lib/util/close_low_fd.c
new file mode 100644 (file)
index 0000000..b11d25f
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * Unix SMB/CIFS implementation.
+ * Samba utility functions
+ * Copyright (C) Volker Lendecke 2014
+ *
+ * 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 "system/filesys.h"
+#include "close_low_fd.h"
+
+_PUBLIC_ int close_low_fd(int fd)
+{
+#ifndef VALGRIND
+       int ret, dev_null;
+
+       dev_null = open("/dev/null", O_RDWR, 0);
+
+       if ((dev_null == -1) && (errno = ENFILE)) {
+               /*
+                * Try to free up an fd
+                */
+               ret = close(fd);
+               if (ret != 0) {
+                       return errno;
+               }
+       }
+
+       dev_null = open("/dev/null", O_RDWR, 0);
+       if (dev_null == -1) {
+               dev_null = open("/dev/null", O_WRONLY, 0);
+       }
+       if (dev_null == -1) {
+               return errno;
+       }
+
+       if (dev_null == fd) {
+               /*
+                * This can happen in the ENFILE case above
+                */
+               return 0;
+       }
+
+       ret = dup2(dev_null, fd);
+       if (ret == -1) {
+               int err = errno;
+               close(dev_null);
+               return err;
+       }
+       close(dev_null);
+#endif
+       return 0;
+}
diff --git a/lib/util/close_low_fd.h b/lib/util/close_low_fd.h
new file mode 100644 (file)
index 0000000..954d1d2
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Unix SMB/CIFS implementation.
+ * Samba utility functions
+ * Copyright (C) Volker Lendecke 2014
+ *
+ * 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 _CLOSE_LOW_FD_H
+#define _CLOSE_LOW_FD_H
+
+/*
+ * Redirect "fd" to /dev/null
+ */
+int close_low_fd(int fd);
+
+#endif
index f83b14cf02ac28579d9b47fe4179c5cf83c91350..2779dd3df853e2dd7f797945734d3212bd76e128 100644 (file)
@@ -23,6 +23,7 @@
 #include "system/filesys.h"
 #include "system/syslog.h"
 #include "lib/util/time_basic.h"
+#include "lib/util/close_low_fd.h"
 
 /* define what facility to use for syslog */
 #ifndef SYSLOG_FACILITY
index b31ee1f8f0a24c4164093db23cce3bdfe3cf5cc9..233b5fd8cb07c6cd6e8d17144f46abb0333c3e60 100644 (file)
@@ -836,13 +836,6 @@ _PUBLIC_ void *idr_find(struct idr_context *idp, int id);
 */
 _PUBLIC_ int idr_remove(struct idr_context *idp, int id);
 
-/* The following definitions come from lib/util/become_daemon.c  */
-
-/**
- Close a fd and open dev/null in its place
-**/
-_PUBLIC_ int close_low_fd(int fd);
-
 /**
  Close the low 3 fd's and open dev/null in their place
 **/
index 8be2c2ec4a114d4e0f2b505f64c8c6e893d607eb..6f625dc061ddff2e2828ccc14ce87aa6786e103e 100755 (executable)
@@ -5,6 +5,11 @@ bld.SAMBA_SUBSYSTEM('time-basic',
                     deps='replace',
                     local_include=False)
 
+bld.SAMBA_SUBSYSTEM('close-low-fd',
+                    source='close_low_fd.c',
+                    deps='replace',
+                    local_include=False)
+
 bld.SAMBA_LIBRARY('samba-util',
                   source='''talloc_stack.c smb_threads.c xfile.c data_blob.c
                     util_file.c time.c rbtree.c rfc1738.c select.c getpass.c
@@ -14,7 +19,7 @@ bld.SAMBA_LIBRARY('samba-util',
                     util_str.c util_str_common.c substitute.c ms_fnmatch.c
                     server_id.c dprintf.c parmlist.c bitmap.c pidfile.c
                     tevent_debug.c util_process.c memcache.c''',
-                  deps='DYNCONFIG time-basic',
+                  deps='DYNCONFIG time-basic close-low-fd',
                   public_deps='talloc tevent execinfo pthread LIBCRYPTO charset util_setid systemd-daemon',
                   public_headers='debug.h attr.h byteorder.h data_blob.h memory.h safe_string.h time.h talloc_stack.h xfile.h dlinklist.h samba_util.h string_wrappers.h',
                   header_path= [ ('dlinklist.h samba_util.h', '.'), ('*', 'util') ],