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