syslog_kernel.c
author viric@llimona
Tue, 30 May 2006 12:58:46 +0200
changeset 59 a8776c046a53
parent 57 de776a4548da
permissions -rw-r--r--
Readme adapted to the last version.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
     1
#include <sys/types.h>
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
     2
#include <unistd.h>
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
     3
#include <errno.h>
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
     4
#include <stdbool.h>
49
ced312cc1eaa Separated the child handling in another module.
viric@llimona
parents: 44
diff changeset
     5
#include <signal.h>
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
     6
#include <stdio.h> // Per sprintf, fprintf
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
     7
#include <string.h> // Per memset
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
     8
#include <assert.h> // Pels assert
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
     9
#include <stdlib.h> // Per l'abort
25
31370b4c0627 Added basic log_file support.
viric@mandarina
parents: 21
diff changeset
    10
#include <fcntl.h> // Per l'open
31370b4c0627 Added basic log_file support.
viric@mandarina
parents: 21
diff changeset
    11
#include <sys/stat.h> // Per l'open
42
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
    12
#include <sys/select.h> // Pel select()
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    13
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    14
#include "rfc3164.h"
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
    15
#include "syslog.h"
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    16
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
    17
/* Program names */
28
a206baaa3ad5 Added main rutines for killing and waiting for childs' dead.
viric@mandarina
parents: 26
diff changeset
    18
static const char syslog_in_unix[] = "syslog_in_unix";
a206baaa3ad5 Added main rutines for killing and waiting for childs' dead.
viric@mandarina
parents: 26
diff changeset
    19
static const char syslog_in_npipe[] = "syslog_in_npipe";
a206baaa3ad5 Added main rutines for killing and waiting for childs' dead.
viric@mandarina
parents: 26
diff changeset
    20
static const char syslog_in_udp[] = "syslog_in_udp";
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    21
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
    22
/* Global variables */
37
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
    23
static bool reconfig = false;
28
a206baaa3ad5 Added main rutines for killing and waiting for childs' dead.
viric@mandarina
parents: 26
diff changeset
    24
a206baaa3ad5 Added main rutines for killing and waiting for childs' dead.
viric@mandarina
parents: 26
diff changeset
    25
/* Code */
a206baaa3ad5 Added main rutines for killing and waiting for childs' dead.
viric@mandarina
parents: 26
diff changeset
    26
static void term_handler(int parameter)
a206baaa3ad5 Added main rutines for killing and waiting for childs' dead.
viric@mandarina
parents: 26
diff changeset
    27
{
a206baaa3ad5 Added main rutines for killing and waiting for childs' dead.
viric@mandarina
parents: 26
diff changeset
    28
	term_childs();
a206baaa3ad5 Added main rutines for killing and waiting for childs' dead.
viric@mandarina
parents: 26
diff changeset
    29
}
a206baaa3ad5 Added main rutines for killing and waiting for childs' dead.
viric@mandarina
parents: 26
diff changeset
    30
37
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
    31
static void reconfig_handler(int parameter)
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
    32
{
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
    33
	reconfig = true;
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
    34
}
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
    35
28
a206baaa3ad5 Added main rutines for killing and waiting for childs' dead.
viric@mandarina
parents: 26
diff changeset
    36
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
    37
static int run_program(const char * restrict programname,
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
    38
		const char * restrict parameter1, const int comm_pipe[])
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    39
{
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    40
	int pid;
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    41
49
ced312cc1eaa Separated the child handling in another module.
viric@llimona
parents: 44
diff changeset
    42
	pid = child_fork();
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    43
	assert(pid != -1);
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    44
	if (pid == 0)
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    45
	{
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    46
		close(0);
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    47
		close(1);
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    48
		dup(comm_pipe[0]);
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    49
		dup(comm_pipe[1]);
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    50
		execl(programname, programname, parameter1, NULL);
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
    51
		/* Unreachable if everything goes well */
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    52
		fprintf(stderr, "Child exec failed(%s %s): %s\n",
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    53
			programname, parameter1, strerror(errno));
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    54
		abort();
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    55
		return 0;
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    56
	}
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    57
	else
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    58
	{
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    59
		fprintf(stderr, "Child forked(%s %s): %i\n",
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    60
			programname, parameter1, pid);
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    61
		return pid;
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    62
	}
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    63
}
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    64
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
    65
static int init_in_unix(const char * restrict socketname,
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
    66
		const int input_pipe[2])
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    67
{
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    68
	return run_program(syslog_in_unix, socketname, input_pipe);
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    69
}
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    70
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
    71
static int init_in_udp(const char * restrict port, const int input_pipe[2])
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    72
{
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
    73
	return run_program(syslog_in_udp, port, input_pipe);
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    74
}
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    75
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
    76
static int init_in_npipe(const char * restrict pipename,
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
    77
		const int input_pipe[2])
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    78
{
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    79
	return run_program(syslog_in_npipe, pipename, input_pipe);
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    80
}
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
    81
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
    82
static void start_childs(const int input_pipe[2])
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
    83
{
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
    84
	char cvalue[MAX_STRING];
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
    85
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
    86
	/* The childs should not run */
25
31370b4c0627 Added basic log_file support.
viric@mandarina
parents: 21
diff changeset
    87
	if (get_config(FROM_UNIX, cvalue, MAX_STRING) > 0)
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
    88
	{
49
ced312cc1eaa Separated the child handling in another module.
viric@llimona
parents: 44
diff changeset
    89
		init_in_unix(cvalue, input_pipe);
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
    90
	}
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
    91
25
31370b4c0627 Added basic log_file support.
viric@mandarina
parents: 21
diff changeset
    92
	if (get_config(FROM_NPIPE, cvalue, MAX_STRING) > 0)
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
    93
	{
49
ced312cc1eaa Separated the child handling in another module.
viric@llimona
parents: 44
diff changeset
    94
		init_in_npipe(cvalue, input_pipe);
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
    95
	}
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
    96
25
31370b4c0627 Added basic log_file support.
viric@mandarina
parents: 21
diff changeset
    97
	if (get_config(FROM_UDP, cvalue, MAX_STRING) > 0)
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
    98
	{
49
ced312cc1eaa Separated the child handling in another module.
viric@llimona
parents: 44
diff changeset
    99
		init_in_udp(cvalue, input_pipe);
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
   100
	}
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
   101
}
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
   102
39
60858d13b22c Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents: 37
diff changeset
   103
static int output_message(const char * restrict msg, const int len)
60858d13b22c Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents: 37
diff changeset
   104
{
42
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   105
	int res1, res2, res;
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   106
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   107
	res = 0;
39
60858d13b22c Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents: 37
diff changeset
   108
42
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   109
	res1 = write_out_udp(msg, len);
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   110
	res2 = write_out_file(msg, len);
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   111
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   112
	if (res1 == -1 || res2 == -1)
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   113
		res = -1;
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   114
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   115
	return res;
39
60858d13b22c Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents: 37
diff changeset
   116
}
60858d13b22c Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents: 37
diff changeset
   117
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
   118
static void kernel_loop()
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   119
{
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   120
	int input_pipe[2];
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   121
	char missatge[MESSAGE_LENGTH+1];
42
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   122
	int res; /* resultat de la crida read */
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   123
	fd_set read_fd_set;
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   124
	struct timeval read_timeout;
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   125
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
   126
	/* Pipe for the childs */
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   127
	pipe(input_pipe);
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   128
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
   129
	start_childs(input_pipe);
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   130
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   131
	/* We don't want to write to the programs */
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   132
	close(input_pipe[1]);
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   133
42
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   134
	FD_ZERO(&read_fd_set);
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   135
	while(true)
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   136
	{
42
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   137
		/* pipe read handle */
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   138
		if (FD_ISSET(input_pipe[0], &read_fd_set))
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   139
		{
42
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   140
			res = read(input_pipe[0], missatge, MESSAGE_LENGTH);
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   141
			assert( res != -1 );
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   142
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   143
			if (res > 0)
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   144
			{
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   145
				/* Output to screen */
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   146
				/* Add a ZERO for displaying */
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   147
				missatge[res] = '\0';
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   148
				fprintf(stderr, "Received: %s\n",missatge);
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   149
				
53
667cd5966695 Finer message processing - now only at file and screen output appear '\n'.
viric@llimona
parents: 51
diff changeset
   150
				process_message(missatge);
42
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   151
				output_message(missatge, res);
51
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents: 49
diff changeset
   152
			} else if (res == 0) /* EOF */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents: 49
diff changeset
   153
			{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents: 49
diff changeset
   154
				break;
42
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   155
			}
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   156
		}
42
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   157
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   158
		/* The select call may be interrupted by a signal */
49
ced312cc1eaa Separated the child handling in another module.
viric@llimona
parents: 44
diff changeset
   159
		if (get_childs_alive() == 0)
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   160
			break;
37
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   161
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   162
		if (reconfig == true)
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   163
			break;
42
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   164
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   165
		/* Prepare the fd_set */
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   166
		FD_ZERO(&read_fd_set);
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   167
		FD_SET(input_pipe[0], &read_fd_set);
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   168
		/* We set the timeout in order to save us easily from the race
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   169
		 * condition for signal receiving (select/pselect) */
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   170
		read_timeout.tv_sec = 5;
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   171
		read_timeout.tv_usec = 0;
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   172
		res = select(input_pipe[0]+1, &read_fd_set, NULL, NULL,
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   173
				&read_timeout);
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   174
		if (res == -1)
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   175
			FD_ZERO(&read_fd_set);
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   176
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   177
		assert( !(res == -1 && errno != EINTR) );
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   178
	}
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   179
25
31370b4c0627 Added basic log_file support.
viric@mandarina
parents: 21
diff changeset
   180
	close(input_pipe[0]);
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   181
}
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   182
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
   183
static void install_signal_handlers()
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   184
{
26
7227789ca718 Added a c file for signal programming.
viric@mandarina
parents: 25
diff changeset
   185
	program_child_handler(child_handler);
28
a206baaa3ad5 Added main rutines for killing and waiting for childs' dead.
viric@mandarina
parents: 26
diff changeset
   186
	program_handler(SIGTERM, term_handler);
36
c6f7b56729d9 The handler for SIGTERM now handles also SIGINT. Very comfortable.
viric@mandarina
parents: 35
diff changeset
   187
	program_handler(SIGINT, term_handler);
42
95f305aef8e3 The kernel now understands SIGHUP for rereading the configuration.
viric@llimona
parents: 39
diff changeset
   188
	program_handler(SIGHUP, reconfig_handler);
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   189
}
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   190
57
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   191
static void check_pid()
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   192
{
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   193
	int fh;
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   194
	char name[MAX_STRING];
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   195
	char buffer[MAX_STRING];
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   196
	int buffer_size;
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   197
	int pid, res;
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   198
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   199
	assert (get_config(PID_FILE, name, MAX_STRING) > 0);
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   200
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   201
	fh = open(name, O_RDONLY);
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   202
	if (fh == -1)
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   203
	{
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   204
		/* If the file doesn't exist, it's normal. */
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   205
		return;
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   206
	}
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   207
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   208
	buffer_size = MAX_STRING;
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   209
	buffer_size = read(fh, buffer, buffer_size);
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   210
	if (buffer_size <= 0)
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   211
	{
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   212
		fprintf(stderr, "Error reading the pid file: %s\n",
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   213
				strerror(errno));
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   214
		close(fh);
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   215
		return;
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   216
	}
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   217
	buffer[buffer_size] = '\0';
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   218
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   219
	pid = atoi(buffer);
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   220
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   221
	if (pid > 0)
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   222
	{
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   223
		res = kill(pid, SIGHUP);
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   224
		if (res == 0)
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   225
		{
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   226
			/* The syslog_kernel seems to be running and restared */
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   227
			fprintf(stderr,"syslog_kernel running. Restarted.\n");
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   228
			close(fh);
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   229
			exit(0);
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   230
		} else if (res == -1)
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   231
		{
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   232
			fprintf(stderr,"An old pid file has been found. "
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   233
				"Ignoring it.\n");
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   234
		}
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   235
	}
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   236
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   237
	close(fh);
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   238
}
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   239
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   240
static void write_pid()
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   241
{
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   242
	int fh;
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   243
	char name[MAX_STRING];
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   244
	char buffer[MAX_STRING];
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   245
	int buffer_size;
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   246
	int res;
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   247
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   248
	assert (get_config(PID_FILE, name, MAX_STRING) > 0);
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   249
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   250
	fh = open(name, O_WRONLY | O_TRUNC | O_CREAT, 0644);
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   251
	if (fh == -1)
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   252
	{
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   253
		fprintf(stderr, "Error opening the pid file for write: %s\n",
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   254
				strerror(errno));
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   255
		return;
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   256
	}
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   257
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   258
	buffer_size = snprintf(buffer, MAX_STRING, "%i", getpid());
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   259
	if (buffer_size < MAX_STRING)
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   260
	{
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   261
		buffer[buffer_size++] = '\n';
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   262
	}
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   263
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   264
	res = write(fh, buffer, buffer_size);
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   265
	if (buffer_size <= 0)
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   266
		fprintf(stderr, "Error writting the pid file: %s\n",
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   267
				strerror(errno));
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   268
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   269
	close(fh);
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   270
}
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   271
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   272
static void remove_pid()
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   273
{
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   274
	int res;
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   275
	char name[MAX_STRING];
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   276
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   277
	assert (get_config(PID_FILE, name, MAX_STRING) > 0);
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   278
	res = unlink(name);
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   279
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   280
	if (res == -1)
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   281
		fprintf(stderr, "Error unlinking the pid file: %s\n",
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   282
				strerror(errno));
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   283
}
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   284
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   285
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   286
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   287
int main(int argn, char *argv[])
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   288
{
35
6b6bbc8a5fdb Added UDP output.
viric@mandarina
parents: 28
diff changeset
   289
	int res;
6b6bbc8a5fdb Added UDP output.
viric@mandarina
parents: 28
diff changeset
   290
57
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   291
	check_pid();
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   292
	write_pid();
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   293
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   294
	install_signal_handlers();
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
   295
37
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   296
	do
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   297
	{
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   298
		reconfig = false;
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   299
		init_config();
35
6b6bbc8a5fdb Added UDP output.
viric@mandarina
parents: 28
diff changeset
   300
37
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   301
		res = init_out_udp();
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   302
		if (res == -2)
39
60858d13b22c Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents: 37
diff changeset
   303
			fprintf(stderr, "UDP output disabled.\n");
37
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   304
		else if (res == -1)
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   305
			fprintf(stderr, "error setting up the UDP output.\n");
28
a206baaa3ad5 Added main rutines for killing and waiting for childs' dead.
viric@mandarina
parents: 26
diff changeset
   306
39
60858d13b22c Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents: 37
diff changeset
   307
		res = init_out_file();
60858d13b22c Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents: 37
diff changeset
   308
		if (res == -2)
60858d13b22c Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents: 37
diff changeset
   309
			fprintf(stderr, "File output disabled.\n");
60858d13b22c Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents: 37
diff changeset
   310
		else if (res == -1)
60858d13b22c Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents: 37
diff changeset
   311
			fprintf(stderr, "error setting up the File output.\n");
60858d13b22c Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents: 37
diff changeset
   312
51
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents: 49
diff changeset
   313
		init_tcp_server();
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents: 49
diff changeset
   314
37
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   315
		kernel_loop();
28
a206baaa3ad5 Added main rutines for killing and waiting for childs' dead.
viric@mandarina
parents: 26
diff changeset
   316
44
ddab92173045 Corrected some debug info.
viric@llimona
parents: 42
diff changeset
   317
		if (reconfig == true)
ddab92173045 Corrected some debug info.
viric@llimona
parents: 42
diff changeset
   318
			fprintf(stderr, "reconfiguring...\n");
ddab92173045 Corrected some debug info.
viric@llimona
parents: 42
diff changeset
   319
55
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 53
diff changeset
   320
		close_out_tcp();
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 53
diff changeset
   321
37
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   322
		term_childs();
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   323
39
60858d13b22c Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents: 37
diff changeset
   324
		close_out_udp();
60858d13b22c Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents: 37
diff changeset
   325
		close_out_file();
60858d13b22c Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents: 37
diff changeset
   326
37
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   327
		wait_childs_die();
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   328
	}
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   329
	while(reconfig == true);
57
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   330
de776a4548da Added the check for a pid file (syslog.pid by default).
viric@llimona
parents: 55
diff changeset
   331
	remove_pid();
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   332
}