[strongSwan-dev] [PATCH 2/5] Add printf hook for tasks of an IKE_SA

Thomas Egerer thomas.egerer at secunet.com
Sun Jun 6 22:53:42 CEST 2010


If used the format specifier %M prints the number of passive, queued,
and active tasks in an IKE_SA plus the string representation of the
tasks in the particular list.
---
 src/libcharon/daemon.c          |    5 +++
 src/libcharon/sa/ike_sa.c       |   13 ++++++++
 src/libcharon/sa/ike_sa.h       |   12 +++++++
 src/libcharon/sa/task_manager.c |   65 +++++++++++++++++++++++++++++++++++++++
 src/libcharon/sa/task_manager.h |   11 ++++++
 5 files changed, 106 insertions(+), 0 deletions(-)

diff --git a/src/libcharon/daemon.c b/src/libcharon/daemon.c
index b338f6e..30a8bc7 100644
--- a/src/libcharon/daemon.c
+++ b/src/libcharon/daemon.c
@@ -437,6 +437,11 @@ bool libcharon_init()
 								  proposal_printf_hook,
 								  PRINTF_HOOK_ARGTYPE_POINTER,
 								  PRINTF_HOOK_ARGTYPE_END);
+	lib->printf_hook->add_handler(lib->printf_hook, 'M',
+								  task_manager_printf_hook,
+								  PRINTF_HOOK_ARGTYPE_POINTER,
+								  PRINTF_HOOK_ARGTYPE_POINTER,
+								  PRINTF_HOOK_ARGTYPE_END);

 	if (lib->integrity &&
 		!lib->integrity->check(lib->integrity, "libcharon", libcharon_init))
diff --git a/src/libcharon/sa/ike_sa.c b/src/libcharon/sa/ike_sa.c
index 0b77f5f..8e31572 100644
--- a/src/libcharon/sa/ike_sa.c
+++ b/src/libcharon/sa/ike_sa.c
@@ -2022,6 +2022,19 @@ static status_t inherit(private_ike_sa_t *this, private_ike_sa_t *other)
 }

 /**
+ * Implementation of ike_sa_t.task_manager_printf_hook.
+ */
+int task_manager_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
+						 const void *const *args)
+{
+	private_ike_sa_t *this = *((private_ike_sa_t **)(args[0]));
+	const char *prefix = *((const char **)(args[1]));
+
+	return this->task_manager->print_task_queues(this->task_manager, dst, len,
+			prefix ? prefix : "");
+}
+
+/**
  * Implementation of ike_sa_t.destroy.
  */
 static void destroy(private_ike_sa_t *this)
diff --git a/src/libcharon/sa/ike_sa.h b/src/libcharon/sa/ike_sa.h
index c61502e..a7671e6 100644
--- a/src/libcharon/sa/ike_sa.h
+++ b/src/libcharon/sa/ike_sa.h
@@ -911,6 +911,18 @@ struct ike_sa_t {
 };

 /**
+ * printf hook function for task_manager_t. This hook actually belongs
+ * into task_manager_t, but since the task_manager is a private member
+ * of the ike_sa it resides in here.
+ *
+ * arguments are:
+ *    ike_sa_t *ike_sa
+ *    const char *prefix
+ */
+int task_manager_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
+		const void *const *args);
+
+/**
  * Creates an ike_sa_t object with a specific ID.
  *
  * @param ike_sa_id 	ike_sa_id_t object to associate with new IKE_SA
diff --git a/src/libcharon/sa/task_manager.c b/src/libcharon/sa/task_manager.c
index 4c8f3be..ecf77ed 100644
--- a/src/libcharon/sa/task_manager.c
+++ b/src/libcharon/sa/task_manager.c
@@ -1029,6 +1029,70 @@ static void reset(private_task_manager_t *this,
 	this->reset = TRUE;
 }

+static size_t print_task_queue(linked_list_t *queue, char **dst, size_t *len,
+		const char *prefix)
+{
+	task_t *task;
+	int printed = 0;
+	bool first = TRUE;
+	size_t written = 0;
+	enumerator_t *enumerator;
+
+	enumerator = queue->create_enumerator(queue);
+	while (enumerator->enumerate(enumerator, (void **)&task))
+	{
+		if (first)
+		{
+			written += print_in_hook(*dst, *len, "%s    ", prefix);
+			first = FALSE;
+		}
+		else
+		{
+			written += print_in_hook(*dst, *len, ",");
+		}
+
+		if (printed == 3)
+		{
+			written += print_in_hook(*dst, *len, "\n%s    ", prefix);
+			printed = 0;
+		}
+
+		written += print_in_hook(*dst, *len, " %N", task_type_names,
+				task->get_type(task));
+		++printed;
+
+	}
+	enumerator->destroy(enumerator);
+	if (written)
+	{
+		written += print_in_hook(*dst, *len, "\n");
+	}
+
+	return written;
+}
+
+/**
+ * Implementation of task_manager_t.print_task_queues
+ */
+static size_t print_task_queues(private_task_manager_t *this, char *dst,
+		size_t len, const char *prefix)
+{
+	size_t written;
+
+	written =  print_in_hook(dst, len, "%s Task-Manager status:\n", prefix);
+	written += print_in_hook(dst, len, "%s   %d Passive Tasks:\n", prefix,
+			this->passive_tasks->get_count(this->passive_tasks));
+	written += print_task_queue(this->passive_tasks, &dst, &len, prefix);
+	written += print_in_hook(dst, len, "%s   %d Queued Tasks:\n", prefix,
+			this->queued_tasks->get_count(this->queued_tasks));
+	written += print_task_queue(this->queued_tasks, &dst, &len, prefix);
+	written += print_in_hook(dst, len, "%s   %d Active Tasks:\n", prefix,
+			this->active_tasks->get_count(this->active_tasks));
+	written += print_task_queue(this->active_tasks, &dst, &len, prefix);
+
+	return written;
+}
+
 /**
  * Implementation of task_manager_t.destroy
  */
@@ -1059,6 +1123,7 @@ task_manager_t *task_manager_create(ike_sa_t *ike_sa)
 	this->public.reset = (void(*)(task_manager_t*,u_int32_t,u_int32_t))reset;
 	this->public.adopt_tasks = (void(*)(task_manager_t*,task_manager_t*))adopt_tasks;
 	this->public.busy = (bool(*)(task_manager_t*))busy;
+	this->public.print_task_queues = (size_t(*)(task_manager_t*,char*,size_t,const char*))print_task_queues;
 	this->public.destroy = (void(*)(task_manager_t*))destroy;

 	this->ike_sa = ike_sa;
diff --git a/src/libcharon/sa/task_manager.h b/src/libcharon/sa/task_manager.h
index 731ed48..003ab7b 100644
--- a/src/libcharon/sa/task_manager.h
+++ b/src/libcharon/sa/task_manager.h
@@ -158,6 +158,17 @@ struct task_manager_t {
 	bool (*busy) (task_manager_t *this);

 	/**
+	 * Print tasks queued to active, passive and queued task queue to buffer
+	 * with the given length.
+	 *
+	 * @param dst			buffer to write to
+	 * @param len			length of the buffer
+	 * @param width			string to prefix to output
+	 * @return				number of bytes written to dst
+	 */
+	size_t (*print_task_queues)(task_manager_t *this, char *dst, size_t len, const char *prefix);
+
+	/**
 	 * Destroy the task_manager_t.
 	 */
 	void (*destroy) (task_manager_t *this);
-- 1.5.6.5




More information about the Dev mailing list