2 f4ade537 2001-12-11 alex * ngIRCd -- The Next Generation IRC Daemon
3 18c92d9d 2005-01-26 alex * Copyright (c)2001-2005 by Alexander Barton (alex@barton.de)
5 fa7b3f0e 2002-12-12 alex * This program is free software; you can redistribute it and/or modify
6 fa7b3f0e 2002-12-12 alex * it under the terms of the GNU General Public License as published by
7 fa7b3f0e 2002-12-12 alex * the Free Software Foundation; either version 2 of the License, or
8 fa7b3f0e 2002-12-12 alex * (at your option) any later version.
9 fa7b3f0e 2002-12-12 alex * Please read the file COPYING, README and AUTHORS for more information.
11 fa7b3f0e 2002-12-12 alex * Main program -- main()
15 ca33cbda 2002-03-12 alex #include "portab.h"
17 ec30cd4e 2005-02-11 alex static char UNUSED id[] = "$Id: ngircd.c,v 1.94 2005/02/11 13:52:37 alex Exp $";
19 ca33cbda 2002-03-12 alex #include "imp.h"
20 f4ade537 2001-12-11 alex #include <assert.h>
21 22cf0c5d 2002-01-12 alex #include <errno.h>
22 f4ade537 2001-12-11 alex #include <stdio.h>
23 75c0bd25 2002-04-04 alex #include <stdlib.h>
24 418add93 2001-12-12 alex #include <signal.h>
25 52424b49 2002-01-11 alex #include <string.h>
26 22cf0c5d 2002-01-12 alex #include <unistd.h>
27 6cafa16a 2004-01-17 alex #include <time.h>
28 03d971d9 2002-01-02 alex #include <sys/types.h>
29 9ec32d15 2002-06-02 alex #include <sys/stat.h>
30 03d971d9 2002-01-02 alex #include <sys/wait.h>
31 95375426 2005-02-10 alex #include <fcntl.h>
32 70c9c051 2002-11-10 alex #include <pwd.h>
33 70c9c051 2002-11-10 alex #include <grp.h>
35 3012c232 2004-05-11 alex #include "defines.h"
36 ba258e65 2002-05-27 alex #include "resolve.h"
37 ba258e65 2002-05-27 alex #include "conn.h"
38 58c6c8f3 2001-12-14 alex #include "client.h"
39 ba258e65 2002-05-27 alex #include "channel.h"
40 418add93 2001-12-12 alex #include "conf.h"
41 25f32ce8 2002-10-07 alex #include "cvs-version.h"
42 ba258e65 2002-05-27 alex #include "lists.h"
43 f4ade537 2001-12-11 alex #include "log.h"
44 1751c869 2001-12-21 alex #include "parse.h"
45 ba258e65 2002-05-27 alex #include "irc.h"
47 29bd35bc 2003-02-23 alex #ifdef RENDEZVOUS
48 29bd35bc 2003-02-23 alex #include "rendezvous.h"
51 ca33cbda 2002-03-12 alex #include "exp.h"
52 f4ade537 2001-12-11 alex #include "ngircd.h"
55 ba258e65 2002-05-27 alex LOCAL VOID Initialize_Signal_Handler PARAMS(( VOID ));
56 ba258e65 2002-05-27 alex LOCAL VOID Signal_Handler PARAMS(( INT Signal ));
58 ba258e65 2002-05-27 alex LOCAL VOID Show_Version PARAMS(( VOID ));
59 ba258e65 2002-05-27 alex LOCAL VOID Show_Help PARAMS(( VOID ));
61 112102b1 2005-02-04 alex LOCAL VOID Pidfile_Create PARAMS(( LONG ));
62 112102b1 2005-02-04 alex LOCAL VOID Pidfile_Delete PARAMS(( VOID ));
64 1fdd5478 2005-02-10 alex LOCAL VOID Fill_Version PARAMS(( VOID ));
66 95375426 2005-02-10 alex LOCAL VOID Setup_FDStreams PARAMS(( VOID ));
70 ba258e65 2002-05-27 alex main( int argc, const char *argv[] )
72 70c9c051 2002-11-10 alex struct passwd *pwd;
73 70c9c051 2002-11-10 alex struct group *grp;
74 a89dc546 2002-03-27 alex BOOLEAN ok, configtest = FALSE;
75 c7b55aa6 2002-10-09 alex LONG pid, n;
78 9ec32d15 2002-06-02 alex umask( 0077 );
80 43a4bc5b 2002-12-19 alex NGIRCd_SignalQuit = NGIRCd_SignalRestart = NGIRCd_SignalRehash = FALSE;
81 43a4bc5b 2002-12-19 alex NGIRCd_NoDaemon = NGIRCd_Passive = FALSE;
82 52424b49 2002-01-11 alex #ifdef DEBUG
83 52424b49 2002-01-11 alex NGIRCd_Debug = FALSE;
85 d79a7d28 2002-01-18 alex #ifdef SNIFFER
86 d79a7d28 2002-01-18 alex NGIRCd_Sniffer = FALSE;
88 6626395c 2002-12-26 alex strlcpy( NGIRCd_ConfFile, SYSCONFDIR, sizeof( NGIRCd_ConfFile ));
89 6626395c 2002-12-26 alex strlcat( NGIRCd_ConfFile, CONFIG_FILE, sizeof( NGIRCd_ConfFile ));
91 1fdd5478 2005-02-10 alex Fill_Version( );
93 52424b49 2002-01-11 alex /* Kommandozeile parsen */
94 52424b49 2002-01-11 alex for( i = 1; i < argc; i++ )
97 52424b49 2002-01-11 alex if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
99 52424b49 2002-01-11 alex /* Lange Option */
101 72accd4e 2002-03-28 alex if( strcmp( argv[i], "--config" ) == 0 )
103 72accd4e 2002-03-28 alex if( i + 1 < argc )
105 0ced4181 2002-12-26 alex /* Ok, there's an parameter left */
106 0ced4181 2002-12-26 alex strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
108 0ced4181 2002-12-26 alex /* next parameter */
109 72accd4e 2002-03-28 alex i++; ok = TRUE;
112 a89dc546 2002-03-27 alex if( strcmp( argv[i], "--configtest" ) == 0 )
114 a89dc546 2002-03-27 alex configtest = TRUE;
117 eea1a88b 2002-02-19 alex #ifdef DEBUG
118 eea1a88b 2002-02-19 alex if( strcmp( argv[i], "--debug" ) == 0 )
120 eea1a88b 2002-02-19 alex NGIRCd_Debug = TRUE;
124 52424b49 2002-01-11 alex if( strcmp( argv[i], "--help" ) == 0 )
126 a89dc546 2002-03-27 alex Show_Version( );
127 a89dc546 2002-03-27 alex puts( "" ); Show_Help( ); puts( "" );
130 eea1a88b 2002-02-19 alex if( strcmp( argv[i], "--nodaemon" ) == 0 )
132 eea1a88b 2002-02-19 alex NGIRCd_NoDaemon = TRUE;
135 eea1a88b 2002-02-19 alex if( strcmp( argv[i], "--passive" ) == 0 )
137 eea1a88b 2002-02-19 alex NGIRCd_Passive = TRUE;
140 d79a7d28 2002-01-18 alex #ifdef SNIFFER
141 d79a7d28 2002-01-18 alex if( strcmp( argv[i], "--sniffer" ) == 0 )
143 d79a7d28 2002-01-18 alex NGIRCd_Sniffer = TRUE;
147 eea1a88b 2002-02-19 alex if( strcmp( argv[i], "--version" ) == 0 )
149 d67d94ea 2002-03-10 alex Show_Version( );
153 52424b49 2002-01-11 alex else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
155 52424b49 2002-01-11 alex /* Kurze Option */
157 c7b55aa6 2002-10-09 alex for( n = 1; n < (LONG)strlen( argv[i] ); n++ )
159 52424b49 2002-01-11 alex ok = FALSE;
160 52424b49 2002-01-11 alex #ifdef DEBUG
161 52424b49 2002-01-11 alex if( argv[i][n] == 'd' )
163 52424b49 2002-01-11 alex NGIRCd_Debug = TRUE;
167 72accd4e 2002-03-28 alex if( argv[i][n] == 'f' )
169 85aad869 2002-03-31 alex if(( ! argv[i][n + 1] ) && ( i + 1 < argc ))
171 0ced4181 2002-12-26 alex /* Ok, next character is a blank */
172 0ced4181 2002-12-26 alex strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
174 0ced4181 2002-12-26 alex /* go to the following parameter */
175 c7b55aa6 2002-10-09 alex i++; n = (LONG)strlen( argv[i] );
179 eea1a88b 2002-02-19 alex if( argv[i][n] == 'n' )
181 eea1a88b 2002-02-19 alex NGIRCd_NoDaemon = TRUE;
184 eea1a88b 2002-02-19 alex if( argv[i][n] == 'p' )
186 eea1a88b 2002-02-19 alex NGIRCd_Passive = TRUE;
189 d79a7d28 2002-01-18 alex #ifdef SNIFFER
190 d79a7d28 2002-01-18 alex if( argv[i][n] == 's' )
192 d79a7d28 2002-01-18 alex NGIRCd_Sniffer = TRUE;
196 c9ccb669 2003-12-29 alex if( argv[i][n] == 't' )
198 c9ccb669 2003-12-29 alex configtest = TRUE;
204 62796722 2003-03-31 alex printf( "%s: invalid option \"-%c\"!\n", PACKAGE_NAME, argv[i][n] );
205 62796722 2003-03-31 alex printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
213 62796722 2003-03-31 alex printf( "%s: invalid option \"%s\"!\n", PACKAGE_NAME, argv[i] );
214 62796722 2003-03-31 alex printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
219 eea1a88b 2002-02-19 alex /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
220 894cd2cd 2005-02-09 alex NGIRCd_DebugLevel[0] = '\0';
221 eea1a88b 2002-02-19 alex #ifdef DEBUG
222 eea1a88b 2002-02-19 alex if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
224 eea1a88b 2002-02-19 alex #ifdef SNIFFER
225 991da8ea 2002-05-18 alex if( NGIRCd_Sniffer )
227 991da8ea 2002-05-18 alex NGIRCd_Debug = TRUE;
228 991da8ea 2002-05-18 alex strcpy( NGIRCd_DebugLevel, "2" );
232 a89dc546 2002-03-27 alex /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
233 a89dc546 2002-03-27 alex if( configtest )
235 a89dc546 2002-03-27 alex Show_Version( ); puts( "" );
236 a89dc546 2002-03-27 alex exit( Conf_Test( ));
239 43a4bc5b 2002-12-19 alex while( ! NGIRCd_SignalQuit )
241 7281b8dd 2004-05-07 alex /* Initialize global variables */
242 7281b8dd 2004-05-07 alex NGIRCd_Start = time( NULL );
243 7281b8dd 2004-05-07 alex (VOID)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
245 7281b8dd 2004-05-07 alex NGIRCd_SignalRehash = FALSE;
246 7281b8dd 2004-05-07 alex NGIRCd_SignalRestart = FALSE;
247 7281b8dd 2004-05-07 alex NGIRCd_SignalQuit = FALSE;
249 7281b8dd 2004-05-07 alex /* Initialize modules, part I */
250 7281b8dd 2004-05-07 alex Log_Init( );
251 7281b8dd 2004-05-07 alex Conf_Init( );
253 7281b8dd 2004-05-07 alex if( Conf_Chroot[0] )
255 7281b8dd 2004-05-07 alex /* Chroot */
256 7281b8dd 2004-05-07 alex if( chdir( Conf_Chroot ) != 0 ) Log( LOG_ERR, "Can't chdir() in ChrootDir (%s): %s", Conf_Chroot, strerror( errno ));
258 7281b8dd 2004-05-07 alex if( chroot( Conf_Chroot ) != 0 ) Log( LOG_ERR, "Can't change root directory to \"%s\": %s", Conf_Chroot, strerror( errno ));
259 7281b8dd 2004-05-07 alex else Log( LOG_INFO, "Changed root and working directory to \"%s\".", Conf_Chroot );
262 7281b8dd 2004-05-07 alex if( Conf_GID != 0 )
264 7281b8dd 2004-05-07 alex /* Set new group ID */
265 7281b8dd 2004-05-07 alex if( setgid( Conf_GID ) != 0 ) Log( LOG_ERR, "Can't change group ID to %u: %s", Conf_GID, strerror( errno ));
267 7281b8dd 2004-05-07 alex if( Conf_UID != 0 )
269 7281b8dd 2004-05-07 alex /* Set new user ID */
270 7281b8dd 2004-05-07 alex if( setuid( Conf_UID ) != 0 ) Log( LOG_ERR, "Can't change user ID to %u: %s", Conf_UID, strerror( errno ));
273 95375426 2005-02-10 alex /* Normally a child process is forked which isn't any longer
274 95375426 2005-02-10 alex * connected to ther controlling terminal. Use "--nodaemon"
275 95375426 2005-02-10 alex * to disable this "daemon mode" (useful for debugging). */
276 22cf0c5d 2002-01-12 alex if( ! NGIRCd_NoDaemon )
278 95375426 2005-02-10 alex /* fork child process */
279 c7b55aa6 2002-10-09 alex pid = (LONG)fork( );
280 22cf0c5d 2002-01-12 alex if( pid > 0 )
282 95375426 2005-02-10 alex /* "Old" process: exit. */
285 22cf0c5d 2002-01-12 alex if( pid < 0 )
287 95375426 2005-02-10 alex /* Error!? */
288 95375426 2005-02-10 alex fprintf( stderr, "%s: Can't fork: %s!\nFatal error, exiting now ...\n", PACKAGE_NAME, strerror( errno ));
292 95375426 2005-02-10 alex /* New child process */
293 95a4b1b1 2002-03-25 alex (VOID)setsid( );
294 22cf0c5d 2002-01-12 alex chdir( "/" );
296 95375426 2005-02-10 alex /* Detach stdin, stdout and stderr */
297 95375426 2005-02-10 alex (VOID)Setup_FDStreams( );
300 112102b1 2005-02-04 alex /* Create PID file */
301 112102b1 2005-02-04 alex pid = (LONG) getpid( );
302 112102b1 2005-02-04 alex Pidfile_Create( pid );
304 8ca1e4d5 2004-09-04 alex /* Show user, group, and PID of the running daemon */
305 8ca1e4d5 2004-09-04 alex pwd = getpwuid( getuid( )); grp = getgrgid( getgid( ));
306 112102b1 2005-02-04 alex 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( ), pid);
308 8ca1e4d5 2004-09-04 alex /* Change working directory to home directory of the user
309 8ca1e4d5 2004-09-04 alex * we are running as (when not running chroot()'ed!) */
310 8ca1e4d5 2004-09-04 alex if( Conf_UID != 0 && ! Conf_Chroot[0] )
312 8ca1e4d5 2004-09-04 alex struct passwd *pwd;
314 8ca1e4d5 2004-09-04 alex pwd = getpwuid( Conf_UID );
315 8ca1e4d5 2004-09-04 alex if( pwd != NULL )
317 8ca1e4d5 2004-09-04 alex if( chdir( pwd->pw_dir ) == 0 ) Log( LOG_DEBUG, "Changed working directory to \"%s\" ...", pwd->pw_dir );
318 8ca1e4d5 2004-09-04 alex else Log( LOG_ERR, "Can't change working directory to \"%s\": %s", pwd->pw_dir, strerror( errno ));
320 8ca1e4d5 2004-09-04 alex else Log( LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID );
323 7281b8dd 2004-05-07 alex /* Initialize modules, part II: these functions are eventually
324 7281b8dd 2004-05-07 alex * called with already dropped privileges ... */
325 ba258e65 2002-05-27 alex Resolve_Init( );
326 ba258e65 2002-05-27 alex Lists_Init( );
327 804b1ec4 2001-12-31 alex Channel_Init( );
328 804b1ec4 2001-12-31 alex Client_Init( );
329 29bd35bc 2003-02-23 alex #ifdef RENDEZVOUS
330 29bd35bc 2003-02-23 alex Rendezvous_Init( );
332 804b1ec4 2001-12-31 alex Conn_Init( );
334 95375426 2005-02-10 alex #ifdef DEBUG
335 95375426 2005-02-10 alex /* Redirect stderr handle to "error file" for debugging
336 95375426 2005-02-10 alex * when not running in "no daemon" mode: */
337 95375426 2005-02-10 alex if( ! NGIRCd_NoDaemon ) Log_InitErrorfile( );
340 804b1ec4 2001-12-31 alex /* Signal-Handler initialisieren */
341 804b1ec4 2001-12-31 alex Initialize_Signal_Handler( );
343 c76e6769 2002-09-02 alex /* Protokoll- und Server-Identifikation erzeugen. Die vom ngIRCd
344 c76e6769 2002-09-02 alex * beim PASS-Befehl verwendete Syntax sowie die erweiterten Flags
345 c76e6769 2002-09-02 alex * sind in doc/Protocol.txt beschrieben. */
346 14cbca87 2002-09-07 alex #ifdef IRCPLUS
347 62796722 2003-03-31 alex sprintf( NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION, IRCPLUSFLAGS );
348 c40592d2 2003-12-26 alex #ifdef ZLIB
349 65f3adca 2002-11-26 alex strcat( NGIRCd_ProtoID, "Z" );
351 c76e6769 2002-09-02 alex if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
353 62796722 2003-03-31 alex sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION );
355 c76e6769 2002-09-02 alex strcat( NGIRCd_ProtoID, " P" );
356 c40592d2 2003-12-26 alex #ifdef ZLIB
357 65f3adca 2002-11-26 alex strcat( NGIRCd_ProtoID, "Z" );
359 65b31ffb 2002-09-02 alex Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
361 b788a3da 2002-06-02 alex /* Vordefinierte Channels anlegen */
362 b788a3da 2002-06-02 alex Channel_InitPredefined( );
364 804b1ec4 2001-12-31 alex /* Listen-Ports initialisieren */
365 e311cc01 2002-11-22 alex if( Conn_InitListeners( ) < 1 )
367 e311cc01 2002-11-22 alex Log( LOG_ALERT, "Server isn't listening on a single port!" );
368 62796722 2003-03-31 alex Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
369 112102b1 2005-02-04 alex Pidfile_Delete( );
373 804b1ec4 2001-12-31 alex /* Hauptschleife */
374 9ec32d15 2002-06-02 alex Conn_Handler( );
376 804b1ec4 2001-12-31 alex /* Alles abmelden */
377 804b1ec4 2001-12-31 alex Conn_Exit( );
378 29bd35bc 2003-02-23 alex #ifdef RENDEZVOUS
379 29bd35bc 2003-02-23 alex Rendezvous_Exit( );
381 804b1ec4 2001-12-31 alex Client_Exit( );
382 804b1ec4 2001-12-31 alex Channel_Exit( );
383 ba258e65 2002-05-27 alex Lists_Exit( );
384 804b1ec4 2001-12-31 alex Log_Exit( );
386 112102b1 2005-02-04 alex Pidfile_Delete( );
390 f4ade537 2001-12-11 alex } /* main */
394 1fdd5478 2005-02-10 alex Fill_Version( VOID )
396 894cd2cd 2005-02-09 alex NGIRCd_VersionAddition[0] = '\0';
398 c40592d2 2003-12-26 alex #ifdef SYSLOG
399 894cd2cd 2005-02-09 alex strcpy( NGIRCd_VersionAddition, "SYSLOG" );
401 c40592d2 2003-12-26 alex #ifdef ZLIB
402 894cd2cd 2005-02-09 alex if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
403 894cd2cd 2005-02-09 alex strcat( NGIRCd_VersionAddition, "ZLIB" );
405 c40592d2 2003-12-26 alex #ifdef TCPWRAP
406 894cd2cd 2005-02-09 alex if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
407 894cd2cd 2005-02-09 alex strcat( NGIRCd_VersionAddition, "TCPWRAP" );
409 439c945d 2003-03-07 alex #ifdef RENDEZVOUS
410 894cd2cd 2005-02-09 alex if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
411 894cd2cd 2005-02-09 alex strcat( NGIRCd_VersionAddition, "RENDEZVOUS" );
413 cb6279f1 2003-12-27 alex #ifdef IDENTAUTH
414 894cd2cd 2005-02-09 alex if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
415 894cd2cd 2005-02-09 alex strcat( NGIRCd_VersionAddition, "IDENT" );
417 52424b49 2002-01-11 alex #ifdef DEBUG
418 894cd2cd 2005-02-09 alex if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
419 894cd2cd 2005-02-09 alex strcat( NGIRCd_VersionAddition, "DEBUG" );
421 52424b49 2002-01-11 alex #ifdef SNIFFER
422 894cd2cd 2005-02-09 alex if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
423 894cd2cd 2005-02-09 alex strcat( NGIRCd_VersionAddition, "SNIFFER" );
425 14cbca87 2002-09-07 alex #ifdef STRICT_RFC
426 894cd2cd 2005-02-09 alex if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
427 894cd2cd 2005-02-09 alex strcat( NGIRCd_VersionAddition, "RFC" );
429 14cbca87 2002-09-07 alex #ifdef IRCPLUS
430 894cd2cd 2005-02-09 alex if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
431 894cd2cd 2005-02-09 alex strcat( NGIRCd_VersionAddition, "IRCPLUS" );
434 894cd2cd 2005-02-09 alex if( NGIRCd_VersionAddition[0] ) strlcat( NGIRCd_VersionAddition, "-", sizeof( NGIRCd_VersionAddition ));
435 894cd2cd 2005-02-09 alex strlcat( NGIRCd_VersionAddition, TARGET_CPU, sizeof( NGIRCd_VersionAddition ));
436 894cd2cd 2005-02-09 alex strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
437 894cd2cd 2005-02-09 alex strlcat( NGIRCd_VersionAddition, TARGET_VENDOR, sizeof( NGIRCd_VersionAddition ));
438 894cd2cd 2005-02-09 alex strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
439 894cd2cd 2005-02-09 alex strlcat( NGIRCd_VersionAddition, TARGET_OS, sizeof( NGIRCd_VersionAddition ));
441 894cd2cd 2005-02-09 alex #ifdef CVSDATE
442 894cd2cd 2005-02-09 alex snprintf( NGIRCd_Version, sizeof NGIRCd_Version,"%s %s(%s)-%s", PACKAGE_NAME, PACKAGE_VERSION, CVSDATE, NGIRCd_VersionAddition);
444 894cd2cd 2005-02-09 alex snprintf( NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s", PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
446 1fdd5478 2005-02-10 alex } /* Fill_Version */
449 e311cc01 2002-11-22 alex GLOBAL VOID
450 cf2d4794 2002-11-22 alex NGIRCd_Rehash( VOID )
452 e311cc01 2002-11-22 alex CHAR old_name[CLIENT_ID_LEN];
454 43a4bc5b 2002-12-19 alex Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
455 43a4bc5b 2002-12-19 alex NGIRCd_SignalRehash = FALSE;
457 e9b0ec91 2004-01-19 alex /* Close down all listening sockets */
458 e311cc01 2002-11-22 alex Conn_ExitListeners( );
460 e9b0ec91 2004-01-19 alex /* Remember old server name */
461 e311cc01 2002-11-22 alex strcpy( old_name, Conf_ServerName );
463 e9b0ec91 2004-01-19 alex /* Re-read configuration ... */
464 a2544e49 2002-12-30 alex Conf_Rehash( );
466 e9b0ec91 2004-01-19 alex /* Recover old server name: it can't be changed during run-time */
467 e311cc01 2002-11-22 alex if( strcmp( old_name, Conf_ServerName ) != 0 )
469 e311cc01 2002-11-22 alex strcpy( Conf_ServerName, old_name );
470 e311cc01 2002-11-22 alex Log( LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name." );
473 e9b0ec91 2004-01-19 alex /* Create new pre-defined channels */
474 e311cc01 2002-11-22 alex Channel_InitPredefined( );
476 e9b0ec91 2004-01-19 alex /* Start listening on sockets */
477 e311cc01 2002-11-22 alex Conn_InitListeners( );
479 24df40ec 2003-03-27 alex /* Sync configuration with established connections */
480 24df40ec 2003-03-27 alex Conn_SyncServerStruct( );
482 43a4bc5b 2002-12-19 alex Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
483 cf2d4794 2002-11-22 alex } /* NGIRCd_Rehash */
487 ba258e65 2002-05-27 alex Initialize_Signal_Handler( VOID )
489 08cf5607 2001-12-26 alex /* Signal-Handler initialisieren: einige Signale
490 08cf5607 2001-12-26 alex * werden ignoriert, andere speziell behandelt. */
492 62266a8d 2002-02-25 alex #ifdef HAVE_SIGACTION
493 62266a8d 2002-02-25 alex /* sigaction() ist vorhanden */
495 418add93 2001-12-12 alex struct sigaction saction;
497 418add93 2001-12-12 alex /* Signal-Struktur initialisieren */
498 418add93 2001-12-12 alex memset( &saction, 0, sizeof( saction ));
499 23253edb 2002-01-22 alex saction.sa_handler = Signal_Handler;
500 9919f38d 2002-02-19 alex #ifdef SA_RESTART
501 6f955d2a 2002-02-23 alex saction.sa_flags |= SA_RESTART;
503 6f955d2a 2002-02-23 alex #ifdef SA_NOCLDWAIT
504 6f955d2a 2002-02-23 alex saction.sa_flags |= SA_NOCLDWAIT;
507 418add93 2001-12-12 alex /* Signal-Handler einhaengen */
508 418add93 2001-12-12 alex sigaction( SIGINT, &saction, NULL );
509 418add93 2001-12-12 alex sigaction( SIGQUIT, &saction, NULL );
510 418add93 2001-12-12 alex sigaction( SIGTERM, &saction, NULL);
511 3fbbfe44 2002-02-17 alex sigaction( SIGHUP, &saction, NULL);
512 03d971d9 2002-01-02 alex sigaction( SIGCHLD, &saction, NULL);
514 eb952e35 2001-12-24 alex /* einige Signale ignorieren */
515 eb952e35 2001-12-24 alex saction.sa_handler = SIG_IGN;
516 eb952e35 2001-12-24 alex sigaction( SIGPIPE, &saction, NULL );
518 62266a8d 2002-02-25 alex /* kein sigaction() vorhanden */
520 62266a8d 2002-02-25 alex /* Signal-Handler einhaengen */
521 62266a8d 2002-02-25 alex signal( SIGINT, Signal_Handler );
522 62266a8d 2002-02-25 alex signal( SIGQUIT, Signal_Handler );
523 62266a8d 2002-02-25 alex signal( SIGTERM, Signal_Handler );
524 62266a8d 2002-02-25 alex signal( SIGHUP, Signal_Handler );
525 62266a8d 2002-02-25 alex signal( SIGCHLD, Signal_Handler );
527 62266a8d 2002-02-25 alex /* einige Signale ignorieren */
528 62266a8d 2002-02-25 alex signal( SIGPIPE, SIG_IGN );
530 418add93 2001-12-12 alex } /* Initialize_Signal_Handler */
534 ba258e65 2002-05-27 alex Signal_Handler( INT Signal )
536 418add93 2001-12-12 alex /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
537 418add93 2001-12-12 alex * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
538 418add93 2001-12-12 alex * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
540 f4ade537 2001-12-11 alex switch( Signal )
542 f4ade537 2001-12-11 alex case SIGTERM:
543 b61b8c4d 2001-12-12 alex case SIGINT:
544 b61b8c4d 2001-12-12 alex case SIGQUIT:
545 418add93 2001-12-12 alex /* wir soll(t)en uns wohl beenden ... */
546 43a4bc5b 2002-12-19 alex NGIRCd_SignalQuit = TRUE;
548 3fbbfe44 2002-02-17 alex case SIGHUP:
549 e311cc01 2002-11-22 alex /* Konfiguration neu einlesen: */
550 43a4bc5b 2002-12-19 alex NGIRCd_SignalRehash = TRUE;
552 03d971d9 2002-01-02 alex case SIGCHLD:
553 03d971d9 2002-01-02 alex /* Child-Prozess wurde beendet. Zombies vermeiden: */
554 03d971d9 2002-01-02 alex while( waitpid( -1, NULL, WNOHANG ) > 0);
556 43a4bc5b 2002-12-19 alex #ifdef DEBUG
558 418add93 2001-12-12 alex /* unbekanntes bzw. unbehandeltes Signal */
559 43a4bc5b 2002-12-19 alex Log( LOG_DEBUG, "Got signal %d! Ignored.", Signal );
562 f4ade537 2001-12-11 alex } /* Signal_Handler */
566 ba258e65 2002-05-27 alex Show_Version( VOID )
568 894cd2cd 2005-02-09 alex puts( NGIRCd_Version );
569 18c92d9d 2005-01-26 alex puts( "Copyright (c)2001-2005 by Alexander Barton (<alex@barton.de>)." );
570 5b27e491 2002-04-01 alex puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
571 52424b49 2002-01-11 alex puts( "This is free software; see the source for copying conditions. There is NO" );
572 52424b49 2002-01-11 alex puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
573 52424b49 2002-01-11 alex } /* Show_Version */
577 ba258e65 2002-05-27 alex Show_Help( VOID )
579 52424b49 2002-01-11 alex #ifdef DEBUG
580 72accd4e 2002-03-28 alex puts( " -d, --debug log extra debug messages" );
582 72accd4e 2002-03-28 alex puts( " -f, --config <f> use file <f> as configuration file" );
583 ec30cd4e 2005-02-11 alex puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
584 ec30cd4e 2005-02-11 alex puts( " -p, --passive disable automatic connections to other servers" );
585 72accd4e 2002-03-28 alex #ifdef SNIFFER
586 72accd4e 2002-03-28 alex puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
588 c9ccb669 2003-12-29 alex puts( " -t, --configtest read, validate and display configuration; then exit" );
589 72accd4e 2002-03-28 alex puts( " --version output version information and exit" );
590 72accd4e 2002-03-28 alex puts( " --help display this help and exit" );
591 52424b49 2002-01-11 alex } /* Show_Help */
595 112102b1 2005-02-04 alex Pidfile_Delete( VOID )
597 112102b1 2005-02-04 alex /* Pidfile configured? */
598 112102b1 2005-02-04 alex if( ! Conf_PidFile[0] ) return;
600 112102b1 2005-02-04 alex #ifdef DEBUG
601 112102b1 2005-02-04 alex Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
604 112102b1 2005-02-04 alex if( unlink( Conf_PidFile ))
605 112102b1 2005-02-04 alex Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
606 112102b1 2005-02-04 alex } /* Pidfile_Delete */
610 112102b1 2005-02-04 alex Pidfile_Create( LONG pid )
612 112102b1 2005-02-04 alex FILE *pidf;
614 112102b1 2005-02-04 alex /* Pidfile configured? */
615 112102b1 2005-02-04 alex if( ! Conf_PidFile[0] ) return;
617 112102b1 2005-02-04 alex #ifdef DEBUG
618 112102b1 2005-02-04 alex Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
621 8edeed84 2005-02-07 alex pidf = fopen( Conf_PidFile, "w" );
623 112102b1 2005-02-04 alex if( ! pidf )
625 112102b1 2005-02-04 alex Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
629 112102b1 2005-02-04 alex if( fprintf( pidf, "%ld\n", pid ) < 0 )
630 112102b1 2005-02-04 alex Log( LOG_ERR, "Can't write PID file (%s): %s", Conf_PidFile, strerror( errno ));
632 112102b1 2005-02-04 alex if( fclose(pidf) != 0 )
633 112102b1 2005-02-04 alex Log( LOG_ERR, "Error closing PID file (%s): %s", Conf_PidFile, strerror( errno ));
634 112102b1 2005-02-04 alex } /* Pidfile_Create */
638 95375426 2005-02-10 alex Setup_FDStreams( VOID )
642 95375426 2005-02-10 alex /* Test if we can open /dev/null for reading and writing. If not
643 95375426 2005-02-10 alex * we are most probably chrooted already and the server has been
644 95375426 2005-02-10 alex * restarted. So we simply don't try to redirect stdXXX ... */
645 95375426 2005-02-10 alex fd = open( "/dev/null", O_RDWR );
646 95375426 2005-02-10 alex if ( fd < 0 ) return;
648 95375426 2005-02-10 alex /* Close "old" stdin/out/err descriptors */
649 f2fbf206 2005-02-10 alex close( 0 ); close( 1 ); close( 2 );
651 95375426 2005-02-10 alex /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
652 95375426 2005-02-10 alex dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
654 95375426 2005-02-10 alex /* Close newly opened file descriptor if not stdin/out/err */
655 95375426 2005-02-10 alex if( fd > 2 ) close( fd );
656 95375426 2005-02-10 alex } /* Setup_FDStreams */
659 f4ade537 2001-12-11 alex /* -eof- */