From: Guillem Jover Date: Mon, 21 Jan 2008 02:25:33 +0000 (+0200) Subject: s-s-d: Refactor max open fd into a new function and coalesce close loop X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d9091a81b6dc449038821451696577ebbd270715;p=dpkg s-s-d: Refactor max open fd into a new function and coalesce close loop --- 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));