Commit Diff


commit - ae63ed04c52a1f116647754f84b02b1928c09921
commit + 112102b10ce991340ba56e272e32bc50a73e1b05
blob - ee91f31c0add2912ba0d329f0e35ba163843ce5b
blob + b5d799d5717b95f7b0b2032adce5f0258ed22e4f
--- ChangeLog
+++ ChangeLog
@@ -12,6 +12,9 @@
 
 ngIRCd CVSHEAD
 
+  - New configuration variable "PidFile", section "[Global]": if defined,
+    the server writes its process ID (PID) to this file. Default: off.
+    Idea of Florian Westphal, <westphal@foo.fh-furtwangen.de>.
   - Code cleanups from Florian Westphal, <westphal@foo.fh-furtwangen.de>.
   - Raised the maximum length of passwords to 20 characters.
   - Fixed a memory leak when resizing the connection pool and realloc()
@@ -19,7 +22,7 @@ ngIRCd CVSHEAD
     Patch from Florian Westphal, <westphal@foo.fh-furtwangen.de>.
   - Added support for the Howl (http://www.porchdogsoft.com/products/howl/)
     Rendezvous API, in addition to the API of Apple (Mac OS X). The available
-    APU will be autodetected when you call "./configure --with-rendezvous".
+    API will be autodetected when you call "./configure --with-rendezvous".
   - Made ngIRCd compile on HP/UX 10.20 with native HP pre-ANSI C compiler and
     most probably other older C compilers on other systems.
   - When the daemon should switch to another user ID (ServerID is defined in
@@ -579,4 +582,4 @@ ngIRCd 0.0.1, 31.12.2001
 
 
 -- 
-$Id: ChangeLog,v 1.258 2005/02/04 14:21:35 alex Exp $
+$Id: ChangeLog,v 1.259 2005/02/04 14:24:20 alex Exp $
blob - 09b9a62f2ccd4d3a07c7f97943f7d824a32adc93
blob + 9b1efedfd95c0c36bcead3d62faeaced96e0b5fe
--- NEWS
+++ NEWS
@@ -12,9 +12,12 @@
 
 ngIRCd CVSHEAD
 
+  - New configuration variable "PidFile", section "[Global]": if defined,
+    the server writes its process ID (PID) to this file. Default: off.
+    Idea of Florian Westphal, <westphal@foo.fh-furtwangen.de>.
   - Added support for the Howl (http://www.porchdogsoft.com/products/howl/)
     Rendezvous API, in addition to the API of Apple (Mac OS X). The available
-    APU will be autodetected when you call "./configure --with-rendezvous".
+    API will be autodetected when you call "./configure --with-rendezvous".
 
 ngIRCd 0.8.0 (2004-06-26)
 
@@ -195,4 +198,4 @@ ngIRCd 0.0.1, 31.12.2001
 
 
 -- 
-$Id: NEWS,v 1.67 2004/12/26 00:14:33 alex Exp $
+$Id: NEWS,v 1.68 2005/02/04 14:24:20 alex Exp $
blob - d872b0de91152c0028f83cb0532c298405815aae
blob + a34f04801ed84b6c6a071a86d6811fe66293660e
--- doc/sample-ngircd.conf
+++ doc/sample-ngircd.conf
@@ -1,4 +1,4 @@
-# $Id: sample-ngircd.conf,v 1.29 2005/01/17 12:51:17 alex Exp $
+# $Id: sample-ngircd.conf,v 1.30 2005/02/04 14:24:21 alex Exp $
 
 #
 # This is a sample configuration file for the ngIRCd, which must be adepted
@@ -67,6 +67,12 @@
 	# with root privileges!
 	;ChrootDir = /var/empty
 
+	# This tells ngircd to write its current process id to a file.
+	# Note that the pidfile is written AFTER chroot and switching uid, i.e.
+	# the Directory the pidfile resides in must be writeable by the ngircd user and
+	# exist in the chroot dir.
+	;PidFile = /var/run/ngircd/ngircd.pid
+
 	# After <PingTimeout> seconds of inactivity the server will send a
 	# PING to the peer to test whether it is alive or not.
 	;PingTimeout = 120
blob - d1696b921afe3c57a20c1efd545e68a482962787
blob + 17090d8a5e4af5df85077d1274b4c91b33ca9fae
--- src/ngircd/conf.c
+++ src/ngircd/conf.c
@@ -14,7 +14,7 @@
 
 #include "portab.h"
 
-static char UNUSED id[] = "$Id: conf.c,v 1.67 2005/01/20 00:13:08 alex Exp $";
+static char UNUSED id[] = "$Id: conf.c,v 1.68 2005/02/04 14:24:21 alex Exp $";
 
 #include "imp.h"
 #include <assert.h>
@@ -122,7 +122,8 @@ Conf_Test( VOID )
 	printf( "  AdminEMail = %s\n", Conf_ServerAdminMail );
 	printf( "  MotdFile = %s\n", Conf_MotdFile );
 	printf( "  MotdPhrase = %s\n", Conf_MotdPhrase );
-	printf( "  ChrootDir= %s\n", Conf_Chroot );
+	printf( "  ChrootDir = %s\n", Conf_Chroot );
+	printf( "  PidFile = %s\n", Conf_PidFile);
 	printf( "  Ports = " );
 	for( i = 0; i < Conf_ListenPorts_Count; i++ )
 	{
@@ -356,6 +357,8 @@ Set_Defaults( BOOLEAN InitServers )
 
 	strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
 
+	strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
+
 	Conf_ListenPorts_Count = 0;
 	strcpy( Conf_ListenAddress, "" );
 
@@ -650,6 +653,16 @@ Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
 
 		return;
 	}
+
+	if ( strcasecmp( Var, "PidFile" ) == 0 )
+	{
+		/* name of pidfile */
+		if( strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile )) >= sizeof( Conf_PidFile ))
+			Config_Error_TooLong( Line, Var );
+
+		return;
+	}
+
 	if( strcasecmp( Var, "ServerUID" ) == 0 )
 	{
 		/* UID the daemon should switch to */
blob - 4fbf2d68597c4d8a69f990d5244570ff69d11104
blob + 93c8ebf0d241b320902281c355188f0ea0489cba
--- src/ngircd/conf.h
+++ src/ngircd/conf.h
@@ -8,7 +8,7 @@
  * (at your option) any later version.
  * Please read the file COPYING, README and AUTHORS for more information.
  *
- * $Id: conf.h,v 1.29 2004/05/07 11:19:21 alex Exp $
+ * $Id: conf.h,v 1.30 2005/02/04 14:24:21 alex Exp $
  *
  * Configuration management (header)
  */
@@ -89,6 +89,9 @@ GLOBAL UINT Conf_GID;
 /* A directory to chroot() in */
 GLOBAL CHAR Conf_Chroot[FNAME_LEN];
 
+/* File with PID of daemon */
+GLOBAL CHAR Conf_PidFile[FNAME_LEN];
+
 /* Timeouts for PING and PONG */
 GLOBAL INT Conf_PingTimeout;
 GLOBAL INT Conf_PongTimeout;
blob - 1b7a1d0d02b79970f8b3a995804c69b7c4195b6d
blob + 12558ef37589a82a4db5d2eb28044d31b0c9367d
--- src/ngircd/defines.h
+++ src/ngircd/defines.h
@@ -8,7 +8,7 @@
  * (at your option) any later version.
  * Please read the file COPYING, README and AUTHORS for more information.
  *
- * $Id: defines.h,v 1.48 2005/01/17 13:14:36 alex Exp $
+ * $Id: defines.h,v 1.49 2005/02/04 14:24:21 alex Exp $
  *
  * Global defines of ngIRCd.
  */
@@ -85,6 +85,7 @@
 #define MOTD_FILE "/ngircd.motd"
 #define MOTD_PHRASE ""
 #define CHROOT_DIR ""
+#define PID_FILE ""
 
 #define ERROR_DIR "/tmp"
 
blob - 52019a76d4c18e038c42c2698f2bf39eabd0d0da
blob + 89b2f7f8b0362406c0bbb2909bb77a52bc389f4a
--- src/ngircd/ngircd.c
+++ src/ngircd/ngircd.c
@@ -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,7 +57,10 @@ 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[] )
 {
@@ -284,10 +287,14 @@ main( int argc, const char *argv[] )
 			(VOID)setsid( );
 			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- */