Now the tcp_server sends the log file.
authorviric@llimona
Tue, 30 May 2006 12:02:13 +0200
changeset 55 c72dbd390cf2
parent 54 a456a2e5fca4
child 56 48b8acd6e878
Now the tcp_server sends the log file. The tcp server is now shutted down by its own function call.
syslog.conf
syslog.h
syslog_kernel.c
tcp_server.c
--- a/syslog.conf	Tue May 30 01:16:33 2006 +0200
+++ b/syslog.conf	Tue May 30 12:02:13 2006 +0200
@@ -1,5 +1,4 @@
 from_unix=patata
-to_udp_host=localhost
-to_udp_port=8000
+to_udp_host=disabled
 tcp_manager=3000
 log_file=prova.log
--- a/syslog.h	Tue May 30 01:16:33 2006 +0200
+++ b/syslog.h	Tue May 30 12:02:13 2006 +0200
@@ -45,6 +45,7 @@
 
 /* tcp_server.c */
 int init_tcp_server();
+void close_out_tcp();
 
 /* childs.c */
 void term_childs();
--- a/syslog_kernel.c	Tue May 30 01:16:33 2006 +0200
+++ b/syslog_kernel.c	Tue May 30 12:02:13 2006 +0200
@@ -218,6 +218,8 @@
 		if (reconfig == true)
 			fprintf(stderr, "reconfiguring...\n");
 
+		close_out_tcp();
+
 		term_childs();
 
 		close_out_udp();
--- a/tcp_server.c	Tue May 30 01:16:33 2006 +0200
+++ b/tcp_server.c	Tue May 30 12:02:13 2006 +0200
@@ -18,7 +18,12 @@
 static int listen_tcp_ipv6(int port);
 static int listen_tcp_ipv4(int port);
 static int accept_connection(int socket);
+static int sendlog(const int socket);
 
+/* Globals */
+static int tcp_server_pid = 0;
+
+/* Code */
 /* Returns 0 on success. Returns -1 on error. */
 int init_tcp_server()
 {
@@ -64,6 +69,7 @@
 	} else if (pid > 0)
 	{
 		/* Parent */
+		tcp_server_pid = pid;
 		fprintf(stderr, "Child forked (tcp server on port %i): %i\n",
 			port, pid);
 		ret = SUCCESS;
@@ -253,7 +259,7 @@
 	{
 		/* Child */
 		close(socket);
-		send(newsock, "hola\n", 5, 0);
+		sendlog(newsock);
 		close(newsock);
 		exit(0);
 		/* Unreachable */
@@ -267,3 +273,60 @@
 
 	return 0;
 }
+
+/* Return -2 when the socket is not enabled. Otherwise, the output of close().*/
+void close_out_tcp()
+{
+	if (tcp_server_pid != 0)
+	{
+		fprintf(stderr,"close_out_tcp: Sending SIGTERM to %i\n", tcp_server_pid);
+		kill(tcp_server_pid, SIGTERM);
+		tcp_server_pid = 0;
+	}
+	return;
+}
+
+static int sendlog(const int socket)
+{
+	int fh;
+	int bytes, res;
+	enum config
+	{
+		BUFFER_SIZE=500
+	};
+	char buffer[BUFFER_SIZE];
+	char *buffer2;
+	char name[MAX_STRING];
+
+	assert (get_config(LOG_FILE, name, MAX_STRING) > 0);
+
+	fh = open(name, O_RDONLY, 0600);
+	if (fh == -1)
+	{
+		fprintf(stderr, "Error opening the log file for read: %s\n",
+				strerror(errno));
+		return -1;
+	}
+
+	while((bytes = read(fh, buffer, BUFFER_SIZE)) > 0)
+	{
+		buffer2 = buffer;
+		while(bytes > 0)
+		{
+			res = send(socket, buffer2, bytes, 0);
+			if (res == 0)
+				sleep(1);
+			else if (res < 0)
+			{
+				close(fh);
+				return -1;
+			}
+
+			bytes = bytes - res;
+			buffer2 = buffer2 + res;
+		}
+	}
+
+	close(fh);
+	return bytes;
+}