The kernel now understands SIGHUP for rereading the configuration.
authorviric@llimona
Mon, 15 May 2006 17:29:22 +0200
changeset 42 95f305aef8e3
parent 41 fc273c0e8250
child 43 9dac7f0c8e2a
The kernel now understands SIGHUP for rereading the configuration.
syslog_kernel.c
--- a/syslog_kernel.c	Mon May 15 17:29:03 2006 +0200
+++ b/syslog_kernel.c	Mon May 15 17:29:22 2006 +0200
@@ -9,6 +9,7 @@
 #include <stdlib.h> // Per l'abort
 #include <fcntl.h> // Per l'open
 #include <sys/stat.h> // Per l'open
+#include <sys/select.h> // Pel select()
 
 #include "rfc3164.h"
 #include "syslog.h"
@@ -181,18 +182,26 @@
 
 static int output_message(const char * restrict msg, const int len)
 {
-	write_out_udp(msg, len);
-	write_out_file(msg, len);
+	int res1, res2, res;
+
+	res = 0;
 
-	return 0;
+	res1 = write_out_udp(msg, len);
+	res2 = write_out_file(msg, len);
+
+	if (res1 == -1 || res2 == -1)
+		res = -1;
+
+	return res;
 }
 
 static void kernel_loop()
 {
 	int input_pipe[2];
 	char missatge[MESSAGE_LENGTH+1];
-	int res_r; /* resultat de la crida read */
-	int errno_r;  /* errno de les crides */
+	int res; /* resultat de la crida read */
+	fd_set read_fd_set;
+	struct timeval read_timeout;
 
 	/* Pipe for the childs */
 	pipe(input_pipe);
@@ -202,40 +211,48 @@
 	/* We don't want to write to the programs */
 	close(input_pipe[1]);
 
+	FD_ZERO(&read_fd_set);
 	while(true)
 	{
-		res_r = read(input_pipe[0], missatge, MESSAGE_LENGTH);
-		errno_r = errno;
-
-		if (res_r > 0)
+		/* pipe read handle */
+		if (FD_ISSET(input_pipe[0], &read_fd_set))
 		{
-			/* Output to screen */
-			/* Add a ZERO for displaying */
-			missatge[res_r] = '\0';
-			fprintf(stderr, "Received: %s\n",missatge);
-			
-			output_message(missatge, res_r);
+			res = read(input_pipe[0], missatge, MESSAGE_LENGTH);
+			assert( res != -1 );
+
+			if (res > 0)
+			{
+				/* Output to screen */
+				/* Add a ZERO for displaying */
+				missatge[res] = '\0';
+				fprintf(stderr, "Received: %s\n",missatge);
+				
+				output_message(missatge, res);
+			}
 		}
-		else if (res_r == -1 && errno == EINTR)
-		{
-			/* The call was interrupted by a signal */
-			if (childs_alive == 0)
-				break;
-			else
-				continue;
-		}
-		else /* End of read: res_r -1 or 0 */
+
+		/* The select call may be interrupted by a signal */
+		if (childs_alive == 0)
 			break;
 
 		if (reconfig == true)
 			break;
+
+		/* Prepare the fd_set */
+		FD_ZERO(&read_fd_set);
+		FD_SET(input_pipe[0], &read_fd_set);
+		/* We set the timeout in order to save us easily from the race
+		 * condition for signal receiving (select/pselect) */
+		read_timeout.tv_sec = 5;
+		read_timeout.tv_usec = 0;
+		res = select(input_pipe[0]+1, &read_fd_set, NULL, NULL,
+				&read_timeout);
+		if (res == -1)
+			FD_ZERO(&read_fd_set);
+
+		assert( !(res == -1 && errno != EINTR) );
 	}
 
-	if (res_r == -1)
-	{
-		fprintf(stderr, "Error reading from the main pipe: %s\n", 
-			strerror(errno_r));
-	}
 	close(input_pipe[0]);
 }
 
@@ -244,7 +261,7 @@
 	program_child_handler(child_handler);
 	program_handler(SIGTERM, term_handler);
 	program_handler(SIGINT, term_handler);
-	program_handler(SIGUSR1, reconfig_handler);
+	program_handler(SIGHUP, reconfig_handler);
 }
 
 static void term_childs()