syslog_kernel.c
author viric@llimona
Tue, 30 May 2006 01:16:33 +0200
changeset 54 a456a2e5fca4
parent 53 667cd5966695
child 55 c72dbd390cf2
permissions -rw-r--r--
Updated TODO for the last release.
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
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   191
int main(int argn, char *argv[])
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   192
{
35
6b6bbc8a5fdb Added UDP output.
viric@mandarina
parents: 28
diff changeset
   193
	int res;
6b6bbc8a5fdb Added UDP output.
viric@mandarina
parents: 28
diff changeset
   194
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   195
	install_signal_handlers();
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents: 19
diff changeset
   196
37
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   197
	do
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   198
	{
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   199
		reconfig = false;
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   200
		init_config();
35
6b6bbc8a5fdb Added UDP output.
viric@mandarina
parents: 28
diff changeset
   201
37
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   202
		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
   203
		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
   204
			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
   205
		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
   206
			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
   207
39
60858d13b22c Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents: 37
diff changeset
   208
		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
   209
		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
   210
			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
   211
		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
   212
			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
   213
51
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents: 49
diff changeset
   214
		init_tcp_server();
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents: 49
diff changeset
   215
37
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   216
		kernel_loop();
28
a206baaa3ad5 Added main rutines for killing and waiting for childs' dead.
viric@mandarina
parents: 26
diff changeset
   217
44
ddab92173045 Corrected some debug info.
viric@llimona
parents: 42
diff changeset
   218
		if (reconfig == true)
ddab92173045 Corrected some debug info.
viric@llimona
parents: 42
diff changeset
   219
			fprintf(stderr, "reconfiguring...\n");
ddab92173045 Corrected some debug info.
viric@llimona
parents: 42
diff changeset
   220
37
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   221
		term_childs();
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   222
39
60858d13b22c Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents: 37
diff changeset
   223
		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
   224
		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
   225
37
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   226
		wait_childs_die();
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   227
	}
2db8451df601 First code for SIGHUP reconfiguration. It still doesn't work fine. More in TODO.
viric@mandarina
parents: 36
diff changeset
   228
	while(reconfig == true);
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
diff changeset
   229
}