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