Log file output written in a C module. Now the kernel doesn't check the errors
authorviric@mandarina
Mon, 15 May 2006 16:57:37 +0200
changeset 39 60858d13b22c
parent 38 65c2ca03cc0f
child 40 8c781743e0d0
Log file output written in a C module. Now the kernel doesn't check the errors in output.
Makefile.common
TODO.txt
out_file.c
out_udp.c
syslog.h
syslog_in_unix.c
syslog_kernel.c
--- a/Makefile.common	Mon May 15 15:19:07 2006 +0200
+++ b/Makefile.common	Mon May 15 16:57:37 2006 +0200
@@ -17,13 +17,14 @@
 syslog_kernel.o: syslog_kernel.c rfc3164.h syslog.h
 config.o: syslog.h
 out_udp.o: syslog.h
+out_file.o: syslog.h
 signals.o: syslog.h
 
 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 signals.o out_udp.o
+syslog_kernel: syslog_kernel.o config.o rfc3164.a signals.o out_udp.o out_file.o
 
 clean:
 	rm -f *.o *.a
--- a/TODO.txt	Mon May 15 15:19:07 2006 +0200
+++ b/TODO.txt	Mon May 15 16:57:37 2006 +0200
@@ -13,3 +13,8 @@
 
 * The childs should control SIGINT. Otherwise, they become killed on Control-C
   under an xterm.
+
+Known problems
+----------------
+Maybe the incoming data will be packetized, or even the outcomming. We don't
+manage that.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/out_file.c	Mon May 15 16:57:37 2006 +0200
@@ -0,0 +1,89 @@
+#include <sys/types.h>
+#include <string.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <assert.h>
+#include <fcntl.h>
+
+#include "syslog.h"
+
+/* Prototypes */
+static int init_handle(const char * restrict name);
+
+/* Local globals */
+static int out_file;
+static bool file_enabled = false;
+
+
+/* Returns -2 when the file wasn't initialized by config, 0 when all was ok,
+ * -1 on more important error */
+int init_out_file()
+{
+	char name[MAX_STRING];
+
+	assert (get_config(LOG_FILE, name, MAX_STRING) > 0);
+
+	if (strncmp(name, "disabled", MAX_STRING) == 0)
+		return -2;
+
+	return init_handle(name);
+}
+
+/* Returns 0 when all was ok, -1 on error */
+static int init_handle(const char * restrict name)
+{
+	/* We must specify the mode, because we add O_CREAD */
+	out_file = open(name, O_WRONLY | O_APPEND | O_CREAT, 0600);
+	if (out_file == -1)
+	{
+		fprintf(stderr, "Error opening the log file: %s\n",
+				strerror(errno));
+		return -1;
+	}
+
+	file_enabled = true;
+
+	return 0;
+}
+
+
+/* Return -2 when the file is not enabled. Otherwise, the output of send().*/
+int write_out_file(const char * restrict buf, const int len)
+{
+	int res;
+
+	if (!file_enabled)
+		return -2;
+
+	res = write(out_file, buf, len);
+	if (res == -1)
+	{
+		fprintf(stderr, "Error writing to the log file: %s\n", 
+			strerror(errno));
+	} 
+
+	return res;
+}
+
+/* Return -2 when the file is not enabled. Otherwise, the output of close().*/
+int close_out_file()
+{
+	int res;
+
+	if (!file_enabled)
+		return -2;
+
+	res = close(out_file);
+
+	if (res == -1)
+	{
+		fprintf(stderr, "Error closing the log file: %s\n", 
+			strerror(errno));
+	}
+	file_enabled = false;
+
+	return res;
+	
+}
--- a/out_udp.c	Mon May 15 15:19:07 2006 +0200
+++ b/out_udp.c	Mon May 15 16:57:37 2006 +0200
@@ -38,8 +38,7 @@
 	return init_socket(node, service);
 }
 
-/* Returns -2 when the socket wasn't initialized by config, 0 when all was ok,
- * -1 on more important error */
+/* Returns 0 when all was ok, -1 on error */
 static int init_socket(const char * restrict node,
 		const char * restrict service)
 {
@@ -104,20 +103,38 @@
 
 
 /* Return -2 when the socket is not enabled. Otherwise, the output of send().*/
-int write_out_udp(const char *buf, const int len)
+int write_out_udp(const char * restrict buf, const int len)
 {
+	int res;
+
 	if (!socket_enabled)
 		return -2;
 
-	return send(out_socket, buf, len, 0);
+	res = send(out_socket, buf, len, 0);
+
+	if (res == -1)
+	{
+		fprintf(stderr, "Error sending to the UDP socket: %s\n", 
+			strerror(errno));
+	}
+
+	return res;
 }
 
 /* Return -2 when the socket is not enabled. Otherwise, the output of close().*/
 int close_out_udp()
 {
+	int res;
 	if (!socket_enabled)
 		return -2;
 
-	return close(out_socket);
-	
+	res = close(out_socket);
+
+	if (res == -1)
+	{
+		fprintf(stderr, "Error closing the output UDP socket: %s\n", 
+			strerror(errno));
+	}
+
+	return res;
 }
--- a/syslog.h	Mon May 15 15:19:07 2006 +0200
+++ b/syslog.h	Mon May 15 16:57:37 2006 +0200
@@ -34,5 +34,10 @@
 
 /* out_udp.c */
 int init_out_udp();
-int write_out_udp(const char *buf, const int len);
+int write_out_udp(const char * restrict buf, const int len);
 int close_out_udp();
+
+/* out_file.c */
+int init_out_file();
+int write_out_file(const char * restrict buf, const int len);
+int close_out_file();
--- a/syslog_in_unix.c	Mon May 15 15:19:07 2006 +0200
+++ b/syslog_in_unix.c	Mon May 15 16:57:37 2006 +0200
@@ -90,6 +90,8 @@
 		/* Prepare the fd_set */
 		FD_ZERO(&read_fd_set);
 		FD_SET(socket_unix, &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;
 		result = select(socket_unix+1, &read_fd_set, NULL, NULL,
--- a/syslog_kernel.c	Mon May 15 15:19:07 2006 +0200
+++ b/syslog_kernel.c	Mon May 15 16:57:37 2006 +0200
@@ -179,24 +179,20 @@
 	}
 }
 
+static int output_message(const char * restrict msg, const int len)
+{
+	write_out_udp(msg, len);
+	write_out_file(msg, len);
+
+	return 0;
+}
+
 static void kernel_loop()
 {
 	int input_pipe[2];
 	char missatge[MESSAGE_LENGTH+1];
-	int res_r, res_w, res_u; /* resultats de la crida read, write, w_udp */
-	int errno_r, errno_w, errno_u;  /* errno de les crides */
-	int output;
-	char value[MAX_STRING];
-
-	assert (get_config(LOG_FILE, value, MAX_STRING) > 0);
-	/* We must specify the mode, because we add O_CREAD */
-	output = open(value, O_WRONLY | O_APPEND | O_CREAT, 0600);
-	if (output == -1)
-	{
-		fprintf(stderr, "Error opening the log file: %s\n",
-				strerror(errno));
-		return;
-	}
+	int res_r; /* resultat de la crida read */
+	int errno_r;  /* errno de les crides */
 
 	/* Pipe for the childs */
 	pipe(input_pipe);
@@ -211,49 +207,14 @@
 		res_r = read(input_pipe[0], missatge, MESSAGE_LENGTH);
 		errno_r = errno;
 
-		res_w = 1; /* To bypass further error checking */
-		res_u = 1; /* To bypass further error checking */
 		if (res_r > 0)
 		{
+			/* Output to screen */
 			/* Add a ZERO for displaying */
 			missatge[res_r] = '\0';
-
-			/* Output to screen */
 			fprintf(stderr, "Received: %s\n",missatge);
-
-			/* Output to file */
-			/* The \0 end is not written */
-			do
-			{
-				res_w = write(output, missatge, res_r);
-				if (res_w == 0)
-					break;
-			}
-			/* Protect against EINTR */
-			while(res_w == -1 && errno == EINTR);
-
-			if (res_w == -1 || res_w == 0)
-			{
-				errno_w = errno;
-				break;
-			}
-
-			/* Output to UDP */
-			/* The \0 end is not sent */
-			do
-			{
-				res_u = write_out_udp(missatge, res_r);
-				if (res_u == res_r)
-					break;
-			}
-			/* Protect against EINTR */
-			while(res_u == -1 && errno == EINTR);
-
-			if (res_u == -1 || res_u == 0)
-			{
-				errno_u = errno;
-				break;
-			}
+			
+			output_message(missatge, res_r);
 		}
 		else if (res_r == -1 && errno == EINTR)
 		{
@@ -276,22 +237,6 @@
 			strerror(errno_r));
 	}
 	close(input_pipe[0]);
-
-	if (res_w == -1)
-	{
-		fprintf(stderr, "Error writing to the log file: %s\n", 
-			strerror(errno_w));
-	} else if (res_w == 0)
-		fprintf(stderr,"Cannot write anymore to the log file.");
-
-	if (res_u == -1)
-	{
-		fprintf(stderr, "Error writing to the UDP socket: %s\n", 
-			strerror(errno_u));
-	} else if (res_w == 0)
-		fprintf(stderr,"Cannot write anymore to the UDP socket.");
-
-	close(output);
 }
 
 static void install_signal_handlers()
@@ -356,16 +301,24 @@
 
 		res = init_out_udp();
 		if (res == -2)
-			fprintf(stderr, "UDP output not in configuration.\n");
+			fprintf(stderr, "UDP output disabled.\n");
 		else if (res == -1)
 			fprintf(stderr, "error setting up the UDP output.\n");
 
+		res = init_out_file();
+		if (res == -2)
+			fprintf(stderr, "File output disabled.\n");
+		else if (res == -1)
+			fprintf(stderr, "error setting up the File output.\n");
+
 		kernel_loop();
 
 		term_childs();
 
+		close_out_udp();
+		close_out_file();
+
 		wait_childs_die();
-		close_out_udp();
 	}
 	while(reconfig == true);
 }