2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
11 * Main program -- main()
17 static char UNUSED id[] = "$Id";
27 #include <sys/types.h>
39 #include "cvs-version.h"
50 LOCAL VOID Initialize_Signal_Handler PARAMS(( VOID ));
51 LOCAL VOID Signal_Handler PARAMS(( INT Signal ));
53 LOCAL VOID Show_Version PARAMS(( VOID ));
54 LOCAL VOID Show_Help PARAMS(( VOID ));
58 main( int argc, const char *argv[] )
62 BOOLEAN ok, configtest = FALSE;
68 NGIRCd_Restart = FALSE;
70 NGIRCd_NoDaemon = FALSE;
71 NGIRCd_Passive = FALSE;
76 NGIRCd_Sniffer = FALSE;
78 strcpy( NGIRCd_ConfFile, CONFIG_FILE );
80 /* Kommandozeile parsen */
81 for( i = 1; i < argc; i++ )
84 if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
88 if( strcmp( argv[i], "--config" ) == 0 )
92 /* Ok, danach kommt noch ein Parameter */
93 strncpy( NGIRCd_ConfFile, argv[i + 1], FNAME_LEN - 1 );
94 NGIRCd_ConfFile[FNAME_LEN - 1] = '\0';
96 /* zum uebernaechsten Parameter */
100 if( strcmp( argv[i], "--configtest" ) == 0 )
106 if( strcmp( argv[i], "--debug" ) == 0 )
112 if( strcmp( argv[i], "--help" ) == 0 )
115 puts( "" ); Show_Help( ); puts( "" );
118 if( strcmp( argv[i], "--nodaemon" ) == 0 )
120 NGIRCd_NoDaemon = TRUE;
123 if( strcmp( argv[i], "--passive" ) == 0 )
125 NGIRCd_Passive = TRUE;
129 if( strcmp( argv[i], "--sniffer" ) == 0 )
131 NGIRCd_Sniffer = TRUE;
135 if( strcmp( argv[i], "--version" ) == 0 )
141 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
145 for( n = 1; n < (LONG)strlen( argv[i] ); n++ )
149 if( argv[i][n] == 'd' )
155 if( argv[i][n] == 'f' )
157 if(( ! argv[i][n + 1] ) && ( i + 1 < argc ))
159 /* Ok, danach kommt ein Leerzeichen */
160 strncpy( NGIRCd_ConfFile, argv[i + 1], FNAME_LEN - 1 );
161 NGIRCd_ConfFile[FNAME_LEN - 1] = '\0';
163 /* zum uebernaechsten Parameter */
164 i++; n = (LONG)strlen( argv[i] );
168 if( argv[i][n] == 'n' )
170 NGIRCd_NoDaemon = TRUE;
173 if( argv[i][n] == 'p' )
175 NGIRCd_Passive = TRUE;
179 if( argv[i][n] == 's' )
181 NGIRCd_Sniffer = TRUE;
188 printf( "%s: invalid option \"-%c\"!\n", PACKAGE, argv[i][n] );
189 printf( "Try \"%s --help\" for more information.\n", PACKAGE );
197 printf( "%s: invalid option \"%s\"!\n", PACKAGE, argv[i] );
198 printf( "Try \"%s --help\" for more information.\n", PACKAGE );
203 /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
204 strcpy( NGIRCd_DebugLevel, "" );
206 if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
212 strcpy( NGIRCd_DebugLevel, "2" );
216 /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
219 Show_Version( ); puts( "" );
223 while( ! NGIRCd_Quit )
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 )
231 /* Daemon im Hintergrund erzeugen */
235 /* "alter" Prozess */
241 printf( "%s: Can't fork: %s!\nFatal error, exiting now ...\n", PACKAGE, strerror( errno ));
245 /* Child-Prozess initialisieren */
250 /* Globale Variablen initialisieren */
251 NGIRCd_Start = time( NULL );
252 (VOID)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
253 NGIRCd_Restart = FALSE;
256 /* Module initialisieren */
265 /* Wenn als root ausgefuehrt und eine andere UID
266 * konfiguriert ist, jetzt zu dieser wechseln */
271 /* Neue Group-ID setzen */
272 if( setgid( Conf_GID ) != 0 ) Log( LOG_ERR, "Can't change Group-ID to %u: %s", Conf_GID, strerror( errno ));
276 /* Neue User-ID setzen */
277 if( setuid( Conf_UID ) != 0 ) Log( LOG_ERR, "Can't change User-ID to %u: %s", Conf_UID, strerror( errno ));
281 /* User, Gruppe und Prozess-ID des Daemon ausgeben */
282 pwd = getpwuid( getuid( )); grp = getgrgid( getgid( ));
283 Log( LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.", pwd ? pwd->pw_name : "unknown", (LONG)getuid( ), grp ? grp->gr_name : "unknown", (LONG)getgid( ), (LONG)getpid( ));
285 /* stderr in "Error-File" umlenken */
286 Log_InitErrorfile( );
288 /* Signal-Handler initialisieren */
289 Initialize_Signal_Handler( );
291 /* Protokoll- und Server-Identifikation erzeugen. Die vom ngIRCd
292 * beim PASS-Befehl verwendete Syntax sowie die erweiterten Flags
293 * sind in doc/Protocol.txt beschrieben. */
295 sprintf( NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE, VERSION, IRCPLUSFLAGS );
297 strcat( NGIRCd_ProtoID, "Z" );
299 if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
301 sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE, VERSION );
303 strcat( NGIRCd_ProtoID, " P" );
305 strcat( NGIRCd_ProtoID, "Z" );
307 Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
309 /* Vordefinierte Channels anlegen */
310 Channel_InitPredefined( );
312 /* Listen-Ports initialisieren */
313 if( Conn_InitListeners( ) < 1 )
315 Log( LOG_ALERT, "Server isn't listening on a single port!" );
316 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
336 NGIRCd_Version( VOID )
338 STATIC CHAR version[126];
341 sprintf( version, "%s %s(%s)-%s", PACKAGE, VERSION, CVSDATE, NGIRCd_VersionAddition( ));
343 sprintf( version, "%s %s-%s", PACKAGE, VERSION, NGIRCd_VersionAddition( ));
346 } /* NGIRCd_Version */
350 NGIRCd_VersionAddition( VOID )
357 if( txt[0] ) strcat( txt, "+" );
358 strcat( txt, "SYSLOG" );
361 if( txt[0] ) strcat( txt, "+" );
362 strcat( txt, "ZLIB" );
365 if( txt[0] ) strcat( txt, "+" );
366 strcat( txt, "DEBUG" );
369 if( txt[0] ) strcat( txt, "+" );
370 strcat( txt, "SNIFFER" );
373 if( txt[0] ) strcat( txt, "+" );
374 strcat( txt, "RFC" );
377 if( txt[0] ) strcat( txt, "+" );
378 strcat( txt, "IRCPLUS" );
381 if( txt[0] ) strcat( txt, "-" );
382 strcat( txt, TARGET_CPU );
384 strcat( txt, TARGET_VENDOR );
386 strcat( txt, TARGET_OS );
389 } /* NGIRCd_VersionAddition */
393 NGIRCd_Rehash( VOID )
395 CHAR old_name[CLIENT_ID_LEN];
397 /* Alle Listen-Sockets schliessen */
398 Conn_ExitListeners( );
400 /* Alten Server-Namen merken */
401 strcpy( old_name, Conf_ServerName );
403 /* Konfiguration neu lesen ... */
406 /* Alten Server-Namen wiederherstellen: dieser
407 * kann nicht zur Laufzeit geaendert werden ... */
408 if( strcmp( old_name, Conf_ServerName ) != 0 )
410 strcpy( Conf_ServerName, old_name );
411 Log( LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name." );
414 /* neue pre-defined Channel anlegen: */
415 Channel_InitPredefined( );
417 /* Listen-Sockets neu anlegen: */
418 Conn_InitListeners( );
420 Log( LOG_INFO, "Re-reading of configuration done." );
421 } /* NGIRCd_Rehash */
425 Initialize_Signal_Handler( VOID )
427 /* Signal-Handler initialisieren: einige Signale
428 * werden ignoriert, andere speziell behandelt. */
430 #ifdef HAVE_SIGACTION
431 /* sigaction() ist vorhanden */
433 struct sigaction saction;
435 /* Signal-Struktur initialisieren */
436 memset( &saction, 0, sizeof( saction ));
437 saction.sa_handler = Signal_Handler;
439 saction.sa_flags |= SA_RESTART;
442 saction.sa_flags |= SA_NOCLDWAIT;
445 /* Signal-Handler einhaengen */
446 sigaction( SIGINT, &saction, NULL );
447 sigaction( SIGQUIT, &saction, NULL );
448 sigaction( SIGTERM, &saction, NULL);
449 sigaction( SIGHUP, &saction, NULL);
450 sigaction( SIGCHLD, &saction, NULL);
452 /* einige Signale ignorieren */
453 saction.sa_handler = SIG_IGN;
454 sigaction( SIGPIPE, &saction, NULL );
456 /* kein sigaction() vorhanden */
458 /* Signal-Handler einhaengen */
459 signal( SIGINT, Signal_Handler );
460 signal( SIGQUIT, Signal_Handler );
461 signal( SIGTERM, Signal_Handler );
462 signal( SIGHUP, Signal_Handler );
463 signal( SIGCHLD, Signal_Handler );
465 /* einige Signale ignorieren */
466 signal( SIGPIPE, SIG_IGN );
468 } /* Initialize_Signal_Handler */
472 Signal_Handler( INT Signal )
474 /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
475 * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
476 * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
483 /* wir soll(t)en uns wohl beenden ... */
484 if( Signal == SIGTERM ) Log( LOG_WARNING|LOG_snotice, "Got TERM signal, terminating now ..." );
485 else if( Signal == SIGINT ) Log( LOG_WARNING|LOG_snotice, "Got INT signal, terminating now ..." );
486 else if( Signal == SIGQUIT ) Log( LOG_WARNING|LOG_snotice, "Got QUIT signal, terminating now ..." );
490 /* Konfiguration neu einlesen: */
491 Log( LOG_WARNING|LOG_snotice, "Got HUP signal, re-reading configuration ..." );
495 /* Child-Prozess wurde beendet. Zombies vermeiden: */
496 while( waitpid( -1, NULL, WNOHANG ) > 0);
499 /* unbekanntes bzw. unbehandeltes Signal */
500 Log( LOG_NOTICE, "Got signal %d! Ignored.", Signal );
502 } /* Signal_Handler */
508 puts( NGIRCd_Version( ));
509 puts( "Copyright (c)2001,2002 by Alexander Barton (<alex@barton.de>)." );
510 puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
511 puts( "This is free software; see the source for copying conditions. There is NO" );
512 puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
520 puts( " -d, --debug log extra debug messages" );
522 puts( " -f, --config <f> use file <f> as configuration file" );
523 puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
524 puts( " -p, --passive disable automatic connections to other servers" );
526 puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
528 puts( " --configtest read, validate and display configuration; then exit" );
529 puts( " --version output version information and exit" );
530 puts( " --help display this help and exit" );