From 9537542647b014894227de81a59fb063fc0dc3e4 Mon Sep 17 00:00:00 2001 From: Alexander Barton Date: Thu, 10 Feb 2005 12:49:04 +0000 Subject: [PATCH] Write "error file" (/tmp/ngircd-XXX.err) only if compiled with debug code ("--enable-debug") and running as daemon process. --- ChangeLog | 4 ++- src/ngircd/log.c | 17 ++++++++++--- src/ngircd/log.h | 7 +++-- src/ngircd/ngircd.c | 62 ++++++++++++++++++++++++++++++++++----------- 4 files changed, 69 insertions(+), 21 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7694623b..7d134b6f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -12,6 +12,8 @@ ngIRCd CVSHEAD + - Write "error file" (/tmp/ngircd-XXX.err) only if compiled with debug + code ("--enable-debug") and running as daemon process. - Don't create version information string each time a client connects but insetead on server startup. By Florian Westphal. - New configuration variable "PidFile", section "[Global]": if defined, @@ -584,4 +586,4 @@ ngIRCd 0.0.1, 31.12.2001 -- -$Id: ChangeLog,v 1.260 2005/02/09 09:52:58 alex Exp $ +$Id: ChangeLog,v 1.261 2005/02/10 12:49:04 alex Exp $ diff --git a/src/ngircd/log.c b/src/ngircd/log.c index 01821829..153ffedd 100644 --- a/src/ngircd/log.c +++ b/src/ngircd/log.c @@ -14,7 +14,7 @@ #include "portab.h" -static char UNUSED id[] = "$Id: log.c,v 1.51 2005/02/09 09:52:58 alex Exp $"; +static char UNUSED id[] = "$Id: log.c,v 1.52 2005/02/10 12:49:04 alex Exp $"; #include "imp.h" #include @@ -44,9 +44,12 @@ static char UNUSED id[] = "$Id: log.c,v 1.51 2005/02/09 09:52:58 alex Exp $"; #include "log.h" -LOCAL CHAR Error_File[FNAME_LEN]; LOCAL CHAR Init_Txt[127]; +#ifdef DEBUG +LOCAL CHAR Error_File[FNAME_LEN]; +#endif + LOCAL VOID Wall_ServerNotice PARAMS(( CHAR *Msg )); @@ -89,10 +92,14 @@ Log_Init( VOID ) #endif if( Init_Txt[0] ) Log( LOG_INFO, "Activating: %s.", Init_Txt ); +#ifdef DEBUG Error_File[0] = '\0'; +#endif } /* Log_Init */ +#ifdef DEBUG + GLOBAL VOID Log_InitErrorfile( VOID ) { @@ -119,6 +126,8 @@ Log_InitErrorfile( VOID ) Log( LOG_DEBUG, "Redirected stderr to \"%s\".", Error_File ); } /* Log_InitErrfile */ +#endif + GLOBAL VOID Log_Exit( VOID ) @@ -127,11 +136,13 @@ Log_Exit( VOID ) if( NGIRCd_SignalRestart ) Log( LOG_NOTICE, "%s done (restarting).", PACKAGE_NAME ); else Log( LOG_NOTICE, "%s done.", PACKAGE_NAME ); +#ifdef DEBUG if( Error_File[0] ) { /* Error-File (stderr) loeschen */ if( unlink( Error_File ) != 0 ) Log( LOG_ERR, "Can't delete \"%s\": %s", Error_File, strerror( errno )); } +#endif #ifdef SYSLOG /* syslog abmelden */ @@ -198,7 +209,7 @@ va_dcl if( Level <= LOG_CRIT ) { - /* Kritische Meldungen in Error-File (stderr) */ + /* log critical messages to stderr */ fprintf( stderr, "%s\n", msg ); fflush( stderr ); } diff --git a/src/ngircd/log.h b/src/ngircd/log.h index 028985c4..24c3f88c 100644 --- a/src/ngircd/log.h +++ b/src/ngircd/log.h @@ -8,7 +8,7 @@ * (at your option) any later version. * Please read the file COPYING, README and AUTHORS for more information. * - * $Id: log.h,v 1.14 2003/12/26 15:55:07 alex Exp $ + * $Id: log.h,v 1.15 2005/02/10 12:49:04 alex Exp $ * * Logging functions (header) */ @@ -38,7 +38,6 @@ GLOBAL VOID Log_Init PARAMS((VOID )); GLOBAL VOID Log_Exit PARAMS((VOID )); -GLOBAL VOID Log_InitErrorfile PARAMS((VOID )); GLOBAL VOID Log PARAMS((INT Level, CONST CHAR *Format, ... )); GLOBAL VOID Log_Init_Resolver PARAMS((VOID )); @@ -46,6 +45,10 @@ GLOBAL VOID Log_Exit_Resolver PARAMS((VOID )); GLOBAL VOID Log_Resolver PARAMS((CONST INT Level, CONST CHAR *Format, ... )); +#ifdef DEBUG +GLOBAL VOID Log_InitErrorfile PARAMS((VOID )); +#endif + #endif diff --git a/src/ngircd/ngircd.c b/src/ngircd/ngircd.c index 191c216a..e39229a0 100644 --- a/src/ngircd/ngircd.c +++ b/src/ngircd/ngircd.c @@ -14,7 +14,7 @@ #include "portab.h" -static char UNUSED id[] = "$Id: ngircd.c,v 1.90 2005/02/09 09:52:58 alex Exp $"; +static char UNUSED id[] = "$Id: ngircd.c,v 1.91 2005/02/10 12:49:04 alex Exp $"; #include "imp.h" #include @@ -28,6 +28,7 @@ static char UNUSED id[] = "$Id: ngircd.c,v 1.90 2005/02/09 09:52:58 alex Exp $"; #include #include #include +#include #include #include @@ -62,6 +63,8 @@ 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[] ) @@ -267,29 +270,31 @@ main( int argc, const char *argv[] ) if( setuid( Conf_UID ) != 0 ) Log( LOG_ERR, "Can't change user ID to %u: %s", Conf_UID, strerror( errno )); } - /* 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. */ + /* 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_NAME, 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( ); } /* Create PID file */ @@ -326,11 +331,11 @@ main( int argc, const char *argv[] ) #endif Conn_Init( ); - /* Redirect stderr handle to "error file" for debugging. - * But don't try to write in the chroot jail, since it's more - * secure to have a chroot dir not writable by the daemon. - */ - if( ! Conf_Chroot[0] ) Log_InitErrorfile( ); +#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( ); @@ -629,4 +634,31 @@ Pidfile_Create( LONG pid ) } /* 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- */ -- 2.39.2