#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.93 2005/02/10 16:55:52 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 Pidfile_Create PARAMS(( LONG ));
LOCAL VOID Pidfile_Delete PARAMS(( VOID ));
-LOCAL VOID NGIRCd_FillVersion PARAMS(( VOID ));
+LOCAL VOID Fill_Version PARAMS(( VOID ));
+
+LOCAL VOID Setup_FDStreams PARAMS(( VOID ));
GLOBAL int
strlcpy( NGIRCd_ConfFile, SYSCONFDIR, sizeof( NGIRCd_ConfFile ));
strlcat( NGIRCd_ConfFile, CONFIG_FILE, sizeof( NGIRCd_ConfFile ));
- NGIRCd_FillVersion( );
+ Fill_Version( );
/* Kommandozeile parsen */
for( i = 1; i < argc; i++ )
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( );
LOCAL VOID
-NGIRCd_FillVersion( VOID )
+Fill_Version( VOID )
{
NGIRCd_VersionAddition[0] = '\0';
#else
snprintf( NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s", PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
#endif
-} /* NGIRCd_FillVersion */
+} /* Fill_Version */
GLOBAL VOID
} /* 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 */
+ close( 0 ); close( 1 ); close( 2 );
+
+ /* 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 );
+} /* Setup_FDStreams */
+
+
/* -eof- */