summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorVolker Lendecke <vl@samba.org>2014-07-31 09:40:04 +0000
committerJeremy Allison <jra@samba.org>2014-08-01 22:11:46 +0200
commita7c243b62c6eaaca317fefa41e12a44b664c12ac (patch)
treeb6841281f77d459f8ad087a6e390a7044487c3c7 /lib
parent4ac12fb2f7b0c949448d38feb0e6dbdb1b928993 (diff)
downloadsamba-a7c243b62c6eaaca317fefa41e12a44b664c12ac.tar.gz
samba-a7c243b62c6eaaca317fefa41e12a44b664c12ac.tar.xz
samba-a7c243b62c6eaaca317fefa41e12a44b664c12ac.zip
lib: Make close_low_fd() independently linkable
Signed-off-by: Volker Lendecke <vl@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
Diffstat (limited to 'lib')
-rw-r--r--lib/util/become_daemon.c44
-rw-r--r--lib/util/close_low_fd.c65
-rw-r--r--lib/util/close_low_fd.h28
-rw-r--r--lib/util/debug.c1
-rw-r--r--lib/util/samba_util.h7
-rwxr-xr-xlib/util/wscript_build7
6 files changed, 101 insertions, 51 deletions
diff --git a/lib/util/become_daemon.c b/lib/util/become_daemon.c
index d940cd7d33..17e0bafe16 100644
--- a/lib/util/become_daemon.c
+++ b/lib/util/become_daemon.c
@@ -27,54 +27,12 @@
#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
index 0000000000..b11d25f657
--- /dev/null
+++ b/lib/util/close_low_fd.c
@@ -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
index 0000000000..954d1d2668
--- /dev/null
+++ b/lib/util/close_low_fd.h
@@ -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
diff --git a/lib/util/debug.c b/lib/util/debug.c
index f83b14cf02..2779dd3df8 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -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
diff --git a/lib/util/samba_util.h b/lib/util/samba_util.h
index b31ee1f8f0..233b5fd8cb 100644
--- a/lib/util/samba_util.h
+++ b/lib/util/samba_util.h
@@ -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
**/
diff --git a/lib/util/wscript_build b/lib/util/wscript_build
index 8be2c2ec4a..6f625dc061 100755
--- a/lib/util/wscript_build
+++ b/lib/util/wscript_build
@@ -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') ],