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