Blob


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 */
18 #include "portab.h"
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>
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"
40 #include "exp.h"
41 #include "ngircd.h"
44 LOCAL VOID Initialize_Signal_Handler( VOID );
45 LOCAL VOID Signal_Handler( INT Signal );
47 LOCAL VOID Initialize_Listen_Ports( VOID );
49 LOCAL VOID Show_Version( VOID );
50 LOCAL VOID Show_Help( VOID );
53 GLOBAL int main( int argc, const char *argv[] )
54 {
55 BOOLEAN ok;
56 INT32 pid, n;
57 INT i;
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
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 */
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;
101 #ifdef SNIFFER
102 if( strcmp( argv[i], "--sniffer" ) == 0 )
104 NGIRCd_Sniffer = TRUE;
105 ok = TRUE;
107 #endif
108 if( strcmp( argv[i], "--version" ) == 0 )
110 Show_Version( );
111 exit( 1 );
114 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
116 /* Kurze Option */
118 for( n = 1; n < (INT32)strlen( argv[i] ); n++ )
120 ok = FALSE;
121 #ifdef DEBUG
122 if( argv[i][n] == 'd' )
124 NGIRCd_Debug = TRUE;
125 ok = TRUE;
127 #endif
128 if( argv[i][n] == 'n' )
130 NGIRCd_NoDaemon = TRUE;
131 ok = TRUE;
133 if( argv[i][n] == 'p' )
135 NGIRCd_Passive = TRUE;
136 ok = TRUE;
138 #ifdef SNIFFER
139 if( argv[i][n] == 's' )
141 NGIRCd_Sniffer = TRUE;
142 ok = TRUE;
144 #endif
146 if( ! ok )
148 printf( PACKAGE": invalid option \"-%c\"!\n", argv[i][n] );
149 puts( "Try \""PACKAGE" --help\" for more information." );
150 exit( 1 );
155 if( ! ok )
157 printf( PACKAGE": invalid option \"%s\"!\n", argv[i] );
158 puts( "Try \""PACKAGE" --help\" for more information." );
159 exit( 1 );
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
172 while( ! NGIRCd_Quit )
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 )
180 /* Daemon im Hintergrund erzeugen */
181 pid = (INT32)fork( );
182 if( pid > 0 )
184 /* "alter" Prozess */
185 exit( 0 );
187 if( pid < 0 )
189 /* Fehler */
190 printf( PACKAGE": Can't fork: %s!\nFatal error, exiting now ...", strerror( errno ));
191 exit( 1 );
194 /* Child-Prozess initialisieren */
195 (VOID)setsid( );
196 chdir( "/" );
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;
205 /* Module initialisieren */
206 Log_Init( );
207 Conf_Init( );
208 Channel_Init( );
209 Client_Init( );
210 Conn_Init( );
212 /* Signal-Handler initialisieren */
213 Initialize_Signal_Handler( );
215 /* Listen-Ports initialisieren */
216 Initialize_Listen_Ports( );
218 /* Hauptschleife */
219 while( TRUE )
221 if( NGIRCd_Quit || NGIRCd_Restart ) break;
222 Conn_Handler( 5 );
225 /* Alles abmelden */
226 Conn_Exit( );
227 Client_Exit( );
228 Channel_Exit( );
229 Conf_Exit( );
230 Log_Exit( );
233 #ifndef DEBUG
234 /* aufraeumen */
235 if( unlink( ERROR_FILE ) != 0 ) Log( LOG_ERR, "Can't delete \""ERROR_FILE"\": %s", strerror( errno ));
236 #endif
238 return 0;
239 } /* main */
242 GLOBAL CHAR *NGIRCd_Version( VOID )
244 STATIC CHAR version[126];
246 sprintf( version, PACKAGE" version "VERSION"-%s", NGIRCd_VersionAddition( ));
247 return version;
248 } /* NGIRCd_Version */
251 GLOBAL CHAR *NGIRCd_VersionAddition( VOID )
253 STATIC CHAR txt[64];
255 strcpy( txt, "" );
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
274 if( txt[0] ) strcat( txt, "-" );
275 strcat( txt, TARGET_CPU"/"TARGET_VENDOR"/"TARGET_OS );
277 return txt;
278 } /* NGIRCd_VersionAddition */
281 LOCAL VOID Initialize_Signal_Handler( VOID )
283 /* Signal-Handler initialisieren: einige Signale
284 * werden ignoriert, andere speziell behandelt. */
286 #ifdef HAVE_SIGACTION
287 /* sigaction() ist vorhanden */
289 struct sigaction saction;
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
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);
308 /* einige Signale ignorieren */
309 saction.sa_handler = SIG_IGN;
310 sigaction( SIGPIPE, &saction, NULL );
311 #else
312 /* kein sigaction() vorhanden */
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 );
321 /* einige Signale ignorieren */
322 signal( SIGPIPE, SIG_IGN );
323 #endif
324 } /* Initialize_Signal_Handler */
327 LOCAL VOID Signal_Handler( INT Signal )
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. */
333 switch( Signal )
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 );
357 } /* Signal_Handler */
360 LOCAL VOID Initialize_Listen_Ports( VOID )
362 /* Ports, auf denen der Server Verbindungen entgegennehmen
363 * soll, initialisieren */
365 INT created, i;
367 created = 0;
368 for( i = 0; i < Conf_ListenPorts_Count; i++ )
370 if( Conn_NewListener( Conf_ListenPorts[i] )) created++;
371 else Log( LOG_ERR, "Can't listen on port %d!", Conf_ListenPorts[i] );
374 if( created < 1 )
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 );
380 } /* Initialize_Listen_Ports */
383 LOCAL VOID Show_Version( VOID )
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 */
392 LOCAL VOID Show_Help( VOID )
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 */
412 /* -eof- */