#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 <assert.h>
#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 ));
#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 )
{
Log( LOG_DEBUG, "Redirected stderr to \"%s\".", Error_File );
} /* Log_InitErrfile */
+#endif
+
GLOBAL VOID
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 */
if( Level <= LOG_CRIT )
{
- /* Kritische Meldungen in Error-File (stderr) */
+ /* log critical messages to stderr */
fprintf( stderr, "%s\n", msg );
fflush( stderr );
}
* (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)
*/
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 ));
GLOBAL VOID Log_Resolver PARAMS((CONST INT Level, CONST CHAR *Format, ... ));
+#ifdef DEBUG
+GLOBAL VOID Log_InitErrorfile PARAMS((VOID ));
+#endif
+
#endif
#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 <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
+#include <fcntl.h>
#include <pwd.h>
#include <grp.h>
LOCAL VOID NGIRCd_FillVersion PARAMS(( VOID ));
+LOCAL VOID Setup_FDStreams PARAMS(( VOID ));
+
GLOBAL int
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 */
#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( );
} /* 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- */