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.
13 ca33cbda 2002-03-12 alex #include "portab.h"
15 45404a16 2005-06-01 alex static char UNUSED id[] = "$Id: ngircd.c,v 1.96 2005/06/01 21:52:18 alex Exp $";
19 45404a16 2005-06-01 alex * The main program, including the C function main() which is called
20 45404a16 2005-06-01 alex * by the loader of the operating system.
23 ca33cbda 2002-03-12 alex #include "imp.h"
24 f4ade537 2001-12-11 alex #include <assert.h>
25 22cf0c5d 2002-01-12 alex #include <errno.h>
26 f4ade537 2001-12-11 alex #include <stdio.h>
27 75c0bd25 2002-04-04 alex #include <stdlib.h>
28 418add93 2001-12-12 alex #include <signal.h>
29 52424b49 2002-01-11 alex #include <string.h>
30 22cf0c5d 2002-01-12 alex #include <unistd.h>
31 6cafa16a 2004-01-17 alex #include <time.h>
32 03d971d9 2002-01-02 alex #include <sys/types.h>
33 9ec32d15 2002-06-02 alex #include <sys/stat.h>
34 03d971d9 2002-01-02 alex #include <sys/wait.h>
35 95375426 2005-02-10 alex #include <fcntl.h>
36 70c9c051 2002-11-10 alex #include <pwd.h>
37 70c9c051 2002-11-10 alex #include <grp.h>
39 3012c232 2004-05-11 alex #include "defines.h"
40 ba258e65 2002-05-27 alex #include "resolve.h"
41 ba258e65 2002-05-27 alex #include "conn.h"
42 58c6c8f3 2001-12-14 alex #include "client.h"
43 ba258e65 2002-05-27 alex #include "channel.h"
44 418add93 2001-12-12 alex #include "conf.h"
45 25f32ce8 2002-10-07 alex #include "cvs-version.h"
46 ba258e65 2002-05-27 alex #include "lists.h"
47 f4ade537 2001-12-11 alex #include "log.h"
48 1751c869 2001-12-21 alex #include "parse.h"
49 ba258e65 2002-05-27 alex #include "irc.h"
51 29bd35bc 2003-02-23 alex #ifdef RENDEZVOUS
52 29bd35bc 2003-02-23 alex #include "rendezvous.h"
55 ca33cbda 2002-03-12 alex #include "exp.h"
56 f4ade537 2001-12-11 alex #include "ngircd.h"
59 8adff592 2005-03-19 fw LOCAL void Initialize_Signal_Handler PARAMS(( void ));
60 8adff592 2005-03-19 fw LOCAL void Signal_Handler PARAMS(( int Signal ));
62 8adff592 2005-03-19 fw LOCAL void Show_Version PARAMS(( void ));
63 8adff592 2005-03-19 fw LOCAL void Show_Help PARAMS(( void ));
65 8adff592 2005-03-19 fw LOCAL void Pidfile_Create PARAMS(( long ));
66 8adff592 2005-03-19 fw LOCAL void Pidfile_Delete PARAMS(( void ));
68 8adff592 2005-03-19 fw LOCAL void Fill_Version PARAMS(( void ));
70 8adff592 2005-03-19 fw LOCAL void Setup_FDStreams PARAMS(( void ));
74 45404a16 2005-06-01 alex * The main() function of ngIRCd.
75 45404a16 2005-06-01 alex * Here all starts: this function is called by the operating system loader,
76 45404a16 2005-06-01 alex * it is the first portion of code executed of ngIRCd.
77 45404a16 2005-06-01 alex * @param argc The number of arguments passed to ngIRCd on the command line.
78 45404a16 2005-06-01 alex * @param argv An array containing all the arguments passed to ngIRCd.
79 45404a16 2005-06-01 alex * @return Global exit code of ngIRCd, zero on success.
82 ba258e65 2002-05-27 alex main( int argc, const char *argv[] )
84 70c9c051 2002-11-10 alex struct passwd *pwd;
85 70c9c051 2002-11-10 alex struct group *grp;
86 8adff592 2005-03-19 fw bool ok, configtest = false;
91 9ec32d15 2002-06-02 alex umask( 0077 );
93 8adff592 2005-03-19 fw NGIRCd_SignalQuit = NGIRCd_SignalRestart = NGIRCd_SignalRehash = false;
94 8adff592 2005-03-19 fw NGIRCd_NoDaemon = NGIRCd_Passive = false;
95 52424b49 2002-01-11 alex #ifdef DEBUG
96 8adff592 2005-03-19 fw NGIRCd_Debug = false;
98 d79a7d28 2002-01-18 alex #ifdef SNIFFER
99 8adff592 2005-03-19 fw NGIRCd_Sniffer = false;
101 6626395c 2002-12-26 alex strlcpy( NGIRCd_ConfFile, SYSCONFDIR, sizeof( NGIRCd_ConfFile ));
102 6626395c 2002-12-26 alex strlcat( NGIRCd_ConfFile, CONFIG_FILE, sizeof( NGIRCd_ConfFile ));
104 1fdd5478 2005-02-10 alex Fill_Version( );
106 52424b49 2002-01-11 alex /* Kommandozeile parsen */
107 52424b49 2002-01-11 alex for( i = 1; i < argc; i++ )
110 52424b49 2002-01-11 alex if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
112 52424b49 2002-01-11 alex /* Lange Option */
114 72accd4e 2002-03-28 alex if( strcmp( argv[i], "--config" ) == 0 )
116 72accd4e 2002-03-28 alex if( i + 1 < argc )
118 0ced4181 2002-12-26 alex /* Ok, there's an parameter left */
119 0ced4181 2002-12-26 alex strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
121 0ced4181 2002-12-26 alex /* next parameter */
122 8adff592 2005-03-19 fw i++; ok = true;
125 a89dc546 2002-03-27 alex if( strcmp( argv[i], "--configtest" ) == 0 )
127 8adff592 2005-03-19 fw configtest = true;
130 eea1a88b 2002-02-19 alex #ifdef DEBUG
131 eea1a88b 2002-02-19 alex if( strcmp( argv[i], "--debug" ) == 0 )
133 8adff592 2005-03-19 fw NGIRCd_Debug = true;
137 52424b49 2002-01-11 alex if( strcmp( argv[i], "--help" ) == 0 )
139 a89dc546 2002-03-27 alex Show_Version( );
140 a89dc546 2002-03-27 alex puts( "" ); Show_Help( ); puts( "" );
143 eea1a88b 2002-02-19 alex if( strcmp( argv[i], "--nodaemon" ) == 0 )
145 8adff592 2005-03-19 fw NGIRCd_NoDaemon = true;
148 eea1a88b 2002-02-19 alex if( strcmp( argv[i], "--passive" ) == 0 )
150 8adff592 2005-03-19 fw NGIRCd_Passive = true;
153 d79a7d28 2002-01-18 alex #ifdef SNIFFER
154 d79a7d28 2002-01-18 alex if( strcmp( argv[i], "--sniffer" ) == 0 )
156 8adff592 2005-03-19 fw NGIRCd_Sniffer = true;
160 eea1a88b 2002-02-19 alex if( strcmp( argv[i], "--version" ) == 0 )
162 d67d94ea 2002-03-10 alex Show_Version( );
166 52424b49 2002-01-11 alex else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
168 52424b49 2002-01-11 alex /* Kurze Option */
169 8adff592 2005-03-19 fw for( n = 1; n < strlen( argv[i] ); n++ )
172 52424b49 2002-01-11 alex #ifdef DEBUG
173 52424b49 2002-01-11 alex if( argv[i][n] == 'd' )
175 8adff592 2005-03-19 fw NGIRCd_Debug = true;
179 72accd4e 2002-03-28 alex if( argv[i][n] == 'f' )
181 85aad869 2002-03-31 alex if(( ! argv[i][n + 1] ) && ( i + 1 < argc ))
183 0ced4181 2002-12-26 alex /* Ok, next character is a blank */
184 0ced4181 2002-12-26 alex strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
186 0ced4181 2002-12-26 alex /* go to the following parameter */
188 8adff592 2005-03-19 fw n = strlen( argv[i] );
192 eea1a88b 2002-02-19 alex if( argv[i][n] == 'n' )
194 8adff592 2005-03-19 fw NGIRCd_NoDaemon = true;
197 eea1a88b 2002-02-19 alex if( argv[i][n] == 'p' )
199 8adff592 2005-03-19 fw NGIRCd_Passive = true;
202 d79a7d28 2002-01-18 alex #ifdef SNIFFER
203 d79a7d28 2002-01-18 alex if( argv[i][n] == 's' )
205 8adff592 2005-03-19 fw NGIRCd_Sniffer = true;
209 c9ccb669 2003-12-29 alex if( argv[i][n] == 't' )
211 8adff592 2005-03-19 fw configtest = true;
217 62796722 2003-03-31 alex printf( "%s: invalid option \"-%c\"!\n", PACKAGE_NAME, argv[i][n] );
218 62796722 2003-03-31 alex printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
226 62796722 2003-03-31 alex printf( "%s: invalid option \"%s\"!\n", PACKAGE_NAME, argv[i] );
227 62796722 2003-03-31 alex printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
232 eea1a88b 2002-02-19 alex /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
233 894cd2cd 2005-02-09 alex NGIRCd_DebugLevel[0] = '\0';
234 eea1a88b 2002-02-19 alex #ifdef DEBUG
235 eea1a88b 2002-02-19 alex if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
237 eea1a88b 2002-02-19 alex #ifdef SNIFFER
238 991da8ea 2002-05-18 alex if( NGIRCd_Sniffer )
240 8adff592 2005-03-19 fw NGIRCd_Debug = true;
241 991da8ea 2002-05-18 alex strcpy( NGIRCd_DebugLevel, "2" );
245 a89dc546 2002-03-27 alex /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
246 a89dc546 2002-03-27 alex if( configtest )
248 a89dc546 2002-03-27 alex Show_Version( ); puts( "" );
249 a89dc546 2002-03-27 alex exit( Conf_Test( ));
252 43a4bc5b 2002-12-19 alex while( ! NGIRCd_SignalQuit )
254 7281b8dd 2004-05-07 alex /* Initialize global variables */
255 7281b8dd 2004-05-07 alex NGIRCd_Start = time( NULL );
256 8adff592 2005-03-19 fw (void)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
258 8adff592 2005-03-19 fw NGIRCd_SignalRehash = false;
259 8adff592 2005-03-19 fw NGIRCd_SignalRestart = false;
260 8adff592 2005-03-19 fw NGIRCd_SignalQuit = false;
262 7281b8dd 2004-05-07 alex /* Initialize modules, part I */
263 7281b8dd 2004-05-07 alex Log_Init( );
264 7281b8dd 2004-05-07 alex Conf_Init( );
266 7281b8dd 2004-05-07 alex if( Conf_Chroot[0] )
268 7281b8dd 2004-05-07 alex /* Chroot */
269 7281b8dd 2004-05-07 alex if( chdir( Conf_Chroot ) != 0 ) Log( LOG_ERR, "Can't chdir() in ChrootDir (%s): %s", Conf_Chroot, strerror( errno ));
271 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 ));
272 7281b8dd 2004-05-07 alex else Log( LOG_INFO, "Changed root and working directory to \"%s\".", Conf_Chroot );
275 7281b8dd 2004-05-07 alex if( Conf_GID != 0 )
277 7281b8dd 2004-05-07 alex /* Set new group ID */
278 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 ));
280 7281b8dd 2004-05-07 alex if( Conf_UID != 0 )
282 7281b8dd 2004-05-07 alex /* Set new user ID */
283 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 ));
286 95375426 2005-02-10 alex /* Normally a child process is forked which isn't any longer
287 95375426 2005-02-10 alex * connected to ther controlling terminal. Use "--nodaemon"
288 95375426 2005-02-10 alex * to disable this "daemon mode" (useful for debugging). */
289 22cf0c5d 2002-01-12 alex if( ! NGIRCd_NoDaemon )
291 95375426 2005-02-10 alex /* fork child process */
292 8adff592 2005-03-19 fw pid = (long)fork( );
293 22cf0c5d 2002-01-12 alex if( pid > 0 )
295 95375426 2005-02-10 alex /* "Old" process: exit. */
298 22cf0c5d 2002-01-12 alex if( pid < 0 )
300 95375426 2005-02-10 alex /* Error!? */
301 95375426 2005-02-10 alex fprintf( stderr, "%s: Can't fork: %s!\nFatal error, exiting now ...\n", PACKAGE_NAME, strerror( errno ));
305 95375426 2005-02-10 alex /* New child process */
306 8adff592 2005-03-19 fw (void)setsid( );
307 22cf0c5d 2002-01-12 alex chdir( "/" );
309 95375426 2005-02-10 alex /* Detach stdin, stdout and stderr */
310 8adff592 2005-03-19 fw Setup_FDStreams( );
313 112102b1 2005-02-04 alex /* Create PID file */
314 8adff592 2005-03-19 fw pid = (long) getpid( );
315 112102b1 2005-02-04 alex Pidfile_Create( pid );
317 8ca1e4d5 2004-09-04 alex /* Show user, group, and PID of the running daemon */
318 8ca1e4d5 2004-09-04 alex pwd = getpwuid( getuid( )); grp = getgrgid( getgid( ));
319 8adff592 2005-03-19 fw 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);
321 8ca1e4d5 2004-09-04 alex /* Change working directory to home directory of the user
322 8ca1e4d5 2004-09-04 alex * we are running as (when not running chroot()'ed!) */
323 8ca1e4d5 2004-09-04 alex if( Conf_UID != 0 && ! Conf_Chroot[0] )
325 8ca1e4d5 2004-09-04 alex struct passwd *pwd;
327 8ca1e4d5 2004-09-04 alex pwd = getpwuid( Conf_UID );
328 8ca1e4d5 2004-09-04 alex if( pwd != NULL )
330 8ca1e4d5 2004-09-04 alex if( chdir( pwd->pw_dir ) == 0 ) Log( LOG_DEBUG, "Changed working directory to \"%s\" ...", pwd->pw_dir );
331 8ca1e4d5 2004-09-04 alex else Log( LOG_ERR, "Can't change working directory to \"%s\": %s", pwd->pw_dir, strerror( errno ));
333 8ca1e4d5 2004-09-04 alex else Log( LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID );
336 7281b8dd 2004-05-07 alex /* Initialize modules, part II: these functions are eventually
337 7281b8dd 2004-05-07 alex * called with already dropped privileges ... */
338 ba258e65 2002-05-27 alex Resolve_Init( );
339 ba258e65 2002-05-27 alex Lists_Init( );
340 804b1ec4 2001-12-31 alex Channel_Init( );
341 804b1ec4 2001-12-31 alex Client_Init( );
342 29bd35bc 2003-02-23 alex #ifdef RENDEZVOUS
343 29bd35bc 2003-02-23 alex Rendezvous_Init( );
345 804b1ec4 2001-12-31 alex Conn_Init( );
347 95375426 2005-02-10 alex #ifdef DEBUG
348 95375426 2005-02-10 alex /* Redirect stderr handle to "error file" for debugging
349 95375426 2005-02-10 alex * when not running in "no daemon" mode: */
350 95375426 2005-02-10 alex if( ! NGIRCd_NoDaemon ) Log_InitErrorfile( );
353 804b1ec4 2001-12-31 alex /* Signal-Handler initialisieren */
354 804b1ec4 2001-12-31 alex Initialize_Signal_Handler( );
356 c76e6769 2002-09-02 alex /* Protokoll- und Server-Identifikation erzeugen. Die vom ngIRCd
357 c76e6769 2002-09-02 alex * beim PASS-Befehl verwendete Syntax sowie die erweiterten Flags
358 c76e6769 2002-09-02 alex * sind in doc/Protocol.txt beschrieben. */
359 14cbca87 2002-09-07 alex #ifdef IRCPLUS
360 62796722 2003-03-31 alex sprintf( NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION, IRCPLUSFLAGS );
361 c40592d2 2003-12-26 alex #ifdef ZLIB
362 65f3adca 2002-11-26 alex strcat( NGIRCd_ProtoID, "Z" );
364 c76e6769 2002-09-02 alex if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
366 62796722 2003-03-31 alex sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION );
368 c76e6769 2002-09-02 alex strcat( NGIRCd_ProtoID, " P" );
369 c40592d2 2003-12-26 alex #ifdef ZLIB
370 65f3adca 2002-11-26 alex strcat( NGIRCd_ProtoID, "Z" );
372 65b31ffb 2002-09-02 alex Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
374 b788a3da 2002-06-02 alex /* Vordefinierte Channels anlegen */
375 b788a3da 2002-06-02 alex Channel_InitPredefined( );
377 804b1ec4 2001-12-31 alex /* Listen-Ports initialisieren */
378 e311cc01 2002-11-22 alex if( Conn_InitListeners( ) < 1 )
380 e311cc01 2002-11-22 alex Log( LOG_ALERT, "Server isn't listening on a single port!" );
381 62796722 2003-03-31 alex Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
382 112102b1 2005-02-04 alex Pidfile_Delete( );
386 804b1ec4 2001-12-31 alex /* Hauptschleife */
387 9ec32d15 2002-06-02 alex Conn_Handler( );
389 804b1ec4 2001-12-31 alex /* Alles abmelden */
390 804b1ec4 2001-12-31 alex Conn_Exit( );
391 29bd35bc 2003-02-23 alex #ifdef RENDEZVOUS
392 29bd35bc 2003-02-23 alex Rendezvous_Exit( );
394 804b1ec4 2001-12-31 alex Client_Exit( );
395 804b1ec4 2001-12-31 alex Channel_Exit( );
396 ba258e65 2002-05-27 alex Lists_Exit( );
397 804b1ec4 2001-12-31 alex Log_Exit( );
399 112102b1 2005-02-04 alex Pidfile_Delete( );
403 f4ade537 2001-12-11 alex } /* main */
407 45404a16 2005-06-01 alex * Generate ngIRCd "version string".
408 45404a16 2005-06-01 alex * This string is generated once and then stored in NGIRCd_Version for
409 45404a16 2005-06-01 alex * further usage, for example by the IRC command VERSION and the --version
410 45404a16 2005-06-01 alex * command line switch.
413 8adff592 2005-03-19 fw Fill_Version( void )
415 894cd2cd 2005-02-09 alex NGIRCd_VersionAddition[0] = '\0';
417 c40592d2 2003-12-26 alex #ifdef SYSLOG
418 894cd2cd 2005-02-09 alex strcpy( NGIRCd_VersionAddition, "SYSLOG" );
420 c40592d2 2003-12-26 alex #ifdef ZLIB
421 894cd2cd 2005-02-09 alex if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
422 894cd2cd 2005-02-09 alex strcat( NGIRCd_VersionAddition, "ZLIB" );
424 c40592d2 2003-12-26 alex #ifdef TCPWRAP
425 894cd2cd 2005-02-09 alex if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
426 894cd2cd 2005-02-09 alex strcat( NGIRCd_VersionAddition, "TCPWRAP" );
428 439c945d 2003-03-07 alex #ifdef RENDEZVOUS
429 894cd2cd 2005-02-09 alex if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
430 894cd2cd 2005-02-09 alex strcat( NGIRCd_VersionAddition, "RENDEZVOUS" );
432 cb6279f1 2003-12-27 alex #ifdef IDENTAUTH
433 894cd2cd 2005-02-09 alex if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
434 894cd2cd 2005-02-09 alex strcat( NGIRCd_VersionAddition, "IDENT" );
436 52424b49 2002-01-11 alex #ifdef DEBUG
437 894cd2cd 2005-02-09 alex if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
438 894cd2cd 2005-02-09 alex strcat( NGIRCd_VersionAddition, "DEBUG" );
440 52424b49 2002-01-11 alex #ifdef SNIFFER
441 894cd2cd 2005-02-09 alex if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
442 894cd2cd 2005-02-09 alex strcat( NGIRCd_VersionAddition, "SNIFFER" );
444 14cbca87 2002-09-07 alex #ifdef STRICT_RFC
445 894cd2cd 2005-02-09 alex if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
446 894cd2cd 2005-02-09 alex strcat( NGIRCd_VersionAddition, "RFC" );
448 14cbca87 2002-09-07 alex #ifdef IRCPLUS
449 894cd2cd 2005-02-09 alex if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
450 894cd2cd 2005-02-09 alex strcat( NGIRCd_VersionAddition, "IRCPLUS" );
453 894cd2cd 2005-02-09 alex if( NGIRCd_VersionAddition[0] ) strlcat( NGIRCd_VersionAddition, "-", sizeof( NGIRCd_VersionAddition ));
454 894cd2cd 2005-02-09 alex strlcat( NGIRCd_VersionAddition, TARGET_CPU, sizeof( NGIRCd_VersionAddition ));
455 894cd2cd 2005-02-09 alex strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
456 894cd2cd 2005-02-09 alex strlcat( NGIRCd_VersionAddition, TARGET_VENDOR, sizeof( NGIRCd_VersionAddition ));
457 894cd2cd 2005-02-09 alex strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
458 894cd2cd 2005-02-09 alex strlcat( NGIRCd_VersionAddition, TARGET_OS, sizeof( NGIRCd_VersionAddition ));
460 894cd2cd 2005-02-09 alex #ifdef CVSDATE
461 894cd2cd 2005-02-09 alex snprintf( NGIRCd_Version, sizeof NGIRCd_Version,"%s %s(%s)-%s", PACKAGE_NAME, PACKAGE_VERSION, CVSDATE, NGIRCd_VersionAddition);
463 894cd2cd 2005-02-09 alex snprintf( NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s", PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
465 1fdd5478 2005-02-10 alex } /* Fill_Version */
469 45404a16 2005-06-01 alex * Reload the server configuration file.
472 8adff592 2005-03-19 fw NGIRCd_Rehash( void )
474 8adff592 2005-03-19 fw char old_name[CLIENT_ID_LEN];
476 43a4bc5b 2002-12-19 alex Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
477 8adff592 2005-03-19 fw NGIRCd_SignalRehash = false;
479 e9b0ec91 2004-01-19 alex /* Close down all listening sockets */
480 e311cc01 2002-11-22 alex Conn_ExitListeners( );
482 e9b0ec91 2004-01-19 alex /* Remember old server name */
483 e311cc01 2002-11-22 alex strcpy( old_name, Conf_ServerName );
485 e9b0ec91 2004-01-19 alex /* Re-read configuration ... */
486 a2544e49 2002-12-30 alex Conf_Rehash( );
488 e9b0ec91 2004-01-19 alex /* Recover old server name: it can't be changed during run-time */
489 e311cc01 2002-11-22 alex if( strcmp( old_name, Conf_ServerName ) != 0 )
491 e311cc01 2002-11-22 alex strcpy( Conf_ServerName, old_name );
492 e311cc01 2002-11-22 alex Log( LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name." );
495 e9b0ec91 2004-01-19 alex /* Create new pre-defined channels */
496 e311cc01 2002-11-22 alex Channel_InitPredefined( );
498 e9b0ec91 2004-01-19 alex /* Start listening on sockets */
499 e311cc01 2002-11-22 alex Conn_InitListeners( );
501 24df40ec 2003-03-27 alex /* Sync configuration with established connections */
502 24df40ec 2003-03-27 alex Conn_SyncServerStruct( );
504 43a4bc5b 2002-12-19 alex Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
505 cf2d4794 2002-11-22 alex } /* NGIRCd_Rehash */
509 45404a16 2005-06-01 alex * Initialize the signal handler.
512 8adff592 2005-03-19 fw Initialize_Signal_Handler( void )
514 08cf5607 2001-12-26 alex /* Signal-Handler initialisieren: einige Signale
515 08cf5607 2001-12-26 alex * werden ignoriert, andere speziell behandelt. */
517 62266a8d 2002-02-25 alex #ifdef HAVE_SIGACTION
518 62266a8d 2002-02-25 alex /* sigaction() ist vorhanden */
520 418add93 2001-12-12 alex struct sigaction saction;
522 418add93 2001-12-12 alex /* Signal-Struktur initialisieren */
523 418add93 2001-12-12 alex memset( &saction, 0, sizeof( saction ));
524 23253edb 2002-01-22 alex saction.sa_handler = Signal_Handler;
525 9919f38d 2002-02-19 alex #ifdef SA_RESTART
526 6f955d2a 2002-02-23 alex saction.sa_flags |= SA_RESTART;
528 6f955d2a 2002-02-23 alex #ifdef SA_NOCLDWAIT
529 6f955d2a 2002-02-23 alex saction.sa_flags |= SA_NOCLDWAIT;
532 418add93 2001-12-12 alex /* Signal-Handler einhaengen */
533 418add93 2001-12-12 alex sigaction( SIGINT, &saction, NULL );
534 418add93 2001-12-12 alex sigaction( SIGQUIT, &saction, NULL );
535 418add93 2001-12-12 alex sigaction( SIGTERM, &saction, NULL);
536 3fbbfe44 2002-02-17 alex sigaction( SIGHUP, &saction, NULL);
537 03d971d9 2002-01-02 alex sigaction( SIGCHLD, &saction, NULL);
539 eb952e35 2001-12-24 alex /* einige Signale ignorieren */
540 eb952e35 2001-12-24 alex saction.sa_handler = SIG_IGN;
541 eb952e35 2001-12-24 alex sigaction( SIGPIPE, &saction, NULL );
543 62266a8d 2002-02-25 alex /* kein sigaction() vorhanden */
545 62266a8d 2002-02-25 alex /* Signal-Handler einhaengen */
546 62266a8d 2002-02-25 alex signal( SIGINT, Signal_Handler );
547 62266a8d 2002-02-25 alex signal( SIGQUIT, Signal_Handler );
548 62266a8d 2002-02-25 alex signal( SIGTERM, Signal_Handler );
549 62266a8d 2002-02-25 alex signal( SIGHUP, Signal_Handler );
550 62266a8d 2002-02-25 alex signal( SIGCHLD, Signal_Handler );
552 62266a8d 2002-02-25 alex /* einige Signale ignorieren */
553 62266a8d 2002-02-25 alex signal( SIGPIPE, SIG_IGN );
555 418add93 2001-12-12 alex } /* Initialize_Signal_Handler */
559 45404a16 2005-06-01 alex * Signal handler of ngIRCd.
560 45404a16 2005-06-01 alex * This function is called whenever ngIRCd catches a signal sent by the
561 45404a16 2005-06-01 alex * user and/or the system to it. For example SIGTERM and SIGHUP.
562 45404a16 2005-06-01 alex * @param Signal Number of the signal to handle.
565 8adff592 2005-03-19 fw Signal_Handler( int Signal )
567 f4ade537 2001-12-11 alex switch( Signal )
569 f4ade537 2001-12-11 alex case SIGTERM:
570 b61b8c4d 2001-12-12 alex case SIGINT:
571 b61b8c4d 2001-12-12 alex case SIGQUIT:
572 418add93 2001-12-12 alex /* wir soll(t)en uns wohl beenden ... */
573 8adff592 2005-03-19 fw NGIRCd_SignalQuit = true;
575 3fbbfe44 2002-02-17 alex case SIGHUP:
576 e311cc01 2002-11-22 alex /* Konfiguration neu einlesen: */
577 8adff592 2005-03-19 fw NGIRCd_SignalRehash = true;
579 03d971d9 2002-01-02 alex case SIGCHLD:
580 03d971d9 2002-01-02 alex /* Child-Prozess wurde beendet. Zombies vermeiden: */
581 03d971d9 2002-01-02 alex while( waitpid( -1, NULL, WNOHANG ) > 0);
583 43a4bc5b 2002-12-19 alex #ifdef DEBUG
585 418add93 2001-12-12 alex /* unbekanntes bzw. unbehandeltes Signal */
586 43a4bc5b 2002-12-19 alex Log( LOG_DEBUG, "Got signal %d! Ignored.", Signal );
589 f4ade537 2001-12-11 alex } /* Signal_Handler */
593 45404a16 2005-06-01 alex * Display copyright and version information of ngIRCd on the console.
596 8adff592 2005-03-19 fw Show_Version( void )
598 894cd2cd 2005-02-09 alex puts( NGIRCd_Version );
599 18c92d9d 2005-01-26 alex puts( "Copyright (c)2001-2005 by Alexander Barton (<alex@barton.de>)." );
600 5b27e491 2002-04-01 alex puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
601 52424b49 2002-01-11 alex puts( "This is free software; see the source for copying conditions. There is NO" );
602 52424b49 2002-01-11 alex puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
603 52424b49 2002-01-11 alex } /* Show_Version */
607 45404a16 2005-06-01 alex * Display a short help text on the console.
608 45404a16 2005-06-01 alex * This help depends on the configuration of the executable and only shows
609 45404a16 2005-06-01 alex * options that are actually enabled.
612 8adff592 2005-03-19 fw Show_Help( void )
614 52424b49 2002-01-11 alex #ifdef DEBUG
615 72accd4e 2002-03-28 alex puts( " -d, --debug log extra debug messages" );
617 72accd4e 2002-03-28 alex puts( " -f, --config <f> use file <f> as configuration file" );
618 ec30cd4e 2005-02-11 alex puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
619 ec30cd4e 2005-02-11 alex puts( " -p, --passive disable automatic connections to other servers" );
620 72accd4e 2002-03-28 alex #ifdef SNIFFER
621 72accd4e 2002-03-28 alex puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
623 c9ccb669 2003-12-29 alex puts( " -t, --configtest read, validate and display configuration; then exit" );
624 72accd4e 2002-03-28 alex puts( " --version output version information and exit" );
625 72accd4e 2002-03-28 alex puts( " --help display this help and exit" );
626 52424b49 2002-01-11 alex } /* Show_Help */
630 45404a16 2005-06-01 alex * Delete the file containing the process ID (PID).
633 8adff592 2005-03-19 fw Pidfile_Delete( void )
635 112102b1 2005-02-04 alex /* Pidfile configured? */
636 112102b1 2005-02-04 alex if( ! Conf_PidFile[0] ) return;
638 112102b1 2005-02-04 alex #ifdef DEBUG
639 112102b1 2005-02-04 alex Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
642 112102b1 2005-02-04 alex if( unlink( Conf_PidFile ))
643 112102b1 2005-02-04 alex Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
644 112102b1 2005-02-04 alex } /* Pidfile_Delete */
648 45404a16 2005-06-01 alex * Create the file containing the process ID of ngIRCd ("PID file").
649 45404a16 2005-06-01 alex * @param pid The process ID to be stored in this file.
652 8adff592 2005-03-19 fw Pidfile_Create( long pid )
654 112102b1 2005-02-04 alex FILE *pidf;
656 112102b1 2005-02-04 alex /* Pidfile configured? */
657 112102b1 2005-02-04 alex if( ! Conf_PidFile[0] ) return;
659 112102b1 2005-02-04 alex #ifdef DEBUG
660 112102b1 2005-02-04 alex Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
663 8edeed84 2005-02-07 alex pidf = fopen( Conf_PidFile, "w" );
665 112102b1 2005-02-04 alex if( ! pidf )
667 112102b1 2005-02-04 alex Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
671 112102b1 2005-02-04 alex if( fprintf( pidf, "%ld\n", pid ) < 0 )
672 112102b1 2005-02-04 alex Log( LOG_ERR, "Can't write PID file (%s): %s", Conf_PidFile, strerror( errno ));
674 112102b1 2005-02-04 alex if( fclose(pidf) != 0 )
675 112102b1 2005-02-04 alex Log( LOG_ERR, "Error closing PID file (%s): %s", Conf_PidFile, strerror( errno ));
676 112102b1 2005-02-04 alex } /* Pidfile_Create */
680 45404a16 2005-06-01 alex * Redirect stdin, stdout and stderr to apropriate file handles.
683 8adff592 2005-03-19 fw Setup_FDStreams( void )
687 95375426 2005-02-10 alex /* Test if we can open /dev/null for reading and writing. If not
688 95375426 2005-02-10 alex * we are most probably chrooted already and the server has been
689 95375426 2005-02-10 alex * restarted. So we simply don't try to redirect stdXXX ... */
690 95375426 2005-02-10 alex fd = open( "/dev/null", O_RDWR );
691 95375426 2005-02-10 alex if ( fd < 0 ) return;
693 95375426 2005-02-10 alex /* Close "old" stdin/out/err descriptors */
694 f2fbf206 2005-02-10 alex close( 0 ); close( 1 ); close( 2 );
696 95375426 2005-02-10 alex /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
697 95375426 2005-02-10 alex dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
699 95375426 2005-02-10 alex /* Close newly opened file descriptor if not stdin/out/err */
700 95375426 2005-02-10 alex if( fd > 2 ) close( fd );
701 95375426 2005-02-10 alex } /* Setup_FDStreams */
704 f4ade537 2001-12-11 alex /* -eof- */