diff -urN ../nagios.orig/base/Makefile.in ./base/Makefile.in
--- ../nagios.orig/base/Makefile.in	Fri Dec 10 14:52:36 2004
+++ ./base/Makefile.in	Mon Apr  4 20:29:18 2005
@@ -115,7 +115,7 @@
 DDATADEPS=$(DDATALIBS)
 
 
-OBJS=$(BROKER_O) checks.o config.o commands.o events.o flapping.o logging.o notifications.o sehandlers.o utils.o $(RDATALIBS) $(CDATALIBS) $(ODATALIBS) $(SDATALIBS) $(PDATALIBS) $(DDATALIBS) $(BASEEXTRALIBS) $(SNPRINTF_O) $(PERLXSI_O)
+OBJS=$(BROKER_O) nagios.o failsafe.o checks.o config.o commands.o events.o flapping.o logging.o notifications.o sehandlers.o utils.o $(RDATALIBS) $(CDATALIBS) $(ODATALIBS) $(SDATALIBS) $(PDATALIBS) $(DDATALIBS) $(BASEEXTRALIBS) $(SNPRINTF_O) $(PERLXSI_O)
 OBJDEPS=$(ODATADEPS) $(ODATADEPS) $(RDATADEPS) $(CDATADEPS) $(SDATADEPS) $(PDATADEPS) $(DDATADEPS) $(BROKER_H)
 
 all: nagios nagiostats
@@ -162,13 +162,13 @@
 
 ########## CGIS ##########
 
-nagios: nagios.c $(OBJS) $(OBJDEPS) $(SRC_INCLUDE)/nagios.h $(SRC_INCLUDE)/locations.h
-	$(CC) $(CFLAGS) -o $@ nagios.c $(OBJS) $(BROKER_LDFLAGS) $(LDFLAGS) $(PERLLIBS) $(MATHLIBS) $(SOCKETLIBS) $(THREADLIBS) $(BROKERLIBS) $(LIBS)
+nagios: $(OBJS) $(OBJDEPS) $(SRC_INCLUDE)/nagios.h $(SRC_INCLUDE)/locations.h
+	$(CC) $(CFLAGS) -o $@ $(OBJS) $(BROKER_LDFLAGS) $(LDFLAGS) $(PERLLIBS) $(MATHLIBS) $(SOCKETLIBS) $(THREADLIBS) $(BROKERLIBS) $(LIBS)
 
 nagiostats: nagiostats.c $(SRC_INCLUDE)/locations.h
 	$(CC) $(CFLAGS) -o $@ nagiostats.c $(LDFLAGS) $(MATHLIBS) $(LIBS)
 
-$(OBJS): $(SRC_INCLUDE)/locations.h
+$(OBJS): $(SRC_INCLUDE)/locations.h $(SRC_INCLUDE)/config.h
 
 clean:
 	rm -f nagios nagiostats core *.o gmon.out
diff -urN ../nagios.orig/base/failsafe.c ./base/failsafe.c
--- ../nagios.orig/base/failsafe.c	Thu Jan  1 01:00:00 1970
+++ ./base/failsafe.c	Mon Apr  4 21:09:01 2005
@@ -0,0 +1,175 @@
+/*
+ * Copyleft: GNU GPL 2.0 or later
+ *
+ * This is a rubbish'ish workaround meant to force Nagios to keep running
+ * even when it's hit by a SIGSEGV, designed to be in place until
+ * the problem causing the crashes is resolved.
+ *
+ */
+
+#include "../include/config.h"
+#include "../include/nagios.h"
+
+#ifdef SIG_MAX
+# undef SIG_MAX
+#endif
+#define SIG_MAX 32
+
+extern unsigned long logging_options;
+extern char *lock_file;
+
+/** local globals **/
+static int fs_sigrecv = 0;
+static pid_t fs_child = 0;
+static char fs_buf[60];
+struct sigaction fs_chld_sigact[SIG_MAX];
+struct sigaction fs_master_sigact[SIG_MAX];
+
+time_t fs_last_fork = 0;
+FILE *fs_logfp = NULL;
+
+
+/** necessary prototypes **/
+static void fs_setsighandler(void);
+
+
+static inline void fs_preserve_sigact_table(int sigs, struct sigaction *sigact_list)
+{
+	int i;
+	for(i = 0; i < sigs; i++) {
+		sigaction(i, NULL, &sigact_list[i]);
+	}
+}
+
+static inline void fs_set_sigact_table(int sigs, struct sigaction *sigact_list)
+{
+	int i;
+	for(i = 0; i < sigs; i++) {
+		sigaction(i, &sigact_list[i], NULL);
+	}
+}
+
+/* called on exit, as determined by atexit(3) */
+static void fs_cleanup(void)
+{
+	/* do we need to unlock first? */
+	unlink(lock_file);
+}
+
+/* the catchall sighandler.
+ * Pass selected userland signals to the child and filter the rest */
+static void fs_sighandler(int sig)
+{
+	fs_sigrecv = sig;
+
+	snprintf(fs_buf, sizeof(fs_buf), "FS: Grandmaster instance caught sig %d. Child is %d\n", sig, fs_child);
+	write_to_log(fs_buf, logging_options, NULL);
+
+	/* it's safe to use sleep here, because signals aren't reset so we
+	 * will get a SIGCHLD when the worker instance is done exiting,
+	 * which will bring us to a dead end after sending SIGKILL to a
+	 * (hopefully) non-existing process */
+	if(sig == SIGTERM) {
+		kill(fs_child, SIGTERM);
+		sleep(9);
+		kill(fs_child, SIGKILL);
+		exit(0);
+	}
+
+	if(sig == SIGHUP) {
+		kill(fs_child, SIGHUP);
+		fs_sigrecv = 0;
+	}
+
+	fs_setsighandler();
+}
+
+static void fs_setsighandler(void)
+{
+	int i;
+	for(i = 0; i < SIG_MAX; i++) signal(i, fs_sighandler);
+}
+
+static int fail_safe_loop(void)
+{
+	pid_t pid = 0;
+	int status = 0;
+
+	pid = waitpid(fs_child, &status, 0);
+
+	if(pid == fs_child) {
+		if(WIFEXITED(status) && WEXITSTATUS(status) == 0) exit(0);
+	}
+
+	if(pid == -1) {
+		if(errno == EINTR && fs_sigrecv == SIGHUP) {
+			signal(SIGCHLD, SIG_IGN);
+			kill(fs_child, SIGTERM);
+			sleep(5);
+			kill(fs_child, SIGKILL);
+			return -1;
+		}
+
+		if(errno == ECHILD) return 1;
+	}
+
+	return 0;
+}
+
+int become_failsafe(void)
+{
+	int foo;
+	static int fs_master = 0;
+	time_t now;
+
+	fs_preserve_sigact_table(SIG_MAX, fs_chld_sigact);
+
+	/* we might have caught a HUP and the checking child will then
+	 * call become_failsafe once more inside the loop in main() */
+	if(fs_master) {
+		snprintf(fs_buf, sizeof(fs_buf), "FS: Master already running with pid %d. HUP?\n", fs_master);
+		write_to_log(fs_buf, logging_options, NULL);
+		return (int)getpid();
+	}
+	fs_master = (int)getpid();
+	atexit(fs_cleanup);
+
+	spawn_again:
+	/* don't respawn more than once every ten seconds,
+	 * or we'll bring the system to its knees in case of bootstrap SIGSEGV */
+	now = time(NULL);
+	if(fs_last_fork && now - fs_last_fork < 10) {
+		sleep(5);
+		goto spawn_again;
+	}
+
+	if(time(NULL) - fs_last_fork)
+	if(!(fs_child = fork())) {
+		fs_set_sigact_table(SIG_MAX, fs_chld_sigact);
+		return (int)getpid();
+	}
+
+	if(fs_child < 0) {
+		write_to_log("Failsafeness failed (ironic, huh?). Running unprotected.", logging_options, NULL);
+		return -1;
+	}
+
+	if(fs_child) {
+		fs_last_fork = now;
+		snprintf(fs_buf, sizeof(fs_buf), "FS: Spawned new worker daemon with pid %d", fs_child);
+		write_to_log(fs_buf, logging_options, NULL);
+		fs_sigrecv = 0;
+		fs_setsighandler();
+
+		/* fail_safe_loop returns -1 when we should suicide
+		 * 1 when we need a new kid, 0 to just wait a little longer */
+		while(1) {
+			foo = fail_safe_loop();
+			if(foo == 1) goto spawn_again;
+			if(foo == -1) exit(0);
+		}
+	}
+
+	/* not reached */
+	return 0;
+}
diff -urN ../nagios.orig/base/nagios.c ./base/nagios.c
--- ../nagios.orig/base/nagios.c	Mon Apr  4 20:26:26 2005
+++ ./base/nagios.c	Mon Apr  4 20:29:18 2005
@@ -596,7 +596,7 @@
 				write_to_all_logs(buffer,NSLOG_PROCESS_INFO);
 
 				/* get new PID */
-				nagios_pid=(int)getpid();
+				nagios_pid=(int)become_failsafe();
 #endif
 			        }
 
diff -urN ../nagios.orig/base/utils.c ./base/utils.c
--- ../nagios.orig/base/utils.c	Tue Mar 29 10:49:39 2005
+++ ./base/utils.c	Mon Apr  4 20:40:55 2005
@@ -2377,10 +2377,10 @@
 
 
 /* saves the name of a macro */
-int add_macrox_name(int index, char *name){
+int add_macrox_name(int idx, char *name){
 
 	/* dup the macro name */
-	macro_x_names[index]=strdup(name);
+	macro_x_names[idx]=strdup(name);
 
 	return OK;
         }
@@ -3413,6 +3413,7 @@
 	struct flock lock;
 	char temp_buffer[MAX_INPUT_BUFFER];
 	char *homedir=NULL;
+	static int previously_daemonized = 0;
 
 #ifdef RLIMIT_CORE
 	struct rlimit limit;
@@ -3425,6 +3426,11 @@
 	else
 		chdir("/");
 
+	/* if we have previously daemonized, we need to skip the
+	 * lock-file part and such, because we won't be forking again
+	 * and thus we could encounter an error (spinlock?) on our own pidfile */
+	if(previously_daemonized) goto daemon_init_out;
+
 	umask(S_IWGRP|S_IWOTH);
 
 	lockfile=open(lock_file,O_RDWR | O_CREAT, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
@@ -3526,15 +3532,6 @@
 		exit(ERROR);
 	        }
 
-	/* prevent daemon from dumping a core file... */
-#ifdef RLIMIT_CORE
-	if(daemon_dumps_core==FALSE){
-		getrlimit(RLIMIT_CORE,&limit);
-		limit.rlim_cur=0;
-		setrlimit(RLIMIT_CORE,&limit);
-	        }
-#endif
-
 	/* write PID to lockfile... */
 	lseek(lockfile,0,SEEK_SET);
 	ftruncate(lockfile,0);
@@ -3557,6 +3554,20 @@
 	open("/dev/null",O_WRONLY);
 	open("/dev/null",O_WRONLY);
 
+	/* ugly, but serviceable. daemon_dumps_core might be changed between
+	 * passes of reading the config, so we need to put the label above it */
+	daemon_init_out:
+	previously_daemonized = 1;
+
+	/* prevent daemon from dumping a core file... */
+#ifdef RLIMIT_CORE
+	if(daemon_dumps_core==FALSE){
+		getrlimit(RLIMIT_CORE,&limit);
+		limit.rlim_cur=0;
+		setrlimit(RLIMIT_CORE,&limit);
+	        }
+#endif
+
 #ifdef USE_EVENT_BROKER
 	/* send program data to broker */
 	broker_program_state(NEBTYPE_PROCESS_DAEMONIZE,NEBFLAG_NONE,NEBATTR_NONE,NULL);
@@ -3608,8 +3619,9 @@
 		else
 			gid=(gid_t)atoi(group);
 
-		/* set effective group ID if other than current EGID */
-		if(gid!=getegid()){
+		/* set effective group ID if other than current EGID,
+		 * but only if we aren't being invoked as a non-root group */
+		if(gid!=getegid() && getegid()==0){
 
 			if(setgid(gid)==-1){
 				snprintf(temp_buffer,sizeof(temp_buffer)-1,"Warning: Could not set effective GID=%d",(int)gid);
@@ -3642,7 +3654,8 @@
 			
 #ifdef HAVE_INITGROUPS
 
-		if(uid!=geteuid()){
+		/* only abort if we were invoked as root and failed to drop privs */
+		if(uid!=geteuid() && geteuid()==0){
 
 			/* initialize supplementary groups */
 			if(initgroups(user,gid)==-1){
@@ -3660,7 +3673,8 @@
 	                        }
 		        }
 #endif
-		if(setuid(uid)==-1){
+		/* only abort if we were invoked as root and failed to drop privs */
+		if(setuid(uid)==-1 && getuid()==0){
 			snprintf(temp_buffer,sizeof(temp_buffer)-1,"Warning: Could not set effective UID=%d",(int)uid);
 			temp_buffer[sizeof(temp_buffer)-1]='\x0';
 			write_to_logs_and_console(temp_buffer,NSLOG_RUNTIME_WARNING,TRUE);
@@ -4382,7 +4396,7 @@
 
 	/* find the end of the string (or buffer) */
 	for(x=temp_mmapfile->current_position;x<temp_mmapfile->file_size;x++){
-		if(*(char *)(temp_mmapfile->mmap_buf+x)=='\n'){
+		if(*((char *)temp_mmapfile->mmap_buf+x)=='\n'){
 			x++;
 			break;
 			}
@@ -4396,7 +4410,7 @@
 		return NULL;
 
 	/* copy string to newly allocated memory and terminate the string */
-	memcpy(buf,(char *)(temp_mmapfile->mmap_buf+temp_mmapfile->current_position),len);
+	memcpy(buf,(char *)temp_mmapfile->mmap_buf+temp_mmapfile->current_position,len);
 	buf[len]='\x0';
 
 	/* update the current position */
diff -urN ../nagios.orig/daemon-init.in ./daemon-init.in
--- ../nagios.orig/daemon-init.in	Tue Mar 29 10:49:38 2005
+++ ./daemon-init.in	Tue Apr  5 00:19:11 2005
@@ -138,7 +138,7 @@
 		status_nagios nagios
 		;;
 
-	restart)
+	restart|reload|force-reload)
 		printf "Running configuration check..."
 		$NagiosBin -v $NagiosCfgFile > /dev/null 2>&1;
 		if [ $? -eq 0 ]; then
@@ -148,31 +148,6 @@
 		else
 			#$NagiosBin -v $NagiosCfgFile
 			echo " FAILED!  Restart aborted.  Check your Nagios configuration."
-			exit 1
-		fi
-		;;
-
-	reload|force-reload)
-		printf "Running configuration check..."
-		$NagiosBin -v $NagiosCfgFile > /dev/null 2>&1;
-		if [ $? -eq 0 ]; then
-			echo "done"
-			if test ! -f $NagiosRunFile; then
-				$0 start
-			else
-				NagiosPID=`head -n 1 $NagiosRunFile`
-				if status_nagios > /dev/null; then
-					printf "Reloading nagios configuration..."
-					killproc_nagios nagios -HUP
-					echo "done"
-				else
-					$0 stop
-					$0 start
-				fi
-			fi
-		else
-			#$NagiosBin -v $NagiosCfgFile
-			echo " FAILED!  Reload aborted.  Check your Nagios configuration."
 			exit 1
 		fi
 		;;
diff -urN ../nagios.orig/include/nagios.h.in ./include/nagios.h.in
--- ../nagios.orig/include/nagios.h.in	Tue Mar 29 10:49:39 2005
+++ ./include/nagios.h.in	Mon Apr  4 20:29:18 2005
@@ -759,3 +759,5 @@
 int set_macro_environment_var(char *,char *,int);
 #endif
 
+/* failsafety (kludgy workaround sort of thing, to be removed later) */
+int become_failsafe(void);
