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