misc: add cloexec_fds_from() helper function
Move logic to set file descriptors to a cloexec_fds_from() function in misc.c so it can be shared by both supervisor-daemon and start-stop-daemon, and hide the details behind.
This commit is contained in:
parent
5bfb592d75
commit
c199c5cf6e
@ -15,6 +15,11 @@
|
|||||||
* except according to the terms contained in the LICENSE file.
|
* except according to the terms contained in the LICENSE file.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifdef HAVE_CLOSE_RANGE
|
||||||
|
/* For close_range() */
|
||||||
|
# define _GNU_SOURCE
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
@ -500,3 +505,25 @@ pid_t get_pid(const char *applet,const char *pidfile)
|
|||||||
|
|
||||||
return pid;
|
return pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef HAVE_CLOSE_RANGE
|
||||||
|
static inline int close_range(int first RC_UNUSED,
|
||||||
|
int last RC_UNUSED,
|
||||||
|
unsigned int flags RC_UNUSED)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#ifndef CLOSE_RANGE_CLOEXEC
|
||||||
|
# define CLOSE_RANGE_CLOEXEC (1U << 2)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void
|
||||||
|
cloexec_fds_from(int first)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
if (close_range(first, UINT_MAX, CLOSE_RANGE_CLOEXEC) < 0) {
|
||||||
|
for (i = getdtablesize() - 1; i >= first; --i)
|
||||||
|
fcntl(i, F_SETFD, FD_CLOEXEC);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -73,4 +73,6 @@ void from_time_t(char *time_string, time_t tv);
|
|||||||
time_t to_time_t(char *timestring);
|
time_t to_time_t(char *timestring);
|
||||||
pid_t get_pid(const char *applet, const char *pidfile);
|
pid_t get_pid(const char *applet, const char *pidfile);
|
||||||
|
|
||||||
|
void cloexec_fds_from(int);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -22,11 +22,6 @@
|
|||||||
#define ONE_SECOND 1000000000
|
#define ONE_SECOND 1000000000
|
||||||
#define ONE_MS 1000000
|
#define ONE_MS 1000000
|
||||||
|
|
||||||
#ifdef HAVE_CLOSE_RANGE
|
|
||||||
/* For close_range() */
|
|
||||||
# define _GNU_SOURCE
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
@ -203,18 +198,6 @@ static inline int ioprio_set(int which RC_UNUSED, int who RC_UNUSED,
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef CLOSE_RANGE_CLOEXEC
|
|
||||||
# define CLOSE_RANGE_CLOEXEC (1U << 2)
|
|
||||||
#endif
|
|
||||||
#ifndef HAVE_CLOSE_RANGE
|
|
||||||
static inline int close_range(int first RC_UNUSED,
|
|
||||||
int last RC_UNUSED,
|
|
||||||
unsigned int flags RC_UNUSED)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static void cleanup(void)
|
static void cleanup(void)
|
||||||
{
|
{
|
||||||
free(changeuser);
|
free(changeuser);
|
||||||
@ -582,9 +565,8 @@ RC_NORETURN static void child_process(char *exec, char **argv)
|
|||||||
if (redirect_stderr || rc_yesno(getenv("EINFO_QUIET")))
|
if (redirect_stderr || rc_yesno(getenv("EINFO_QUIET")))
|
||||||
dup2(stderr_fd, STDERR_FILENO);
|
dup2(stderr_fd, STDERR_FILENO);
|
||||||
|
|
||||||
if (close_range(3, UINT_MAX, CLOSE_RANGE_CLOEXEC) < 0)
|
cloexec_fds_from(3);
|
||||||
for (i = getdtablesize() - 1; i >= 3; --i)
|
|
||||||
fcntl(i, F_SETFD, FD_CLOEXEC);
|
|
||||||
cmdline = make_cmdline(argv);
|
cmdline = make_cmdline(argv);
|
||||||
syslog(LOG_INFO, "Child command line: %s", cmdline);
|
syslog(LOG_INFO, "Child command line: %s", cmdline);
|
||||||
free(cmdline);
|
free(cmdline);
|
||||||
|
Loading…
Reference in New Issue
Block a user