]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/ngircd.c
Whitespace fixes.
[ngircd-alex.git] / src / ngircd / ngircd.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2005 by 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  * Main program -- main()
12  */
13
14
15 #include "portab.h"
16
17 static char UNUSED id[] = "$Id: ngircd.c,v 1.94 2005/02/11 13:52:37 alex Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <signal.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <time.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/wait.h>
31 #include <fcntl.h>
32 #include <pwd.h>
33 #include <grp.h>
34
35 #include "defines.h"
36 #include "resolve.h"
37 #include "conn.h"
38 #include "client.h"
39 #include "channel.h"
40 #include "conf.h"
41 #include "cvs-version.h"
42 #include "lists.h"
43 #include "log.h"
44 #include "parse.h"
45 #include "irc.h"
46
47 #ifdef RENDEZVOUS
48 #include "rendezvous.h"
49 #endif
50
51 #include "exp.h"
52 #include "ngircd.h"
53
54
55 LOCAL VOID Initialize_Signal_Handler PARAMS(( VOID ));
56 LOCAL VOID Signal_Handler PARAMS(( INT Signal ));
57
58 LOCAL VOID Show_Version PARAMS(( VOID ));
59 LOCAL VOID Show_Help PARAMS(( VOID ));
60
61 LOCAL VOID Pidfile_Create PARAMS(( LONG ));
62 LOCAL VOID Pidfile_Delete PARAMS(( VOID ));
63
64 LOCAL VOID Fill_Version PARAMS(( VOID ));
65
66 LOCAL VOID Setup_FDStreams PARAMS(( VOID ));
67
68
69 GLOBAL int
70 main( int argc, const char *argv[] )
71 {
72         struct passwd *pwd;
73         struct group *grp;
74         BOOLEAN ok, configtest = FALSE;
75         LONG pid, n;
76         INT i;
77
78         umask( 0077 );
79
80         NGIRCd_SignalQuit = NGIRCd_SignalRestart = NGIRCd_SignalRehash = FALSE;
81         NGIRCd_NoDaemon = NGIRCd_Passive = FALSE;
82 #ifdef DEBUG
83         NGIRCd_Debug = FALSE;
84 #endif
85 #ifdef SNIFFER
86         NGIRCd_Sniffer = FALSE;
87 #endif
88         strlcpy( NGIRCd_ConfFile, SYSCONFDIR, sizeof( NGIRCd_ConfFile ));
89         strlcat( NGIRCd_ConfFile, CONFIG_FILE, sizeof( NGIRCd_ConfFile ));
90
91         Fill_Version( );
92
93         /* Kommandozeile parsen */
94         for( i = 1; i < argc; i++ )
95         {
96                 ok = FALSE;
97                 if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
98                 {
99                         /* Lange Option */
100
101                         if( strcmp( argv[i], "--config" ) == 0 )
102                         {
103                                 if( i + 1 < argc )
104                                 {
105                                         /* Ok, there's an parameter left */
106                                         strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
107
108                                         /* next parameter */
109                                         i++; ok = TRUE;
110                                 }
111                         }
112                         if( strcmp( argv[i], "--configtest" ) == 0 )
113                         {
114                                 configtest = TRUE;
115                                 ok = TRUE;
116                         }
117 #ifdef DEBUG
118                         if( strcmp( argv[i], "--debug" ) == 0 )
119                         {
120                                 NGIRCd_Debug = TRUE;
121                                 ok = TRUE;
122                         }
123 #endif
124                         if( strcmp( argv[i], "--help" ) == 0 )
125                         {
126                                 Show_Version( );
127                                 puts( "" ); Show_Help( ); puts( "" );
128                                 exit( 1 );
129                         }
130                         if( strcmp( argv[i], "--nodaemon" ) == 0 )
131                         {
132                                 NGIRCd_NoDaemon = TRUE;
133                                 ok = TRUE;
134                         }
135                         if( strcmp( argv[i], "--passive" ) == 0 )
136                         {
137                                 NGIRCd_Passive = TRUE;
138                                 ok = TRUE;
139                         }
140 #ifdef SNIFFER
141                         if( strcmp( argv[i], "--sniffer" ) == 0 )
142                         {
143                                 NGIRCd_Sniffer = TRUE;
144                                 ok = TRUE;
145                         }
146 #endif
147                         if( strcmp( argv[i], "--version" ) == 0 )
148                         {
149                                 Show_Version( );
150                                 exit( 1 );
151                         }
152                 }
153                 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
154                 {
155                         /* Kurze Option */
156                         
157                         for( n = 1; n < (LONG)strlen( argv[i] ); n++ )
158                         {
159                                 ok = FALSE;
160 #ifdef DEBUG
161                                 if( argv[i][n] == 'd' )
162                                 {
163                                         NGIRCd_Debug = TRUE;
164                                         ok = TRUE;
165                                 }
166 #endif
167                                 if( argv[i][n] == 'f' )
168                                 {
169                                         if(( ! argv[i][n + 1] ) && ( i + 1 < argc ))
170                                         {
171                                                 /* Ok, next character is a blank */
172                                                 strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
173
174                                                 /* go to the following parameter */
175                                                 i++; n = (LONG)strlen( argv[i] );
176                                                 ok = TRUE;
177                                         }
178                                 }
179                                 if( argv[i][n] == 'n' )
180                                 {
181                                         NGIRCd_NoDaemon = TRUE;
182                                         ok = TRUE;
183                                 }
184                                 if( argv[i][n] == 'p' )
185                                 {
186                                         NGIRCd_Passive = TRUE;
187                                         ok = TRUE;
188                                 }
189 #ifdef SNIFFER
190                                 if( argv[i][n] == 's' )
191                                 {
192                                         NGIRCd_Sniffer = TRUE;
193                                         ok = TRUE;
194                                 }
195 #endif
196                                 if( argv[i][n] == 't' )
197                                 {
198                                         configtest = TRUE;
199                                         ok = TRUE;
200                                 }
201
202                                 if( ! ok )
203                                 {
204                                         printf( "%s: invalid option \"-%c\"!\n", PACKAGE_NAME, argv[i][n] );
205                                         printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
206                                         exit( 1 );
207                                 }
208                         }
209
210                 }
211                 if( ! ok )
212                 {
213                         printf( "%s: invalid option \"%s\"!\n", PACKAGE_NAME, argv[i] );
214                         printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
215                         exit( 1 );
216                 }
217         }
218
219         /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
220         NGIRCd_DebugLevel[0] = '\0';
221 #ifdef DEBUG
222         if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
223 #endif
224 #ifdef SNIFFER
225         if( NGIRCd_Sniffer )
226         {
227                 NGIRCd_Debug = TRUE;
228                 strcpy( NGIRCd_DebugLevel, "2" );
229         }
230 #endif
231
232         /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
233         if( configtest )
234         {
235                 Show_Version( ); puts( "" );
236                 exit( Conf_Test( ));
237         }
238         
239         while( ! NGIRCd_SignalQuit )
240         {
241                 /* Initialize global variables */
242                 NGIRCd_Start = time( NULL );
243                 (VOID)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
244
245                 NGIRCd_SignalRehash = FALSE;
246                 NGIRCd_SignalRestart = FALSE;
247                 NGIRCd_SignalQuit = FALSE;
248
249                 /* Initialize modules, part I */
250                 Log_Init( );
251                 Conf_Init( );
252
253                 if( Conf_Chroot[0] )
254                 {
255                         /* Chroot */
256                         if( chdir( Conf_Chroot ) != 0 ) Log( LOG_ERR, "Can't chdir() in ChrootDir (%s): %s", Conf_Chroot, strerror( errno ));
257
258                         if( chroot( Conf_Chroot ) != 0 ) Log( LOG_ERR, "Can't change root directory to \"%s\": %s", Conf_Chroot, strerror( errno ));
259                         else Log( LOG_INFO, "Changed root and working directory to \"%s\".", Conf_Chroot );
260                 }
261
262                 if( Conf_GID != 0 )
263                 {
264                         /* Set new group ID */
265                         if( setgid( Conf_GID ) != 0 ) Log( LOG_ERR, "Can't change group ID to %u: %s", Conf_GID, strerror( errno ));
266                 }
267                 if( Conf_UID != 0 )
268                 {
269                         /* Set new user ID */
270                         if( setuid( Conf_UID ) != 0 ) Log( LOG_ERR, "Can't change user ID to %u: %s", Conf_UID, strerror( errno ));
271                 }
272
273                 /* Normally a child process is forked which isn't any longer
274                  * connected to ther controlling terminal. Use "--nodaemon"
275                  * to disable this "daemon mode" (useful for debugging). */
276                 if( ! NGIRCd_NoDaemon )
277                 {
278                         /* fork child process */
279                         pid = (LONG)fork( );
280                         if( pid > 0 )
281                         {
282                                 /* "Old" process: exit. */
283                                 exit( 0 );
284                         }
285                         if( pid < 0 )
286                         {
287                                 /* Error!? */
288                                 fprintf( stderr, "%s: Can't fork: %s!\nFatal error, exiting now ...\n", PACKAGE_NAME, strerror( errno ));
289                                 exit( 1 );
290                         }
291
292                         /* New child process */
293                         (VOID)setsid( );
294                         chdir( "/" );
295
296                         /* Detach stdin, stdout and stderr */
297                         (VOID)Setup_FDStreams( );
298                 }
299
300                 /* Create PID file */
301                 pid = (LONG) getpid( );
302                 Pidfile_Create( pid );
303
304                 /* Show user, group, and PID of the running daemon */
305                 pwd = getpwuid( getuid( )); grp = getgrgid( getgid( ));
306                 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);
307
308                 /* Change working directory to home directory of the user
309                  * we are running as (when not running chroot()'ed!) */
310                 if( Conf_UID != 0 && ! Conf_Chroot[0] )
311                 {
312                         struct passwd *pwd;
313
314                         pwd = getpwuid( Conf_UID );
315                         if( pwd != NULL )
316                         {
317                                 if( chdir( pwd->pw_dir ) == 0 ) Log( LOG_DEBUG, "Changed working directory to \"%s\" ...", pwd->pw_dir );
318                                 else Log( LOG_ERR, "Can't change working directory to \"%s\": %s", pwd->pw_dir, strerror( errno ));
319                         }
320                         else Log( LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID );
321                 }
322
323                 /* Initialize modules, part II: these functions are eventually
324                  * called with already dropped privileges ... */
325                 Resolve_Init( );
326                 Lists_Init( );
327                 Channel_Init( );
328                 Client_Init( );
329 #ifdef RENDEZVOUS
330                 Rendezvous_Init( );
331 #endif
332                 Conn_Init( );
333
334 #ifdef DEBUG
335                 /* Redirect stderr handle to "error file" for debugging
336                  * when not running in "no daemon" mode: */
337                 if( ! NGIRCd_NoDaemon ) Log_InitErrorfile( );
338 #endif
339
340                 /* Signal-Handler initialisieren */
341                 Initialize_Signal_Handler( );
342
343                 /* Protokoll- und Server-Identifikation erzeugen. Die vom ngIRCd
344                  * beim PASS-Befehl verwendete Syntax sowie die erweiterten Flags
345                  * sind in doc/Protocol.txt beschrieben. */
346 #ifdef IRCPLUS
347                 sprintf( NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION, IRCPLUSFLAGS );
348 #ifdef ZLIB
349                 strcat( NGIRCd_ProtoID, "Z" );
350 #endif
351                 if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
352 #else
353                 sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION );
354 #endif
355                 strcat( NGIRCd_ProtoID, " P" );
356 #ifdef ZLIB
357                 strcat( NGIRCd_ProtoID, "Z" );
358 #endif
359                 Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
360
361                 /* Vordefinierte Channels anlegen */
362                 Channel_InitPredefined( );
363
364                 /* Listen-Ports initialisieren */
365                 if( Conn_InitListeners( ) < 1 )
366                 {
367                         Log( LOG_ALERT, "Server isn't listening on a single port!" );
368                         Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
369                         Pidfile_Delete( );
370                         exit( 1 );
371                 }
372                 
373                 /* Hauptschleife */
374                 Conn_Handler( );
375
376                 /* Alles abmelden */
377                 Conn_Exit( );
378 #ifdef RENDEZVOUS
379                 Rendezvous_Exit( );
380 #endif
381                 Client_Exit( );
382                 Channel_Exit( );
383                 Lists_Exit( );
384                 Log_Exit( );
385
386                 Pidfile_Delete( );
387         }
388
389         return 0;
390 } /* main */
391
392
393 LOCAL VOID
394 Fill_Version( VOID )
395 {
396         NGIRCd_VersionAddition[0] = '\0';
397
398 #ifdef SYSLOG
399         strcpy( NGIRCd_VersionAddition, "SYSLOG" );
400 #endif
401 #ifdef ZLIB
402         if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
403         strcat( NGIRCd_VersionAddition, "ZLIB" );
404 #endif
405 #ifdef TCPWRAP
406         if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
407         strcat( NGIRCd_VersionAddition, "TCPWRAP" );
408 #endif
409 #ifdef RENDEZVOUS
410         if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
411         strcat( NGIRCd_VersionAddition, "RENDEZVOUS" );
412 #endif
413 #ifdef IDENTAUTH
414         if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
415         strcat( NGIRCd_VersionAddition, "IDENT" );
416 #endif
417 #ifdef DEBUG
418         if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
419         strcat( NGIRCd_VersionAddition, "DEBUG" );
420 #endif
421 #ifdef SNIFFER
422         if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
423         strcat( NGIRCd_VersionAddition, "SNIFFER" );
424 #endif
425 #ifdef STRICT_RFC
426         if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
427         strcat( NGIRCd_VersionAddition, "RFC" );
428 #endif
429 #ifdef IRCPLUS
430         if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
431         strcat( NGIRCd_VersionAddition, "IRCPLUS" );
432 #endif
433
434         if( NGIRCd_VersionAddition[0] ) strlcat( NGIRCd_VersionAddition, "-", sizeof( NGIRCd_VersionAddition ));
435         strlcat( NGIRCd_VersionAddition, TARGET_CPU, sizeof( NGIRCd_VersionAddition ));
436         strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
437         strlcat( NGIRCd_VersionAddition, TARGET_VENDOR, sizeof( NGIRCd_VersionAddition ));
438         strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
439         strlcat( NGIRCd_VersionAddition, TARGET_OS, sizeof( NGIRCd_VersionAddition ));
440
441 #ifdef CVSDATE
442         snprintf( NGIRCd_Version, sizeof NGIRCd_Version,"%s %s(%s)-%s", PACKAGE_NAME, PACKAGE_VERSION, CVSDATE, NGIRCd_VersionAddition);
443 #else
444         snprintf( NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s", PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
445 #endif
446 } /* Fill_Version */
447
448
449 GLOBAL VOID
450 NGIRCd_Rehash( VOID )
451 {
452         CHAR old_name[CLIENT_ID_LEN];
453
454         Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
455         NGIRCd_SignalRehash = FALSE;
456
457         /* Close down all listening sockets */
458         Conn_ExitListeners( );
459
460         /* Remember old server name */
461         strcpy( old_name, Conf_ServerName );
462
463         /* Re-read configuration ... */
464         Conf_Rehash( );
465
466         /* Recover old server name: it can't be changed during run-time */
467         if( strcmp( old_name, Conf_ServerName ) != 0 )
468         {
469                 strcpy( Conf_ServerName, old_name );
470                 Log( LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name." );
471         }
472
473         /* Create new pre-defined channels */
474         Channel_InitPredefined( );
475         
476         /* Start listening on sockets */
477         Conn_InitListeners( );
478
479         /* Sync configuration with established connections */
480         Conn_SyncServerStruct( );
481
482         Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
483 } /* NGIRCd_Rehash */
484
485
486 LOCAL VOID
487 Initialize_Signal_Handler( VOID )
488 {
489         /* Signal-Handler initialisieren: einige Signale
490          * werden ignoriert, andere speziell behandelt. */
491
492 #ifdef HAVE_SIGACTION
493         /* sigaction() ist vorhanden */
494
495         struct sigaction saction;
496
497         /* Signal-Struktur initialisieren */
498         memset( &saction, 0, sizeof( saction ));
499         saction.sa_handler = Signal_Handler;
500 #ifdef SA_RESTART
501         saction.sa_flags |= SA_RESTART;
502 #endif
503 #ifdef SA_NOCLDWAIT
504         saction.sa_flags |= SA_NOCLDWAIT;
505 #endif
506
507         /* Signal-Handler einhaengen */
508         sigaction( SIGINT, &saction, NULL );
509         sigaction( SIGQUIT, &saction, NULL );
510         sigaction( SIGTERM, &saction, NULL);
511         sigaction( SIGHUP, &saction, NULL);
512         sigaction( SIGCHLD, &saction, NULL);
513
514         /* einige Signale ignorieren */
515         saction.sa_handler = SIG_IGN;
516         sigaction( SIGPIPE, &saction, NULL );
517 #else
518         /* kein sigaction() vorhanden */
519
520         /* Signal-Handler einhaengen */
521         signal( SIGINT, Signal_Handler );
522         signal( SIGQUIT, Signal_Handler );
523         signal( SIGTERM, Signal_Handler );
524         signal( SIGHUP, Signal_Handler );
525         signal( SIGCHLD, Signal_Handler );
526
527         /* einige Signale ignorieren */
528         signal( SIGPIPE, SIG_IGN );
529 #endif
530 } /* Initialize_Signal_Handler */
531
532
533 LOCAL VOID
534 Signal_Handler( INT Signal )
535 {
536         /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
537          * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
538          * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
539
540         switch( Signal )
541         {
542                 case SIGTERM:
543                 case SIGINT:
544                 case SIGQUIT:
545                         /* wir soll(t)en uns wohl beenden ... */
546                         NGIRCd_SignalQuit = TRUE;
547                         break;
548                 case SIGHUP:
549                         /* Konfiguration neu einlesen: */
550                         NGIRCd_SignalRehash = TRUE;
551                         break;
552                 case SIGCHLD:
553                         /* Child-Prozess wurde beendet. Zombies vermeiden: */
554                         while( waitpid( -1, NULL, WNOHANG ) > 0);
555                         break;
556 #ifdef DEBUG
557                 default:
558                         /* unbekanntes bzw. unbehandeltes Signal */
559                         Log( LOG_DEBUG, "Got signal %d! Ignored.", Signal );
560 #endif
561         }
562 } /* Signal_Handler */
563
564
565 LOCAL VOID
566 Show_Version( VOID )
567 {
568         puts( NGIRCd_Version );
569         puts( "Copyright (c)2001-2005 by Alexander Barton (<alex@barton.de>)." );
570         puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
571         puts( "This is free software; see the source for copying conditions. There is NO" );
572         puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
573 } /* Show_Version */
574
575
576 LOCAL VOID
577 Show_Help( VOID )
578 {
579 #ifdef DEBUG
580         puts( "  -d, --debug        log extra debug messages" );
581 #endif
582         puts( "  -f, --config <f>   use file <f> as configuration file" );
583         puts( "  -n, --nodaemon     don't fork and don't detach from controlling terminal" );
584         puts( "  -p, --passive      disable automatic connections to other servers" );
585 #ifdef SNIFFER
586         puts( "  -s, --sniffer      enable network sniffer and display all IRC traffic" );
587 #endif
588         puts( "  -t, --configtest   read, validate and display configuration; then exit" );
589         puts( "      --version      output version information and exit" );
590         puts( "      --help         display this help and exit" );
591 } /* Show_Help */
592
593
594 LOCAL VOID
595 Pidfile_Delete( VOID )
596 {
597         /* Pidfile configured? */
598         if( ! Conf_PidFile[0] ) return;
599
600 #ifdef DEBUG
601         Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
602 #endif
603
604         if( unlink( Conf_PidFile ))
605                 Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
606 } /* Pidfile_Delete */
607
608
609 LOCAL VOID
610 Pidfile_Create( LONG pid )
611 {
612         FILE *pidf;
613
614         /* Pidfile configured? */
615         if( ! Conf_PidFile[0] ) return;
616
617 #ifdef DEBUG
618         Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
619 #endif
620
621         pidf = fopen( Conf_PidFile, "w" );
622
623         if( ! pidf )
624         {
625                 Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
626                 return;
627         }
628
629         if( fprintf( pidf, "%ld\n", pid ) < 0 )
630                 Log( LOG_ERR, "Can't write PID file (%s): %s", Conf_PidFile, strerror( errno ));
631
632         if( fclose(pidf) != 0 )
633                 Log( LOG_ERR, "Error closing PID file (%s): %s", Conf_PidFile, strerror( errno ));
634 } /* Pidfile_Create */
635
636
637 LOCAL VOID
638 Setup_FDStreams( VOID )
639 {
640         INT fd;
641
642         /* Test if we can open /dev/null for reading and writing. If not
643          * we are most probably chrooted already and the server has been
644          * restarted. So we simply don't try to redirect stdXXX ... */
645         fd = open( "/dev/null", O_RDWR );
646         if ( fd < 0 ) return;
647
648         /* Close "old" stdin/out/err descriptors */
649         close( 0 ); close( 1 ); close( 2 );
650
651         /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
652         dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
653
654         /* Close newly opened file descriptor if not stdin/out/err */
655         if( fd > 2 ) close( fd );
656 } /* Setup_FDStreams */
657
658
659 /* -eof- */