X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fngircd%2Fngircd.c;h=e39229a06d97e252c61515d74fce8c3e16abeb6f;hb=9537542647b014894227de81a59fb063fc0dc3e4;hp=37cc97fc5d97fe71809f292cd06af1c3393efc59;hpb=e311cc0140b2edff3927ce9b2117d328735445ec;p=ngircd-alex.git diff --git a/src/ngircd/ngircd.c b/src/ngircd/ngircd.c index 37cc97fc..e39229a0 100644 --- a/src/ngircd/ngircd.c +++ b/src/ngircd/ngircd.c @@ -1,22 +1,21 @@ /* * ngIRCd -- The Next Generation IRC Daemon - * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de) + * Copyright (c)2001-2005 by Alexander Barton (alex@barton.de) * - * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen - * der GNU General Public License (GPL), wie von der Free Software Foundation - * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2 - * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version. - * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste - * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS. + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * Please read the file COPYING, README and AUTHORS for more information. * - * $Id: ngircd.c,v 1.60 2002/11/22 17:59:43 alex Exp $ - * - * ngircd.c: Hier beginnt alles ;-) + * Main program -- main() */ #include "portab.h" +static char UNUSED id[] = "$Id: ngircd.c,v 1.91 2005/02/10 12:49:04 alex Exp $"; + #include "imp.h" #include #include @@ -25,25 +24,30 @@ #include #include #include +#include #include #include #include -#include +#include #include #include +#include "defines.h" #include "resolve.h" #include "conn.h" #include "client.h" #include "channel.h" #include "conf.h" #include "cvs-version.h" -#include "defines.h" #include "lists.h" #include "log.h" #include "parse.h" #include "irc.h" +#ifdef RENDEZVOUS +#include "rendezvous.h" +#endif + #include "exp.h" #include "ngircd.h" @@ -54,6 +58,13 @@ LOCAL VOID Signal_Handler PARAMS(( INT Signal )); LOCAL VOID Show_Version PARAMS(( VOID )); LOCAL VOID Show_Help PARAMS(( VOID )); +LOCAL VOID Pidfile_Create PARAMS(( LONG )); +LOCAL VOID Pidfile_Delete PARAMS(( VOID )); + +LOCAL VOID NGIRCd_FillVersion PARAMS(( VOID )); + +LOCAL VOID Setup_FDStreams PARAMS(( VOID )); + GLOBAL int main( int argc, const char *argv[] ) @@ -66,17 +77,18 @@ main( int argc, const char *argv[] ) umask( 0077 ); - NGIRCd_Restart = FALSE; - NGIRCd_Quit = FALSE; - NGIRCd_NoDaemon = FALSE; - NGIRCd_Passive = FALSE; + NGIRCd_SignalQuit = NGIRCd_SignalRestart = NGIRCd_SignalRehash = FALSE; + NGIRCd_NoDaemon = NGIRCd_Passive = FALSE; #ifdef DEBUG NGIRCd_Debug = FALSE; #endif #ifdef SNIFFER NGIRCd_Sniffer = FALSE; #endif - strcpy( NGIRCd_ConfFile, CONFIG_FILE ); + strlcpy( NGIRCd_ConfFile, SYSCONFDIR, sizeof( NGIRCd_ConfFile )); + strlcat( NGIRCd_ConfFile, CONFIG_FILE, sizeof( NGIRCd_ConfFile )); + + NGIRCd_FillVersion( ); /* Kommandozeile parsen */ for( i = 1; i < argc; i++ ) @@ -90,11 +102,10 @@ main( int argc, const char *argv[] ) { if( i + 1 < argc ) { - /* Ok, danach kommt noch ein Parameter */ - strncpy( NGIRCd_ConfFile, argv[i + 1], FNAME_LEN - 1 ); - NGIRCd_ConfFile[FNAME_LEN - 1] = '\0'; + /* Ok, there's an parameter left */ + strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile )); - /* zum uebernaechsten Parameter */ + /* next parameter */ i++; ok = TRUE; } } @@ -157,11 +168,10 @@ main( int argc, const char *argv[] ) { if(( ! argv[i][n + 1] ) && ( i + 1 < argc )) { - /* Ok, danach kommt ein Leerzeichen */ - strncpy( NGIRCd_ConfFile, argv[i + 1], FNAME_LEN - 1 ); - NGIRCd_ConfFile[FNAME_LEN - 1] = '\0'; + /* Ok, next character is a blank */ + strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile )); - /* zum uebernaechsten Parameter */ + /* go to the following parameter */ i++; n = (LONG)strlen( argv[i] ); ok = TRUE; } @@ -183,11 +193,16 @@ main( int argc, const char *argv[] ) ok = TRUE; } #endif + if( argv[i][n] == 't' ) + { + configtest = TRUE; + ok = TRUE; + } if( ! ok ) { - printf( "%s: invalid option \"-%c\"!\n", PACKAGE, argv[i][n] ); - printf( "Try \"%s --help\" for more information.\n", PACKAGE ); + printf( "%s: invalid option \"-%c\"!\n", PACKAGE_NAME, argv[i][n] ); + printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME ); exit( 1 ); } } @@ -195,14 +210,14 @@ main( int argc, const char *argv[] ) } if( ! ok ) { - printf( "%s: invalid option \"%s\"!\n", PACKAGE, argv[i] ); - printf( "Try \"%s --help\" for more information.\n", PACKAGE ); + printf( "%s: invalid option \"%s\"!\n", PACKAGE_NAME, argv[i] ); + printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME ); exit( 1 ); } } /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */ - strcpy( NGIRCd_DebugLevel, "" ); + NGIRCd_DebugLevel[0] = '\0'; #ifdef DEBUG if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" ); #endif @@ -221,70 +236,106 @@ main( int argc, const char *argv[] ) exit( Conf_Test( )); } - while( ! NGIRCd_Quit ) + while( ! NGIRCd_SignalQuit ) { - /* In der Regel wird ein Sub-Prozess ge-fork()'t, der - * nicht mehr mit dem Terminal verbunden ist. Mit der - * Option "--nodaemon" kann dies (z.B. zum Debuggen) - * verhindert werden. */ + /* Initialize global variables */ + NGIRCd_Start = time( NULL ); + (VOID)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start )); + + NGIRCd_SignalRehash = FALSE; + NGIRCd_SignalRestart = FALSE; + NGIRCd_SignalQuit = FALSE; + + /* Initialize modules, part I */ + Log_Init( ); + Conf_Init( ); + + if( Conf_Chroot[0] ) + { + /* Chroot */ + if( chdir( Conf_Chroot ) != 0 ) Log( LOG_ERR, "Can't chdir() in ChrootDir (%s): %s", Conf_Chroot, strerror( errno )); + + if( chroot( Conf_Chroot ) != 0 ) Log( LOG_ERR, "Can't change root directory to \"%s\": %s", Conf_Chroot, strerror( errno )); + else Log( LOG_INFO, "Changed root and working directory to \"%s\".", Conf_Chroot ); + } + + if( Conf_GID != 0 ) + { + /* Set new group ID */ + if( setgid( Conf_GID ) != 0 ) Log( LOG_ERR, "Can't change group ID to %u: %s", Conf_GID, strerror( errno )); + } + if( Conf_UID != 0 ) + { + /* Set new user ID */ + if( setuid( Conf_UID ) != 0 ) Log( LOG_ERR, "Can't change user ID to %u: %s", Conf_UID, strerror( errno )); + } + + /* Normally a child process is forked which isn't any longer + * connected to ther controlling terminal. Use "--nodaemon" + * to disable this "daemon mode" (useful for debugging). */ if( ! NGIRCd_NoDaemon ) { - /* Daemon im Hintergrund erzeugen */ + /* fork child process */ pid = (LONG)fork( ); if( pid > 0 ) { - /* "alter" Prozess */ + /* "Old" process: exit. */ exit( 0 ); } if( pid < 0 ) { - /* Fehler */ - printf( "%s: Can't fork: %s!\nFatal error, exiting now ...\n", PACKAGE, strerror( errno )); + /* Error!? */ + fprintf( stderr, "%s: Can't fork: %s!\nFatal error, exiting now ...\n", PACKAGE_NAME, strerror( errno )); exit( 1 ); } - /* Child-Prozess initialisieren */ + /* New child process */ (VOID)setsid( ); chdir( "/" ); + + /* Detach stdin, stdout and stderr */ + (VOID)Setup_FDStreams( ); } - - /* Globale Variablen initialisieren */ - NGIRCd_Start = time( NULL ); - (VOID)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start )); - NGIRCd_Restart = FALSE; - NGIRCd_Quit = FALSE; - /* Module initialisieren */ - Log_Init( ); - Resolve_Init( ); - Conf_Init( ); - Lists_Init( ); - Channel_Init( ); - Client_Init( ); - Conn_Init( ); + /* Create PID file */ + pid = (LONG) getpid( ); + Pidfile_Create( pid ); - /* Wenn als root ausgefuehrt und eine andere UID - * konfiguriert ist, jetzt zu dieser wechseln */ - if( getuid( ) == 0 ) + /* Show user, group, and PID of the running daemon */ + pwd = getpwuid( getuid( )); grp = getgrgid( getgid( )); + 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); + + /* Change working directory to home directory of the user + * we are running as (when not running chroot()'ed!) */ + if( Conf_UID != 0 && ! Conf_Chroot[0] ) { - if( Conf_GID != 0 ) - { - /* Neue Group-ID setzen */ - if( setgid( Conf_GID ) != 0 ) Log( LOG_ERR, "Can't change Group-ID to %u: %s", Conf_GID, strerror( errno )); - } - if( Conf_UID != 0 ) + struct passwd *pwd; + + pwd = getpwuid( Conf_UID ); + if( pwd != NULL ) { - /* Neue User-ID setzen */ - if( setuid( Conf_UID ) != 0 ) Log( LOG_ERR, "Can't change User-ID to %u: %s", Conf_UID, strerror( errno )); + if( chdir( pwd->pw_dir ) == 0 ) Log( LOG_DEBUG, "Changed working directory to \"%s\" ...", pwd->pw_dir ); + else Log( LOG_ERR, "Can't change working directory to \"%s\": %s", pwd->pw_dir, strerror( errno )); } + else Log( LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID ); } - - /* User, Gruppe und Prozess-ID des Daemon ausgeben */ - pwd = getpwuid( getuid( )); grp = getgrgid( getgid( )); - 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( ), (LONG)getpid( )); - /* stderr in "Error-File" umlenken */ - Log_InitErrorfile( ); + /* Initialize modules, part II: these functions are eventually + * called with already dropped privileges ... */ + Resolve_Init( ); + Lists_Init( ); + Channel_Init( ); + Client_Init( ); +#ifdef RENDEZVOUS + Rendezvous_Init( ); +#endif + Conn_Init( ); + +#ifdef DEBUG + /* Redirect stderr handle to "error file" for debugging + * when not running in "no daemon" mode: */ + if( ! NGIRCd_NoDaemon ) Log_InitErrorfile( ); +#endif /* Signal-Handler initialisieren */ Initialize_Signal_Handler( ); @@ -293,12 +344,18 @@ main( int argc, const char *argv[] ) * beim PASS-Befehl verwendete Syntax sowie die erweiterten Flags * sind in doc/Protocol.txt beschrieben. */ #ifdef IRCPLUS - sprintf( NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE, VERSION, IRCPLUSFLAGS ); + sprintf( NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION, IRCPLUSFLAGS ); +#ifdef ZLIB + strcat( NGIRCd_ProtoID, "Z" ); +#endif if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" ); #else - sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE, VERSION ); + sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION ); #endif strcat( NGIRCd_ProtoID, " P" ); +#ifdef ZLIB + strcat( NGIRCd_ProtoID, "Z" ); +#endif Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID ); /* Vordefinierte Channels anlegen */ @@ -308,7 +365,8 @@ main( int argc, const char *argv[] ) if( Conn_InitListeners( ) < 1 ) { Log( LOG_ALERT, "Server isn't listening on a single port!" ); - Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE ); + Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME ); + Pidfile_Delete( ); exit( 1 ); } @@ -317,99 +375,112 @@ main( int argc, const char *argv[] ) /* Alles abmelden */ Conn_Exit( ); +#ifdef RENDEZVOUS + Rendezvous_Exit( ); +#endif Client_Exit( ); Channel_Exit( ); Lists_Exit( ); Log_Exit( ); + + Pidfile_Delete( ); } return 0; } /* main */ -GLOBAL CHAR * -NGIRCd_Version( VOID ) -{ - STATIC CHAR version[126]; - -#ifdef CVSDATE - sprintf( version, "%s %s(%s)-%s", PACKAGE, VERSION, CVSDATE, NGIRCd_VersionAddition( )); -#else - sprintf( version, "%s %s-%s", PACKAGE, VERSION, NGIRCd_VersionAddition( )); -#endif - return version; -} /* NGIRCd_Version */ - - -GLOBAL CHAR * -NGIRCd_VersionAddition( VOID ) +LOCAL VOID +NGIRCd_FillVersion( VOID ) { - STATIC CHAR txt[64]; - - strcpy( txt, "" ); + NGIRCd_VersionAddition[0] = '\0'; -#ifdef USE_SYSLOG - if( txt[0] ) strcat( txt, "+" ); - strcat( txt, "SYSLOG" ); +#ifdef SYSLOG + strcpy( NGIRCd_VersionAddition, "SYSLOG" ); +#endif +#ifdef ZLIB + if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" ); + strcat( NGIRCd_VersionAddition, "ZLIB" ); +#endif +#ifdef TCPWRAP + if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" ); + strcat( NGIRCd_VersionAddition, "TCPWRAP" ); +#endif +#ifdef RENDEZVOUS + if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" ); + strcat( NGIRCd_VersionAddition, "RENDEZVOUS" ); +#endif +#ifdef IDENTAUTH + if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" ); + strcat( NGIRCd_VersionAddition, "IDENT" ); #endif #ifdef DEBUG - if( txt[0] ) strcat( txt, "+" ); - strcat( txt, "DEBUG" ); + if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" ); + strcat( NGIRCd_VersionAddition, "DEBUG" ); #endif #ifdef SNIFFER - if( txt[0] ) strcat( txt, "+" ); - strcat( txt, "SNIFFER" ); + if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" ); + strcat( NGIRCd_VersionAddition, "SNIFFER" ); #endif #ifdef STRICT_RFC - if( txt[0] ) strcat( txt, "+" ); - strcat( txt, "RFC" ); + if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" ); + strcat( NGIRCd_VersionAddition, "RFC" ); #endif #ifdef IRCPLUS - if( txt[0] ) strcat( txt, "+" ); - strcat( txt, "IRCPLUS" ); + if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" ); + strcat( NGIRCd_VersionAddition, "IRCPLUS" ); #endif - - if( txt[0] ) strcat( txt, "-" ); - strcat( txt, TARGET_CPU ); - strcat( txt, "/" ); - strcat( txt, TARGET_VENDOR ); - strcat( txt, "/" ); - strcat( txt, TARGET_OS ); - return txt; -} /* NGIRCd_VersionAddition */ + if( NGIRCd_VersionAddition[0] ) strlcat( NGIRCd_VersionAddition, "-", sizeof( NGIRCd_VersionAddition )); + strlcat( NGIRCd_VersionAddition, TARGET_CPU, sizeof( NGIRCd_VersionAddition )); + strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition )); + strlcat( NGIRCd_VersionAddition, TARGET_VENDOR, sizeof( NGIRCd_VersionAddition )); + strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition )); + strlcat( NGIRCd_VersionAddition, TARGET_OS, sizeof( NGIRCd_VersionAddition )); + +#ifdef CVSDATE + snprintf( NGIRCd_Version, sizeof NGIRCd_Version,"%s %s(%s)-%s", PACKAGE_NAME, PACKAGE_VERSION, CVSDATE, NGIRCd_VersionAddition); +#else + snprintf( NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s", PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition); +#endif +} /* NGIRCd_FillVersion */ GLOBAL VOID -NGIRCd_Reload( VOID ) +NGIRCd_Rehash( VOID ) { CHAR old_name[CLIENT_ID_LEN]; - - /* Alle Listen-Sockets schliessen */ + + Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" ); + NGIRCd_SignalRehash = FALSE; + + /* Close down all listening sockets */ Conn_ExitListeners( ); - /* Alten Server-Namen merken */ + /* Remember old server name */ strcpy( old_name, Conf_ServerName ); - /* Konfiguration neu lesen ... */ - Conf_Init( ); + /* Re-read configuration ... */ + Conf_Rehash( ); - /* Alten Server-Namen wiederherstellen: dieser - * kann nicht zur Laufzeit geaendert werden ... */ + /* Recover old server name: it can't be changed during run-time */ if( strcmp( old_name, Conf_ServerName ) != 0 ) { strcpy( Conf_ServerName, old_name ); Log( LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name." ); } - /* neue pre-defined Channel anlegen: */ + /* Create new pre-defined channels */ Channel_InitPredefined( ); - /* Listen-Sockets neu anlegen: */ + /* Start listening on sockets */ Conn_InitListeners( ); - Log( LOG_INFO, "Re-reading of configuration done." ); -} /* NGIRCd_Reload */ + /* Sync configuration with established connections */ + Conn_SyncServerStruct( ); + + Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." ); +} /* NGIRCd_Rehash */ LOCAL VOID @@ -472,23 +543,21 @@ Signal_Handler( INT Signal ) case SIGINT: case SIGQUIT: /* wir soll(t)en uns wohl beenden ... */ - if( Signal == SIGTERM ) Log( LOG_WARNING|LOG_snotice, "Got TERM signal, terminating now ..." ); - else if( Signal == SIGINT ) Log( LOG_WARNING|LOG_snotice, "Got INT signal, terminating now ..." ); - else if( Signal == SIGQUIT ) Log( LOG_WARNING|LOG_snotice, "Got QUIT signal, terminating now ..." ); - NGIRCd_Quit = TRUE; + NGIRCd_SignalQuit = TRUE; break; case SIGHUP: /* Konfiguration neu einlesen: */ - Log( LOG_WARNING|LOG_snotice, "Got HUP signal, re-reading configuration ..." ); - NGIRCd_Reload( ); + NGIRCd_SignalRehash = TRUE; break; case SIGCHLD: /* Child-Prozess wurde beendet. Zombies vermeiden: */ while( waitpid( -1, NULL, WNOHANG ) > 0); break; +#ifdef DEBUG default: /* unbekanntes bzw. unbehandeltes Signal */ - Log( LOG_NOTICE, "Got signal %d! Ignored.", Signal ); + Log( LOG_DEBUG, "Got signal %d! Ignored.", Signal ); +#endif } } /* Signal_Handler */ @@ -496,8 +565,8 @@ Signal_Handler( INT Signal ) LOCAL VOID Show_Version( VOID ) { - puts( NGIRCd_Version( )); - puts( "Copyright (c)2001,2002 by Alexander Barton ()." ); + puts( NGIRCd_Version ); + puts( "Copyright (c)2001-2005 by Alexander Barton ()." ); puts( "Homepage: \n" ); puts( "This is free software; see the source for copying conditions. There is NO" ); puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." ); @@ -516,10 +585,80 @@ Show_Help( VOID ) #ifdef SNIFFER puts( " -s, --sniffer enable network sniffer and display all IRC traffic" ); #endif - puts( " --configtest read, validate and display configuration; then exit" ); + puts( " -t, --configtest read, validate and display configuration; then exit" ); puts( " --version output version information and exit" ); puts( " --help display this help and exit" ); } /* Show_Help */ +LOCAL VOID +Pidfile_Delete( VOID ) +{ + /* Pidfile configured? */ + if( ! Conf_PidFile[0] ) return; + +#ifdef DEBUG + Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile ); +#endif + + if( unlink( Conf_PidFile )) + Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno )); +} /* Pidfile_Delete */ + + +LOCAL VOID +Pidfile_Create( LONG pid ) +{ + FILE *pidf; + + /* Pidfile configured? */ + if( ! Conf_PidFile[0] ) return; + +#ifdef DEBUG + Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile ); +#endif + + pidf = fopen( Conf_PidFile, "w" ); + + if( ! pidf ) + { + Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno )); + return; + } + + if( fprintf( pidf, "%ld\n", pid ) < 0 ) + Log( LOG_ERR, "Can't write PID file (%s): %s", Conf_PidFile, strerror( errno )); + + if( fclose(pidf) != 0 ) + Log( LOG_ERR, "Error closing PID file (%s): %s", Conf_PidFile, strerror( errno )); +} /* Pidfile_Create */ + + +LOCAL VOID +Setup_FDStreams( VOID ) +{ + int fd; + + /* Test if we can open /dev/null for reading and writing. If not + * we are most probably chrooted already and the server has been + * restarted. So we simply don't try to redirect stdXXX ... */ + fd = open( "/dev/null", O_RDWR ); + if ( fd < 0 ) return; + + /* Close "old" stdin/out/err descriptors */ + fclose( stdin ); fclose( stdout ); fclose( stderr ); + + /* Create new stdin(0), stdout(1) and stderr(2) descriptors */ + dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 ); + + /* Close newly opened file descriptor if not stdin/out/err */ + if( fd > 2 ) close( fd ); + + /* Assign FILE handles for stdin/out/err */ + stdin = fdopen( 0, "r" ); + stdout = fdopen( 1, "w" ); + stderr = fdopen( 2, "w" ); +} /* Setup_FDStreams */ + + /* -eof- */