Skip to content

Commit

Permalink
supervise-daemon: rename HAVE_CLOSE_RANGE_EXEC to HAVE_CLOSE_RANGE
Browse files Browse the repository at this point in the history
Use HAVE_CLOSE_RANGE to tell if system provides a close_range(2)
wrapper, which better explains the purpose.

Add a compat inline which returns -1 if close_range is unavailable.
  • Loading branch information
ncopa committed Aug 30, 2023
1 parent c1cd3c9 commit 6b57dc0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ if cc.has_function('closefrom', prefix: '#define _GNU_SOURCE\n#include <unistd.h
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')
add_project_arguments('-DHAVE_CLOSE_RANGE', language: 'c')
endif

if cc.has_function('strlcpy', prefix: '#define _GNU_SOURCE\n#include <string.h>')
Expand Down
16 changes: 13 additions & 3 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 Down Expand Up @@ -203,6 +203,18 @@ static inline int ioprio_set(int which RC_UNUSED, int who RC_UNUSED,
}
#endif

#ifndef CLOSE_RANGE_CLOEXEC
# define CLOSE_RANGE_CLOEXEC (1U << 2)
#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)
{
return -1;
}
#endif

static void cleanup(void)
{
free(changeuser);
Expand Down Expand Up @@ -570,9 +582,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
if (close_range(3, UINT_MAX, CLOSE_RANGE_CLOEXEC) < 0)
#endif
for (i = getdtablesize() - 1; i >= 3; --i)
fcntl(i, F_SETFD, FD_CLOEXEC);
cmdline = make_cmdline(argv);
Expand Down

0 comments on commit 6b57dc0

Please sign in to comment.