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