X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fngircd%2Fngircd.c;h=33dfdbc55600465ab9a9314e506cc05cc8250c3f;hb=45404a1644f510b5e5e9d67daa9faf8613653621;hp=8ced6e3f8903c8698e2aa505c8c558a15798b539;hpb=7281b8dd4d917f8b7f7c36460b78b686427ce973;p=ngircd-alex.git diff --git a/src/ngircd/ngircd.c b/src/ngircd/ngircd.c index 8ced6e3f..33dfdbc5 100644 --- a/src/ngircd/ngircd.c +++ b/src/ngircd/ngircd.c @@ -1,20 +1,24 @@ /* * 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 * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * Please read the file COPYING, README and AUTHORS for more information. - * - * Main program -- main() */ #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.96 2005/06/01 21:52:18 alex Exp $"; + +/** + * @file + * The main program, including the C function main() which is called + * by the loader of the operating system. + */ #include "imp.h" #include @@ -28,16 +32,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" @@ -51,39 +56,57 @@ static char UNUSED id[] = "$Id: ngircd.c,v 1.84 2004/05/07 11:19:21 alex Exp $"; #include "ngircd.h" -LOCAL VOID Initialize_Signal_Handler PARAMS(( VOID )); -LOCAL VOID Signal_Handler PARAMS(( INT Signal )); +LOCAL void Initialize_Signal_Handler PARAMS(( void )); +LOCAL void Signal_Handler PARAMS(( int Signal )); -LOCAL VOID Show_Version PARAMS(( VOID )); -LOCAL VOID Show_Help PARAMS(( VOID )); +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 )); + + +/** + * The main() function of ngIRCd. + * Here all starts: this function is called by the operating system loader, + * it is the first portion of code executed of ngIRCd. + * @param argc The number of arguments passed to ngIRCd on the command line. + * @param argv An array containing all the arguments passed to ngIRCd. + * @return Global exit code of ngIRCd, zero on success. + */ GLOBAL int main( int argc, const char *argv[] ) { struct passwd *pwd; struct group *grp; - BOOLEAN ok, configtest = FALSE; - LONG pid, n; - INT i; + bool ok, configtest = false; + long pid; + int i; + size_t n; umask( 0077 ); - NGIRCd_SignalQuit = NGIRCd_SignalRestart = NGIRCd_SignalRehash = FALSE; - NGIRCd_NoDaemon = NGIRCd_Passive = FALSE; + NGIRCd_SignalQuit = NGIRCd_SignalRestart = NGIRCd_SignalRehash = false; + NGIRCd_NoDaemon = NGIRCd_Passive = false; #ifdef DEBUG - NGIRCd_Debug = FALSE; + NGIRCd_Debug = false; #endif #ifdef SNIFFER - NGIRCd_Sniffer = FALSE; + NGIRCd_Sniffer = false; #endif 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++ ) { - ok = FALSE; + ok = false; if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' )) { /* Lange Option */ @@ -96,19 +119,19 @@ main( int argc, const char *argv[] ) strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile )); /* next parameter */ - i++; ok = TRUE; + i++; ok = true; } } if( strcmp( argv[i], "--configtest" ) == 0 ) { - configtest = TRUE; - ok = TRUE; + configtest = true; + ok = true; } #ifdef DEBUG if( strcmp( argv[i], "--debug" ) == 0 ) { - NGIRCd_Debug = TRUE; - ok = TRUE; + NGIRCd_Debug = true; + ok = true; } #endif if( strcmp( argv[i], "--help" ) == 0 ) @@ -119,19 +142,19 @@ main( int argc, const char *argv[] ) } if( strcmp( argv[i], "--nodaemon" ) == 0 ) { - NGIRCd_NoDaemon = TRUE; - ok = TRUE; + NGIRCd_NoDaemon = true; + ok = true; } if( strcmp( argv[i], "--passive" ) == 0 ) { - NGIRCd_Passive = TRUE; - ok = TRUE; + NGIRCd_Passive = true; + ok = true; } #ifdef SNIFFER if( strcmp( argv[i], "--sniffer" ) == 0 ) { - NGIRCd_Sniffer = TRUE; - ok = TRUE; + NGIRCd_Sniffer = true; + ok = true; } #endif if( strcmp( argv[i], "--version" ) == 0 ) @@ -143,15 +166,14 @@ main( int argc, const char *argv[] ) else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' )) { /* Kurze Option */ - - for( n = 1; n < (LONG)strlen( argv[i] ); n++ ) + for( n = 1; n < strlen( argv[i] ); n++ ) { - ok = FALSE; + ok = false; #ifdef DEBUG if( argv[i][n] == 'd' ) { - NGIRCd_Debug = TRUE; - ok = TRUE; + NGIRCd_Debug = true; + ok = true; } #endif if( argv[i][n] == 'f' ) @@ -162,31 +184,32 @@ main( int argc, const char *argv[] ) strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile )); /* go to the following parameter */ - i++; n = (LONG)strlen( argv[i] ); - ok = TRUE; + i++; + n = strlen( argv[i] ); + ok = true; } } if( argv[i][n] == 'n' ) { - NGIRCd_NoDaemon = TRUE; - ok = TRUE; + NGIRCd_NoDaemon = true; + ok = true; } if( argv[i][n] == 'p' ) { - NGIRCd_Passive = TRUE; - ok = TRUE; + NGIRCd_Passive = true; + ok = true; } #ifdef SNIFFER if( argv[i][n] == 's' ) { - NGIRCd_Sniffer = TRUE; - ok = TRUE; + NGIRCd_Sniffer = true; + ok = true; } #endif if( argv[i][n] == 't' ) { - configtest = TRUE; - ok = TRUE; + configtest = true; + ok = true; } if( ! ok ) @@ -207,14 +230,14 @@ 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 #ifdef SNIFFER if( NGIRCd_Sniffer ) { - NGIRCd_Debug = TRUE; + NGIRCd_Debug = true; strcpy( NGIRCd_DebugLevel, "2" ); } #endif @@ -230,11 +253,11 @@ main( int argc, const char *argv[] ) { /* Initialize global variables */ NGIRCd_Start = time( NULL ); - (VOID)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start )); + (void)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start )); - NGIRCd_SignalRehash = FALSE; - NGIRCd_SignalRestart = FALSE; - NGIRCd_SignalQuit = FALSE; + NGIRCd_SignalRehash = false; + NGIRCd_SignalRestart = false; + NGIRCd_SignalQuit = false; /* Initialize modules, part I */ Log_Init( ); @@ -260,29 +283,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 */ - pid = (LONG)fork( ); + /* 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 */ - (VOID)setsid( ); + /* New child process */ + (void)setsid( ); chdir( "/" ); + + /* Detach stdin, stdout and stderr */ + 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 +344,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 +379,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,88 +395,86 @@ 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 ) +/** + * Generate ngIRCd "version string". + * This string is generated once and then stored in NGIRCd_Version for + * further usage, for example by the IRC command VERSION and the --version + * command line switch. + */ +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 -NGIRCd_Rehash( VOID ) +/** + * Reload the server configuration file. + */ +GLOBAL void +NGIRCd_Rehash( void ) { - CHAR old_name[CLIENT_ID_LEN]; + char old_name[CLIENT_ID_LEN]; Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" ); - NGIRCd_SignalRehash = FALSE; + NGIRCd_SignalRehash = false; /* Close down all listening sockets */ Conn_ExitListeners( ); @@ -462,8 +505,11 @@ NGIRCd_Rehash( VOID ) } /* NGIRCd_Rehash */ -LOCAL VOID -Initialize_Signal_Handler( VOID ) +/** + * Initialize the signal handler. + */ +LOCAL void +Initialize_Signal_Handler( void ) { /* Signal-Handler initialisieren: einige Signale * werden ignoriert, andere speziell behandelt. */ @@ -509,24 +555,26 @@ Initialize_Signal_Handler( VOID ) } /* Initialize_Signal_Handler */ -LOCAL VOID -Signal_Handler( INT Signal ) +/** + * Signal handler of ngIRCd. + * This function is called whenever ngIRCd catches a signal sent by the + * user and/or the system to it. For example SIGTERM and SIGHUP. + * @param Signal Number of the signal to handle. + */ +LOCAL void +Signal_Handler( int Signal ) { - /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft, - * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die - * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */ - switch( Signal ) { case SIGTERM: case SIGINT: case SIGQUIT: /* wir soll(t)en uns wohl beenden ... */ - NGIRCd_SignalQuit = TRUE; + NGIRCd_SignalQuit = true; break; case SIGHUP: /* Konfiguration neu einlesen: */ - NGIRCd_SignalRehash = TRUE; + NGIRCd_SignalRehash = true; break; case SIGCHLD: /* Child-Prozess wurde beendet. Zombies vermeiden: */ @@ -541,26 +589,34 @@ Signal_Handler( INT Signal ) } /* Signal_Handler */ -LOCAL VOID -Show_Version( VOID ) +/** + * Display copyright and version information of ngIRCd on the console. + */ +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." ); } /* Show_Version */ -LOCAL VOID -Show_Help( VOID ) +/** + * Display a short help text on the console. + * This help depends on the configuration of the executable and only shows + * options that are actually enabled. + */ +LOCAL void +Show_Help( void ) { #ifdef DEBUG puts( " -d, --debug log extra debug messages" ); #endif puts( " -f, --config use file 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 @@ -570,4 +626,79 @@ Show_Help( VOID ) } /* Show_Help */ +/** + * Delete the file containing the process ID (PID). + */ +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 */ + + +/** + * Create the file containing the process ID of ngIRCd ("PID file"). + * @param pid The process ID to be stored in this file. + */ +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 */ + + +/** + * Redirect stdin, stdout and stderr to apropriate file handles. + */ +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- */