Added basic log_file support.
--- a/syslog.conf Sat May 13 23:27:42 2006 +0200
+++ b/syslog.conf Sat May 13 23:30:53 2006 +0200
@@ -0,0 +1,2 @@
+from_unix=patata
+log_file=prova.log
--- a/syslog_kernel.c Sat May 13 23:27:42 2006 +0200
+++ b/syslog_kernel.c Sat May 13 23:30:53 2006 +0200
@@ -8,6 +8,8 @@
#include <signal.h> // Pels senyals
#include <assert.h> // Pels assert
#include <stdlib.h> // Per l'abort
+#include <fcntl.h> // Per l'open
+#include <sys/stat.h> // Per l'open
#include "rfc3164.h"
#include "syslog.h"
@@ -102,7 +104,7 @@
char cvalue[MAX_STRING];
/* The childs should not run */
- if (get_config(FROM_UNIX, cvalue) > 0)
+ if (get_config(FROM_UNIX, cvalue, MAX_STRING) > 0)
{
child[CHILD_UNIX].input_pipe[0] = input_pipe[0];
child[CHILD_UNIX].input_pipe[1] = input_pipe[1];
@@ -113,7 +115,7 @@
child[CHILD_UNIX].running = true;
}
- if (get_config(FROM_NPIPE, cvalue) > 0)
+ if (get_config(FROM_NPIPE, cvalue, MAX_STRING) > 0)
{
child[CHILD_NPIPE].input_pipe[0] = input_pipe[0];
child[CHILD_NPIPE].input_pipe[1] = input_pipe[1];
@@ -124,7 +126,7 @@
child[CHILD_NPIPE].running = true;
}
- if (get_config(FROM_UDP, cvalue) > 0)
+ if (get_config(FROM_UDP, cvalue, MAX_STRING) > 0)
{
child[CHILD_UDP].input_pipe[0] = input_pipe[0];
child[CHILD_UDP].input_pipe[1] = input_pipe[1];
@@ -140,7 +142,20 @@
{
int input_pipe[2];
char missatge[MESSAGE_LENGTH+1];
- int res;
+ int res_r, res_w; /* resultats de la crida read i write */
+ int errno_r; /* errno de la crida read */
+ 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;
+ }
/* Pipe for the childs */
pipe(input_pipe);
@@ -152,28 +167,48 @@
while(true)
{
- res = read(input_pipe[0], missatge, MESSAGE_LENGTH);
+ res_r = read(input_pipe[0], missatge, MESSAGE_LENGTH);
+ errno_r = errno;
- if (res > 0)
+ if (res_r > 0)
{
- /* Add a ZERO */
- missatge[res] = '\0';
- /* The \n is included in the message */
- printf("Received: %s",missatge);
+ /* Add a ZERO for displaying */
+ missatge[res_r] = '\0';
+ fprintf(stderr, "Received: %s\n",missatge);
+ 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)
+ break;
}
- else if (res == -1 && errno == EINTR)
+ else if (res_r == -1 && errno == EINTR)
+ /* The call was interrupted by a signal */
continue;
- else
+ else /* End of read: res_r -1 or 0 */
break;
}
- if (res == -1)
+ if (res_r == -1)
{
- printf("Error reading from the main pipe: %s\n",
+ fprintf(stderr, "Error reading from the main pipe: %s\n",
+ strerror(errno_r));
+ }
+ close(input_pipe[0]);
+
+ if (res_w == -1)
+ {
+ fprintf(stderr, "Error writing to the log file: %s\n",
strerror(errno));
- }
+ } else if (res_w == 0)
+ fprintf(stderr,"Cannot write anymore to the log file.");
-
+ close(output);
}
static void install_signal_handlers()