Now everything works - the rfc3164 message parser must be improved.
authorviric@llimona
Thu, 06 Apr 2006 01:13:18 +0200
changeset 15 fea6e87812f0
parent 14 ef38057e925a
child 16 de00e98450eb
Now everything works - the rfc3164 message parser must be improved.
Makefile
Makefile.common
rfc3164.c
syslog_in_npipe.c
syslog_in_udp.c
syslog_in_unix.c
syslog_kernel.c
unix_writer.c
--- a/Makefile	Thu Mar 30 00:26:58 2006 +0200
+++ b/Makefile	Thu Apr 06 01:13:18 2006 +0200
@@ -1,28 +1,10 @@
 # GNU CC
 CC=gcc
-CFLAGS=-ansi -pedantic -std=c99 -g
+CODE_OPTIONS=-D_POSIX_C_SOURCE=200112L -D_ISOC99_SOURCE
+CFLAGS=-Wall -ansi -pedantic -std=c99 -g $(CODE_OPTIONS)
 
 # Tiny C
 #CC=tcc
 #CFLAGS=
 
-all: syslog_in_npipe syslog_in_udp syslog_in_unix unix_writer
-
-%.a: 
-	ar rc $@ $<
-
-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: syslog_in_npipe.o rfc3164.a
-syslog_in_udp: syslog_in_udp.o rfc3164.a
-syslog_in_unix: syslog_in_unix.o rfc3164.a
-unix_writer: unix_writer.o
-
-clean:
-	rm -f *.o *.a
-	rm -f syslog_in_npipe syslog_in_udp syslog_in_unix
+include Makefile.common
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Makefile.common	Thu Apr 06 01:13:18 2006 +0200
@@ -0,0 +1,32 @@
+# GNU CC
+#CC=gcc
+#CODE_OPTIONS=-D_POSIX_C_SOURCE=200112L -D_ISOC99_SOURCE
+#CFLAGS=-Wall -ansi -pedantic -std=c99 -g $(CODE_OPTIONS)
+
+# Tiny C
+#CC=tcc
+#CFLAGS=
+
+all: syslog_in_npipe syslog_in_udp syslog_in_unix unix_writer syslog_kernel
+
+%.a: 
+	ar src $@ $<
+
+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_kernel.o: syslog_kernel.c rfc3164.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
+unix_writer: unix_writer.o
+syslog_kernel: syslog_kernel.o rfc3164.a
+
+clean:
+	rm -f *.o *.a
+	rm -f syslog_in_npipe syslog_in_udp syslog_in_unix unix_writer \
+		syslog_kernel
--- a/rfc3164.c	Thu Mar 30 00:26:58 2006 +0200
+++ b/rfc3164.c	Thu Apr 06 01:13:18 2006 +0200
@@ -1,12 +1,13 @@
 #include "rfc3164.h"
-#include <stdio.h>
+#include <stdio.h>  // snprintf
+#include <unistd.h> // write
+#include <string.h> // strlen
 
 
 int process_message(char * restrict buffer)
 {
 	int i;
-	char lletra;
-	int status;
+	char message[MESSAGE_LENGTH+1];
 
 	for(i=0; i<MESSAGE_LENGTH; i++)
 	{
@@ -22,9 +23,12 @@
 		}
 	}
 	if(i == MESSAGE_LENGTH)
-		buffer[MESSAGE_LENGTH-1] == '\0';
+		buffer[MESSAGE_LENGTH-1] = '\0';
 	/* Debug */
-	fprintf(stderr,"M: %s\n", buffer);
+	/* fprintf(stderr,"M: %s\n", buffer); */
+
+	snprintf(message, MESSAGE_LENGTH+1,"%s\n", buffer);
+	write(1, message, strlen(message));
 
 	return i;
 }
--- a/syslog_in_npipe.c	Thu Mar 30 00:26:58 2006 +0200
+++ b/syslog_in_npipe.c	Thu Apr 06 01:13:18 2006 +0200
@@ -26,7 +26,7 @@
 	result = stat(path, &tmpStatus);
 	if (result == -1)
 	{
-		printf("Error trying to stat the file: %s\n", 
+		fprintf(stderr, "Error trying to stat the file: %s\n", 
 			strerror(errno));
 		abort();
 	}
@@ -55,14 +55,14 @@
 		/* Obrim la named fifo síncrona. */
 		fd_entrada = open(filename, O_RDONLY);
 		if (fd_entrada < 0) {
-			printf("Error opening the named pipe: %s\n",
+			fprintf(stderr, "Error opening the named pipe: %s\n",
 				strerror(errno));
 			return 1;
 		}
 
 		status = fcntl(fd_entrada, F_SETLKW, &blocking);
 		if (fd_entrada < 0) {
-			printf("Error locking the named pipe: %s\n",
+			fprintf(stderr, "Error locking the named pipe: %s\n",
 				strerror(errno));
 			return 1;
 		}
@@ -71,20 +71,40 @@
 			MESSAGE_LENGTH)) > 0)
 		{
 			process_message(missatge);
+			/* Debug */
+			if(!strcmp(missatge,"close"))
+			{
+				/* Code duplication !! */
+				blocking.l_type = F_UNLCK;
+				status = fcntl(fd_entrada, F_SETLKW, &blocking);
+				if (fd_entrada < 0) {
+					fprintf(stderr,
+						"Error unlocking the named "
+						"pipe: %s\n",
+						strerror(errno));
+					abort();
+				}
+				close (fd_entrada);
+				/* End of code duplication */
+				return 1;
+			}
 		}
 
 		if (status < 0)
 		{
-			printf("Error reading the named pipe: %s\n",
+			fprintf(stderr, "Error reading the named pipe: %s\n",
 				strerror(errno));
+			/* Code duplication !! */
 			blocking.l_type = F_UNLCK;
 			status = fcntl(fd_entrada, F_SETLKW, &blocking);
 			if (fd_entrada < 0) {
-				printf("Error unlocking the named pipe: %s\n",
+				fprintf(stderr,
+					"Error unlocking the named pipe: %s\n",
 					strerror(errno));
 				abort();
 			}
 			close (fd_entrada);
+			/* End of code duplication */
 			return 1;
 		}
 	}
@@ -92,7 +112,9 @@
 	blocking.l_type = F_UNLCK;
 	status = fcntl(fd_entrada, F_SETLKW, &blocking);
 	if (fd_entrada < 0) {
-		printf("Error unlocking the named pipe: %s\n", strerror(errno));
+		fprintf(stderr,
+			"Error unlocking the named pipe: %s\n",
+			strerror(errno));
 		abort();
 	}
 
@@ -112,7 +134,7 @@
 	/* !!! */
 	if(!isNamedPipe(argv[1])) // Abortarà si falla
 	{
-		printf("The file is not of a named pipe.\n");
+		fprintf(stderr, "The file is not of a named pipe.\n");
 		abort();
 	};
 
--- a/syslog_in_udp.c	Thu Mar 30 00:26:58 2006 +0200
+++ b/syslog_in_udp.c	Thu Apr 06 01:13:18 2006 +0200
@@ -29,7 +29,7 @@
 	protocol = getprotobyname("udp");
 	if (protocol == NULL)
 	{
-		printf("Cannot get the protocol number for udp.\n");
+		fprintf(stderr, "Cannot get the protocol number for udp.\n");
 		abort();
 	}
 	
@@ -48,7 +48,7 @@
 	socket_ipv6 = socket(PF_INET6, SOCK_DGRAM, udp_proto_number() );
 	if (socket_ipv6 == -1)
 	{
-		printf("IPv6 socket failed.\n");
+		fprintf(stderr, "IPv6 socket failed.\n");
 		abort();
 	}
 
@@ -57,7 +57,8 @@
 	if (setsockopt(socket_ipv6, SOL_SOCKET, SO_REUSEADDR,
 	                         (char *)&on,sizeof(on)) < 0)
 	{
-		printf("IPv6 setsockopt() failed: %s.\n", strerror(errno));
+		fprintf(stderr, "IPv6 setsockopt() failed: %s.\n",
+			strerror(errno));
 		abort();
 	}
 	
@@ -95,7 +96,7 @@
 	socket_ipv4 = socket(PF_INET, SOCK_DGRAM, udp_proto_number() );
 	if (socket_ipv4 == -1)
 	{
-		printf("IPv4 socket not supported.\n");
+		fprintf(stderr, "IPv4 socket not supported.\n");
 		abort();
 	}
 
@@ -104,7 +105,8 @@
 	if (setsockopt(socket_ipv4, SOL_SOCKET, SO_REUSEADDR,
 	                         (char *)&on,sizeof(on)) < 0)
 	{
-		printf("IPv4 setsockopt() failed: %s.\n", strerror(errno));
+		fprintf(stderr, "IPv4 setsockopt() failed: %s.\n",
+			strerror(errno));
 		abort();
 	}
 
@@ -129,7 +131,7 @@
 	return socket_ipv4;
 }
 
-int server_loop(int port)
+void server_loop(int port)
 {
 	int socket_ipv6, socket_ipv4;
 	fd_set listen_sockets;
@@ -168,7 +170,8 @@
 			continue;
 		else if (result == -1)
 		{
-			printf("Error in select(): %s\n", strerror(errno));
+			fprintf(stderr, "Error in select(): %s\n",
+				strerror(errno));
 			abort();
 		}
 
@@ -177,12 +180,18 @@
 		{
 			recv(socket_ipv4, message, MESSAGE_LENGTH, 0);
 			process_message(message);
+			/* Debug */
+			if(!strcmp(message,"close"))
+				break;
 		}
 
 		if (FD_ISSET(socket_ipv6, &listen_sockets))
 		{
 			recv(socket_ipv6, message, MESSAGE_LENGTH, 0);
 			process_message(message);
+			/* Debug */
+			if(!strcmp(message,"close"))
+				break;
 		}
 	}
 }
--- a/syslog_in_unix.c	Thu Mar 30 00:26:58 2006 +0200
+++ b/syslog_in_unix.c	Thu Apr 06 01:13:18 2006 +0200
@@ -21,7 +21,7 @@
 	int socket_unix;
 	struct sockaddr_un source_unix;
 	int result;
-	int on=1;
+	int sockopt;
 
 
 	socket_unix = socket(PF_UNIX, SOCK_DGRAM, 0 );
@@ -31,6 +31,12 @@
 		abort();
 	}
 
+	/* Allow socket reuse */
+	sockopt = 1;
+	result = setsockopt(socket_unix, SOL_SOCKET, SO_REUSEADDR, &sockopt,
+		sizeof(sockopt));
+	assert(result == 0);
+
 	/* Unix socket listen address */
 	memset(&source_unix, 0, sizeof(source_unix));
 	source_unix.sun_family = AF_UNIX;
@@ -44,7 +50,7 @@
 	return socket_unix;
 }
 
-int server_loop(const char * restrict socketname)
+void server_loop(const char * restrict socketname)
 {
 	int socket_unix;
 	int result;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/syslog_kernel.c	Thu Apr 06 01:13:18 2006 +0200
@@ -0,0 +1,146 @@
+#include <sys/types.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdbool.h>
+#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 "rfc3164.h"
+
+const char syslog_in_unix[] = "syslog_in_unix";
+const char syslog_in_npipe[] = "syslog_in_npipe";
+const char syslog_in_udp[] = "syslog_in_udp";
+
+#define TEST_NPIPE "prova_npipe"
+#define TEST_UDP 4000
+#define TEST_UNIX "prova_unix"
+
+void child_handler(int parameter)
+{
+	pid_t pid;
+	int status;
+
+	do
+	{
+		pid = waitpid(-1, &status, WNOHANG);
+		/* Debug */
+		if (WIFEXITED(status))
+		{
+			fprintf(stderr, "Child %i died fine:%i\n",
+				pid, WEXITSTATUS(status));
+		} else 
+			fprintf(stderr, "Child %i signaled strange.\n",
+				pid);
+	} while(pid == 0);
+}
+
+int run_program(const char * restrict programname,
+		const char * restrict parameter1, int comm_pipe[])
+{
+	int pid;
+
+	pid = fork();
+	assert(pid != -1);
+	if (pid == 0)
+	{
+		close(0);
+		close(1);
+		dup(comm_pipe[0]);
+		dup(comm_pipe[1]);
+		execl(programname, programname, parameter1, NULL);
+		/* Unreachable */
+		fprintf(stderr, "Child exec failed(%s %s): %s\n",
+			programname, parameter1, strerror(errno));
+		abort();
+		return 0;
+	}
+	else
+	{
+		fprintf(stderr, "Child forked(%s %s): %i\n",
+			programname, parameter1, pid);
+		return pid;
+	}
+}
+
+int init_in_unix(const char * restrict socketname, int input_pipe[])
+{
+	return run_program(syslog_in_unix, socketname, input_pipe);
+}
+
+int init_in_udp(unsigned int port, int input_pipe[])
+{
+	char buffer[20];
+	
+	snprintf(buffer, 20, "%u", port);
+
+	return run_program(syslog_in_udp, buffer, input_pipe);
+}
+
+int init_in_npipe(const char * restrict pipename, int input_pipe[])
+{
+	return run_program(syslog_in_npipe, pipename, input_pipe);
+}
+
+void kernel_loop()
+{
+	int input_pipe[2];
+	char missatge[MESSAGE_LENGTH+1];
+	int status;
+
+	pipe(input_pipe);
+
+	init_in_npipe(TEST_NPIPE, input_pipe);
+	init_in_udp(TEST_UDP, input_pipe);
+	init_in_unix(TEST_UNIX, input_pipe);
+
+	/* We don't want to write to the programs */
+	close(input_pipe[1]);
+
+	while(true)
+	{
+		status = read(input_pipe[0], missatge, MESSAGE_LENGTH);
+
+		if (status > 0)
+		{
+			/* Add a ZERO */
+			missatge[status] = '\0';
+			/* The \n is included in the message */
+			printf("Received: %s",missatge);
+		}
+		else if (status == -1 && errno == EINTR)
+			continue;
+		else
+			break;
+	}
+
+	if (status == -1)
+	{
+		printf("Error reading from the main pipe: %s\n", 
+			strerror(errno));
+	}
+
+
+}
+
+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);
+}
+
+int main(int argn, char *argv[])
+{
+	install_signal_handlers();
+	kernel_loop();
+}
--- a/unix_writer.c	Thu Mar 30 00:26:58 2006 +0200
+++ b/unix_writer.c	Thu Apr 06 01:13:18 2006 +0200
@@ -19,7 +19,6 @@
 	int socket_unix;
 	struct sockaddr_un source_unix;
 	int result;
-	int on=1;
 
 
 	socket_unix = socket(PF_UNIX, SOCK_DGRAM, 0 );
@@ -27,6 +26,8 @@
 	{
 		printf("Unix socket creation error: %s\n", strerror(errno));
 		abort();
+		/* Unreachable */
+		return -1;
 	}
 
 	/* Unix socket listen address */
@@ -39,6 +40,8 @@
 	{
 		printf("Unix socket connect error: %s\n", strerror(errno));
 		abort();
+		/* Unreachable */
+		return -1;
 	}
 
 	/* Here we should take care of the socket permissions */
@@ -46,7 +49,7 @@
 	return socket_unix;
 }
 
-int writer_loop(const char * restrict socketname)
+void writer_loop(const char * restrict socketname)
 {
 	int socket_unix;
 	int result;