]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/ngircd.c
New configuration variable "PidFile", section "[Global]": if defined,
[ngircd-alex.git] / src / ngircd / ngircd.c
index 52019a76d4c18e038c42c2698f2bf39eabd0d0da..89b2f7f8b0362406c0bbb2909bb77a52bc389f4a 100644 (file)
@@ -14,7 +14,7 @@
 
 #include "portab.h"
 
-static char UNUSED id[] = "$Id: ngircd.c,v 1.87 2005/01/26 22:03:15 alex Exp $";
+static char UNUSED id[] = "$Id: ngircd.c,v 1.88 2005/02/04 14:24:21 alex Exp $";
 
 #include "imp.h"
 #include <assert.h>
@@ -57,6 +57,9 @@ 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 ));
+
 
 GLOBAL int
 main( int argc, const char *argv[] )
@@ -285,9 +288,13 @@ main( int argc, const char *argv[] )
                        chdir( "/" );
                }
 
+               /* 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( ), (LONG)getpid( ));
+               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!) */
@@ -350,6 +357,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 );
                }
                
@@ -365,6 +373,8 @@ main( int argc, const char *argv[] )
                Channel_Exit( );
                Lists_Exit( );
                Log_Exit( );
+
+               Pidfile_Delete( );
        }
 
        return 0;
@@ -585,4 +595,47 @@ 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;
+
+       pidf = fopen( Conf_PidFile, "w" );
+
+#ifdef DEBUG
+       Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
+#endif
+
+       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 */
+
+
 /* -eof- */