mod_netconf: close NETCONF sessions

Close all inactive NETCONF sessions.
Close all NETCONF sessions on exit.

- refs #970
diff --git a/src/mod_netconf.c b/src/mod_netconf.c
index 3142225..8d7476d 100644
--- a/src/mod_netconf.c
+++ b/src/mod_netconf.c
@@ -73,12 +73,15 @@
 #endif
 
 #include "message_type.h"
-
+#include "mod_netconf.h"
 
 #define MAX_PROCS 5
 #define SOCKET_FILENAME "/tmp/mod_netconf.sock"
 #define MAX_SOCKET_CL 10
 #define BUFFER_SIZE 4096
+#define NOTIFICATION_QUEUE_SIZE 10
+#define ACTIVITY_CHECK_INTERVAL	10  /**< timeout in seconds, how often activity is checked */
+#define ACTIVITY_TIMEOUT	180  /**< timeout in seconds, after this time, session is automaticaly closed. */
 
 /* sleep in master process for non-blocking socket reading */
 #define SLEEP_TIME 200
@@ -93,7 +96,6 @@
 #define NCWITHDEFAULTS	NCWD_MODE_NOTSET
 
 
-
 #define MSG_OK 0
 #define MSG_OPEN  1
 #define MSG_DATA  2
@@ -103,31 +105,12 @@
 
 module AP_MODULE_DECLARE_DATA netconf_module;
 
-typedef struct {
-	apr_pool_t *pool;
-	apr_proc_t *forkproc;
-	char* sockname;
-} mod_netconf_cfg;
-
-struct pass_to_thread {
-	int client; /**< opened socket */
-	apr_pool_t * pool; /**< ?? */
-	server_rec * server; /**< ?? */
-	apr_hash_t * netconf_sessions_list; /**< ?? */
-};
-
-struct session_with_mutex {
-	struct nc_session * session; /**< netconf session */
-	pthread_mutex_t lock; /**< mutex protecting the session from multiple access */
-};
-
 pthread_rwlock_t session_lock; /**< mutex protecting netconf_session_list from multiple access errors */
 
 volatile int isterminated = 0;
 
 static char* password;
 
-
 static void signal_handler(int sign)
 {
 	switch (sign) {
@@ -247,6 +230,7 @@
 			return NULL;
 		}
 		locked_session->session = session;
+		locked_session->last_activity = apr_time_now();
 		pthread_mutex_init (&locked_session->lock, NULL);
 		ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_lock");
 		/* get exclusive access to sessions_list (conns) */
@@ -350,6 +334,7 @@
 			}
 			return EXIT_FAILURE;
 		}
+		locked_session->last_activity = apr_time_now();
 		/* send the request and get the reply */
 		msgt = nc_session_send_recv(session, rpc, &reply);
 
@@ -437,6 +422,7 @@
 			}
 			return NULL;
 		}
+		locked_session->last_activity = apr_time_now();
 		/* send the request and get the reply */
 		msgt = nc_session_send_recv(session, rpc, &reply);
 
@@ -1309,13 +1295,83 @@
 			}
 		}
 	}
-
 	free (arg);
 
 	return retval;
 }
 
-/*
+/**
+ * \brief Close all open NETCONF sessions.
+ *
+ * During termination of mod_netconf, it is useful to close all remaining
+ * sessions. This function iterates over the list of sessions and close them
+ * all.
+ *
+ * \param[in] server  pointer to server_rec for logging
+ * \param[in] p  apr pool needed for hash table iterating
+ * \param[in] ht  hash table of session_with_mutex structs
+ */
+static void close_all_nc_sessions(server_rec* server, apr_pool_t *p, apr_hash_t *ht)
+{
+	apr_hash_index_t *hi;
+	void *val = NULL;
+	struct session_with_mutex *swm = NULL;
+	struct nc_session *ns = NULL;
+	const char *hashed_key = NULL;
+	apr_ssize_t hashed_key_length;
+
+	for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
+		apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
+		ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
+		swm = (struct session_with_mutex *) val;
+		if (swm != NULL) {
+			pthread_mutex_lock(&swm->lock);
+			if (swm->session != NULL) {
+				ns = swm->session;
+				nc_session_close(ns, NC_SESSION_TERM_CLOSED);
+				nc_session_free(ns);
+				swm->session = NULL;
+			}
+			pthread_mutex_unlock(&swm->lock);
+		}
+	}
+}
+
+static void check_timeout_and_close(server_rec* server, apr_pool_t *p, apr_hash_t *ht)
+{
+	apr_hash_index_t *hi;
+	void *val = NULL;
+	struct nc_session *ns = NULL;
+	struct session_with_mutex *swm = NULL;
+	const char *hashed_key = NULL;
+	apr_ssize_t hashed_key_length;
+	apr_time_t current_time = apr_time_now();
+
+	for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
+		apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
+		swm = (struct session_with_mutex *) val;
+		if (swm == NULL) {
+			continue;
+		}
+		ns = swm->session;
+		if (ns == NULL) {
+			continue;
+		}
+		pthread_mutex_lock(&swm->lock);
+		if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
+			ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
+			nc_session_close(ns, NC_SESSION_TERM_CLOSED);
+			nc_session_free (ns);
+			ns = NULL;
+			/* remove session from the active sessions list */
+			apr_hash_set(ht, hashed_key, APR_HASH_KEY_STRING, NULL);
+		}
+		pthread_mutex_unlock(&swm->lock);
+	}
+}
+
+
+/**
  * This is actually implementation of NETCONF client
  * - requests are received from UNIX socket in the predefined format
  * - results are replied through the same way
@@ -1328,8 +1384,7 @@
 	struct timeval tv;
 	struct sockaddr_un local, remote;
 	int lsock, client, ret, i, pthread_count = 0;
-	char use_notifications = 0;
-	unsigned int olds = 0;
+	unsigned int olds = 0, timediff = 0;
 	socklen_t len;
 	mod_netconf_cfg *cfg;
 	apr_hash_t *netconf_sessions_list;
@@ -1417,14 +1472,18 @@
 	fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
 	while (isterminated == 0) {
 		gettimeofday(&tv, NULL);
+		timediff = (unsigned int)tv.tv_sec - olds;
 		#ifdef WITH_NOTIFICATIONS
-		if (((unsigned int)tv.tv_sec - olds) > 60) {
+		if (timediff > 60) {
 			ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "handling notifications");
 		}
 		if (use_notifications == 1) {
 			notification_handle();
 		}
 		#endif
+		if (timediff > ACTIVITY_CHECK_INTERVAL) {
+			check_timeout_and_close(server, pool, netconf_sessions_list);
+		}
 
 		/* open incoming connection if any */
 		len = sizeof(remote);
@@ -1476,6 +1535,7 @@
 		ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Running %d threads", pthread_count);
 	}
 
+	ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf terminating...");
 	/* join all threads */
 	for (i=0; i<pthread_count; i++) {
 		pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
@@ -1488,6 +1548,9 @@
 	notification_close();
 	#endif
 
+	/* close all NETCONF sessions */
+	close_all_nc_sessions(server, pool, netconf_sessions_list);
+
 	/* destroy rwlock */
 	pthread_rwlock_destroy(&session_lock);
 	pthread_rwlockattr_destroy(&lock_attrs);
diff --git a/src/mod_netconf.h b/src/mod_netconf.h
new file mode 100644
index 0000000..bc97002
--- /dev/null
+++ b/src/mod_netconf.h
@@ -0,0 +1,84 @@
+/*!
+ * \file mod_netconf.c
+ * \brief NETCONF Apache modul for Netopeer
+ * \author Tomas Cejka <cejkat@cesnet.cz>
+ * \author Radek Krejci <rkrejci@cesnet.cz>
+ * \date 2011
+ * \date 2012
+ * \date 2013
+ */
+/*
+ * Copyright (C) 2011-2013 CESNET
+ *
+ * LICENSE TERMS
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ * 3. Neither the name of the Company nor the names of its contributors
+ *    may be used to endorse or promote products derived from this
+ *    software without specific prior written permission.
+ *
+ * ALTERNATIVELY, provided that this notice is retained in full, this
+ * product may be distributed under the terms of the GNU General Public
+ * License (GPL) version 2 or later, in which case the provisions
+ * of the GPL apply INSTEAD OF those given above.
+ *
+ * This software is provided ``as is'', and any express or implied
+ * warranties, including, but not limited to, the implied warranties of
+ * merchantability and fitness for a particular purpose are disclaimed.
+ * In no event shall the company or contributors be liable for any
+ * direct, indirect, incidental, special, exemplary, or consequential
+ * damages (including, but not limited to, procurement of substitute
+ * goods or services; loss of use, data, or profits; or business
+ * interruption) however caused and on any theory of liability, whether
+ * in contract, strict liability, or tort (including negligence or
+ * otherwise) arising in any way out of the use of this software, even
+ * if advised of the possibility of such damage.
+ *
+ */
+#ifndef __MOD_NETCONF_COMMON_H
+#define __MOD_NETCONF_COMMON_H
+
+#include <pthread.h>
+#include <httpd.h>
+#include <http_log.h>
+#include <http_config.h>
+#include <apr_hash.h>
+
+struct pass_to_thread {
+	int client; /**< opened socket */
+	apr_pool_t * pool; /**< ?? */
+	server_rec * server; /**< ?? */
+	apr_hash_t * netconf_sessions_list; /**< ?? */
+};
+
+typedef struct notification {
+	time_t eventtime;
+	char* content;
+} notification_t;
+
+struct session_with_mutex {
+	struct nc_session * session; /**< netconf session */
+	apr_array_header_t *notifications;
+	apr_time_t last_activity;
+	pthread_mutex_t lock; /**< mutex protecting the session from multiple access */
+};
+
+typedef struct {
+	apr_pool_t *pool;
+	apr_proc_t *forkproc;
+	char* sockname;
+} mod_netconf_cfg;
+
+
+extern pthread_rwlock_t session_lock; /**< mutex protecting netconf_session_list from multiple access errors */
+
+#endif
+