Blame SOURCES/freeradius-connection-fall-through-to-global-module-triggers.patch

75e927
From 132992fe92d53d62499d8c4672feafe210efc573 Mon Sep 17 00:00:00 2001
75e927
From: Nikolai Kondrashov <Nikolai.Kondrashov@redhat.com>
75e927
Date: Fri, 24 Oct 2014 14:37:11 +0300
75e927
Subject: [PATCH 4/4] connection: Fall through to global module triggers
75e927
75e927
Make module connection pool triggers use global module trigger
75e927
configuration, if there is no "trigger" section in the pool section.
75e927
75e927
Use fully-qualified module-specific trigger names for module-specific
75e927
connection pools in connection.c.
75e927
75e927
E.g. trigger "modules.ldap.open", instead of just "open" for pools
75e927
initialized with fr_connection_pool_module_init, being passed "ldap"
75e927
config section.
75e927
75e927
Send triggers even if the pool has no "trigger" section.
75e927
75e927
This makes exec_trigger fall through to global module triggers, if the
75e927
pool configuration doesn't have the "trigger" section.
75e927
---
75e927
 src/include/connection.h |  3 +-
75e927
 src/main/connection.c    | 81 ++++++++++++++++++++++++++++++------------------
75e927
 2 files changed, 53 insertions(+), 31 deletions(-)
75e927
75e927
diff --git a/src/include/connection.h b/src/include/connection.h
75e927
index e3752d5..eaf44e8 100644
75e927
--- a/src/include/connection.h
75e927
+++ b/src/include/connection.h
75e927
@@ -81,7 +81,8 @@ fr_connection_pool_t *fr_connection_pool_init(CONF_SECTION *parent,
75e927
 					      void *opaque,
75e927
 					      fr_connection_create_t c,
75e927
 					      fr_connection_alive_t a,
75e927
-					      char const *prefix);
75e927
+					      char const *log_prefix,
75e927
+					      char const *trigger_prefix);
75e927
 void fr_connection_pool_delete(fr_connection_pool_t *pool);
75e927
 
75e927
 void *fr_connection_get(fr_connection_pool_t *pool);
75e927
diff --git a/src/main/connection.c b/src/main/connection.c
75e927
index 5f0c8f6..aec4f9d 100644
75e927
--- a/src/main/connection.c
75e927
+++ b/src/main/connection.c
75e927
@@ -110,10 +110,6 @@ struct fr_connection_pool_t {
75e927
 	uint32_t       	idle_timeout;	//!< How long a connection can be idle
75e927
 					//!< before being closed.
75e927
 
75e927
-	bool		trigger;	//!< If true execute connection triggers
75e927
-					//!< associated with the connection
75e927
-					//!< pool.
75e927
-
75e927
 	bool		spread;		//!< If true requests will be spread
75e927
 					//!< across all connections, instead of
75e927
 					//!< re-using the most recently used
75e927
@@ -158,6 +154,11 @@ struct fr_connection_pool_t {
75e927
 					//!< messages created by the connection
75e927
 					//!< pool code.
75e927
 
75e927
+	char const	*trigger_prefix;	//!< Prefix to prepend to
75e927
+						//!< names of all triggers
75e927
+						//!< fired by the connection
75e927
+						//!< pool code.
75e927
+
75e927
 	fr_connection_create_t	create;	//!< Function used to create new
75e927
 					//!< connections.
75e927
 	fr_connection_alive_t	alive;	//!< Function used to check status
75e927
@@ -271,6 +272,20 @@ static void fr_connection_link_tail(fr_connection_pool_t *pool,
75e927
 	}
75e927
 }
75e927
 
75e927
+/** Send a connection pool trigger.
75e927
+ *
75e927
+ * @param[in] pool to send trigger for.
75e927
+ * @param[in] name_suffix trigger name suffix.
75e927
+ */
75e927
+static void fr_connection_exec_trigger(fr_connection_pool_t *pool,
75e927
+					char const *name_suffix)
75e927
+{
75e927
+	char name[64];
75e927
+	rad_assert(pool != NULL);
75e927
+	rad_assert(name_suffix != NULL);
75e927
+	snprintf(name, sizeof(name), "%s%s", pool->trigger_prefix, name_suffix);
75e927
+	exec_trigger(NULL, pool->cs, name, true);
75e927
+}
75e927
 
75e927
 /** Spawns a new connection
75e927
  *
75e927
@@ -403,7 +418,7 @@ static fr_connection_t *fr_connection_spawn(fr_connection_pool_t *pool,
75e927
 
75e927
 	pthread_mutex_unlock(&pool->mutex);
75e927
 
75e927
-	if (pool->trigger) exec_trigger(NULL, pool->cs, "open", true);
75e927
+	fr_connection_exec_trigger(pool, "open");
75e927
 
75e927
 	return this;
75e927
 }
75e927
@@ -436,7 +451,7 @@ static void fr_connection_close(fr_connection_pool_t *pool,
75e927
 		pool->active--;
75e927
 	}
75e927
 
75e927
-	if (pool->trigger) exec_trigger(NULL, pool->cs, "close", true);
75e927
+	fr_connection_exec_trigger(pool, "close");
75e927
 
75e927
 	fr_connection_unlink(pool, this);
75e927
 	rad_assert(pool->num > 0);
75e927
@@ -542,7 +557,7 @@ void fr_connection_pool_delete(fr_connection_pool_t *pool)
75e927
 		fr_connection_close(pool, this);
75e927
 	}
75e927
 
75e927
-	if (pool->trigger) exec_trigger(NULL, pool->cs, "stop", true);
75e927
+	fr_connection_exec_trigger(pool, "stop");
75e927
 
75e927
 	rad_assert(pool->head == NULL);
75e927
 	rad_assert(pool->tail == NULL);
75e927
@@ -559,33 +574,36 @@ void fr_connection_pool_delete(fr_connection_pool_t *pool)
75e927
  * @param[in] opaque data pointer to pass to callbacks.
75e927
  * @param[in] c Callback to create new connections.
75e927
  * @param[in] a Callback to check the status of connections.
75e927
- * @param[in] prefix override, if NULL will be set automatically from the module CONF_SECTION.
75e927
+ * @param[in] log_prefix override, if NULL will be set automatically from the module CONF_SECTION.
75e927
  * @return A new connection pool or NULL on error.
75e927
  */
75e927
 fr_connection_pool_t *fr_connection_pool_module_init(CONF_SECTION *module,
75e927
 						     void *opaque,
75e927
 						     fr_connection_create_t c,
75e927
 						     fr_connection_alive_t a,
75e927
-						     char const *prefix)
75e927
+						     char const *log_prefix)
75e927
 {
75e927
 	CONF_SECTION *cs, *mycs;
75e927
 	char buff[128];
75e927
+	char trigger_prefix[64];
75e927
 
75e927
 	fr_connection_pool_t *pool;
75e927
+	char const *cs_name1, *cs_name2;
75e927
 
75e927
 	int ret;
75e927
 
75e927
 #define CONNECTION_POOL_CF_KEY "connection_pool"
75e927
 #define parent_name(_x) cf_section_name(cf_item_parent(cf_sectiontoitem(_x)))
75e927
 
75e927
-	if (!prefix) {
75e927
-		char const *cs_name1, *cs_name2;
75e927
-		cs_name1 = cf_section_name1(module);
75e927
-		cs_name2 = cf_section_name2(module);
75e927
-		if (!cs_name2) cs_name2 = cs_name1;
75e927
+	cs_name1 = cf_section_name1(module);
75e927
+	cs_name2 = cf_section_name2(module);
75e927
+	if (!cs_name2) cs_name2 = cs_name1;
75e927
+
75e927
+	snprintf(trigger_prefix, sizeof(trigger_prefix), "modules.%s.", cs_name1);
75e927
 
75e927
+	if (!log_prefix) {
75e927
 		snprintf(buff, sizeof(buff), "rlm_%s (%s)", cs_name1, cs_name2);
75e927
-		prefix = buff;
75e927
+		log_prefix = buff;
75e927
 	}
75e927
 
75e927
 	/*
75e927
@@ -597,11 +615,11 @@ fr_connection_pool_t *fr_connection_pool_module_init(CONF_SECTION *module,
75e927
 		return NULL;
75e927
 
75e927
 	case 1:
75e927
-		DEBUG4("%s: Using pool section from \"%s\"", prefix, parent_name(cs));
75e927
+		DEBUG4("%s: Using pool section from \"%s\"", log_prefix, parent_name(cs));
75e927
 		break;
75e927
 
75e927
 	case 0:
75e927
-		DEBUG4("%s: Using local pool section", prefix);
75e927
+		DEBUG4("%s: Using local pool section", log_prefix);
75e927
 		break;
75e927
 	}
75e927
 
75e927
@@ -610,7 +628,7 @@ fr_connection_pool_t *fr_connection_pool_module_init(CONF_SECTION *module,
75e927
 	 */
75e927
 	mycs = cf_section_sub_find(module, "pool");
75e927
 	if (!mycs) {
75e927
-		DEBUG4("%s: Adding pool section to \"%s\" to store pool references", prefix,
75e927
+		DEBUG4("%s: Adding pool section to \"%s\" to store pool references", log_prefix,
75e927
 		       cf_section_name(module));
75e927
 
75e927
 		mycs = cf_section_alloc(module, "pool", NULL);
75e927
@@ -622,7 +640,7 @@ fr_connection_pool_t *fr_connection_pool_module_init(CONF_SECTION *module,
75e927
 	 *	Use our own local pool.
75e927
 	 */
75e927
 	if (!cs) {
75e927
-		DEBUG4("%s: \"%s.pool\" section not found, using \"%s.pool\"", prefix,
75e927
+		DEBUG4("%s: \"%s.pool\" section not found, using \"%s.pool\"", log_prefix,
75e927
 		       parent_name(cs), parent_name(mycs));
75e927
 		cs = mycs;
75e927
 	}
75e927
@@ -636,16 +654,16 @@ fr_connection_pool_t *fr_connection_pool_module_init(CONF_SECTION *module,
75e927
 	 */
75e927
 	pool = cf_data_find(cs, CONNECTION_POOL_CF_KEY);
75e927
 	if (!pool) {
75e927
-		DEBUG4("%s: No pool reference found in \"%s.pool\"", prefix, parent_name(cs));
75e927
-		pool = fr_connection_pool_init(module, cs, opaque, c, a, prefix);
75e927
+		DEBUG4("%s: No pool reference found in \"%s.pool\"", log_prefix, parent_name(cs));
75e927
+		pool = fr_connection_pool_init(module, cs, opaque, c, a, log_prefix, trigger_prefix);
75e927
 		if (!pool) return NULL;
75e927
 
75e927
-		DEBUG4("%s: Adding pool reference %p to \"%s.pool\"", prefix, pool, parent_name(cs));
75e927
+		DEBUG4("%s: Adding pool reference %p to \"%s.pool\"", log_prefix, pool, parent_name(cs));
75e927
 		cf_data_add(cs, CONNECTION_POOL_CF_KEY, pool, NULL);
75e927
 		return pool;
75e927
 	}
75e927
 
75e927
-	DEBUG4("%s: Found pool reference %p in \"%s.pool\"", prefix, pool, parent_name(cs));
75e927
+	DEBUG4("%s: Found pool reference %p in \"%s.pool\"", log_prefix, pool, parent_name(cs));
75e927
 
75e927
 	/*
75e927
 	 *	We're reusing pool data add it to our local config
75e927
@@ -653,7 +671,7 @@ fr_connection_pool_t *fr_connection_pool_module_init(CONF_SECTION *module,
75e927
 	 *	re-use a pool through this module.
75e927
 	 */
75e927
 	if (mycs != cs) {
75e927
-		DEBUG4("%s: Copying pool reference %p from \"%s.pool\" to \"%s.pool\"", prefix, pool,
75e927
+		DEBUG4("%s: Copying pool reference %p from \"%s.pool\" to \"%s.pool\"", log_prefix, pool,
75e927
 		       parent_name(cs), parent_name(mycs));
75e927
 		cf_data_add(mycs, CONNECTION_POOL_CF_KEY, pool, NULL);
75e927
 	}
75e927
@@ -676,7 +694,8 @@ fr_connection_pool_t *fr_connection_pool_module_init(CONF_SECTION *module,
75e927
  * @param[in] opaque data pointer to pass to callbacks.
75e927
  * @param[in] c Callback to create new connections.
75e927
  * @param[in] a Callback to check the status of connections.
75e927
- * @param[in] prefix to prepend to all log messages.
75e927
+ * @param[in] log_prefix prefix to prepend to all log messages.
75e927
+ * @param[in] trigger_prefix prefix to prepend to all trigger names.
75e927
  * @return A new connection pool or NULL on error.
75e927
  */
75e927
 fr_connection_pool_t *fr_connection_pool_init(CONF_SECTION *parent,
75e927
@@ -684,7 +703,8 @@ fr_connection_pool_t *fr_connection_pool_init(CONF_SECTION *parent,
75e927
 					      void *opaque,
75e927
 					      fr_connection_create_t c,
75e927
 					      fr_connection_alive_t a,
75e927
-					      char const *prefix)
75e927
+					      char const *log_prefix,
75e927
+					      char const *trigger_prefix)
75e927
 {
75e927
 	uint32_t i;
75e927
 	fr_connection_pool_t *pool;
75e927
@@ -720,7 +740,9 @@ fr_connection_pool_t *fr_connection_pool_init(CONF_SECTION *parent,
75e927
 
75e927
 	pool->head = pool->tail = NULL;
75e927
 
75e927
-	pool->log_prefix = prefix ? talloc_typed_strdup(pool, prefix) : "core";
75e927
+	pool->log_prefix = log_prefix ? talloc_typed_strdup(pool, log_prefix) : "core";
75e927
+	pool->trigger_prefix = trigger_prefix ?
75e927
+					talloc_typed_strdup(pool, trigger_prefix) : "";
75e927
 
75e927
 #ifdef HAVE_PTHREAD_H
75e927
 	pthread_mutex_init(&pool->mutex, NULL);
75e927
@@ -729,7 +751,6 @@ fr_connection_pool_t *fr_connection_pool_init(CONF_SECTION *parent,
75e927
 	DEBUG("%s: Initialising connection pool", pool->log_prefix);
75e927
 
75e927
 	if (cf_section_parse(cs, pool, connection_config) < 0) goto error;
75e927
-	if (cf_section_sub_find(cs, "trigger")) pool->trigger = true;
75e927
 
75e927
 	/*
75e927
 	 *	Some simple limits
75e927
@@ -780,7 +801,7 @@ fr_connection_pool_t *fr_connection_pool_init(CONF_SECTION *parent,
75e927
 		}
75e927
 	}
75e927
 
75e927
-	if (pool->trigger) exec_trigger(NULL, pool->cs, "start", true);
75e927
+	fr_connection_exec_trigger(pool, "start");
75e927
 
75e927
 	return pool;
75e927
 }
75e927
@@ -1222,7 +1243,7 @@ void *fr_connection_reconnect(fr_connection_pool_t *pool, void *conn)
75e927
 		return NULL;
75e927
 	}
75e927
 
75e927
-	if (pool->trigger) exec_trigger(NULL, pool->cs, "close", true);
75e927
+	fr_connection_exec_trigger(pool, "close");
75e927
 	this->connection = new_conn;
75e927
 	pthread_mutex_unlock(&pool->mutex);
75e927
 
75e927
-- 
75e927
2.1.1
75e927