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