From 6bb5e1db525f44a7ab40e9391244dbe65daa08cc Mon Sep 17 00:00:00 2001 From: nafets Date: Wed, 19 Nov 2014 22:34:23 +0100 Subject: avoid a segfault, if ulimit is set to unlimited --- lib/utils_cmd.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c index 9e214bd4..e41a9821 100644 --- a/lib/utils_cmd.c +++ b/lib/utils_cmd.c @@ -116,6 +116,14 @@ cmd_init (void) } #endif + /* if maxfd is unnaturally high, we force it to a lower value + * ( e.g. on SunOS, when ulimit is set to unlimited: 2147483647 this would cause + * a segfault when following calloc is called ... ) */ + + if ( maxfd > 2048 ) { + maxfd = 2048; + } + if (!_cmd_pids) _cmd_pids = calloc (maxfd, sizeof (pid_t)); } -- cgit v1.2.3-74-g34f1 From 7660540c31342eb7ea2f2ac24950094bf74f0a6f Mon Sep 17 00:00:00 2001 From: Sven Nierlein Date: Fri, 28 Nov 2014 15:25:43 +0100 Subject: make constants from maxfd values (#1300) its good practice to use constants instead of (random) values. Signed-off-by: Sven Nierlein --- lib/utils_cmd.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c index e41a9821..7eb9a3a0 100644 --- a/lib/utils_cmd.c +++ b/lib/utils_cmd.c @@ -79,12 +79,14 @@ static pid_t *_cmd_pids = NULL; * If that fails and the macro isn't defined, we fall back to an educated * guess. There's no guarantee that our guess is adequate and the program * will die with SIGSEGV if it isn't and the upper boundary is breached. */ +#define DEFAULT_MAXFD 256 /* fallback value if no max open files value is set */ +#define MAXFD_LIMIT 8192 /* upper limit of open files */ #ifdef _SC_OPEN_MAX static long maxfd = 0; #elif defined(OPEN_MAX) # define maxfd OPEN_MAX #else /* sysconf macro unavailable, so guess (may be wildly inaccurate) */ -# define maxfd 256 +# define maxfd DEFAULT_MAXFD #endif @@ -112,7 +114,7 @@ cmd_init (void) if (!maxfd && (maxfd = sysconf (_SC_OPEN_MAX)) < 0) { /* possibly log or emit a warning here, since there's no * guarantee that our guess at maxfd will be adequate */ - maxfd = 256; + maxfd = DEFAULT_MAXFD; } #endif @@ -120,8 +122,8 @@ cmd_init (void) * ( e.g. on SunOS, when ulimit is set to unlimited: 2147483647 this would cause * a segfault when following calloc is called ... ) */ - if ( maxfd > 2048 ) { - maxfd = 2048; + if ( maxfd > MAXFD_LIMIT ) { + maxfd = MAXFD_LIMIT; } if (!_cmd_pids) -- cgit v1.2.3-74-g34f1