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