Blame


1 f4ade537 2001-12-11 alex /*
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)
4 f4ade537 2001-12-11 alex *
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.
10 f4ade537 2001-12-11 alex */
11 f4ade537 2001-12-11 alex
12 f4ade537 2001-12-11 alex
13 ca33cbda 2002-03-12 alex #include "portab.h"
14 fa7b3f0e 2002-12-12 alex
15 45404a16 2005-06-01 alex static char UNUSED id[] = "$Id: ngircd.c,v 1.96 2005/06/01 21:52:18 alex Exp $";
16 f4ade537 2001-12-11 alex
17 45404a16 2005-06-01 alex /**
18 45404a16 2005-06-01 alex * @file
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.
21 45404a16 2005-06-01 alex */
22 45404a16 2005-06-01 alex
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>
38 b61b8c4d 2001-12-12 alex
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"
50 29bd35bc 2003-02-23 alex
51 29bd35bc 2003-02-23 alex #ifdef RENDEZVOUS
52 29bd35bc 2003-02-23 alex #include "rendezvous.h"
53 29bd35bc 2003-02-23 alex #endif
54 f4ade537 2001-12-11 alex
55 ca33cbda 2002-03-12 alex #include "exp.h"
56 f4ade537 2001-12-11 alex #include "ngircd.h"
57 f4ade537 2001-12-11 alex
58 f4ade537 2001-12-11 alex
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 ));
61 f4ade537 2001-12-11 alex
62 8adff592 2005-03-19 fw LOCAL void Show_Version PARAMS(( void ));
63 8adff592 2005-03-19 fw LOCAL void Show_Help PARAMS(( void ));
64 9856253d 2001-12-30 alex
65 8adff592 2005-03-19 fw LOCAL void Pidfile_Create PARAMS(( long ));
66 8adff592 2005-03-19 fw LOCAL void Pidfile_Delete PARAMS(( void ));
67 52424b49 2002-01-11 alex
68 8adff592 2005-03-19 fw LOCAL void Fill_Version PARAMS(( void ));
69 95375426 2005-02-10 alex
70 8adff592 2005-03-19 fw LOCAL void Setup_FDStreams PARAMS(( void ));
71 112102b1 2005-02-04 alex
72 894cd2cd 2005-02-09 alex
73 45404a16 2005-06-01 alex /**
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.
80 45404a16 2005-06-01 alex */
81 ba258e65 2002-05-27 alex GLOBAL int
82 ba258e65 2002-05-27 alex main( int argc, const char *argv[] )
83 f4ade537 2001-12-11 alex {
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;
87 8adff592 2005-03-19 fw long pid;
88 8adff592 2005-03-19 fw int i;
89 8adff592 2005-03-19 fw size_t n;
90 52424b49 2002-01-11 alex
91 9ec32d15 2002-06-02 alex umask( 0077 );
92 9ec32d15 2002-06-02 alex
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;
97 52424b49 2002-01-11 alex #endif
98 d79a7d28 2002-01-18 alex #ifdef SNIFFER
99 8adff592 2005-03-19 fw NGIRCd_Sniffer = false;
100 d79a7d28 2002-01-18 alex #endif
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 ));
103 894cd2cd 2005-02-09 alex
104 1fdd5478 2005-02-10 alex Fill_Version( );
105 52424b49 2002-01-11 alex
106 52424b49 2002-01-11 alex /* Kommandozeile parsen */
107 52424b49 2002-01-11 alex for( i = 1; i < argc; i++ )
108 52424b49 2002-01-11 alex {
109 8adff592 2005-03-19 fw ok = false;
110 52424b49 2002-01-11 alex if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
111 52424b49 2002-01-11 alex {
112 52424b49 2002-01-11 alex /* Lange Option */
113 52424b49 2002-01-11 alex
114 72accd4e 2002-03-28 alex if( strcmp( argv[i], "--config" ) == 0 )
115 72accd4e 2002-03-28 alex {
116 72accd4e 2002-03-28 alex if( i + 1 < argc )
117 72accd4e 2002-03-28 alex {
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 ));
120 72accd4e 2002-03-28 alex
121 0ced4181 2002-12-26 alex /* next parameter */
122 8adff592 2005-03-19 fw i++; ok = true;
123 72accd4e 2002-03-28 alex }
124 72accd4e 2002-03-28 alex }
125 a89dc546 2002-03-27 alex if( strcmp( argv[i], "--configtest" ) == 0 )
126 a89dc546 2002-03-27 alex {
127 8adff592 2005-03-19 fw configtest = true;
128 8adff592 2005-03-19 fw ok = true;
129 a89dc546 2002-03-27 alex }
130 eea1a88b 2002-02-19 alex #ifdef DEBUG
131 eea1a88b 2002-02-19 alex if( strcmp( argv[i], "--debug" ) == 0 )
132 eea1a88b 2002-02-19 alex {
133 8adff592 2005-03-19 fw NGIRCd_Debug = true;
134 8adff592 2005-03-19 fw ok = true;
135 eea1a88b 2002-02-19 alex }
136 eea1a88b 2002-02-19 alex #endif
137 52424b49 2002-01-11 alex if( strcmp( argv[i], "--help" ) == 0 )
138 52424b49 2002-01-11 alex {
139 a89dc546 2002-03-27 alex Show_Version( );
140 a89dc546 2002-03-27 alex puts( "" ); Show_Help( ); puts( "" );
141 52424b49 2002-01-11 alex exit( 1 );
142 52424b49 2002-01-11 alex }
143 eea1a88b 2002-02-19 alex if( strcmp( argv[i], "--nodaemon" ) == 0 )
144 52424b49 2002-01-11 alex {
145 8adff592 2005-03-19 fw NGIRCd_NoDaemon = true;
146 8adff592 2005-03-19 fw ok = true;
147 52424b49 2002-01-11 alex }
148 eea1a88b 2002-02-19 alex if( strcmp( argv[i], "--passive" ) == 0 )
149 52424b49 2002-01-11 alex {
150 8adff592 2005-03-19 fw NGIRCd_Passive = true;
151 8adff592 2005-03-19 fw ok = true;
152 d79a7d28 2002-01-18 alex }
153 d79a7d28 2002-01-18 alex #ifdef SNIFFER
154 d79a7d28 2002-01-18 alex if( strcmp( argv[i], "--sniffer" ) == 0 )
155 d79a7d28 2002-01-18 alex {
156 8adff592 2005-03-19 fw NGIRCd_Sniffer = true;
157 8adff592 2005-03-19 fw ok = true;
158 52424b49 2002-01-11 alex }
159 52424b49 2002-01-11 alex #endif
160 eea1a88b 2002-02-19 alex if( strcmp( argv[i], "--version" ) == 0 )
161 52424b49 2002-01-11 alex {
162 d67d94ea 2002-03-10 alex Show_Version( );
163 eea1a88b 2002-02-19 alex exit( 1 );
164 52424b49 2002-01-11 alex }
165 52424b49 2002-01-11 alex }
166 52424b49 2002-01-11 alex else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
167 52424b49 2002-01-11 alex {
168 52424b49 2002-01-11 alex /* Kurze Option */
169 8adff592 2005-03-19 fw for( n = 1; n < strlen( argv[i] ); n++ )
170 52424b49 2002-01-11 alex {
171 8adff592 2005-03-19 fw ok = false;
172 52424b49 2002-01-11 alex #ifdef DEBUG
173 52424b49 2002-01-11 alex if( argv[i][n] == 'd' )
174 52424b49 2002-01-11 alex {
175 8adff592 2005-03-19 fw NGIRCd_Debug = true;
176 8adff592 2005-03-19 fw ok = true;
177 52424b49 2002-01-11 alex }
178 52424b49 2002-01-11 alex #endif
179 72accd4e 2002-03-28 alex if( argv[i][n] == 'f' )
180 72accd4e 2002-03-28 alex {
181 85aad869 2002-03-31 alex if(( ! argv[i][n + 1] ) && ( i + 1 < argc ))
182 72accd4e 2002-03-28 alex {
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 ));
185 72accd4e 2002-03-28 alex
186 0ced4181 2002-12-26 alex /* go to the following parameter */
187 8adff592 2005-03-19 fw i++;
188 8adff592 2005-03-19 fw n = strlen( argv[i] );
189 8adff592 2005-03-19 fw ok = true;
190 72accd4e 2002-03-28 alex }
191 72accd4e 2002-03-28 alex }
192 eea1a88b 2002-02-19 alex if( argv[i][n] == 'n' )
193 eea1a88b 2002-02-19 alex {
194 8adff592 2005-03-19 fw NGIRCd_NoDaemon = true;
195 8adff592 2005-03-19 fw ok = true;
196 eea1a88b 2002-02-19 alex }
197 eea1a88b 2002-02-19 alex if( argv[i][n] == 'p' )
198 eea1a88b 2002-02-19 alex {
199 8adff592 2005-03-19 fw NGIRCd_Passive = true;
200 8adff592 2005-03-19 fw ok = true;
201 eea1a88b 2002-02-19 alex }
202 d79a7d28 2002-01-18 alex #ifdef SNIFFER
203 d79a7d28 2002-01-18 alex if( argv[i][n] == 's' )
204 d79a7d28 2002-01-18 alex {
205 8adff592 2005-03-19 fw NGIRCd_Sniffer = true;
206 8adff592 2005-03-19 fw ok = true;
207 d79a7d28 2002-01-18 alex }
208 d79a7d28 2002-01-18 alex #endif
209 c9ccb669 2003-12-29 alex if( argv[i][n] == 't' )
210 c9ccb669 2003-12-29 alex {
211 8adff592 2005-03-19 fw configtest = true;
212 8adff592 2005-03-19 fw ok = true;
213 c9ccb669 2003-12-29 alex }
214 52424b49 2002-01-11 alex
215 52424b49 2002-01-11 alex if( ! ok )
216 52424b49 2002-01-11 alex {
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 );
219 52424b49 2002-01-11 alex exit( 1 );
220 52424b49 2002-01-11 alex }
221 52424b49 2002-01-11 alex }
222 f4ade537 2001-12-11 alex
223 52424b49 2002-01-11 alex }
224 52424b49 2002-01-11 alex if( ! ok )
225 52424b49 2002-01-11 alex {
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 );
228 52424b49 2002-01-11 alex exit( 1 );
229 52424b49 2002-01-11 alex }
230 52424b49 2002-01-11 alex }
231 52424b49 2002-01-11 alex
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" );
236 eea1a88b 2002-02-19 alex #endif
237 eea1a88b 2002-02-19 alex #ifdef SNIFFER
238 991da8ea 2002-05-18 alex if( NGIRCd_Sniffer )
239 991da8ea 2002-05-18 alex {
240 8adff592 2005-03-19 fw NGIRCd_Debug = true;
241 991da8ea 2002-05-18 alex strcpy( NGIRCd_DebugLevel, "2" );
242 991da8ea 2002-05-18 alex }
243 eea1a88b 2002-02-19 alex #endif
244 a89dc546 2002-03-27 alex
245 a89dc546 2002-03-27 alex /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
246 a89dc546 2002-03-27 alex if( configtest )
247 a89dc546 2002-03-27 alex {
248 a89dc546 2002-03-27 alex Show_Version( ); puts( "" );
249 a89dc546 2002-03-27 alex exit( Conf_Test( ));
250 a89dc546 2002-03-27 alex }
251 eea1a88b 2002-02-19 alex
252 43a4bc5b 2002-12-19 alex while( ! NGIRCd_SignalQuit )
253 804b1ec4 2001-12-31 alex {
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 ));
257 7281b8dd 2004-05-07 alex
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;
261 7281b8dd 2004-05-07 alex
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( );
265 7281b8dd 2004-05-07 alex
266 7281b8dd 2004-05-07 alex if( Conf_Chroot[0] )
267 7281b8dd 2004-05-07 alex {
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 ));
270 7281b8dd 2004-05-07 alex
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 );
273 7281b8dd 2004-05-07 alex }
274 7281b8dd 2004-05-07 alex
275 7281b8dd 2004-05-07 alex if( Conf_GID != 0 )
276 7281b8dd 2004-05-07 alex {
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 ));
279 7281b8dd 2004-05-07 alex }
280 7281b8dd 2004-05-07 alex if( Conf_UID != 0 )
281 7281b8dd 2004-05-07 alex {
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 ));
284 7281b8dd 2004-05-07 alex }
285 7281b8dd 2004-05-07 alex
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 )
290 22cf0c5d 2002-01-12 alex {
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 )
294 22cf0c5d 2002-01-12 alex {
295 95375426 2005-02-10 alex /* "Old" process: exit. */
296 22cf0c5d 2002-01-12 alex exit( 0 );
297 22cf0c5d 2002-01-12 alex }
298 22cf0c5d 2002-01-12 alex if( pid < 0 )
299 22cf0c5d 2002-01-12 alex {
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 ));
302 22cf0c5d 2002-01-12 alex exit( 1 );
303 22cf0c5d 2002-01-12 alex }
304 fe2bc90e 2002-03-06 alex
305 95375426 2005-02-10 alex /* New child process */
306 8adff592 2005-03-19 fw (void)setsid( );
307 22cf0c5d 2002-01-12 alex chdir( "/" );
308 95375426 2005-02-10 alex
309 95375426 2005-02-10 alex /* Detach stdin, stdout and stderr */
310 8adff592 2005-03-19 fw Setup_FDStreams( );
311 22cf0c5d 2002-01-12 alex }
312 112102b1 2005-02-04 alex
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 );
316 8ca1e4d5 2004-09-04 alex
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);
320 8ca1e4d5 2004-09-04 alex
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] )
324 8ca1e4d5 2004-09-04 alex {
325 8ca1e4d5 2004-09-04 alex struct passwd *pwd;
326 ff280ce4 2001-12-12 alex
327 8ca1e4d5 2004-09-04 alex pwd = getpwuid( Conf_UID );
328 8ca1e4d5 2004-09-04 alex if( pwd != NULL )
329 8ca1e4d5 2004-09-04 alex {
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 ));
332 8ca1e4d5 2004-09-04 alex }
333 8ca1e4d5 2004-09-04 alex else Log( LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID );
334 8ca1e4d5 2004-09-04 alex }
335 8ca1e4d5 2004-09-04 alex
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( );
344 29bd35bc 2003-02-23 alex #endif
345 804b1ec4 2001-12-31 alex Conn_Init( );
346 f4ade537 2001-12-11 alex
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( );
351 95375426 2005-02-10 alex #endif
352 fe06f6e9 2002-03-29 alex
353 804b1ec4 2001-12-31 alex /* Signal-Handler initialisieren */
354 804b1ec4 2001-12-31 alex Initialize_Signal_Handler( );
355 65b31ffb 2002-09-02 alex
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" );
363 65f3adca 2002-11-26 alex #endif
364 c76e6769 2002-09-02 alex if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
365 14cbca87 2002-09-07 alex #else
366 62796722 2003-03-31 alex sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION );
367 14cbca87 2002-09-07 alex #endif
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" );
371 65f3adca 2002-11-26 alex #endif
372 65b31ffb 2002-09-02 alex Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
373 9856253d 2001-12-30 alex
374 b788a3da 2002-06-02 alex /* Vordefinierte Channels anlegen */
375 b788a3da 2002-06-02 alex Channel_InitPredefined( );
376 b788a3da 2002-06-02 alex
377 804b1ec4 2001-12-31 alex /* Listen-Ports initialisieren */
378 e311cc01 2002-11-22 alex if( Conn_InitListeners( ) < 1 )
379 e311cc01 2002-11-22 alex {
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( );
383 e311cc01 2002-11-22 alex exit( 1 );
384 e311cc01 2002-11-22 alex }
385 e311cc01 2002-11-22 alex
386 804b1ec4 2001-12-31 alex /* Hauptschleife */
387 9ec32d15 2002-06-02 alex Conn_Handler( );
388 804b1ec4 2001-12-31 alex
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( );
393 29bd35bc 2003-02-23 alex #endif
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( );
398 112102b1 2005-02-04 alex
399 112102b1 2005-02-04 alex Pidfile_Delete( );
400 804b1ec4 2001-12-31 alex }
401 fe2bc90e 2002-03-06 alex
402 f4ade537 2001-12-11 alex return 0;
403 f4ade537 2001-12-11 alex } /* main */
404 f4ade537 2001-12-11 alex
405 f4ade537 2001-12-11 alex
406 45404a16 2005-06-01 alex /**
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.
411 45404a16 2005-06-01 alex */
412 8adff592 2005-03-19 fw LOCAL void
413 8adff592 2005-03-19 fw Fill_Version( void )
414 3fbbfe44 2002-02-17 alex {
415 894cd2cd 2005-02-09 alex NGIRCd_VersionAddition[0] = '\0';
416 3fbbfe44 2002-02-17 alex
417 c40592d2 2003-12-26 alex #ifdef SYSLOG
418 894cd2cd 2005-02-09 alex strcpy( NGIRCd_VersionAddition, "SYSLOG" );
419 52424b49 2002-01-11 alex #endif
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" );
423 439c945d 2003-03-07 alex #endif
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" );
427 439c945d 2003-03-07 alex #endif
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" );
431 cb6279f1 2003-12-27 alex #endif
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" );
435 65f3adca 2002-11-26 alex #endif
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" );
439 52424b49 2002-01-11 alex #endif
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" );
443 52424b49 2002-01-11 alex #endif
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" );
447 14cbca87 2002-09-07 alex #endif
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" );
451 14cbca87 2002-09-07 alex #endif
452 52424b49 2002-01-11 alex
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 ));
459 e311cc01 2002-11-22 alex
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);
462 894cd2cd 2005-02-09 alex #else
463 894cd2cd 2005-02-09 alex snprintf( NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s", PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
464 894cd2cd 2005-02-09 alex #endif
465 1fdd5478 2005-02-10 alex } /* Fill_Version */
466 e311cc01 2002-11-22 alex
467 894cd2cd 2005-02-09 alex
468 45404a16 2005-06-01 alex /**
469 45404a16 2005-06-01 alex * Reload the server configuration file.
470 45404a16 2005-06-01 alex */
471 8adff592 2005-03-19 fw GLOBAL void
472 8adff592 2005-03-19 fw NGIRCd_Rehash( void )
473 e311cc01 2002-11-22 alex {
474 8adff592 2005-03-19 fw char old_name[CLIENT_ID_LEN];
475 43a4bc5b 2002-12-19 alex
476 43a4bc5b 2002-12-19 alex Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
477 8adff592 2005-03-19 fw NGIRCd_SignalRehash = false;
478 43a4bc5b 2002-12-19 alex
479 e9b0ec91 2004-01-19 alex /* Close down all listening sockets */
480 e311cc01 2002-11-22 alex Conn_ExitListeners( );
481 52424b49 2002-01-11 alex
482 e9b0ec91 2004-01-19 alex /* Remember old server name */
483 e311cc01 2002-11-22 alex strcpy( old_name, Conf_ServerName );
484 3fbbfe44 2002-02-17 alex
485 e9b0ec91 2004-01-19 alex /* Re-read configuration ... */
486 a2544e49 2002-12-30 alex Conf_Rehash( );
487 e311cc01 2002-11-22 alex
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 )
490 e311cc01 2002-11-22 alex {
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." );
493 e311cc01 2002-11-22 alex }
494 e311cc01 2002-11-22 alex
495 e9b0ec91 2004-01-19 alex /* Create new pre-defined channels */
496 e311cc01 2002-11-22 alex Channel_InitPredefined( );
497 e311cc01 2002-11-22 alex
498 e9b0ec91 2004-01-19 alex /* Start listening on sockets */
499 e311cc01 2002-11-22 alex Conn_InitListeners( );
500 e311cc01 2002-11-22 alex
501 24df40ec 2003-03-27 alex /* Sync configuration with established connections */
502 24df40ec 2003-03-27 alex Conn_SyncServerStruct( );
503 24df40ec 2003-03-27 alex
504 43a4bc5b 2002-12-19 alex Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
505 cf2d4794 2002-11-22 alex } /* NGIRCd_Rehash */
506 e311cc01 2002-11-22 alex
507 e311cc01 2002-11-22 alex
508 45404a16 2005-06-01 alex /**
509 45404a16 2005-06-01 alex * Initialize the signal handler.
510 45404a16 2005-06-01 alex */
511 8adff592 2005-03-19 fw LOCAL void
512 8adff592 2005-03-19 fw Initialize_Signal_Handler( void )
513 418add93 2001-12-12 alex {
514 08cf5607 2001-12-26 alex /* Signal-Handler initialisieren: einige Signale
515 08cf5607 2001-12-26 alex * werden ignoriert, andere speziell behandelt. */
516 418add93 2001-12-12 alex
517 62266a8d 2002-02-25 alex #ifdef HAVE_SIGACTION
518 62266a8d 2002-02-25 alex /* sigaction() ist vorhanden */
519 62266a8d 2002-02-25 alex
520 418add93 2001-12-12 alex struct sigaction saction;
521 418add93 2001-12-12 alex
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;
527 9919f38d 2002-02-19 alex #endif
528 6f955d2a 2002-02-23 alex #ifdef SA_NOCLDWAIT
529 6f955d2a 2002-02-23 alex saction.sa_flags |= SA_NOCLDWAIT;
530 6f955d2a 2002-02-23 alex #endif
531 418add93 2001-12-12 alex
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);
538 eb952e35 2001-12-24 alex
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 );
542 62266a8d 2002-02-25 alex #else
543 62266a8d 2002-02-25 alex /* kein sigaction() vorhanden */
544 62266a8d 2002-02-25 alex
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 );
551 62266a8d 2002-02-25 alex
552 62266a8d 2002-02-25 alex /* einige Signale ignorieren */
553 62266a8d 2002-02-25 alex signal( SIGPIPE, SIG_IGN );
554 62266a8d 2002-02-25 alex #endif
555 418add93 2001-12-12 alex } /* Initialize_Signal_Handler */
556 418add93 2001-12-12 alex
557 418add93 2001-12-12 alex
558 45404a16 2005-06-01 alex /**
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.
563 45404a16 2005-06-01 alex */
564 8adff592 2005-03-19 fw LOCAL void
565 8adff592 2005-03-19 fw Signal_Handler( int Signal )
566 f4ade537 2001-12-11 alex {
567 f4ade537 2001-12-11 alex switch( Signal )
568 f4ade537 2001-12-11 alex {
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;
574 f4ade537 2001-12-11 alex break;
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;
578 3fbbfe44 2002-02-17 alex break;
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);
582 03d971d9 2002-01-02 alex break;
583 43a4bc5b 2002-12-19 alex #ifdef DEBUG
584 f4ade537 2001-12-11 alex default:
585 418add93 2001-12-12 alex /* unbekanntes bzw. unbehandeltes Signal */
586 43a4bc5b 2002-12-19 alex Log( LOG_DEBUG, "Got signal %d! Ignored.", Signal );
587 43a4bc5b 2002-12-19 alex #endif
588 f4ade537 2001-12-11 alex }
589 f4ade537 2001-12-11 alex } /* Signal_Handler */
590 f4ade537 2001-12-11 alex
591 f4ade537 2001-12-11 alex
592 45404a16 2005-06-01 alex /**
593 45404a16 2005-06-01 alex * Display copyright and version information of ngIRCd on the console.
594 45404a16 2005-06-01 alex */
595 8adff592 2005-03-19 fw LOCAL void
596 8adff592 2005-03-19 fw Show_Version( void )
597 52424b49 2002-01-11 alex {
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 */
604 52424b49 2002-01-11 alex
605 52424b49 2002-01-11 alex
606 45404a16 2005-06-01 alex /**
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.
610 45404a16 2005-06-01 alex */
611 8adff592 2005-03-19 fw LOCAL void
612 8adff592 2005-03-19 fw Show_Help( void )
613 52424b49 2002-01-11 alex {
614 52424b49 2002-01-11 alex #ifdef DEBUG
615 72accd4e 2002-03-28 alex puts( " -d, --debug log extra debug messages" );
616 52424b49 2002-01-11 alex #endif
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" );
622 9951e135 2002-01-21 alex #endif
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 */
627 52424b49 2002-01-11 alex
628 52424b49 2002-01-11 alex
629 45404a16 2005-06-01 alex /**
630 45404a16 2005-06-01 alex * Delete the file containing the process ID (PID).
631 45404a16 2005-06-01 alex */
632 8adff592 2005-03-19 fw LOCAL void
633 8adff592 2005-03-19 fw Pidfile_Delete( void )
634 112102b1 2005-02-04 alex {
635 112102b1 2005-02-04 alex /* Pidfile configured? */
636 112102b1 2005-02-04 alex if( ! Conf_PidFile[0] ) return;
637 112102b1 2005-02-04 alex
638 112102b1 2005-02-04 alex #ifdef DEBUG
639 112102b1 2005-02-04 alex Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
640 112102b1 2005-02-04 alex #endif
641 112102b1 2005-02-04 alex
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 */
645 112102b1 2005-02-04 alex
646 112102b1 2005-02-04 alex
647 45404a16 2005-06-01 alex /**
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.
650 45404a16 2005-06-01 alex */
651 8adff592 2005-03-19 fw LOCAL void
652 8adff592 2005-03-19 fw Pidfile_Create( long pid )
653 112102b1 2005-02-04 alex {
654 112102b1 2005-02-04 alex FILE *pidf;
655 112102b1 2005-02-04 alex
656 112102b1 2005-02-04 alex /* Pidfile configured? */
657 112102b1 2005-02-04 alex if( ! Conf_PidFile[0] ) return;
658 112102b1 2005-02-04 alex
659 112102b1 2005-02-04 alex #ifdef DEBUG
660 112102b1 2005-02-04 alex Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
661 112102b1 2005-02-04 alex #endif
662 112102b1 2005-02-04 alex
663 8edeed84 2005-02-07 alex pidf = fopen( Conf_PidFile, "w" );
664 8edeed84 2005-02-07 alex
665 112102b1 2005-02-04 alex if( ! pidf )
666 112102b1 2005-02-04 alex {
667 112102b1 2005-02-04 alex Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
668 112102b1 2005-02-04 alex return;
669 112102b1 2005-02-04 alex }
670 112102b1 2005-02-04 alex
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 ));
673 112102b1 2005-02-04 alex
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 */
677 95375426 2005-02-10 alex
678 95375426 2005-02-10 alex
679 45404a16 2005-06-01 alex /**
680 45404a16 2005-06-01 alex * Redirect stdin, stdout and stderr to apropriate file handles.
681 45404a16 2005-06-01 alex */
682 8adff592 2005-03-19 fw LOCAL void
683 8adff592 2005-03-19 fw Setup_FDStreams( void )
684 95375426 2005-02-10 alex {
685 8adff592 2005-03-19 fw int fd;
686 95375426 2005-02-10 alex
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;
692 95375426 2005-02-10 alex
693 95375426 2005-02-10 alex /* Close "old" stdin/out/err descriptors */
694 f2fbf206 2005-02-10 alex close( 0 ); close( 1 ); close( 2 );
695 112102b1 2005-02-04 alex
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 );
698 112102b1 2005-02-04 alex
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 */
702 95375426 2005-02-10 alex
703 95375426 2005-02-10 alex
704 f4ade537 2001-12-11 alex /* -eof- */