mirror of
https://github.com/samba-team/samba.git
synced 2025-08-04 08:22:08 +03:00
lib: Make close_low_fd() independently linkable
Signed-off-by: Volker Lendecke <vl@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
This commit is contained in:
committed by
Jeremy Allison
parent
4ac12fb2f7
commit
a7c243b62c
@ -27,54 +27,12 @@
|
|||||||
#if HAVE_SYSTEMD
|
#if HAVE_SYSTEMD
|
||||||
#include <systemd/sd-daemon.h>
|
#include <systemd/sd-daemon.h>
|
||||||
#endif
|
#endif
|
||||||
|
#include "lib/util/close_low_fd.h"
|
||||||
|
|
||||||
/*******************************************************************
|
/*******************************************************************
|
||||||
Close the low 3 fd's and open dev/null in their place.
|
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)
|
_PUBLIC_ void close_low_fds(bool stdin_too, bool stdout_too, bool stderr_too)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
65
lib/util/close_low_fd.c
Normal file
65
lib/util/close_low_fd.c
Normal file
@ -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;
|
||||||
|
}
|
28
lib/util/close_low_fd.h
Normal file
28
lib/util/close_low_fd.h
Normal file
@ -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
|
@ -23,6 +23,7 @@
|
|||||||
#include "system/filesys.h"
|
#include "system/filesys.h"
|
||||||
#include "system/syslog.h"
|
#include "system/syslog.h"
|
||||||
#include "lib/util/time_basic.h"
|
#include "lib/util/time_basic.h"
|
||||||
|
#include "lib/util/close_low_fd.h"
|
||||||
|
|
||||||
/* define what facility to use for syslog */
|
/* define what facility to use for syslog */
|
||||||
#ifndef SYSLOG_FACILITY
|
#ifndef SYSLOG_FACILITY
|
||||||
|
@ -836,13 +836,6 @@ _PUBLIC_ void *idr_find(struct idr_context *idp, int id);
|
|||||||
*/
|
*/
|
||||||
_PUBLIC_ int idr_remove(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
|
Close the low 3 fd's and open dev/null in their place
|
||||||
**/
|
**/
|
||||||
|
@ -5,6 +5,11 @@ bld.SAMBA_SUBSYSTEM('time-basic',
|
|||||||
deps='replace',
|
deps='replace',
|
||||||
local_include=False)
|
local_include=False)
|
||||||
|
|
||||||
|
bld.SAMBA_SUBSYSTEM('close-low-fd',
|
||||||
|
source='close_low_fd.c',
|
||||||
|
deps='replace',
|
||||||
|
local_include=False)
|
||||||
|
|
||||||
bld.SAMBA_LIBRARY('samba-util',
|
bld.SAMBA_LIBRARY('samba-util',
|
||||||
source='''talloc_stack.c smb_threads.c xfile.c data_blob.c
|
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
|
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
|
util_str.c util_str_common.c substitute.c ms_fnmatch.c
|
||||||
server_id.c dprintf.c parmlist.c bitmap.c pidfile.c
|
server_id.c dprintf.c parmlist.c bitmap.c pidfile.c
|
||||||
tevent_debug.c util_process.c memcache.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_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',
|
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') ],
|
header_path= [ ('dlinklist.h samba_util.h', '.'), ('*', 'util') ],
|
||||||
|
Reference in New Issue
Block a user