Skip to content

Commit

Permalink
start-stop-daemon, supervise-daemon: use close_range with musl
Browse files Browse the repository at this point in the history
Make sure that we use close_range also with musl libc, using syscall
directly.
  • Loading branch information
ncopa committed Aug 29, 2023
1 parent 86efc43 commit 3d2a626
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
7 changes: 4 additions & 3 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,10 @@ endif
if cc.has_function('closefrom', prefix: '#define _GNU_SOURCE\n#include <unistd.h>')
add_project_arguments('-DHAVE_CLOSEFROM', language: 'c')
endif
if cc.has_function('close_range', prefix: '#define _GNU_SOURCE\n#include <unistd.h>') and \
cc.has_header_symbol('unistd.h', 'CLOSE_RANGE_CLOEXEC', prefix: '#define _GNU_SOURCE')
add_project_arguments('-DHAVE_CLOSE_RANGE_CLOEXEC', language: 'c')
if cc.has_function('close_range', prefix: '#define _GNU_SOURCE\n#include <unistd.h>')
add_project_arguments('-DHAVE_CLOSE_RANGE', language: 'c')
elif cc.has_header('linux/close_range.h')
add_project_arguments('-DHAVE_LINUX_CLOSE_RANGE_H', language: 'c')
endif

incdir = include_directories('src/shared')
Expand Down
26 changes: 24 additions & 2 deletions src/start-stop-daemon/start-stop-daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
#ifdef __linux__
# include <sys/syscall.h> /* For io priority */
# include <sys/prctl.h> /* For prctl */
# if !defined(HAVE_CLOSE_RANGE) && defined(HAVE_LINUX_CLOSE_RANGE_H)
# include <linux/close_range.h>
# endif
#endif
#include <termios.h>
#include <time.h>
Expand Down Expand Up @@ -191,6 +194,22 @@ static inline int ioprio_set(int which RC_UNUSED,
}
#endif

#if !defined(SYS_close_range) && defined(__NR_close_range)
# define SYS_close_range __NR_close_range
#endif
#ifndef HAVE_CLOSE_RANGE
static inline int close_range(unsigned int first RC_UNUSED,
unsigned int last RC_UNUSED,
unsigned int flags RC_UNUSED)
{
#if defined(SYS_close_range)
return syscall(SYS_close_range, first, last, flags);
#else
return -1;
#endif
}
#endif

static void
cleanup(void)
{
Expand Down Expand Up @@ -1101,8 +1120,11 @@ int main(int argc, char **argv)
#ifdef HAVE_CLOSEFROM
closefrom(3);
#else
for (i = getdtablesize() - 1; i >= 3; --i)
close(i);
# ifdef CLOSE_RANGE_UNSHARE
if (close_range(3, 0, CLOSE_RANGE_UNSHARE) < 0)
# endif
for (i = getdtablesize() - 1; i >= 3; --i)
close(i);
#endif

if (scheduler != NULL) {
Expand Down
23 changes: 21 additions & 2 deletions src/supervise-daemon/supervise-daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#define ONE_SECOND 1000000000
#define ONE_MS 1000000

#ifdef HAVE_CLOSE_RANGE_CLOEXEC
#ifdef HAVE_CLOSE_RANGE
/* For close_range() */
# define _GNU_SOURCE
#endif
Expand All @@ -43,6 +43,9 @@
#ifdef __linux__
# include <sys/syscall.h> /* For io priority */
# include <sys/prctl.h> /* For prctl */
# if !defined(HAVE_CLOSE_FROM) && defined(HAVE_LINUX_CLOSE_RANGE_H)
# include <linux/close_range.h>
# endif
#endif
#include <syslog.h>
#include <sys/ioctl.h>
Expand Down Expand Up @@ -203,6 +206,22 @@ static inline int ioprio_set(int which RC_UNUSED, int who RC_UNUSED,
}
#endif

#if !defined(SYS_close_range) && defined(__NR_close_range)
# define SYS_close_range __NR_close_range
#endif
#ifndef HAVE_CLOSE_RANGE
static inline int close_range(unsigned int first RC_UNUSED,
unsigned int last RC_UNUSED,
unsigned int flags RC_UNUSED)
{
#ifdef SYS_close_range
return syscall(SYS_close_range, first, last, flags);
#else
return -1;
#endif
}
#endif

static void cleanup(void)
{
free(changeuser);
Expand Down Expand Up @@ -570,7 +589,7 @@ RC_NORETURN static void child_process(char *exec, char **argv)
if (redirect_stderr || rc_yesno(getenv("EINFO_QUIET")))
dup2(stderr_fd, STDERR_FILENO);

#ifdef HAVE_CLOSE_RANGE_CLOEXEC
#ifdef CLOSE_RANGE_CLOEXEC
if (close_range(3, UINT_MAX, CLOSE_RANGE_CLOEXEC) < 0)
#endif
for (i = getdtablesize() - 1; i >= 3; --i)
Expand Down

0 comments on commit 3d2a626

Please sign in to comment.