- ERROR_FILE wurde durch ERROR_DIR ersetzt.
[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.37 2002/03/28 14:15:33 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 #ifndef DEBUG
272         /* aufraeumen */
273         if( unlink( ERROR_FILE ) != 0 ) Log( LOG_ERR, "Can't delete \""ERROR_FILE"\": %s", strerror( errno ));
274 #endif
275
276         return 0;
277 } /* main */
278
279
280 GLOBAL CHAR *NGIRCd_Version( VOID )
281 {
282         STATIC CHAR version[126];
283
284         sprintf( version, PACKAGE" version "VERSION"-%s", NGIRCd_VersionAddition( ));
285         return version;
286 } /* NGIRCd_Version */
287
288
289 GLOBAL CHAR *NGIRCd_VersionAddition( VOID )
290 {
291         STATIC CHAR txt[64];
292
293         strcpy( txt, "" );
294
295 #ifdef USE_SYSLOG
296         if( txt[0] ) strcat( txt, "+" );
297         strcat( txt, "SYSLOG" );
298 #endif
299 #ifdef STRICT_RFC
300         if( txt[0] ) strcat( txt, "+" );
301         strcat( txt, "RFC" );
302 #endif
303 #ifdef DEBUG
304         if( txt[0] ) strcat( txt, "+" );
305         strcat( txt, "DEBUG" );
306 #endif
307 #ifdef SNIFFER
308         if( txt[0] ) strcat( txt, "+" );
309         strcat( txt, "SNIFFER" );
310 #endif
311
312         if( txt[0] ) strcat( txt, "-" );
313         strcat( txt, TARGET_CPU"/"TARGET_VENDOR"/"TARGET_OS );
314
315         return txt;
316 } /* NGIRCd_VersionAddition */
317
318
319 LOCAL VOID Initialize_Signal_Handler( VOID )
320 {
321         /* Signal-Handler initialisieren: einige Signale
322          * werden ignoriert, andere speziell behandelt. */
323
324 #ifdef HAVE_SIGACTION
325         /* sigaction() ist vorhanden */
326
327         struct sigaction saction;
328
329         /* Signal-Struktur initialisieren */
330         memset( &saction, 0, sizeof( saction ));
331         saction.sa_handler = Signal_Handler;
332 #ifdef SA_RESTART
333         saction.sa_flags |= SA_RESTART;
334 #endif
335 #ifdef SA_NOCLDWAIT
336         saction.sa_flags |= SA_NOCLDWAIT;
337 #endif
338
339         /* Signal-Handler einhaengen */
340         sigaction( SIGINT, &saction, NULL );
341         sigaction( SIGQUIT, &saction, NULL );
342         sigaction( SIGTERM, &saction, NULL);
343         sigaction( SIGHUP, &saction, NULL);
344         sigaction( SIGCHLD, &saction, NULL);
345
346         /* einige Signale ignorieren */
347         saction.sa_handler = SIG_IGN;
348         sigaction( SIGPIPE, &saction, NULL );
349 #else
350         /* kein sigaction() vorhanden */
351
352         /* Signal-Handler einhaengen */
353         signal( SIGINT, Signal_Handler );
354         signal( SIGQUIT, Signal_Handler );
355         signal( SIGTERM, Signal_Handler );
356         signal( SIGHUP, Signal_Handler );
357         signal( SIGCHLD, Signal_Handler );
358
359         /* einige Signale ignorieren */
360         signal( SIGPIPE, SIG_IGN );
361 #endif
362 } /* Initialize_Signal_Handler */
363
364
365 LOCAL VOID Signal_Handler( INT Signal )
366 {
367         /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
368          * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
369          * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
370
371         switch( Signal )
372         {
373                 case SIGTERM:
374                 case SIGINT:
375                 case SIGQUIT:
376                         /* wir soll(t)en uns wohl beenden ... */
377                         if( Signal == SIGTERM ) Log( LOG_WARNING, "Got TERM signal, terminating now ..." );
378                         else if( Signal == SIGINT ) Log( LOG_WARNING, "Got INT signal, terminating now ..." );
379                         else if( Signal == SIGQUIT ) Log( LOG_WARNING, "Got QUIT signal, terminating now ..." );
380                         NGIRCd_Quit = TRUE;
381                         break;
382                 case SIGHUP:
383                         /* neu starten */
384                         Log( LOG_WARNING, "Got HUP signal, restarting now ..." );
385                         NGIRCd_Restart = TRUE;
386                         break;
387                 case SIGCHLD:
388                         /* Child-Prozess wurde beendet. Zombies vermeiden: */
389                         while( waitpid( -1, NULL, WNOHANG ) > 0);
390                         break;
391                 default:
392                         /* unbekanntes bzw. unbehandeltes Signal */
393                         Log( LOG_NOTICE, "Got signal %d! Ignored.", Signal );
394         }
395 } /* Signal_Handler */
396
397
398 LOCAL VOID Initialize_Listen_Ports( VOID )
399 {
400         /* Ports, auf denen der Server Verbindungen entgegennehmen
401          * soll, initialisieren */
402         
403         INT created, i;
404
405         created = 0;
406         for( i = 0; i < Conf_ListenPorts_Count; i++ )
407         {
408                 if( Conn_NewListener( Conf_ListenPorts[i] )) created++;
409                 else Log( LOG_ERR, "Can't listen on port %d!", Conf_ListenPorts[i] );
410         }
411
412         if( created < 1 )
413         {
414                 Log( LOG_ALERT, "Server isn't listening on a single port!" );
415                 Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
416                 exit( 1 );
417         }
418 } /* Initialize_Listen_Ports */
419
420
421 LOCAL VOID Show_Version( VOID )
422 {
423         puts( NGIRCd_Version( ));
424         puts( "Copyright (c)2001,2002 by Alexander Barton (alex@barton.de).\n" );
425         puts( "This is free software; see the source for copying conditions. There is NO" );
426         puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
427 } /* Show_Version */
428
429
430 LOCAL VOID Show_Help( VOID )
431 {
432 #ifdef DEBUG
433         puts( "  -d, --debug        log extra debug messages" );
434 #endif
435         puts( "  -f, --config <f>   use file <f> as configuration file" );
436         puts( "  -n, --nodaemon     don't fork and don't detatch from controlling terminal" );
437         puts( "  -p, --passive      disable automatic connections to other servers" );
438 #ifdef SNIFFER
439         puts( "  -s, --sniffer      enable network sniffer and display all IRC traffic" );
440 #endif
441         puts( "      --configtest   read, validate and display configuration; then exit" );
442         puts( "      --version      output version information and exit" );
443         puts( "      --help         display this help and exit" );
444 } /* Show_Help */
445
446
447 /* -eof- */