Просмотр исходного кода

Added option to write a PID file

Ethan Galstad 20 лет назад
Родитель
Сommit
c69da9d3c9
8 измененных файлов с 101 добавлено и 34 удалено
  1. 2 1
      Changelog
  2. 1 1
      configure
  3. 1 1
      configure.in
  4. 1 1
      include/common.h
  5. 20 0
      include/nrpe.h
  6. 1 1
      nrpe.spec
  7. 8 1
      sample-config/nrpe.cfg.in
  8. 67 28
      src/nrpe.c

+ 2 - 1
Changelog

@@ -3,7 +3,7 @@ NRPE Changelog
 **************
 
 
-3.0 - ??/??/2006
+2.2 - 01/??/2006
 ----------------
 - Spec file fix
 - Patch to add Tru64 and IRIX support (Ton Voon)
@@ -11,6 +11,7 @@ NRPE Changelog
 - Fixed bug with config file lines with only whitespace
 - Fixed bug with missing getopt() command line option for -V
 - Removed sample FreeBSD init script (now maintained by FreeBSD port)
+- Added config file option for writing a PID file
 
 
 2.1 - 01/19/2004

+ 1 - 1
configure

@@ -1294,7 +1294,7 @@ ac_compiler_gnu=$ac_cv_c_compiler_gnu
 
 
 PKG_NAME=nrpe
-PKG_VERSION="3.0"
+PKG_VERSION="2.2"
 PKG_HOME_URL="http://www.nagios.org/"
 PKG_REL_DATE="01-20-2006"
 

+ 1 - 1
configure.in

@@ -9,7 +9,7 @@ AC_CONFIG_HEADER(include/config.h)
 AC_PREFIX_DEFAULT(/usr/local/nagios)
 
 PKG_NAME=nrpe
-PKG_VERSION="3.0"
+PKG_VERSION="2.2"
 PKG_HOME_URL="http://www.nagios.org/"
 PKG_REL_DATE="01-20-2006"
 

+ 1 - 1
include/common.h

@@ -23,7 +23,7 @@
 
 #include "config.h"
 
-#define PROGRAM_VERSION "3.0"
+#define PROGRAM_VERSION "2.2"
 #define MODIFICATION_DATE "01-20-2006"
 
 #define OK		0

+ 20 - 0
include/nrpe.h

@@ -31,3 +31,23 @@ typedef struct command_struct{
 	struct command_struct *next;
         }command;
 
+int process_arguments(int,char **);
+void wait_for_connections(void);
+void handle_connection(int);
+int read_config_file(char *);
+int read_config_dir(char *);
+int add_command(char *,char *);
+command *find_command(char *);
+void sighandler(int);
+int drop_privileges(char *,char *);
+int write_pid_file(void);
+int check_privileges(void);
+void free_memory(void);
+int is_an_allowed_host(char *);
+int validate_request(packet *);
+int contains_nasty_metachars(char *);
+int process_macros(char *,char *,int);
+int my_system(char *,int,int *,char *,int);            	/* executes a command via popen(), but also protects against timeouts */
+void my_system_sighandler(int);				/* handles timeouts when executing commands via my_system() */
+
+

+ 1 - 1
nrpe.spec

@@ -1,5 +1,5 @@
 %define name nrpe
-%define version 3.0
+%define version 2.2
 %define release 1
 %define nsusr nagios
 %define nsgrp nagios

+ 8 - 1
sample-config/nrpe.cfg.in

@@ -2,7 +2,7 @@
 # Sample NRPE Config File 
 # Written by: Ethan Galstad (nagios@nagios.org)
 # 
-# Last Modified: 10-13-2003
+# Last Modified: 01-20-2006
 #
 # NOTES:
 # This is a sample configuration file for the NRPE daemon.  It needs to be
@@ -11,6 +11,13 @@
 #############################################################################
 
 
+# PID FILE
+# The name of the file in which the NRPE daemon should write it's process ID
+# number.  The file is only written if the NRPE daemon is started by the root
+# user and is running in standalone mode.
+
+pid_file=/var/run/nrpe.pid
+
 
 # PORT NUMBER
 # Port number we should wait for connections on.

+ 67 - 28
src/nrpe.c

@@ -32,30 +32,18 @@ int allow_severity=LOG_INFO;
 int deny_severity=LOG_WARNING;
 #endif
 
+#ifdef HAVE_SSL
+SSL_METHOD *meth;
+SSL_CTX *ctx;
+int use_ssl=TRUE;
+#else
+int use_ssl=FALSE;
+#endif
+
 #define DEFAULT_COMMAND_TIMEOUT	60			/* default timeout for execution of plugins */
 #define MAXFD                   64
 #define NASTY_METACHARS         "|`&><'\"\\[]{}"
 
-
-int process_arguments(int,char **);
-void wait_for_connections(void);
-void handle_connection(int);
-int read_config_file(char *);
-int read_config_dir(char *);
-int add_command(char *,char *);
-command *find_command(char *);
-void sighandler(int);
-int drop_privileges(char *,char *);
-int check_privileges(void);
-void free_memory(void);
-int is_an_allowed_host(char *);
-int validate_request(packet *);
-int contains_nasty_metachars(char *);
-int process_macros(char *,char *,int);
-int my_system(char *,int,int *,char *,int);            	/* executes a command via popen(), but also protects against timeouts */
-void my_system_sighandler(int);				/* handles timeouts when executing commands via my_system() */
-
-
 char    *command_name=NULL;
 char    *macro_argv[MAX_COMMAND_ARGUMENTS];
 
@@ -70,6 +58,8 @@ command *command_list=NULL;
 char    *nrpe_user=NULL;
 char    *nrpe_group=NULL;
 
+char    *pid_file=NULL;
+
 int     allow_arguments=FALSE;
 
 int     show_help=FALSE;
@@ -78,13 +68,6 @@ int     show_version=FALSE;
 int     use_inetd=TRUE;
 int     debug=FALSE;
 
-#ifdef HAVE_SSL
-SSL_METHOD *meth;
-SSL_CTX *ctx;
-int use_ssl=TRUE;
-#else
-int use_ssl=FALSE;
-#endif
 
 
 
@@ -243,13 +226,17 @@ int main(int argc, char **argv){
 
 	/* else daemonize and start listening for requests... */
 	else if(fork()==0){
-
+		
 		/* we're a daemon - set up a new process group */
 		setsid();
 
 		/* ignore SIGHUP */
 		signal(SIGHUP, SIG_IGN);
 
+		/* write pid file */
+		if(write_pid_file()==ERROR)
+			return STATE_CRITICAL;
+
 		chdir("/");
 		/*umask(0);*/
 
@@ -418,6 +405,9 @@ int read_config_file(char *filename){
 			        }
 		        }
 
+		else if(!strcmp(varname,"pid_file"))
+			pid_file=strdup(varvalue);
+
 		else{
 			syslog(LOG_WARNING,"Unknown option specified in config file '%s' - Line %d\n",filename,line);
 			continue;
@@ -1302,6 +1292,55 @@ int drop_privileges(char *user, char *group){
         }
 
 
+/* write an optional pid file */
+int write_pid_file(void){
+	int fd;
+	int result=0;
+	pid_t pid=0;
+	char pbuf[16];
+
+	/* no pid file was specified */
+	if(pid_file==NULL)
+		return OK;
+
+	/* read existing pid file */
+	if((fd=open(pid_file,O_RDONLY))>=0){
+
+		result=read(fd,pbuf,(sizeof pbuf)-1);
+
+		close(fd);
+
+		if(result>0){
+
+			pbuf[result]='\x0';
+			pid=(pid_t)atoi(pbuf);
+
+			/* if previous process is no longer running running, remove the old pid file */
+			if(pid && (pid==getpid() || kill(pid,0)<0))
+				unlink(pid_file);
+
+			/* previous process is still running */
+			else{
+				syslog(LOG_ERR,"There's already a nrpe server running.");
+				return ERROR;
+			        }
+		        }
+	        } 
+
+	/* write new pid file */
+	if((fd=open(pid_file,O_WRONLY | O_CREAT,0644))>=0){
+		sprintf(pbuf,"%d\n",(int)getpid());
+		write(fd,pbuf,strlen(pbuf));
+		close(fd);
+	        }
+	else{
+		syslog(LOG_ERR,"Cannot write to pidfile '%s'.",pid_file);
+		perror("TEST");
+	        }
+
+	return OK;
+        }
+
 
 
 /* bail if daemon is running as root */