From d9091a81b6dc449038821451696577ebbd270715 Mon Sep 17 00:00:00 2001 From: Guillem Jover Date: Mon, 21 Jan 2008 04:25:33 +0200 Subject: [PATCH] s-s-d: Refactor max open fd into a new function and coalesce close loop --- ChangeLog | 7 +++++++ configure.ac | 2 +- utils/start-stop-daemon.c | 18 +++++++++++------- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index a108f5eb..bcfeabfe 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2008-01-21 Guillem Jover + + * configure.ac (AC_CHECK_FUNCS): Add getdtablesize. + * utils/start-stop-daemon.c (get_open_fd_max): New function. + (main) [OShpux]: Remove. + [!OShpux]: Use get_open_fd_max instead of hardcoded getdtablesize. + 2008-01-21 Guillem Jover * man/start-stop-daemon.8: Fix quotation to get rid of a spurious diff --git a/configure.ac b/configure.ac index 72612793..40774c94 100644 --- a/configure.ac +++ b/configure.ac @@ -93,7 +93,7 @@ DPKG_C_ATTRIBUTE # Checks for library functions. DPKG_FUNC_VA_COPY AC_CHECK_FUNCS([vsnprintf snprintf strerror strsignal scandir alphasort]) -AC_CHECK_FUNCS([unsetenv lchown setsid bcopy isascii memcpy]) +AC_CHECK_FUNCS([unsetenv lchown setsid bcopy isascii memcpy getdtablesize]) SJR_COMPILER_WARNINGS SJR_COMPILER_OPTIMISATIONS diff --git a/utils/start-stop-daemon.c b/utils/start-stop-daemon.c index d973cb00..cf34c6d5 100644 --- a/utils/start-stop-daemon.c +++ b/utils/start-stop-daemon.c @@ -257,6 +257,15 @@ xgettimeofday(struct timeval *tv) if (gettimeofday(tv, NULL) != 0) fatal("gettimeofday failed: %s", strerror(errno)); } +static long +get_open_fd_max(void) +{ +#ifdef HAVE_GETDTABLESIZE + return getdtablesize(); +#else + return sysconf(_SC_OPEN_MAX); +#endif +} static void daemonize(void) @@ -1430,15 +1439,10 @@ main(int argc, char **argv) dup2(devnull_fd, 0); /* stdin */ dup2(devnull_fd, 1); /* stdout */ dup2(devnull_fd, 2); /* stderr */ -#if defined(OShpux) - /* Now close all extra fds */ - for (i = sysconf(_SC_OPEN_MAX) - 1; i >= 3; --i) - close(i); -#else + /* Now close all extra fds */ - for (i = getdtablesize() - 1; i >= 3; --i) + for (i = get_open_fd_max() - 1; i >= 3; --i) close(i); -#endif } execv(startas, argv); fatal("Unable to start %s: %s", startas, strerror(errno)); -- 2.39.5