]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/ngircd.c
New configuration option "OperServerMode".
[ngircd-alex.git] / src / ngircd / ngircd.c
index 89b2f7f8b0362406c0bbb2909bb77a52bc389f4a..263c3a542e597d69126623d6cae196ac29a3b5dd 100644 (file)
@@ -14,7 +14,7 @@
 
 #include "portab.h"
 
-static char UNUSED id[] = "$Id: ngircd.c,v 1.88 2005/02/04 14:24:21 alex Exp $";
+static char UNUSED id[] = "$Id: ngircd.c,v 1.94 2005/02/11 13:52:37 alex Exp $";
 
 #include "imp.h"
 #include <assert.h>
@@ -28,6 +28,7 @@ static char UNUSED id[] = "$Id: ngircd.c,v 1.88 2005/02/04 14:24:21 alex Exp $";
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
+#include <fcntl.h>
 #include <pwd.h>
 #include <grp.h>
 
@@ -60,6 +61,10 @@ LOCAL VOID Show_Help PARAMS(( VOID ));
 LOCAL VOID Pidfile_Create PARAMS(( LONG ));
 LOCAL VOID Pidfile_Delete PARAMS(( VOID ));
 
+LOCAL VOID Fill_Version PARAMS(( VOID ));
+
+LOCAL VOID Setup_FDStreams PARAMS(( VOID ));
+
 
 GLOBAL int
 main( int argc, const char *argv[] )
@@ -83,6 +88,8 @@ main( int argc, const char *argv[] )
        strlcpy( NGIRCd_ConfFile, SYSCONFDIR, sizeof( NGIRCd_ConfFile ));
        strlcat( NGIRCd_ConfFile, CONFIG_FILE, sizeof( NGIRCd_ConfFile ));
 
+       Fill_Version( );
+
        /* Kommandozeile parsen */
        for( i = 1; i < argc; i++ )
        {
@@ -210,7 +217,7 @@ main( int argc, const char *argv[] )
        }
 
        /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
-       strcpy( NGIRCd_DebugLevel, "" );
+       NGIRCd_DebugLevel[0] = '\0';
 #ifdef DEBUG
        if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
 #endif
@@ -263,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 */
@@ -322,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( );
@@ -381,73 +390,60 @@ main( int argc, const char *argv[] )
 } /* main */
 
 
-GLOBAL CHAR *
-NGIRCd_Version( VOID )
-{
-       STATIC CHAR version[126];
-       
-#ifdef CVSDATE
-       sprintf( version, "%s %s(%s)-%s", PACKAGE_NAME, PACKAGE_VERSION, CVSDATE, NGIRCd_VersionAddition( ));
-#else
-       sprintf( version, "%s %s-%s", PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition( ));
-#endif
-       return version;
-} /* NGIRCd_Version */
-
-
-GLOBAL CHAR *
-NGIRCd_VersionAddition( VOID )
+LOCAL VOID
+Fill_Version( VOID )
 {
-       STATIC CHAR txt[200];
-
-       strcpy( txt, "" );
+       NGIRCd_VersionAddition[0] = '\0';
 
 #ifdef SYSLOG
-       if( txt[0] ) strcat( txt, "+" );
-       strcat( txt, "SYSLOG" );
+       strcpy( NGIRCd_VersionAddition, "SYSLOG" );
 #endif
 #ifdef ZLIB
-       if( txt[0] ) strcat( txt, "+" );
-       strcat( txt, "ZLIB" );
+       if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
+       strcat( NGIRCd_VersionAddition, "ZLIB" );
 #endif
 #ifdef TCPWRAP
-       if( txt[0] ) strcat( txt, "+" );
-       strcat( txt, "TCPWRAP" );
+       if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
+       strcat( NGIRCd_VersionAddition, "TCPWRAP" );
 #endif
 #ifdef RENDEZVOUS
-       if( txt[0] ) strcat( txt, "+" );
-       strcat( txt, "RENDEZVOUS" );
+       if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
+       strcat( NGIRCd_VersionAddition, "RENDEZVOUS" );
 #endif
 #ifdef IDENTAUTH
-       if( txt[0] ) strcat( txt, "+" );
-       strcat( txt, "IDENT" );
+       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] ) strlcat( txt, "-", sizeof( txt ));
-       strlcat( txt, TARGET_CPU, sizeof( txt ));
-       strlcat( txt, "/", sizeof( txt ));
-       strlcat( txt, TARGET_VENDOR, sizeof( txt ));
-       strlcat( txt, "/", sizeof( txt ));
-       strlcat( txt, TARGET_OS, sizeof( txt ));
 
-       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
+} /* Fill_Version */
 
 
 GLOBAL VOID
@@ -569,7 +565,7 @@ Signal_Handler( INT Signal )
 LOCAL VOID
 Show_Version( VOID )
 {
-       puts( NGIRCd_Version( ));
+       puts( NGIRCd_Version );
        puts( "Copyright (c)2001-2005 by Alexander Barton (<alex@barton.de>)." );
        puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
        puts( "This is free software; see the source for copying conditions. There is NO" );
@@ -584,8 +580,8 @@ Show_Help( VOID )
        puts( "  -d, --debug        log extra debug messages" );
 #endif
        puts( "  -f, --config <f>   use file <f> as configuration file" );
-        puts( "  -n, --nodaemon     don't fork and don't detach from controlling terminal" );
-        puts( "  -p, --passive      disable automatic connections to other servers" );
+       puts( "  -n, --nodaemon     don't fork and don't detach from controlling terminal" );
+       puts( "  -p, --passive      disable automatic connections to other servers" );
 #ifdef SNIFFER
        puts( "  -s, --sniffer      enable network sniffer and display all IRC traffic" );
 #endif
@@ -618,12 +614,12 @@ Pidfile_Create( LONG pid )
        /* Pidfile configured? */
        if( ! Conf_PidFile[0] ) return;
 
-       pidf = fopen( Conf_PidFile, "w" );
-
 #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 ));
@@ -638,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- */