Added a c file for signal programming.
authorviric@mandarina
Sat, 13 May 2006 23:50:24 +0200
changeset 26 7227789ca718
parent 25 31370b4c0627
child 27 b01603838e42
Added a c file for signal programming. Managed the error of waitpid for basic catching the no-more-childs case, at least showing info.
Makefile.common
signals.c
syslog.h
syslog_in_npipe.c
syslog_in_udp.c
syslog_in_unix.c
syslog_kernel.c
--- a/Makefile.common	Sat May 13 23:30:53 2006 +0200
+++ b/Makefile.common	Sat May 13 23:50:24 2006 +0200
@@ -11,17 +11,18 @@
 rfc3164.o: rfc3164.h
 rfc3164.a: rfc3164.o
 
-syslog_in_npipe.o: rfc3164.h syslog_in_npipe.c
-syslog_in_udp.o: syslog_in_udp.c rfc3164.h
-syslog_in_unix.o: syslog_in_unix.c rfc3164.h
+syslog_in_npipe.o: rfc3164.h syslog_in_npipe.c syslog.h
+syslog_in_udp.o: syslog_in_udp.c rfc3164.h syslog.h
+syslog_in_unix.o: syslog_in_unix.c rfc3164.h syslog.h
 syslog_kernel.o: syslog_kernel.c rfc3164.h syslog.h
 config.o: syslog.h
+signals.o: syslog.h
 
-syslog_in_npipe: syslog_in_npipe.o rfc3164.a
-syslog_in_udp: syslog_in_udp.o rfc3164.a
-syslog_in_unix: syslog_in_unix.o rfc3164.a
+syslog_in_npipe: syslog_in_npipe.o rfc3164.a signals.o
+syslog_in_udp: syslog_in_udp.o rfc3164.a signals.o
+syslog_in_unix: syslog_in_unix.o rfc3164.a signals.o
 unix_writer: unix_writer.o
-syslog_kernel: syslog_kernel.o config.o rfc3164.a
+syslog_kernel: syslog_kernel.o config.o rfc3164.a signals.o
 
 clean:
 	rm -f *.o *.a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/signals.c	Sat May 13 23:50:24 2006 +0200
@@ -0,0 +1,29 @@
+#include <signal.h> // Pels senyals
+#include <string.h> // Pel memset
+#include <assert.h>
+
+void program_ignore_hup()
+{
+	struct sigaction my_action;
+	int result;
+
+	my_action.sa_handler = SIG_IGN;
+	my_action.sa_flags = SA_NOCLDSTOP;
+	memset(&my_action.sa_mask, 0, sizeof(my_action.sa_mask));
+
+	result = sigaction(SIGHUP, &my_action, NULL);
+	assert(result == 0);
+}
+
+void program_child_handler(void (*child_handler)(int))
+{
+	struct sigaction my_action;
+	int result;
+
+	my_action.sa_handler = child_handler;
+	my_action.sa_flags = SA_NOCLDSTOP;
+	memset(&my_action.sa_mask, 0, sizeof(my_action.sa_mask));
+
+	result = sigaction(SIGCHLD, &my_action, NULL);
+	assert(result == 0);
+}
--- a/syslog.h	Sat May 13 23:30:53 2006 +0200
+++ b/syslog.h	Sat May 13 23:50:24 2006 +0200
@@ -24,6 +24,9 @@
 /* Prototypes -------------------- */
 
 /* config.c */
-
 void init_config();
 int get_config(const enum etype_config type, char * restrict value, int max);
+
+/* signals.c */
+void program_child_handler(void (*child_handler)(int));
+void program_ignore_hup();
--- a/syslog_in_npipe.c	Sat May 13 23:30:53 2006 +0200
+++ b/syslog_in_npipe.c	Sat May 13 23:50:24 2006 +0200
@@ -11,6 +11,7 @@
 #include <fcntl.h>  // Pel lockf()
 
 #include "rfc3164.h"
+#include "syslog.h"
 
 
 void showHelp(const char * restrict program)
@@ -139,6 +140,6 @@
 		abort();
 	};
 
-
+	program_ignore_hup();
 	return pipe_server(argv[1]);
 }
--- a/syslog_in_udp.c	Sat May 13 23:30:53 2006 +0200
+++ b/syslog_in_udp.c	Sat May 13 23:50:24 2006 +0200
@@ -13,6 +13,7 @@
 #include <sys/select.h> // Pel select()
 
 #include "rfc3164.h"
+#include "syslog.h"
 
 #define LISTEN_QUEUE = 10;
 
@@ -224,6 +225,7 @@
 		return 2;
 	}
 
+	program_ignore_hup();
 	server_loop(port);
 
 	return 0;
--- a/syslog_in_unix.c	Sat May 13 23:30:53 2006 +0200
+++ b/syslog_in_unix.c	Sat May 13 23:50:24 2006 +0200
@@ -9,6 +9,7 @@
 #include <assert.h> // Per assert()
 
 #include "rfc3164.h"
+#include "syslog.h"
 
 void show_help(const char * restrict program)
 {
@@ -91,6 +92,8 @@
 		return 2;
 	}
 
+	program_ignore_hup();
+
 	server_loop(argv[1]);
 
 	return 0;
--- a/syslog_kernel.c	Sat May 13 23:30:53 2006 +0200
+++ b/syslog_kernel.c	Sat May 13 23:50:24 2006 +0200
@@ -5,7 +5,6 @@
 #include <stdio.h> // Per sprintf, fprintf
 #include <sys/wait.h> // Per wait
 #include <string.h> // Per memset
-#include <signal.h> // Pels senyals
 #include <assert.h> // Pels assert
 #include <stdlib.h> // Per l'abort
 #include <fcntl.h> // Per l'open
@@ -43,6 +42,20 @@
 	do
 	{
 		pid = waitpid(-1, &status, WNOHANG);
+		if (pid == -1)
+		{
+			if(errno == ECHILD)
+			{
+				/* We don't have more childs! */
+				fprintf(stderr, "We don't have more childs.\n");
+				break;
+			} else
+			{
+				fprintf(stderr, "waitpid failed. %s\n",
+					strerror(errno));
+				break;
+			}
+		}
 		/* Debug */
 		if (WIFEXITED(status))
 		{
@@ -213,15 +226,7 @@
 
 static void install_signal_handlers()
 {
-	struct sigaction my_action;
-	int result;
-
-	my_action.sa_handler = child_handler;
-	my_action.sa_flags = SA_NOCLDSTOP;
-	memset(&my_action.sa_mask, 0, sizeof(my_action.sa_mask));
-
-	result = sigaction(SIGCHLD, &my_action, NULL);
-	assert(result == 0);
+	program_child_handler(child_handler);
 }