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: ngircd.c,v 1.65 2002/12/19 04:29:59 alex Exp $";
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_SignalQuit = NGIRCd_SignalRestart = NGIRCd_SignalRehash = FALSE;
69 NGIRCd_NoDaemon = NGIRCd_Passive = FALSE;
74 NGIRCd_Sniffer = FALSE;
76 strcpy( NGIRCd_ConfFile, CONFIG_FILE );
78 /* Kommandozeile parsen */
79 for( i = 1; i < argc; i++ )
82 if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
86 if( strcmp( argv[i], "--config" ) == 0 )
90 /* Ok, danach kommt noch ein Parameter */
91 strncpy( NGIRCd_ConfFile, argv[i + 1], FNAME_LEN - 1 );
92 NGIRCd_ConfFile[FNAME_LEN - 1] = '\0';
94 /* zum uebernaechsten Parameter */
98 if( strcmp( argv[i], "--configtest" ) == 0 )
104 if( strcmp( argv[i], "--debug" ) == 0 )
110 if( strcmp( argv[i], "--help" ) == 0 )
113 puts( "" ); Show_Help( ); puts( "" );
116 if( strcmp( argv[i], "--nodaemon" ) == 0 )
118 NGIRCd_NoDaemon = TRUE;
121 if( strcmp( argv[i], "--passive" ) == 0 )
123 NGIRCd_Passive = TRUE;
127 if( strcmp( argv[i], "--sniffer" ) == 0 )
129 NGIRCd_Sniffer = TRUE;
133 if( strcmp( argv[i], "--version" ) == 0 )
139 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
143 for( n = 1; n < (LONG)strlen( argv[i] ); n++ )
147 if( argv[i][n] == 'd' )
153 if( argv[i][n] == 'f' )
155 if(( ! argv[i][n + 1] ) && ( i + 1 < argc ))
157 /* Ok, danach kommt ein Leerzeichen */
158 strncpy( NGIRCd_ConfFile, argv[i + 1], FNAME_LEN - 1 );
159 NGIRCd_ConfFile[FNAME_LEN - 1] = '\0';
161 /* zum uebernaechsten Parameter */
162 i++; n = (LONG)strlen( argv[i] );
166 if( argv[i][n] == 'n' )
168 NGIRCd_NoDaemon = TRUE;
171 if( argv[i][n] == 'p' )
173 NGIRCd_Passive = TRUE;
177 if( argv[i][n] == 's' )
179 NGIRCd_Sniffer = TRUE;
186 printf( "%s: invalid option \"-%c\"!\n", PACKAGE, argv[i][n] );
187 printf( "Try \"%s --help\" for more information.\n", PACKAGE );
195 printf( "%s: invalid option \"%s\"!\n", PACKAGE, argv[i] );
196 printf( "Try \"%s --help\" for more information.\n", PACKAGE );
201 /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
202 strcpy( NGIRCd_DebugLevel, "" );
204 if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
210 strcpy( NGIRCd_DebugLevel, "2" );
214 /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
217 Show_Version( ); puts( "" );
221 while( ! NGIRCd_SignalQuit )
223 /* In der Regel wird ein Sub-Prozess ge-fork()'t, der
224 * nicht mehr mit dem Terminal verbunden ist. Mit der
225 * Option "--nodaemon" kann dies (z.B. zum Debuggen)
226 * verhindert werden. */
227 if( ! NGIRCd_NoDaemon )
229 /* Daemon im Hintergrund erzeugen */
233 /* "alter" Prozess */
239 printf( "%s: Can't fork: %s!\nFatal error, exiting now ...\n", PACKAGE, strerror( errno ));
243 /* Child-Prozess initialisieren */
248 /* Globale Variablen initialisieren */
249 NGIRCd_Start = time( NULL );
250 (VOID)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
251 NGIRCd_SignalRehash = FALSE;
252 NGIRCd_SignalRestart = FALSE;
253 NGIRCd_SignalQuit = FALSE;
255 /* Module initialisieren */
264 /* Wenn als root ausgefuehrt und eine andere UID
265 * konfiguriert ist, jetzt zu dieser wechseln */
270 /* Neue Group-ID setzen */
271 if( setgid( Conf_GID ) != 0 ) Log( LOG_ERR, "Can't change Group-ID to %u: %s", Conf_GID, strerror( errno ));
275 /* Neue User-ID setzen */
276 if( setuid( Conf_UID ) != 0 ) Log( LOG_ERR, "Can't change User-ID to %u: %s", Conf_UID, strerror( errno ));
280 /* User, Gruppe und Prozess-ID des Daemon ausgeben */
281 pwd = getpwuid( getuid( )); grp = getgrgid( getgid( ));
282 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( ));
284 /* stderr in "Error-File" umlenken */
285 Log_InitErrorfile( );
287 /* Signal-Handler initialisieren */
288 Initialize_Signal_Handler( );
290 /* Protokoll- und Server-Identifikation erzeugen. Die vom ngIRCd
291 * beim PASS-Befehl verwendete Syntax sowie die erweiterten Flags
292 * sind in doc/Protocol.txt beschrieben. */
294 sprintf( NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE, VERSION, IRCPLUSFLAGS );
296 strcat( NGIRCd_ProtoID, "Z" );
298 if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
300 sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE, VERSION );
302 strcat( NGIRCd_ProtoID, " P" );
304 strcat( NGIRCd_ProtoID, "Z" );
306 Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
308 /* Vordefinierte Channels anlegen */
309 Channel_InitPredefined( );
311 /* Listen-Ports initialisieren */
312 if( Conn_InitListeners( ) < 1 )
314 Log( LOG_ALERT, "Server isn't listening on a single port!" );
315 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
335 NGIRCd_Version( VOID )
337 STATIC CHAR version[126];
340 sprintf( version, "%s %s(%s)-%s", PACKAGE, VERSION, CVSDATE, NGIRCd_VersionAddition( ));
342 sprintf( version, "%s %s-%s", PACKAGE, VERSION, NGIRCd_VersionAddition( ));
345 } /* NGIRCd_Version */
349 NGIRCd_VersionAddition( VOID )
356 if( txt[0] ) strcat( txt, "+" );
357 strcat( txt, "SYSLOG" );
360 if( txt[0] ) strcat( txt, "+" );
361 strcat( txt, "ZLIB" );
364 if( txt[0] ) strcat( txt, "+" );
365 strcat( txt, "DEBUG" );
368 if( txt[0] ) strcat( txt, "+" );
369 strcat( txt, "SNIFFER" );
372 if( txt[0] ) strcat( txt, "+" );
373 strcat( txt, "RFC" );
376 if( txt[0] ) strcat( txt, "+" );
377 strcat( txt, "IRCPLUS" );
380 if( txt[0] ) strcat( txt, "-" );
381 strcat( txt, TARGET_CPU );
383 strcat( txt, TARGET_VENDOR );
385 strcat( txt, TARGET_OS );
388 } /* NGIRCd_VersionAddition */
392 NGIRCd_Rehash( VOID )
394 CHAR old_name[CLIENT_ID_LEN];
396 Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
397 NGIRCd_SignalRehash = FALSE;
399 /* Alle Listen-Sockets schliessen */
400 Conn_ExitListeners( );
402 /* Alten Server-Namen merken */
403 strcpy( old_name, Conf_ServerName );
405 /* Konfiguration neu lesen ... */
408 /* Alten Server-Namen wiederherstellen: dieser
409 * kann nicht zur Laufzeit geaendert werden ... */
410 if( strcmp( old_name, Conf_ServerName ) != 0 )
412 strcpy( Conf_ServerName, old_name );
413 Log( LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name." );
416 /* neue pre-defined Channel anlegen: */
417 Channel_InitPredefined( );
419 /* Listen-Sockets neu anlegen: */
420 Conn_InitListeners( );
422 Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
423 } /* NGIRCd_Rehash */
427 Initialize_Signal_Handler( VOID )
429 /* Signal-Handler initialisieren: einige Signale
430 * werden ignoriert, andere speziell behandelt. */
432 #ifdef HAVE_SIGACTION
433 /* sigaction() ist vorhanden */
435 struct sigaction saction;
437 /* Signal-Struktur initialisieren */
438 memset( &saction, 0, sizeof( saction ));
439 saction.sa_handler = Signal_Handler;
441 saction.sa_flags |= SA_RESTART;
444 saction.sa_flags |= SA_NOCLDWAIT;
447 /* Signal-Handler einhaengen */
448 sigaction( SIGINT, &saction, NULL );
449 sigaction( SIGQUIT, &saction, NULL );
450 sigaction( SIGTERM, &saction, NULL);
451 sigaction( SIGHUP, &saction, NULL);
452 sigaction( SIGCHLD, &saction, NULL);
454 /* einige Signale ignorieren */
455 saction.sa_handler = SIG_IGN;
456 sigaction( SIGPIPE, &saction, NULL );
458 /* kein sigaction() vorhanden */
460 /* Signal-Handler einhaengen */
461 signal( SIGINT, Signal_Handler );
462 signal( SIGQUIT, Signal_Handler );
463 signal( SIGTERM, Signal_Handler );
464 signal( SIGHUP, Signal_Handler );
465 signal( SIGCHLD, Signal_Handler );
467 /* einige Signale ignorieren */
468 signal( SIGPIPE, SIG_IGN );
470 } /* Initialize_Signal_Handler */
474 Signal_Handler( INT Signal )
476 /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
477 * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
478 * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
485 /* wir soll(t)en uns wohl beenden ... */
486 NGIRCd_SignalQuit = TRUE;
489 /* Konfiguration neu einlesen: */
490 NGIRCd_SignalRehash = TRUE;
493 /* Child-Prozess wurde beendet. Zombies vermeiden: */
494 while( waitpid( -1, NULL, WNOHANG ) > 0);
498 /* unbekanntes bzw. unbehandeltes Signal */
499 Log( LOG_DEBUG, "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" );