]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/ngircd.c
remove NGIRCd_SignalRehash
[ngircd-alex.git] / src / ngircd / ngircd.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2010 Alexander Barton (alex@barton.de).
4  *
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.
10  */
11
12
13 #include "portab.h"
14
15 /**
16  * @file
17  * The main program, including the C function main() which is called
18  * by the loader of the operating system.
19  */
20
21 #include "imp.h"
22 #include <assert.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <signal.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <time.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <pwd.h>
34 #include <grp.h>
35
36 #if defined(DEBUG) && defined(HAVE_MTRACE)
37 #include <mcheck.h>
38 #endif
39
40 #include "defines.h"
41 #include "conn.h"
42 #include "conf-ssl.h"
43 #include "channel.h"
44 #include "conf.h"
45 #include "lists.h"
46 #include "log.h"
47 #include "parse.h"
48 #include "sighandlers.h"
49 #include "io.h"
50 #include "irc.h"
51
52 #ifdef ZEROCONF
53 #include "rendezvous.h"
54 #endif
55
56 #include "exp.h"
57 #include "ngircd.h"
58
59
60 static void Show_Version PARAMS(( void ));
61 static void Show_Help PARAMS(( void ));
62
63 static void Pidfile_Create PARAMS(( pid_t pid ));
64 static void Pidfile_Delete PARAMS(( void ));
65
66 static void Fill_Version PARAMS(( void ));
67
68 static void Setup_FDStreams PARAMS(( int fd ));
69
70 static bool NGIRCd_Init PARAMS(( bool ));
71
72 /**
73  * The main() function of ngIRCd.
74  * Here all starts: this function is called by the operating system loader,
75  * it is the first portion of code executed of ngIRCd.
76  * @param argc The number of arguments passed to ngIRCd on the command line.
77  * @param argv An array containing all the arguments passed to ngIRCd.
78  * @return Global exit code of ngIRCd, zero on success.
79  */
80 GLOBAL int
81 main( int argc, const char *argv[] )
82 {
83         bool ok, configtest = false;
84         bool NGIRCd_NoDaemon = false;
85         int i;
86         size_t n;
87
88 #if defined(DEBUG) && defined(HAVE_MTRACE)
89         /* enable GNU libc memory tracing when running in debug mode
90          * and functionality available */
91         mtrace();
92 #endif
93
94         umask( 0077 );
95
96         NGIRCd_SignalQuit = NGIRCd_SignalRestart = false;
97         NGIRCd_Passive = false;
98 #ifdef DEBUG
99         NGIRCd_Debug = false;
100 #endif
101 #ifdef SNIFFER
102         NGIRCd_Sniffer = false;
103 #endif
104         strlcpy( NGIRCd_ConfFile, SYSCONFDIR, sizeof( NGIRCd_ConfFile ));
105         strlcat( NGIRCd_ConfFile, CONFIG_FILE, sizeof( NGIRCd_ConfFile ));
106
107         Fill_Version( );
108
109         /* parse conmmand line */
110         for( i = 1; i < argc; i++ )
111         {
112                 ok = false;
113                 if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
114                 {
115                         /* long option */
116                         if( strcmp( argv[i], "--config" ) == 0 )
117                         {
118                                 if( i + 1 < argc )
119                                 {
120                                         /* Ok, there's an parameter left */
121                                         strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
122
123                                         /* next parameter */
124                                         i++; ok = true;
125                                 }
126                         }
127                         if( strcmp( argv[i], "--configtest" ) == 0 )
128                         {
129                                 configtest = true;
130                                 ok = true;
131                         }
132 #ifdef DEBUG
133                         if( strcmp( argv[i], "--debug" ) == 0 )
134                         {
135                                 NGIRCd_Debug = true;
136                                 ok = true;
137                         }
138 #endif
139                         if( strcmp( argv[i], "--help" ) == 0 )
140                         {
141                                 Show_Version( );
142                                 puts( "" ); Show_Help( ); puts( "" );
143                                 exit( 1 );
144                         }
145                         if( strcmp( argv[i], "--nodaemon" ) == 0 )
146                         {
147                                 NGIRCd_NoDaemon = true;
148                                 ok = true;
149                         }
150                         if( strcmp( argv[i], "--passive" ) == 0 )
151                         {
152                                 NGIRCd_Passive = true;
153                                 ok = true;
154                         }
155 #ifdef SNIFFER
156                         if( strcmp( argv[i], "--sniffer" ) == 0 )
157                         {
158                                 NGIRCd_Sniffer = true;
159                                 ok = true;
160                         }
161 #endif
162                         if( strcmp( argv[i], "--version" ) == 0 )
163                         {
164                                 Show_Version( );
165                                 exit( 1 );
166                         }
167                 }
168                 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
169                 {
170                         /* short option */
171                         for( n = 1; n < strlen( argv[i] ); n++ )
172                         {
173                                 ok = false;
174 #ifdef DEBUG
175                                 if (argv[i][n] == 'd') {
176                                         NGIRCd_Debug = true;
177                                         ok = true;
178                                 }
179 #endif
180                                 if (argv[i][n] == 'f') {
181                                         if(( ! argv[i][n + 1] ) && ( i + 1 < argc ))
182                                         {
183                                                 /* Ok, next character is a blank */
184                                                 strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
185
186                                                 /* go to the following parameter */
187                                                 i++;
188                                                 n = strlen( argv[i] );
189                                                 ok = true;
190                                         }
191                                 }
192
193                                 if (argv[i][n] == 'h') {
194                                         Show_Version();
195                                         puts(""); Show_Help(); puts("");
196                                         exit(1);
197                                 }
198
199                                 if (argv[i][n] == 'n') {
200                                         NGIRCd_NoDaemon = true;
201                                         ok = true;
202                                 }
203                                 if (argv[i][n] == 'p') {
204                                         NGIRCd_Passive = true;
205                                         ok = true;
206                                 }
207 #ifdef SNIFFER
208                                 if (argv[i][n] == 's') {
209                                         NGIRCd_Sniffer = true;
210                                         ok = true;
211                                 }
212 #endif
213                                 if (argv[i][n] == 't') {
214                                         configtest = true;
215                                         ok = true;
216                                 }
217
218                                 if (argv[i][n] == 'V') {
219                                         Show_Version();
220                                         exit(1);
221                                 }
222
223                                 if (! ok) {
224                                         printf( "%s: invalid option \"-%c\"!\n", PACKAGE_NAME, argv[i][n] );
225                                         printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
226                                         exit( 1 );
227                                 }
228                         }
229
230                 }
231                 if( ! ok )
232                 {
233                         printf( "%s: invalid option \"%s\"!\n", PACKAGE_NAME, argv[i] );
234                         printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
235                         exit( 1 );
236                 }
237         }
238
239         /* Debug-Level (for IRCs "VERSION" command) */
240         NGIRCd_DebugLevel[0] = '\0';
241 #ifdef DEBUG
242         if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
243 #endif
244 #ifdef SNIFFER
245         if( NGIRCd_Sniffer )
246         {
247                 NGIRCd_Debug = true;
248                 strcpy( NGIRCd_DebugLevel, "2" );
249         }
250 #endif
251
252         if( configtest )
253         {
254                 Show_Version( ); puts( "" );
255                 exit( Conf_Test( ));
256         }
257         
258         while( ! NGIRCd_SignalQuit )
259         {
260                 /* Initialize global variables */
261                 NGIRCd_Start = time( NULL );
262                 (void)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
263
264                 NGIRCd_SignalRestart = false;
265                 NGIRCd_SignalQuit = false;
266
267                 /* Initialize modules, part I */
268                 Log_Init( ! NGIRCd_NoDaemon );
269                 Conf_Init( );
270
271                 /* Initialize the "main program": chroot environment, user and
272                  * group ID, ... */
273                 if (!NGIRCd_Init(NGIRCd_NoDaemon)) {
274                         Log(LOG_ALERT, "Fatal: Initialization failed");
275                         exit(1);
276                 }
277
278                 /* Initialize modules, part II: these functions are eventually
279                  * called with already dropped privileges ... */
280                 Channel_Init( );
281                 Client_Init( );
282 #ifdef ZEROCONF
283                 Rendezvous_Init( );
284 #endif
285                 Conn_Init( );
286
287 #ifdef DEBUG
288                 /* Redirect stderr handle to "error file" for debugging
289                  * when not running in "no daemon" mode: */
290                 if( ! NGIRCd_NoDaemon ) Log_InitErrorfile( );
291 #endif
292                 if (!io_library_init(CONNECTION_POOL)) {
293                         Log(LOG_ALERT, "Fatal: Cannot initialize IO routines: %s", strerror(errno));
294                         exit(1);
295                 }
296
297                 if (!Signals_Init()) {
298                         Log(LOG_ALERT, "Fatal: Could not set up signal handlers: %s", strerror(errno));
299                         exit(1);
300                 }
301
302                 /*
303                  * create protocol and server identification.
304                  * The syntax used by ngIRCd in PASS commands and the extended flags
305                  * are described in doc/Protocol.txt
306                  */
307 #ifdef IRCPLUS
308                 snprintf( NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION, IRCPLUSFLAGS );
309 #ifdef ZLIB
310                 strcat( NGIRCd_ProtoID, "Z" );
311 #endif
312                 if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
313 #else
314                 snprintf( NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION );
315 #endif
316                 strlcat( NGIRCd_ProtoID, " P", sizeof NGIRCd_ProtoID );
317 #ifdef ZLIB
318                 strlcat( NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID );
319 #endif
320                 LogDebug("Protocol and server ID is \"%s\".", NGIRCd_ProtoID);
321
322                 Channel_InitPredefined( );
323
324                 if( Conn_InitListeners( ) < 1 )
325                 {
326                         Log( LOG_ALERT, "Server isn't listening on a single port!" );
327                         Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
328                         Pidfile_Delete( );
329                         exit( 1 );
330                 }
331                 
332                 /* Hauptschleife */
333                 Conn_Handler( );
334
335                 /* Alles abmelden */
336                 Conn_Exit( );
337 #ifdef ZEROCONF
338                 Rendezvous_Exit( );
339 #endif
340                 Client_Exit( );
341                 Channel_Exit( );
342                 Log_Exit( );
343         }
344         Pidfile_Delete( );
345
346         return 0;
347 } /* main */
348
349
350 /**
351  * Generate ngIRCd "version string".
352  * This string is generated once and then stored in NGIRCd_Version for
353  * further usage, for example by the IRC command VERSION and the --version
354  * command line switch.
355  */
356 static void
357 Fill_Version( void )
358 {
359         NGIRCd_VersionAddition[0] = '\0';
360
361 #ifdef SYSLOG
362         strlcpy( NGIRCd_VersionAddition, "SYSLOG", sizeof NGIRCd_VersionAddition );
363 #endif
364 #ifdef ZLIB
365         if( NGIRCd_VersionAddition[0] )
366                 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
367         strlcat( NGIRCd_VersionAddition, "ZLIB", sizeof NGIRCd_VersionAddition );
368 #endif
369 #ifdef SSL_SUPPORT
370         if ( NGIRCd_VersionAddition[0] ) strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
371         strlcat( NGIRCd_VersionAddition, "SSL", sizeof NGIRCd_VersionAddition );
372 #endif
373 #ifdef TCPWRAP
374         if( NGIRCd_VersionAddition[0] )
375                         strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
376         strlcat( NGIRCd_VersionAddition, "TCPWRAP", sizeof NGIRCd_VersionAddition );
377 #endif
378 #ifdef ZEROCONF
379         if( NGIRCd_VersionAddition[0] )
380                 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
381         strlcat( NGIRCd_VersionAddition, "ZEROCONF", sizeof NGIRCd_VersionAddition );
382 #endif
383 #ifdef IDENTAUTH
384         if( NGIRCd_VersionAddition[0] )
385                 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
386         strlcat( NGIRCd_VersionAddition, "IDENT", sizeof NGIRCd_VersionAddition );
387 #endif
388 #ifdef PAM
389         if (NGIRCd_VersionAddition[0])
390                 strlcat(NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition);
391         strlcat(NGIRCd_VersionAddition, "PAM", sizeof NGIRCd_VersionAddition);
392 #endif
393 #ifdef DEBUG
394         if( NGIRCd_VersionAddition[0] )
395                 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
396         strlcat( NGIRCd_VersionAddition, "DEBUG", sizeof NGIRCd_VersionAddition );
397 #endif
398 #ifdef SNIFFER
399         if( NGIRCd_VersionAddition[0] )
400                 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
401         strlcat( NGIRCd_VersionAddition, "SNIFFER", sizeof NGIRCd_VersionAddition );
402 #endif
403 #ifdef STRICT_RFC
404         if( NGIRCd_VersionAddition[0] )
405                 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
406         strlcat( NGIRCd_VersionAddition, "RFC", sizeof NGIRCd_VersionAddition );
407 #endif
408 #ifdef IRCPLUS
409         if( NGIRCd_VersionAddition[0] )
410                 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
411         strlcat( NGIRCd_VersionAddition, "IRCPLUS", sizeof NGIRCd_VersionAddition );
412 #endif
413 #ifdef WANT_IPV6
414         if (NGIRCd_VersionAddition[0])
415                 strlcat(NGIRCd_VersionAddition, "+", sizeof(NGIRCd_VersionAddition));
416         strlcat(NGIRCd_VersionAddition, "IPv6", sizeof(NGIRCd_VersionAddition));
417 #endif
418         if( NGIRCd_VersionAddition[0] )
419                 strlcat( NGIRCd_VersionAddition, "-", sizeof( NGIRCd_VersionAddition ));
420
421         strlcat( NGIRCd_VersionAddition, TARGET_CPU, sizeof( NGIRCd_VersionAddition ));
422         strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
423         strlcat( NGIRCd_VersionAddition, TARGET_VENDOR, sizeof( NGIRCd_VersionAddition ));
424         strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
425         strlcat( NGIRCd_VersionAddition, TARGET_OS, sizeof( NGIRCd_VersionAddition ));
426
427         snprintf(NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s",
428                  PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
429         } /* Fill_Version */
430
431
432 /**
433  * Display copyright and version information of ngIRCd on the console.
434  */
435 static void
436 Show_Version( void )
437 {
438         puts( NGIRCd_Version );
439         puts( "Copyright (c)2001-2010 Alexander Barton (<alex@barton.de>) and Contributors." );
440         puts( "Homepage: <http://ngircd.barton.de/>\n" );
441         puts( "This is free software; see the source for copying conditions. There is NO" );
442         puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
443 } /* Show_Version */
444
445
446 /**
447  * Display a short help text on the console.
448  * This help depends on the configuration of the executable and only shows
449  * options that are actually enabled.
450  */
451 static void
452 Show_Help( void )
453 {
454 #ifdef DEBUG
455         puts( "  -d, --debug        log extra debug messages" );
456 #endif
457         puts( "  -f, --config <f>   use file <f> as configuration file" );
458         puts( "  -n, --nodaemon     don't fork and don't detach from controlling terminal" );
459         puts( "  -p, --passive      disable automatic connections to other servers" );
460 #ifdef SNIFFER
461         puts( "  -s, --sniffer      enable network sniffer and display all IRC traffic" );
462 #endif
463         puts( "  -t, --configtest   read, validate and display configuration; then exit" );
464         puts( "  -V, --version      output version information and exit" );
465         puts( "  -h, --help         display this help and exit" );
466 } /* Show_Help */
467
468
469 /**
470  * Delete the file containing the process ID (PID).
471  */
472 static void
473 Pidfile_Delete( void )
474 {
475         /* Pidfile configured? */
476         if( ! Conf_PidFile[0] ) return;
477
478 #ifdef DEBUG
479         Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
480 #endif
481
482         if( unlink( Conf_PidFile ))
483                 Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
484 } /* Pidfile_Delete */
485
486
487 /**
488  * Create the file containing the process ID of ngIRCd ("PID file").
489  * @param pid The process ID to be stored in this file.
490  */
491 static void
492 Pidfile_Create(pid_t pid)
493 {
494         int pidfd;
495         char pidbuf[64];
496         int len;
497
498         /* Pidfile configured? */
499         if( ! Conf_PidFile[0] ) return;
500
501 #ifdef DEBUG
502         Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
503 #endif
504
505         pidfd = open( Conf_PidFile, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
506         if ( pidfd < 0 ) {
507                 Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
508                 return;
509         }
510
511         len = snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
512         if (len < 0 || len >= (int)sizeof pidbuf) {
513                 Log( LOG_ERR, "Error converting pid");
514                 return;
515         }
516         
517         if (write(pidfd, pidbuf, (size_t)len) != (ssize_t)len)
518                 Log( LOG_ERR, "Can't write PID file (%s): %s", Conf_PidFile, strerror( errno ));
519
520         if( close(pidfd) != 0 )
521                 Log( LOG_ERR, "Error closing PID file (%s): %s", Conf_PidFile, strerror( errno ));
522 } /* Pidfile_Create */
523
524
525 /**
526  * Redirect stdin, stdout and stderr to apropriate file handles.
527  */
528 static void
529 Setup_FDStreams(int fd)
530 {
531         if (fd < 0)
532                 return;
533
534         fflush(stdout);
535         fflush(stderr);
536
537         /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
538         dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
539 } /* Setup_FDStreams */
540
541
542 static bool
543 NGIRCd_getNobodyID(uid_t *uid, gid_t *gid )
544 {
545         struct passwd *pwd;
546
547 #ifdef __CYGWIN__
548         /* Cygwin kludge.
549          * It can return EINVAL instead of EPERM
550          * so, if we are already unprivileged,
551          * use id of current user.
552          */
553         if (geteuid() && getuid()) {
554                 *uid = getuid();
555                 *gid = getgid();
556                 return true;
557         }
558 #endif
559
560         pwd = getpwnam("nobody");
561         if (!pwd) return false;
562
563         if ( !pwd->pw_uid || !pwd->pw_gid)
564                 return false;
565
566         *uid = pwd->pw_uid;     
567         *gid = pwd->pw_gid;
568         endpwent();
569
570         return true;    
571 }
572
573
574 static bool
575 NGIRCd_Init( bool NGIRCd_NoDaemon ) 
576 {
577         static bool initialized;
578         bool chrooted = false;
579         struct passwd *pwd;
580         struct group *grp;
581         int real_errno, fd = -1;
582         pid_t pid;
583
584         if (initialized)
585                 return true;
586
587         if (!NGIRCd_NoDaemon) {
588                 /* open /dev/null before chroot() */
589                 fd = open( "/dev/null", O_RDWR);
590                 if (fd < 0)
591                         Log(LOG_WARNING, "Could not open /dev/null: %s", strerror(errno));
592         }
593
594         if (!ConnSSL_InitLibrary())
595                 Log(LOG_WARNING,
596                     "Warning: Error during SSL initialization, continuing ...");
597
598         if( Conf_Chroot[0] ) {
599                 if( chdir( Conf_Chroot ) != 0 ) {
600                         Log( LOG_ERR, "Can't chdir() in ChrootDir (%s): %s", Conf_Chroot, strerror( errno ));
601                         goto out;
602                 }
603
604                 if( chroot( Conf_Chroot ) != 0 ) {
605                         if (errno != EPERM) {
606                                 Log( LOG_ERR, "Can't change root directory to \"%s\": %s",
607                                                                 Conf_Chroot, strerror( errno ));
608                                 goto out;
609                         }
610                 } else {
611                         chrooted = true;
612                         Log( LOG_INFO, "Changed root and working directory to \"%s\".", Conf_Chroot );
613                 }
614         }
615
616         if (Conf_UID == 0) {
617                 Log(LOG_INFO, "ServerUID must not be 0, using \"nobody\" instead.", Conf_UID);
618
619                 if (! NGIRCd_getNobodyID(&Conf_UID, &Conf_GID)) {
620                         Log(LOG_WARNING, "Could not get user/group ID of user \"nobody\": %s",
621                                         errno ? strerror(errno) : "not found" );
622                         goto out;
623                 }
624         }
625
626         if (getgid() != Conf_GID) {
627                 /* Change group ID */
628                 if (setgid(Conf_GID) != 0) {
629                         real_errno = errno;
630                         Log( LOG_ERR, "Can't change group ID to %u: %s", Conf_GID, strerror( errno ));
631                         if (real_errno != EPERM) 
632                                 goto out;
633                 }
634         }
635
636         if (getuid() != Conf_UID) {
637                 /* Change user ID */
638                 if (setuid(Conf_UID) != 0) {
639                         real_errno = errno;
640                         Log(LOG_ERR, "Can't change user ID to %u: %s", Conf_UID, strerror(errno));
641                         if (real_errno != EPERM) 
642                                 goto out;
643                 }
644         }
645
646         initialized = true;
647
648         /* Normally a child process is forked which isn't any longer
649          * connected to ther controlling terminal. Use "--nodaemon"
650          * to disable this "daemon mode" (useful for debugging). */
651         if ( ! NGIRCd_NoDaemon ) {
652                 pid = fork( );
653                 if( pid > 0 ) {
654                         /* "Old" process: exit. */
655                         exit( 0 );
656                 }
657                 if( pid < 0 ) {
658                         /* Error!? */
659                         fprintf( stderr, "%s: Can't fork: %s!\nFatal error, exiting now ...\n",
660                                                                 PACKAGE_NAME, strerror( errno ));
661                         exit( 1 );
662                 }
663
664                 /* New child process */
665 #ifndef NeXT
666                 (void)setsid( );
667 #else
668                 setpgrp(0, getpid());
669 #endif
670                 if (chdir( "/" ) != 0)
671                         Log(LOG_ERR, "Can't change directory to '/': %s",
672                                      strerror(errno));
673
674                 /* Detach stdin, stdout and stderr */
675                 Setup_FDStreams(fd);
676                 if (fd > 2) {
677                         close(fd);
678                         fd = -1;
679                 }
680         }
681         pid = getpid();
682
683         Pidfile_Create( pid );
684
685         /* Check UID/GID we are running as, can be different from values
686          * configured (e. g. if we were already started with a UID>0. */
687         Conf_UID = getuid();
688         Conf_GID = getgid();
689
690         pwd = getpwuid( Conf_UID );
691         grp = getgrgid( Conf_GID );
692
693         Log(LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.",
694                                 pwd ? pwd->pw_name : "unknown", (long)Conf_UID,
695                                 grp ? grp->gr_name : "unknown", (long)Conf_GID, (long)pid);
696
697         if (chrooted) {
698                 Log(LOG_INFO, "Running with root directory \"%s\".",
699                     Conf_Chroot );
700                 return true;
701         } else
702                 Log(LOG_INFO, "Not running with changed root directory.");
703
704         /* Change working directory to home directory of the user
705          * we are running as (only when running in daemon mode and not in chroot) */
706
707         if (pwd) {
708                 if (!NGIRCd_NoDaemon ) {
709                         if( chdir( pwd->pw_dir ) == 0 ) 
710                                 Log( LOG_DEBUG, "Changed working directory to \"%s\" ...", pwd->pw_dir );
711                         else 
712                                 Log( LOG_INFO, "Notice: Can't change working directory to \"%s\": %s",
713                                                                 pwd->pw_dir, strerror( errno ));
714                 }
715         } else {
716                 Log( LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID );
717         }
718
719         return true;
720  out:
721         if (fd > 2)
722                 close(fd);
723         return false;
724 }
725
726 /* -eof- */