about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ngircd/conf.c9
-rw-r--r--src/ngircd/conf.h5
-rw-r--r--src/ngircd/conn.c16
3 files changed, 28 insertions, 2 deletions
diff --git a/src/ngircd/conf.c b/src/ngircd/conf.c
index 929ab054..835b5ea4 100644
--- a/src/ngircd/conf.c
+++ b/src/ngircd/conf.c
@@ -370,6 +370,7 @@ Conf_Test( void )
 
 	puts("[LIMITS]");
 	printf("  ConnectRetry = %d\n", Conf_ConnectRetry);
+	printf("  IdleTimeout = %d\n", Conf_IdleTimeout);
 	printf("  MaxConnections = %d\n", Conf_MaxConnections);
 	printf("  MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
 	printf("  MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
@@ -736,6 +737,7 @@ Set_Defaults(bool InitServers)
 
 	/* Limits */
 	Conf_ConnectRetry = 60;
+	Conf_IdleTimeout = 0;
 	Conf_MaxConnections = 0;
 	Conf_MaxConnectionsIP = 5;
 	Conf_MaxJoins = 10;
@@ -1241,6 +1243,7 @@ CheckLegacyGlobalOption(int Line, char *Var, char *Arg)
 		return "[Options]";
 	}
 	if (strcasecmp(Var, "ConnectRetry") == 0
+	    || strcasecmp(Var, "IdleTimeout") == 0
 	    || strcasecmp(Var, "MaxConnections") == 0
 	    || strcasecmp(Var, "MaxConnectionsIP") == 0
 	    || strcasecmp(Var, "MaxJoins") == 0
@@ -1490,6 +1493,12 @@ Handle_LIMITS(int Line, char *Var, char *Arg)
 		}
 		return;
 	}
+	if (strcasecmp(Var, "IdleTimeout") == 0) {
+		Conf_IdleTimeout = atoi(Arg);
+		if (!Conf_IdleTimeout && strcmp(Arg, "0"))
+			Config_Error_NaN(Line, Var);
+		return;
+	}
 	if (strcasecmp(Var, "MaxConnections") == 0) {
 		Conf_MaxConnections = atoi(Arg);
 		if (!Conf_MaxConnections && strcmp(Arg, "0"))
diff --git a/src/ngircd/conf.h b/src/ngircd/conf.h
index c203b570..bbf4f36c 100644
--- a/src/ngircd/conf.h
+++ b/src/ngircd/conf.h
@@ -1,6 +1,6 @@
 /*
  * ngIRCd -- The Next Generation IRC Daemon
- * Copyright (c)2001-2012 Alexander Barton (alex@barton.de) and Contributors.
+ * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
  *
  * 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
@@ -211,6 +211,9 @@ GLOBAL bool Conf_ConnectIPv6;
 /** Try to connect to remote systems using the IPv4 protocol (true) */
 GLOBAL bool Conf_ConnectIPv4;
 
+/** Idle timout (seconds), after which the daemon should exit */
+GLOBAL int Conf_IdleTimeout;
+
 /** Maximum number of simultaneous connections to this server */
 GLOBAL int Conf_MaxConnections;
 
diff --git a/src/ngircd/conn.c b/src/ngircd/conn.c
index 378509f9..cfa67eaf 100644
--- a/src/ngircd/conn.c
+++ b/src/ngircd/conn.c
@@ -1,6 +1,6 @@
 /*
  * ngIRCd -- The Next Generation IRC Daemon
- * Copyright (c)2001-2012 Alexander Barton (alex@barton.de) and Contributors.
+ * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
  *
  * 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
@@ -121,6 +121,8 @@ static void cb_Read_Resolver_Result PARAMS((int sock, UNUSED short what));
 static void cb_Connect_to_Server PARAMS((int sock, UNUSED short what));
 static void cb_clientserver PARAMS((int sock, short what));
 
+time_t idle_t = 0;
+
 
 /**
  * Get number of sockets available from systemd(8).
@@ -906,6 +908,15 @@ Conn_Handler(void)
 			    PACKAGE_NAME);
 			exit(1);
 		}
+
+		/* Should ngIRCd timeout when idle? */
+		if (Conf_IdleTimeout > 0 && NumConnectionsAccepted > 0
+		    && idle_t > 0 && time(NULL) - idle_t >= Conf_IdleTimeout) {
+			LogDebug("Server idle timeout reached: %d second%s. Initiating shutdown ...",
+				 Conf_IdleTimeout,
+				 Conf_IdleTimeout == 1 ? "" : "s");
+			NGIRCd_SignalQuit = true;
+		}
 	}
 
 	if (NGIRCd_SignalQuit)
@@ -1267,6 +1278,8 @@ Conn_Close( CONN_ID Idx, const char *LogMsg, const char *FwdMsg, bool InformClie
 		NumConnections--;
 	LogDebug("Shutdown of connection %d completed, %ld connection%s left.",
 		 Idx, NumConnections, NumConnections != 1 ? "s" : "");
+
+	idle_t = NumConnections > 0 ? 0 : time(NULL);
 } /* Conn_Close */
 
 
@@ -1638,6 +1651,7 @@ static void
 Account_Connection(void)
 {
 	NumConnections++;
+	idle_t = 0;
 	if (NumConnections > NumConnectionsMax)
 		NumConnectionsMax = NumConnections;
 	LogDebug("Total number of connections now %lu (max %lu).",