Sfoglia il codice sorgente

Merge pull request #1304 from monitoring-plugins/pr-1183-enhanced

merge pull request #1300 while moving values to constants
Sven Nierlein 11 anni fa
parent
commit
8b272a7f7b
1 ha cambiato i file con 12 aggiunte e 2 eliminazioni
  1. 12 2
      lib/utils_cmd.c

+ 12 - 2
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
  * 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
  * 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. */
  * 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
 #ifdef _SC_OPEN_MAX
 static long maxfd = 0;
 static long maxfd = 0;
 #elif defined(OPEN_MAX)
 #elif defined(OPEN_MAX)
 # define maxfd OPEN_MAX
 # define maxfd OPEN_MAX
 #else	/* sysconf macro unavailable, so guess (may be wildly inaccurate) */
 #else	/* sysconf macro unavailable, so guess (may be wildly inaccurate) */
-# define maxfd 256
+# define maxfd DEFAULT_MAXFD
 #endif
 #endif
 
 
 
 
@@ -112,10 +114,18 @@ cmd_init (void)
 	if (!maxfd && (maxfd = sysconf (_SC_OPEN_MAX)) < 0) {
 	if (!maxfd && (maxfd = sysconf (_SC_OPEN_MAX)) < 0) {
 		/* possibly log or emit a warning here, since there's no
 		/* possibly log or emit a warning here, since there's no
 		 * guarantee that our guess at maxfd will be adequate */
 		 * guarantee that our guess at maxfd will be adequate */
-		maxfd = 256;
+		maxfd = DEFAULT_MAXFD;
 	}
 	}
 #endif
 #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 > MAXFD_LIMIT ) {
+		maxfd = MAXFD_LIMIT;
+	}
+
 	if (!_cmd_pids)
 	if (!_cmd_pids)
 		_cmd_pids = calloc (maxfd, sizeof (pid_t));
 		_cmd_pids = calloc (maxfd, sizeof (pid_t));
 }
 }