Parcourir la source

add rudimentary fork() limiter

Bryan Heden il y a 8 ans
Parent
commit
42ce4afa86
2 fichiers modifiés avec 29 ajouts et 0 suppressions
  1. 7 0
      sample-config/nrpe.cfg.in
  2. 22 0
      src/nrpe.c

+ 7 - 0
sample-config/nrpe.cfg.in

@@ -161,6 +161,13 @@ allow_bash_command_substitution=0
 # command_prefix=/usr/bin/sudo
 
 
+# MAX COMMANDS
+# This specifies how many children processes may be spawned at any one
+# time, essentially limiting the fork()s that occur.
+# Default (0) is set to unlimited
+# max_commands=0
+
+
 
 # COMMAND TIMEOUT
 # This specifies the maximum number of seconds that the NRPE daemon will

+ 22 - 0
src/nrpe.c

@@ -112,6 +112,8 @@ int       show_help = FALSE;
 int       show_license = FALSE;
 int       show_version = FALSE;
 int       use_inetd = TRUE;
+int 	  commands_running = 0;
+int       max_commands = 0;
 int       debug = FALSE;
 int       use_src = FALSE;		/* Define parameter for SRC option */
 int       no_forking = FALSE;
@@ -815,6 +817,14 @@ int read_config_file(char *filename)
 			if (read_config_file(varvalue) == ERROR)
 				logit(LOG_ERR, "Continuing with errors...");
 
+		} else if (!strcmp(varname, "max_commands")) {
+
+			max_commands = atoi(varvalue);
+			if (max_commands < 0) {
+				logit(LOG_WARNING, "max_commands set too low, setting to 0\n");
+				max_commands = 0;
+			}
+
 		} else if (!strcmp(varname, "server_port")) {
 			server_port = atoi(varvalue);
 			if (server_port < 1024) {
@@ -2159,6 +2169,14 @@ int my_system(char *command, int timeout, int *early_timeout, char **output)
 	if (command == NULL)		/* if no command was passed, return with no error */
 		return STATE_OK;
 
+	/* make sure that we are within max_commands boundaries before attempting */
+	if (max_commands != 0) {
+		while (commands_running >= max_commands) {
+			logit(LOG_WARNING, "Commands choked. Sleeping 1s - commands_running: %d, max_commands: %d", commands_running, max_commands);
+			sleep(1);
+		}
+	}
+
 	pipe(fd);					/* create a pipe */
 
 	/* make the pipe non-blocking */
@@ -2246,6 +2264,8 @@ int my_system(char *command, int timeout, int *early_timeout, char **output)
 	} else {
 		/* parent waits for child to finish executing command */
 
+		commands_running++;
+
 		close(fd[1]);			/* close pipe for writing */
 		waitpid(pid, &status, 0);	/* wait for child to exit */
 		time(&end_time);		/* get the end time for running the command */
@@ -2296,6 +2316,8 @@ int my_system(char *command, int timeout, int *early_timeout, char **output)
 		}
 
 		close(fd[0]);			/* close the pipe for reading */
+
+		commands_running--;
 	}
 
 #ifdef DEBUG