]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/ngircd.c
- "check"-Target für "make check" und "make distcheck" begonnen ...
[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.25 2002/02/19 20:30:47 alex Exp $
13  *
14  * ngircd.c: Hier beginnt alles ;-)
15  *
16  * $Log: ngircd.c,v $
17  * Revision 1.25  2002/02/19 20:30:47  alex
18  * - SA_RESTART wird fuer Signale nur noch gesetzt, wenn es definiert ist.
19  *
20  * Revision 1.24  2002/02/19 20:08:24  alex
21  * - "Passive-Mode" implementiert: kein Auto-Conect zu anderen Servern.
22  * - NGIRCd_DebugLevel wird (fuer VERSION-Befehl) ermittelt.
23  *
24  * Revision 1.23  2002/02/17 23:40:21  alex
25  * - neue Funktion NGIRCd_VersionAddition(). NGIRCd_Version() aufgespaltet.
26  *
27  * Revision 1.22  2002/01/22 17:15:39  alex
28  * - die Fehlermeldung "interrupted system call" sollte nicht mehr auftreten.
29  *
30  * Revision 1.21  2002/01/21 00:02:11  alex
31  * - Hilfetexte korrigiert und ergaenzt (Sniffer).
32  *
33  * Revision 1.20  2002/01/18 11:12:11  alex
34  * - der Sniffer wird nun nur noch aktiviert, wenn auf Kommandozeile angegeben.
35  *
36  * Revision 1.19  2002/01/12 00:17:28  alex
37  * - ngIRCd wandelt sich nun selber in einen Daemon (Hintergrundprozess) um.
38  *
39  * Revision 1.18  2002/01/11 14:45:18  alex
40  * - Kommandozeilen-Parser implementiert: Debug- und No-Daemon-Modus, Hilfe.
41  *
42  * Revision 1.17  2002/01/02 02:51:16  alex
43  * - Signal-Handler fuer SIGCHLD: so sollten Zombies nicht mehr vorkommen.
44  *
45  * Revision 1.15  2001/12/31 02:18:51  alex
46  * - viele neue Befehle (WHOIS, ISON, OPER, DIE, RESTART),
47  * - neuen Header "defines.h" mit (fast) allen Konstanten.
48  * - Code Cleanups und viele "kleine" Aenderungen & Bugfixes.
49  *
50  * Revision 1.14  2001/12/30 19:26:12  alex
51  * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
52  *
53  * Revision 1.13  2001/12/30 11:42:00  alex
54  * - der Server meldet nun eine ordentliche "Start-Zeit".
55  *
56  * Revision 1.12  2001/12/29 03:07:36  alex
57  * - einige Loglevel geaendert.
58  *
59  * Revision 1.11  2001/12/26 14:45:37  alex
60  * - "Code Cleanups".
61  *
62  * Revision 1.10  2001/12/24 01:34:38  alex
63  * - Signal-Handler aufgeraeumt; u.a. SIGPIPE wird nun korrekt ignoriert.
64  *
65  * Revision 1.9  2001/12/21 22:24:50  alex
66  * - neues Modul "parse" wird initialisiert und abgemeldet.
67  *
68  * Revision 1.8  2001/12/14 08:15:26  alex
69  * - neue Module (irc, client, channel) werden an- und abgemeldet.
70  * - zweiter Listen-Socket wird zu Testzwecken konfiguriert.
71  *
72  * Revision 1.7  2001/12/13 01:31:46  alex
73  * - Conn_Handler() wird nun mit einem Timeout aufgerufen.
74  *
75  * Revision 1.6  2001/12/12 23:30:42  alex
76  * - Log-Meldungen an syslog angepasst.
77  * - NGIRCd_Quit ist nun das Flag zum Beenden des ngircd.
78  *
79  * Revision 1.5  2001/12/12 17:21:21  alex
80  * - mehr Unterfunktionen eingebaut, Modul besser strukturiert & dokumentiert.
81  * - Anpassungen an neue Module.
82  *
83  * Revision 1.4  2001/12/12 01:58:53  alex
84  * - Test auf socklen_t verbessert.
85  *
86  * Revision 1.3  2001/12/12 01:40:39  alex
87  * - ein paar mehr Kommentare; Variablennamen verstaendlicher gemacht.
88  * - fehlenden Header <arpa/inet.h> ergaenz.
89  * - SIGINT und SIGQUIT werden nun ebenfalls behandelt.
90  *
91  * Revision 1.2  2001/12/11 22:04:21  alex
92  * - Test auf stdint.h (HAVE_STDINT_H) hinzugefuegt.
93  *
94  * Revision 1.1.1.1  2001/12/11 21:53:04  alex
95  * - Imported sources to CVS.
96  */
97
98
99 #define PORTAB_CHECK_TYPES              /* Prueffunktion einbinden, s.u. */
100
101
102 #include <portab.h>
103 #include "global.h"
104
105 #include <imp.h>
106
107 #include <assert.h>
108 #include <errno.h>
109 #include <stdio.h>
110 #include <signal.h>
111 #include <string.h>
112 #include <unistd.h>
113 #include <sys/types.h>
114 #include <sys/wait.h>
115 #include <time.h>
116
117 #include "channel.h"
118 #include "client.h"
119 #include "conf.h"
120 #include "conn.h"
121 #include "irc.h"
122 #include "log.h"
123 #include "parse.h"
124
125 #include <exp.h>
126 #include "ngircd.h"
127
128
129 LOCAL VOID Initialize_Signal_Handler( VOID );
130 LOCAL VOID Signal_Handler( INT Signal );
131
132 LOCAL VOID Initialize_Listen_Ports( VOID );
133
134 LOCAL VOID Show_Version( VOID );
135 LOCAL VOID Show_Help( VOID );
136
137
138 GLOBAL INT main( INT argc, CONST CHAR *argv[] )
139 {
140         BOOLEAN ok;
141         INT pid, i, n;
142
143         /* Datentypen der portab-Library ueberpruefen */
144         portab_check_types( );
145
146         NGIRCd_Restart = FALSE;
147         NGIRCd_Quit = FALSE;
148         NGIRCd_NoDaemon = FALSE;
149         NGIRCd_Passive = FALSE;
150 #ifdef DEBUG
151         NGIRCd_Debug = FALSE;
152 #endif
153 #ifdef SNIFFER
154         NGIRCd_Sniffer = FALSE;
155 #endif
156
157         /* Kommandozeile parsen */
158         for( i = 1; i < argc; i++ )
159         {
160                 ok = FALSE;
161                 if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
162                 {
163                         /* Lange Option */
164
165 #ifdef DEBUG
166                         if( strcmp( argv[i], "--debug" ) == 0 )
167                         {
168                                 NGIRCd_Debug = TRUE;
169                                 ok = TRUE;
170                         }
171 #endif
172                         if( strcmp( argv[i], "--help" ) == 0 )
173                         {
174                                 Show_Version( ); puts( "" );
175                                 Show_Help( ); puts( "" );
176                                 exit( 1 );
177                         }
178                         if( strcmp( argv[i], "--nodaemon" ) == 0 )
179                         {
180                                 NGIRCd_NoDaemon = TRUE;
181                                 ok = TRUE;
182                         }
183                         if( strcmp( argv[i], "--passive" ) == 0 )
184                         {
185                                 NGIRCd_Passive = TRUE;
186                                 ok = TRUE;
187                         }
188 #ifdef SNIFFER
189                         if( strcmp( argv[i], "--sniffer" ) == 0 )
190                         {
191                                 NGIRCd_Sniffer = TRUE;
192                                 ok = TRUE;
193                         }
194 #endif
195                         if( strcmp( argv[i], "--version" ) == 0 )
196                         {
197                                 Show_Version( );
198                                 exit( 1 );
199                         }
200                 }
201                 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
202                 {
203                         /* Kurze Option */
204                         
205                         for( n = 1; n < strlen( argv[i] ); n++ )
206                         {
207                                 ok = FALSE;
208 #ifdef DEBUG
209                                 if( argv[i][n] == 'd' )
210                                 {
211                                         NGIRCd_Debug = TRUE;
212                                         ok = TRUE;
213                                 }
214 #endif
215                                 if( argv[i][n] == 'n' )
216                                 {
217                                         NGIRCd_NoDaemon = TRUE;
218                                         ok = TRUE;
219                                 }
220                                 if( argv[i][n] == 'p' )
221                                 {
222                                         NGIRCd_Passive = TRUE;
223                                         ok = TRUE;
224                                 }
225 #ifdef SNIFFER
226                                 if( argv[i][n] == 's' )
227                                 {
228                                         NGIRCd_Sniffer = TRUE;
229                                         ok = TRUE;
230                                 }
231 #endif
232
233                                 if( ! ok )
234                                 {
235                                         printf( PACKAGE": invalid option \"-%c\"!\n", argv[i][n] );
236                                         puts( "Try \""PACKAGE" --help\" for more information." );
237                                         exit( 1 );
238                                 }
239                         }
240
241                 }
242                 if( ! ok )
243                 {
244                         printf( PACKAGE": invalid option \"%s\"!\n", argv[i] );
245                         puts( "Try \""PACKAGE" --help\" for more information." );
246                         exit( 1 );
247                 }
248         }
249
250         /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
251         strcpy( NGIRCd_DebugLevel, "" );
252 #ifdef DEBUG
253         if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
254 #endif
255 #ifdef SNIFFER
256         if( NGIRCd_Sniffer ) strcpy( NGIRCd_DebugLevel, "2" );
257 #endif
258         
259         while( ! NGIRCd_Quit )
260         {
261                 /* In der Regel wird ein Sub-Prozess ge-fork()'t, der
262                  * nicht mehr mit dem Terminal verbunden ist. Mit der
263                  * Option "--nodaemon" kann dies (z.B. zum Debuggen)
264                  * verhindert werden. */
265                 if( ! NGIRCd_NoDaemon )
266                 {
267                         /* Daemon im Hintergrund erzeugen */
268                         pid = fork( );
269                         if( pid > 0 )
270                         {
271                                 /* "alter" Prozess */
272                                 exit( 0 );
273                         }
274                         if( pid < 0 )
275                         {
276                                 /* Fehler */
277                                 printf( PACKAGE": Can't fork: %s!\nFatal error, exiting now ...", strerror( errno ));
278                                 exit( 1 );
279                         }
280                         setsid( );
281                         chdir( "/" );
282                 }
283         
284                 /* Globale Variablen initialisieren */
285                 NGIRCd_Start = time( NULL );
286                 strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
287                 NGIRCd_Restart = FALSE;
288                 NGIRCd_Quit = FALSE;
289
290                 /* Module initialisieren */
291                 Log_Init( );
292                 Conf_Init( );
293                 Parse_Init( );
294                 IRC_Init( );
295                 Channel_Init( );
296                 Client_Init( );
297                 Conn_Init( );
298
299                 /* Signal-Handler initialisieren */
300                 Initialize_Signal_Handler( );
301
302                 /* Listen-Ports initialisieren */
303                 Initialize_Listen_Ports( );
304
305                 /* Hauptschleife */
306                 while( TRUE )
307                 {
308                         if( NGIRCd_Quit || NGIRCd_Restart ) break;
309                         Conn_Handler( 5 );
310                 }
311
312                 /* Alles abmelden */
313                 Conn_Exit( );
314                 Client_Exit( );
315                 Channel_Exit( );
316                 IRC_Exit( );
317                 Parse_Exit( );
318                 Conf_Exit( );
319                 Log_Exit( );
320         }
321         return 0;
322 } /* main */
323
324
325 GLOBAL CHAR *NGIRCd_Version( VOID )
326 {
327         STATIC CHAR version[126];
328
329         sprintf( version, PACKAGE" version "VERSION"-%s", NGIRCd_VersionAddition( ));
330         return version;
331 } /* NGIRCd_Version */
332
333
334 GLOBAL CHAR *NGIRCd_VersionAddition( VOID )
335 {
336         STATIC CHAR txt[64];
337
338         strcpy( txt, "" );
339
340 #ifdef USE_SYSLOG
341         if( txt[0] ) strcat( txt, "+" );
342         strcat( txt, "SYSLOG" );
343 #endif
344 #ifdef STRICT_RFC
345         if( txt[0] ) strcat( txt, "+" );
346         strcat( txt, "RFC" );
347 #endif
348 #ifdef DEBUG
349         if( txt[0] ) strcat( txt, "+" );
350         strcat( txt, "DEBUG" );
351 #endif
352 #ifdef SNIFFER
353         if( txt[0] ) strcat( txt, "+" );
354         strcat( txt, "SNIFFER" );
355 #endif
356
357         if( txt[0] ) strcat( txt, "-" );
358         strcat( txt, P_OSNAME"/"P_ARCHNAME );
359
360         return txt;
361 } /* NGIRCd_VersionAddition */
362
363
364 LOCAL VOID Initialize_Signal_Handler( VOID )
365 {
366         /* Signal-Handler initialisieren: einige Signale
367          * werden ignoriert, andere speziell behandelt. */
368
369         struct sigaction saction;
370
371         /* Signal-Struktur initialisieren */
372         memset( &saction, 0, sizeof( saction ));
373         saction.sa_handler = Signal_Handler;
374 #ifdef SA_RESTART
375         saction.sa_flags = SA_RESTART;
376 #endif
377
378         /* Signal-Handler einhaengen */
379         sigaction( SIGINT, &saction, NULL );
380         sigaction( SIGQUIT, &saction, NULL );
381         sigaction( SIGTERM, &saction, NULL);
382         sigaction( SIGHUP, &saction, NULL);
383         sigaction( SIGCHLD, &saction, NULL);
384
385         /* einige Signale ignorieren */
386         saction.sa_handler = SIG_IGN;
387         sigaction( SIGPIPE, &saction, NULL );
388 } /* Initialize_Signal_Handler */
389
390
391 LOCAL VOID Signal_Handler( INT Signal )
392 {
393         /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
394          * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
395          * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
396
397         switch( Signal )
398         {
399                 case SIGTERM:
400                 case SIGINT:
401                 case SIGQUIT:
402                         /* wir soll(t)en uns wohl beenden ... */
403                         if( Signal == SIGTERM ) Log( LOG_WARNING, "Got TERM signal, terminating now ..." );
404                         else if( Signal == SIGINT ) Log( LOG_WARNING, "Got INT signal, terminating now ..." );
405                         else if( Signal == SIGQUIT ) Log( LOG_WARNING, "Got QUIT signal, terminating now ..." );
406                         NGIRCd_Quit = TRUE;
407                         break;
408                 case SIGHUP:
409                         /* neu starten */
410                         Log( LOG_WARNING, "Got HUP signal, restarting now ..." );
411                         NGIRCd_Restart = TRUE;
412                         break;
413                 case SIGCHLD:
414                         /* Child-Prozess wurde beendet. Zombies vermeiden: */
415                         while( waitpid( -1, NULL, WNOHANG ) > 0);
416                         break;
417                 default:
418                         /* unbekanntes bzw. unbehandeltes Signal */
419                         Log( LOG_NOTICE, "Got signal %d! Ignored.", Signal );
420         }
421 } /* Signal_Handler */
422
423
424 LOCAL VOID Initialize_Listen_Ports( VOID )
425 {
426         /* Ports, auf denen der Server Verbindungen entgegennehmen
427          * soll, initialisieren */
428         
429         INT created, i;
430
431         created = 0;
432         for( i = 0; i < Conf_ListenPorts_Count; i++ )
433         {
434                 if( Conn_NewListener( Conf_ListenPorts[i] )) created++;
435                 else Log( LOG_ERR, "Can't listen on port %d!", Conf_ListenPorts[i] );
436         }
437
438         if( created < 1 )
439         {
440                 Log( LOG_ALERT, "Server isn't listening on a single port!" );
441                 Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
442                 exit( 1 );
443         }
444 } /* Initialize_Listen_Ports */
445
446
447 LOCAL VOID Show_Version( VOID )
448 {
449         puts( NGIRCd_Version( ));
450         puts( "Copyright (c)2001,2002 by Alexander Barton (alex@barton.de).\n" );
451         puts( "This is free software; see the source for copying conditions. There is NO" );
452         puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
453 } /* Show_Version */
454
455
456 LOCAL VOID Show_Help( VOID )
457 {
458 #ifdef DEBUG
459         puts( "  -d, --debug       log extra debug messages" );
460 #endif
461         puts( "  -n, --nodaemon    don't fork and don't detatch from controlling terminal" );
462         puts( "  -p, --passive     disable automatic connections to other servers" );
463 #ifdef SNIFFER
464         puts( "  -s, --sniffer     enable network sniffer and display all IRC traffic" );
465 #endif
466         puts( "      --version     output version information and exit" );
467         puts( "      --help        display this help and exit" );
468 } /* Show_Help */
469
470
471 /* -eof- */