]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/ngircd.c
Don't play games with FILE* stdin/out/err, only touch "real" descriptors.
[ngircd-alex.git] / src / ngircd / ngircd.c
index 191c216a1ac0df60fe38362338328c82e6500250..84b329e3f2d6ac147069bdfa7f1b05ea5659a98f 100644 (file)
@@ -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.93 2005/02/10 16:55:52 alex Exp $";
 
 #include "imp.h"
 #include <assert.h>
@@ -28,6 +28,7 @@ static char UNUSED id[] = "$Id: ngircd.c,v 1.90 2005/02/09 09:52:58 alex Exp $";
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
+#include <fcntl.h>
 #include <pwd.h>
 #include <grp.h>
 
@@ -60,7 +61,9 @@ 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 Fill_Version PARAMS(( VOID ));
+
+LOCAL VOID Setup_FDStreams PARAMS(( VOID ));
 
 
 GLOBAL int
@@ -85,7 +88,7 @@ main( int argc, const char *argv[] )
        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++ )
@@ -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( );
@@ -386,7 +391,7 @@ main( int argc, const char *argv[] )
 
 
 LOCAL VOID
-NGIRCd_FillVersion( VOID )
+Fill_Version( VOID )
 {
        NGIRCd_VersionAddition[0] = '\0';
 
@@ -438,7 +443,7 @@ NGIRCd_FillVersion( VOID )
 #else
        snprintf( NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s", PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
 #endif
-} /* NGIRCd_FillVersion */
+} /* Fill_Version */
 
 
 GLOBAL VOID
@@ -629,4 +634,26 @@ 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 */
+       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- */