e386812de91cb52e878c6a23568c87bf12cb5f3b
[ngircd-alex.git] / src / ngircd / ngircd.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
4  *
5  * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6  * der GNU General Public License (GPL), wie von der Free Software Foundation
7  * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8  * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9  * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10  * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
11  *
12  * $Id: ngircd.c,v 1.38 2002/03/29 20:59:22 alex Exp $
13  *
14  * ngircd.c: Hier beginnt alles ;-)
15  */
16
17
18 #include "portab.h"
19
20 #include "imp.h"
21 #include <assert.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <signal.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <time.h>
30
31 #include "channel.h"
32 #include "client.h"
33 #include "conf.h"
34 #include "conn.h"
35 #include "defines.h"
36 #include "irc.h"
37 #include "log.h"
38 #include "parse.h"
39
40 #include "exp.h"
41 #include "ngircd.h"
42
43
44 LOCAL VOID Initialize_Signal_Handler( VOID );
45 LOCAL VOID Signal_Handler( INT Signal );
46
47 LOCAL VOID Initialize_Listen_Ports( VOID );
48
49 LOCAL VOID Show_Version( VOID );
50 LOCAL VOID Show_Help( VOID );
51
52
53 GLOBAL int main( int argc, const char *argv[] )
54 {
55         BOOLEAN ok, configtest = FALSE;
56         INT32 pid, n;
57         INT i;
58
59         NGIRCd_Restart = FALSE;
60         NGIRCd_Quit = FALSE;
61         NGIRCd_NoDaemon = FALSE;
62         NGIRCd_Passive = FALSE;
63 #ifdef DEBUG
64         NGIRCd_Debug = FALSE;
65 #endif
66 #ifdef SNIFFER
67         NGIRCd_Sniffer = FALSE;
68 #endif
69         strcpy( NGIRCd_ConfFile, CONFIG_FILE );
70
71         /* Kommandozeile parsen */
72         for( i = 1; i < argc; i++ )
73         {
74                 ok = FALSE;
75                 if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
76                 {
77                         /* Lange Option */
78
79                         if( strcmp( argv[i], "--config" ) == 0 )
80                         {
81                                 if( i + 1 < argc )
82                                 {
83                                         /* Ok, danach kommt noch ein Parameter */
84                                         strncpy( NGIRCd_ConfFile, argv[i + 1], FNAME_LEN - 1 );
85                                         NGIRCd_ConfFile[FNAME_LEN - 1] = '\0';
86
87                                         /* zum uebernaechsten Parameter */
88                                         i++; ok = TRUE;
89                                 }
90                         }
91                         if( strcmp( argv[i], "--configtest" ) == 0 )
92                         {
93                                 configtest = TRUE;
94                                 ok = TRUE;
95                         }
96 #ifdef DEBUG
97                         if( strcmp( argv[i], "--debug" ) == 0 )
98                         {
99                                 NGIRCd_Debug = TRUE;
100                                 ok = TRUE;
101                         }
102 #endif
103                         if( strcmp( argv[i], "--help" ) == 0 )
104                         {
105                                 Show_Version( );
106                                 puts( "" ); Show_Help( ); puts( "" );
107                                 exit( 1 );
108                         }
109                         if( strcmp( argv[i], "--nodaemon" ) == 0 )
110                         {
111                                 NGIRCd_NoDaemon = TRUE;
112                                 ok = TRUE;
113                         }
114                         if( strcmp( argv[i], "--passive" ) == 0 )
115                         {
116                                 NGIRCd_Passive = TRUE;
117                                 ok = TRUE;
118                         }
119 #ifdef SNIFFER
120                         if( strcmp( argv[i], "--sniffer" ) == 0 )
121                         {
122                                 NGIRCd_Sniffer = TRUE;
123                                 ok = TRUE;
124                         }
125 #endif
126                         if( strcmp( argv[i], "--version" ) == 0 )
127                         {
128                                 Show_Version( );
129                                 exit( 1 );
130                         }
131                 }
132                 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
133                 {
134                         /* Kurze Option */
135                         
136                         for( n = 1; n < (INT32)strlen( argv[i] ); n++ )
137                         {
138                                 ok = FALSE;
139 #ifdef DEBUG
140                                 if( argv[i][n] == 'd' )
141                                 {
142                                         NGIRCd_Debug = TRUE;
143                                         ok = TRUE;
144                                 }
145 #endif
146                                 if( argv[i][n] == 'f' )
147                                 {
148                                         if(( ! argv[i][n+i] ) && ( i + 1 < argc ))
149                                         {
150                                                 /* Ok, danach kommt ein Leerzeichen */
151                                                 strncpy( NGIRCd_ConfFile, argv[i + 1], FNAME_LEN - 1 );
152                                                 NGIRCd_ConfFile[FNAME_LEN - 1] = '\0';
153
154                                                 /* zum uebernaechsten Parameter */
155                                                 i++; n = strlen( argv[i] );
156                                                 ok = TRUE;
157                                         }
158                                 }
159                                 if( argv[i][n] == 'n' )
160                                 {
161                                         NGIRCd_NoDaemon = TRUE;
162                                         ok = TRUE;
163                                 }
164                                 if( argv[i][n] == 'p' )
165                                 {
166                                         NGIRCd_Passive = TRUE;
167                                         ok = TRUE;
168                                 }
169 #ifdef SNIFFER
170                                 if( argv[i][n] == 's' )
171                                 {
172                                         NGIRCd_Sniffer = TRUE;
173                                         ok = TRUE;
174                                 }
175 #endif
176
177                                 if( ! ok )
178                                 {
179                                         printf( PACKAGE": invalid option \"-%c\"!\n", argv[i][n] );
180                                         puts( "Try \""PACKAGE" --help\" for more information." );
181                                         exit( 1 );
182                                 }
183                         }
184
185                 }
186                 if( ! ok )
187                 {
188                         printf( PACKAGE": invalid option \"%s\"!\n", argv[i] );
189                         puts( "Try \""PACKAGE" --help\" for more information." );
190                         exit( 1 );
191                 }
192         }
193
194         /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
195         strcpy( NGIRCd_DebugLevel, "" );
196 #ifdef DEBUG
197         if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
198 #endif
199 #ifdef SNIFFER
200         if( NGIRCd_Sniffer ) strcpy( NGIRCd_DebugLevel, "2" );
201 #endif
202
203         /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
204         if( configtest )
205         {
206                 Show_Version( ); puts( "" );
207                 exit( Conf_Test( ));
208         }
209         
210         while( ! NGIRCd_Quit )
211         {
212                 /* In der Regel wird ein Sub-Prozess ge-fork()'t, der
213                  * nicht mehr mit dem Terminal verbunden ist. Mit der
214                  * Option "--nodaemon" kann dies (z.B. zum Debuggen)
215                  * verhindert werden. */
216                 if( ! NGIRCd_NoDaemon )
217                 {
218                         /* Daemon im Hintergrund erzeugen */
219                         pid = (INT32)fork( );
220                         if( pid > 0 )
221                         {
222                                 /* "alter" Prozess */
223                                 exit( 0 );
224                         }
225                         if( pid < 0 )
226                         {
227                                 /* Fehler */
228                                 printf( PACKAGE": Can't fork: %s!\nFatal error, exiting now ...", strerror( errno ));
229                                 exit( 1 );
230                         }
231
232                         /* Child-Prozess initialisieren */
233                         (VOID)setsid( );
234                         chdir( "/" );
235                 }
236         
237                 /* Globale Variablen initialisieren */
238                 NGIRCd_Start = time( NULL );
239                 (VOID)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
240                 NGIRCd_Restart = FALSE;
241                 NGIRCd_Quit = FALSE;
242
243                 /* Module initialisieren */
244                 Log_Init( );
245                 Conf_Init( );
246                 Channel_Init( );
247                 Client_Init( );
248                 Conn_Init( );
249
250                 /* Signal-Handler initialisieren */
251                 Initialize_Signal_Handler( );
252
253                 /* Listen-Ports initialisieren */
254                 Initialize_Listen_Ports( );
255
256                 /* Hauptschleife */
257                 while( TRUE )
258                 {
259                         if( NGIRCd_Quit || NGIRCd_Restart ) break;
260                         Conn_Handler( 5 );
261                 }
262
263                 /* Alles abmelden */
264                 Conn_Exit( );
265                 Client_Exit( );
266                 Channel_Exit( );
267                 Conf_Exit( );
268                 Log_Exit( );
269         }
270
271         return 0;
272 } /* main */
273
274
275 GLOBAL CHAR *NGIRCd_Version( VOID )
276 {
277         STATIC CHAR version[126];
278
279         sprintf( version, PACKAGE" version "VERSION"-%s", NGIRCd_VersionAddition( ));
280         return version;
281 } /* NGIRCd_Version */
282
283
284 GLOBAL CHAR *NGIRCd_VersionAddition( VOID )
285 {
286         STATIC CHAR txt[64];
287
288         strcpy( txt, "" );
289
290 #ifdef USE_SYSLOG
291         if( txt[0] ) strcat( txt, "+" );
292         strcat( txt, "SYSLOG" );
293 #endif
294 #ifdef STRICT_RFC
295         if( txt[0] ) strcat( txt, "+" );
296         strcat( txt, "RFC" );
297 #endif
298 #ifdef DEBUG
299         if( txt[0] ) strcat( txt, "+" );
300         strcat( txt, "DEBUG" );
301 #endif
302 #ifdef SNIFFER
303         if( txt[0] ) strcat( txt, "+" );
304         strcat( txt, "SNIFFER" );
305 #endif
306
307         if( txt[0] ) strcat( txt, "-" );
308         strcat( txt, TARGET_CPU"/"TARGET_VENDOR"/"TARGET_OS );
309
310         return txt;
311 } /* NGIRCd_VersionAddition */
312
313
314 LOCAL VOID Initialize_Signal_Handler( VOID )
315 {
316         /* Signal-Handler initialisieren: einige Signale
317          * werden ignoriert, andere speziell behandelt. */
318
319 #ifdef HAVE_SIGACTION
320         /* sigaction() ist vorhanden */
321
322         struct sigaction saction;
323
324         /* Signal-Struktur initialisieren */
325         memset( &saction, 0, sizeof( saction ));
326         saction.sa_handler = Signal_Handler;
327 #ifdef SA_RESTART
328         saction.sa_flags |= SA_RESTART;
329 #endif
330 #ifdef SA_NOCLDWAIT
331         saction.sa_flags |= SA_NOCLDWAIT;
332 #endif
333
334         /* Signal-Handler einhaengen */
335         sigaction( SIGINT, &saction, NULL );
336         sigaction( SIGQUIT, &saction, NULL );
337         sigaction( SIGTERM, &saction, NULL);
338         sigaction( SIGHUP, &saction, NULL);
339         sigaction( SIGCHLD, &saction, NULL);
340
341         /* einige Signale ignorieren */
342         saction.sa_handler = SIG_IGN;
343         sigaction( SIGPIPE, &saction, NULL );
344 #else
345         /* kein sigaction() vorhanden */
346
347         /* Signal-Handler einhaengen */
348         signal( SIGINT, Signal_Handler );
349         signal( SIGQUIT, Signal_Handler );
350         signal( SIGTERM, Signal_Handler );
351         signal( SIGHUP, Signal_Handler );
352         signal( SIGCHLD, Signal_Handler );
353
354         /* einige Signale ignorieren */
355         signal( SIGPIPE, SIG_IGN );
356 #endif
357 } /* Initialize_Signal_Handler */
358
359
360 LOCAL VOID Signal_Handler( INT Signal )
361 {
362         /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
363          * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
364          * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
365
366         switch( Signal )
367         {
368                 case SIGTERM:
369                 case SIGINT:
370                 case SIGQUIT:
371                         /* wir soll(t)en uns wohl beenden ... */
372                         if( Signal == SIGTERM ) Log( LOG_WARNING, "Got TERM signal, terminating now ..." );
373                         else if( Signal == SIGINT ) Log( LOG_WARNING, "Got INT signal, terminating now ..." );
374                         else if( Signal == SIGQUIT ) Log( LOG_WARNING, "Got QUIT signal, terminating now ..." );
375                         NGIRCd_Quit = TRUE;
376                         break;
377                 case SIGHUP:
378                         /* neu starten */
379                         Log( LOG_WARNING, "Got HUP signal, restarting now ..." );
380                         NGIRCd_Restart = TRUE;
381                         break;
382                 case SIGCHLD:
383                         /* Child-Prozess wurde beendet. Zombies vermeiden: */
384                         while( waitpid( -1, NULL, WNOHANG ) > 0);
385                         break;
386                 default:
387                         /* unbekanntes bzw. unbehandeltes Signal */
388                         Log( LOG_NOTICE, "Got signal %d! Ignored.", Signal );
389         }
390 } /* Signal_Handler */
391
392
393 LOCAL VOID Initialize_Listen_Ports( VOID )
394 {
395         /* Ports, auf denen der Server Verbindungen entgegennehmen
396          * soll, initialisieren */
397         
398         INT created, i;
399
400         created = 0;
401         for( i = 0; i < Conf_ListenPorts_Count; i++ )
402         {
403                 if( Conn_NewListener( Conf_ListenPorts[i] )) created++;
404                 else Log( LOG_ERR, "Can't listen on port %d!", Conf_ListenPorts[i] );
405         }
406
407         if( created < 1 )
408         {
409                 Log( LOG_ALERT, "Server isn't listening on a single port!" );
410                 Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
411                 exit( 1 );
412         }
413 } /* Initialize_Listen_Ports */
414
415
416 LOCAL VOID Show_Version( VOID )
417 {
418         puts( NGIRCd_Version( ));
419         puts( "Copyright (c)2001,2002 by Alexander Barton (alex@barton.de).\n" );
420         puts( "This is free software; see the source for copying conditions. There is NO" );
421         puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
422 } /* Show_Version */
423
424
425 LOCAL VOID Show_Help( VOID )
426 {
427 #ifdef DEBUG
428         puts( "  -d, --debug        log extra debug messages" );
429 #endif
430         puts( "  -f, --config <f>   use file <f> as configuration file" );
431         puts( "  -n, --nodaemon     don't fork and don't detatch from controlling terminal" );
432         puts( "  -p, --passive      disable automatic connections to other servers" );
433 #ifdef SNIFFER
434         puts( "  -s, --sniffer      enable network sniffer and display all IRC traffic" );
435 #endif
436         puts( "      --configtest   read, validate and display configuration; then exit" );
437         puts( "      --version      output version information and exit" );
438         puts( "      --help         display this help and exit" );
439 } /* Show_Help */
440
441
442 /* -eof- */