--- 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;
+}