]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/ngircd.c
4291c79f9d1a9ca53cb4778ef0923e2f06180358
[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.36 2002/03/27 16:41:25 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], "--configtest" ) == 0 )
80                         {
81                                 configtest = TRUE;
82                                 ok = TRUE;
83                         }
84 #ifdef DEBUG
85                         if( strcmp( argv[i], "--debug" ) == 0 )
86                         {
87                                 NGIRCd_Debug = TRUE;
88                                 ok = TRUE;
89                         }
90 #endif
91                         if( strcmp( argv[i], "--help" ) == 0 )
92                         {
93                                 Show_Version( );
94                                 puts( "" ); Show_Help( ); puts( "" );
95                                 exit( 1 );
96                         }
97                         if( strcmp( argv[i], "--nodaemon" ) == 0 )
98                         {
99                                 NGIRCd_NoDaemon = TRUE;
100                                 ok = TRUE;
101                         }
102                         if( strcmp( argv[i], "--passive" ) == 0 )
103                         {
104                                 NGIRCd_Passive = TRUE;
105                                 ok = TRUE;
106                         }
107 #ifdef SNIFFER
108                         if( strcmp( argv[i], "--sniffer" ) == 0 )
109                         {
110                                 NGIRCd_Sniffer = TRUE;
111                                 ok = TRUE;
112                         }
113 #endif
114                         if( strcmp( argv[i], "--version" ) == 0 )
115                         {
116                                 Show_Version( );
117                                 exit( 1 );
118                         }
119                 }
120                 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
121                 {
122                         /* Kurze Option */
123                         
124                         for( n = 1; n < (INT32)strlen( argv[i] ); n++ )
125                         {
126                                 ok = FALSE;
127 #ifdef DEBUG
128                                 if( argv[i][n] == 'd' )
129                                 {
130                                         NGIRCd_Debug = TRUE;
131                                         ok = TRUE;
132                                 }
133 #endif
134                                 if( argv[i][n] == 'n' )
135                                 {
136                                         NGIRCd_NoDaemon = TRUE;
137                                         ok = TRUE;
138                                 }
139                                 if( argv[i][n] == 'p' )
140                                 {
141                                         NGIRCd_Passive = TRUE;
142                                         ok = TRUE;
143                                 }
144 #ifdef SNIFFER
145                                 if( argv[i][n] == 's' )
146                                 {
147                                         NGIRCd_Sniffer = TRUE;
148                                         ok = TRUE;
149                                 }
150 #endif
151
152                                 if( ! ok )
153                                 {
154                                         printf( PACKAGE": invalid option \"-%c\"!\n", argv[i][n] );
155                                         puts( "Try \""PACKAGE" --help\" for more information." );
156                                         exit( 1 );
157                                 }
158                         }
159
160                 }
161                 if( ! ok )
162                 {
163                         printf( PACKAGE": invalid option \"%s\"!\n", argv[i] );
164                         puts( "Try \""PACKAGE" --help\" for more information." );
165                         exit( 1 );
166                 }
167         }
168
169         /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
170         strcpy( NGIRCd_DebugLevel, "" );
171 #ifdef DEBUG
172         if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
173 #endif
174 #ifdef SNIFFER
175         if( NGIRCd_Sniffer ) strcpy( NGIRCd_DebugLevel, "2" );
176 #endif
177
178         /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
179         if( configtest )
180         {
181                 Show_Version( ); puts( "" );
182                 exit( Conf_Test( ));
183         }
184         
185         while( ! NGIRCd_Quit )
186         {
187                 /* In der Regel wird ein Sub-Prozess ge-fork()'t, der
188                  * nicht mehr mit dem Terminal verbunden ist. Mit der
189                  * Option "--nodaemon" kann dies (z.B. zum Debuggen)
190                  * verhindert werden. */
191                 if( ! NGIRCd_NoDaemon )
192                 {
193                         /* Daemon im Hintergrund erzeugen */
194                         pid = (INT32)fork( );
195                         if( pid > 0 )
196                         {
197                                 /* "alter" Prozess */
198                                 exit( 0 );
199                         }
200                         if( pid < 0 )
201                         {
202                                 /* Fehler */
203                                 printf( PACKAGE": Can't fork: %s!\nFatal error, exiting now ...", strerror( errno ));
204                                 exit( 1 );
205                         }
206
207                         /* Child-Prozess initialisieren */
208                         (VOID)setsid( );
209                         chdir( "/" );
210                 }
211         
212                 /* Globale Variablen initialisieren */
213                 NGIRCd_Start = time( NULL );
214                 (VOID)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
215                 NGIRCd_Restart = FALSE;
216                 NGIRCd_Quit = FALSE;
217
218                 /* Module initialisieren */
219                 Log_Init( );
220                 Conf_Init( );
221                 Channel_Init( );
222                 Client_Init( );
223                 Conn_Init( );
224
225                 /* Signal-Handler initialisieren */
226                 Initialize_Signal_Handler( );
227
228                 /* Listen-Ports initialisieren */
229                 Initialize_Listen_Ports( );
230
231                 /* Hauptschleife */
232                 while( TRUE )
233                 {
234                         if( NGIRCd_Quit || NGIRCd_Restart ) break;
235                         Conn_Handler( 5 );
236                 }
237
238                 /* Alles abmelden */
239                 Conn_Exit( );
240                 Client_Exit( );
241                 Channel_Exit( );
242                 Conf_Exit( );
243                 Log_Exit( );
244         }
245
246 #ifndef DEBUG
247         /* aufraeumen */
248         if( unlink( ERROR_FILE ) != 0 ) Log( LOG_ERR, "Can't delete \""ERROR_FILE"\": %s", strerror( errno ));
249 #endif
250
251         return 0;
252 } /* main */
253
254
255 GLOBAL CHAR *NGIRCd_Version( VOID )
256 {
257         STATIC CHAR version[126];
258
259         sprintf( version, PACKAGE" version "VERSION"-%s", NGIRCd_VersionAddition( ));
260         return version;
261 } /* NGIRCd_Version */
262
263
264 GLOBAL CHAR *NGIRCd_VersionAddition( VOID )
265 {
266         STATIC CHAR txt[64];
267
268         strcpy( txt, "" );
269
270 #ifdef USE_SYSLOG
271         if( txt[0] ) strcat( txt, "+" );
272         strcat( txt, "SYSLOG" );
273 #endif
274 #ifdef STRICT_RFC
275         if( txt[0] ) strcat( txt, "+" );
276         strcat( txt, "RFC" );
277 #endif
278 #ifdef DEBUG
279         if( txt[0] ) strcat( txt, "+" );
280         strcat( txt, "DEBUG" );
281 #endif
282 #ifdef SNIFFER
283         if( txt[0] ) strcat( txt, "+" );
284         strcat( txt, "SNIFFER" );
285 #endif
286
287         if( txt[0] ) strcat( txt, "-" );
288         strcat( txt, TARGET_CPU"/"TARGET_VENDOR"/"TARGET_OS );
289
290         return txt;
291 } /* NGIRCd_VersionAddition */
292
293
294 LOCAL VOID Initialize_Signal_Handler( VOID )
295 {
296         /* Signal-Handler initialisieren: einige Signale
297          * werden ignoriert, andere speziell behandelt. */
298
299 #ifdef HAVE_SIGACTION
300         /* sigaction() ist vorhanden */
301
302         struct sigaction saction;
303
304         /* Signal-Struktur initialisieren */
305         memset( &saction, 0, sizeof( saction ));
306         saction.sa_handler = Signal_Handler;
307 #ifdef SA_RESTART
308         saction.sa_flags |= SA_RESTART;
309 #endif
310 #ifdef SA_NOCLDWAIT
311         saction.sa_flags |= SA_NOCLDWAIT;
312 #endif
313
314         /* Signal-Handler einhaengen */
315         sigaction( SIGINT, &saction, NULL );
316         sigaction( SIGQUIT, &saction, NULL );
317         sigaction( SIGTERM, &saction, NULL);
318         sigaction( SIGHUP, &saction, NULL);
319         sigaction( SIGCHLD, &saction, NULL);
320
321         /* einige Signale ignorieren */
322         saction.sa_handler = SIG_IGN;
323         sigaction( SIGPIPE, &saction, NULL );
324 #else
325         /* kein sigaction() vorhanden */
326
327         /* Signal-Handler einhaengen */
328         signal( SIGINT, Signal_Handler );
329         signal( SIGQUIT, Signal_Handler );
330         signal( SIGTERM, Signal_Handler );
331         signal( SIGHUP, Signal_Handler );
332         signal( SIGCHLD, Signal_Handler );
333
334         /* einige Signale ignorieren */
335         signal( SIGPIPE, SIG_IGN );
336 #endif
337 } /* Initialize_Signal_Handler */
338
339
340 LOCAL VOID Signal_Handler( INT Signal )
341 {
342         /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
343          * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
344          * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
345
346         switch( Signal )
347         {
348                 case SIGTERM:
349                 case SIGINT:
350                 case SIGQUIT:
351                         /* wir soll(t)en uns wohl beenden ... */
352                         if( Signal == SIGTERM ) Log( LOG_WARNING, "Got TERM signal, terminating now ..." );
353                         else if( Signal == SIGINT ) Log( LOG_WARNING, "Got INT signal, terminating now ..." );
354                         else if( Signal == SIGQUIT ) Log( LOG_WARNING, "Got QUIT signal, terminating now ..." );
355                         NGIRCd_Quit = TRUE;
356                         break;
357                 case SIGHUP:
358                         /* neu starten */
359                         Log( LOG_WARNING, "Got HUP signal, restarting now ..." );
360                         NGIRCd_Restart = TRUE;
361                         break;
362                 case SIGCHLD:
363                         /* Child-Prozess wurde beendet. Zombies vermeiden: */
364                         while( waitpid( -1, NULL, WNOHANG ) > 0);
365                         break;
366                 default:
367                         /* unbekanntes bzw. unbehandeltes Signal */
368                         Log( LOG_NOTICE, "Got signal %d! Ignored.", Signal );
369         }
370 } /* Signal_Handler */
371
372
373 LOCAL VOID Initialize_Listen_Ports( VOID )
374 {
375         /* Ports, auf denen der Server Verbindungen entgegennehmen
376          * soll, initialisieren */
377         
378         INT created, i;
379
380         created = 0;
381         for( i = 0; i < Conf_ListenPorts_Count; i++ )
382         {
383                 if( Conn_NewListener( Conf_ListenPorts[i] )) created++;
384                 else Log( LOG_ERR, "Can't listen on port %d!", Conf_ListenPorts[i] );
385         }
386
387         if( created < 1 )
388         {
389                 Log( LOG_ALERT, "Server isn't listening on a single port!" );
390                 Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
391                 exit( 1 );
392         }
393 } /* Initialize_Listen_Ports */
394
395
396 LOCAL VOID Show_Version( VOID )
397 {
398         puts( NGIRCd_Version( ));
399         puts( "Copyright (c)2001,2002 by Alexander Barton (alex@barton.de).\n" );
400         puts( "This is free software; see the source for copying conditions. There is NO" );
401         puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
402 } /* Show_Version */
403
404
405 LOCAL VOID Show_Help( VOID )
406 {
407 #ifdef DEBUG
408         puts( "  -d, --debug       log extra debug messages" );
409 #endif
410         puts( "  -n, --nodaemon    don't fork and don't detatch from controlling terminal" );
411         puts( "  -p, --passive     disable automatic connections to other servers" );
412 #ifdef SNIFFER
413         puts( "  -s, --sniffer     enable network sniffer and display all IRC traffic" );
414 #endif
415         puts( "      --configtest  read, validate and display configuration; then exit" );
416         puts( "      --version     output version information and exit" );
417         puts( "      --help        display this help and exit" );
418 } /* Show_Help */
419
420
421 /* -eof- */