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