X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=ngircd-alex.git;a=blobdiff_plain;f=src%2Fngircd%2Fngircd.c;h=84b329e3f2d6ac147069bdfa7f1b05ea5659a98f;hp=8ced6e3f8903c8698e2aa505c8c558a15798b539;hb=f2fbf206b6d945922534f7b3d33a9e8b9771d986;hpb=7281b8dd4d917f8b7f7c36460b78b686427ce973 diff --git a/src/ngircd/ngircd.c b/src/ngircd/ngircd.c index 8ced6e3f..84b329e3 100644 --- a/src/ngircd/ngircd.c +++ b/src/ngircd/ngircd.c @@ -1,6 +1,6 @@ /* * ngIRCd -- The Next Generation IRC Daemon - * Copyright (c)2001-2004 by Alexander Barton (alex@barton.de) + * Copyright (c)2001-2005 by Alexander Barton (alex@barton.de) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -14,7 +14,7 @@ #include "portab.h" -static char UNUSED id[] = "$Id: ngircd.c,v 1.84 2004/05/07 11:19:21 alex Exp $"; +static char UNUSED id[] = "$Id: ngircd.c,v 1.93 2005/02/10 16:55:52 alex Exp $"; #include "imp.h" #include @@ -28,16 +28,17 @@ static char UNUSED id[] = "$Id: ngircd.c,v 1.84 2004/05/07 11:19:21 alex Exp $"; #include #include #include +#include #include #include +#include "defines.h" #include "resolve.h" #include "conn.h" #include "client.h" #include "channel.h" #include "conf.h" #include "cvs-version.h" -#include "defines.h" #include "lists.h" #include "log.h" #include "parse.h" @@ -57,6 +58,13 @@ LOCAL VOID Signal_Handler PARAMS(( INT Signal )); LOCAL VOID Show_Version PARAMS(( VOID )); 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[] ) @@ -80,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++ ) { @@ -207,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 @@ -260,29 +270,54 @@ 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 */ + pid = (LONG) getpid( ); + Pidfile_Create( pid ); + + /* Show user, group, and PID of the running daemon */ + pwd = getpwuid( getuid( )); grp = getgrgid( getgid( )); + Log( LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.", pwd ? pwd->pw_name : "unknown", (LONG)getuid( ), grp ? grp->gr_name : "unknown", (LONG)getgid( ), pid); + + /* Change working directory to home directory of the user + * we are running as (when not running chroot()'ed!) */ + if( Conf_UID != 0 && ! Conf_Chroot[0] ) + { + struct passwd *pwd; + + pwd = getpwuid( Conf_UID ); + if( pwd != NULL ) + { + if( chdir( pwd->pw_dir ) == 0 ) Log( LOG_DEBUG, "Changed working directory to \"%s\" ...", pwd->pw_dir ); + else Log( LOG_ERR, "Can't change working directory to \"%s\": %s", pwd->pw_dir, strerror( errno )); + } + else Log( LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID ); } /* Initialize modules, part II: these functions are eventually @@ -296,15 +331,11 @@ main( int argc, const char *argv[] ) #endif Conn_Init( ); - /* Show user, group, and PID of the running daemon */ - pwd = getpwuid( getuid( )); grp = getgrgid( getgid( )); - Log( LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.", pwd ? pwd->pw_name : "unknown", (LONG)getuid( ), grp ? grp->gr_name : "unknown", (LONG)getgid( ), (LONG)getpid( )); - - /* 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( ); @@ -335,6 +366,7 @@ main( int argc, const char *argv[] ) { Log( LOG_ALERT, "Server isn't listening on a single port!" ); Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME ); + Pidfile_Delete( ); exit( 1 ); } @@ -350,79 +382,68 @@ main( int argc, const char *argv[] ) Channel_Exit( ); Lists_Exit( ); Log_Exit( ); + + Pidfile_Delete( ); } return 0; } /* 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 @@ -544,8 +565,8 @@ Signal_Handler( INT Signal ) LOCAL VOID Show_Version( VOID ) { - puts( NGIRCd_Version( )); - puts( "Copyright (c)2001-2004 by Alexander Barton ()." ); + puts( NGIRCd_Version ); + puts( "Copyright (c)2001-2005 by Alexander Barton ()." ); puts( "Homepage: \n" ); puts( "This is free software; see the source for copying conditions. There is NO" ); puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." ); @@ -570,4 +591,69 @@ Show_Help( VOID ) } /* Show_Help */ +LOCAL VOID +Pidfile_Delete( VOID ) +{ + /* Pidfile configured? */ + if( ! Conf_PidFile[0] ) return; + +#ifdef DEBUG + Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile ); +#endif + + if( unlink( Conf_PidFile )) + Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno )); +} /* Pidfile_Delete */ + + +LOCAL VOID +Pidfile_Create( LONG pid ) +{ + FILE *pidf; + + /* Pidfile configured? */ + if( ! Conf_PidFile[0] ) return; + +#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 )); + return; + } + + if( fprintf( pidf, "%ld\n", pid ) < 0 ) + Log( LOG_ERR, "Can't write PID file (%s): %s", Conf_PidFile, strerror( errno )); + + if( fclose(pidf) != 0 ) + Log( LOG_ERR, "Error closing PID file (%s): %s", Conf_PidFile, strerror( errno )); +} /* 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- */