* for more details.
*/
+#define _GNU_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <getopt.h>
+#include <dlfcn.h>
#include <libgen.h>
#include "conftest.h"
+#include "hooks/hook.h"
#include <threading/thread.h>
/**
* Print usage information
*/
-static void usage(char *error)
+static void usage(FILE *out)
{
- FILE *out = stdout;
-
- if (error)
- {
- out = stderr;
- fprintf(out, "%s\n", error);
- }
- else
- {
- fprintf(out, "strongSwan %s conftest\n", VERSION);
- }
fprintf(out, "Usage:\n");
fprintf(out, " --help show usage information\n");
fprintf(out, " --version show conftest version\n");
return TRUE;
}
+/**
+ * Load configured hooks
+ */
+static bool load_hooks()
+{
+ enumerator_t *enumerator;
+ char *name, buf[64];
+ hook_t *(*create)(void);
+ hook_t *hook;
+
+ enumerator = conftest->test->create_section_enumerator(conftest->test,
+ "hooks");
+ while (enumerator->enumerate(enumerator, &name))
+ {
+ snprintf(buf, sizeof(buf), "%s_hook_create", name);
+ create = dlsym(RTLD_DEFAULT, buf);
+ if (create)
+ {
+ hook = create();
+ if (hook)
+ {
+ conftest->hooks->insert_last(conftest->hooks, hook);
+ charon->bus->add_listener(charon->bus, &hook->listener);
+ }
+ }
+ else
+ {
+ fprintf(stderr, "dlsym() for hook '%s' failed: %s\n", name, dlerror());
+ enumerator->destroy(enumerator);
+ return FALSE;
+ }
+ }
+ enumerator->destroy(enumerator);
+ return TRUE;
+}
+
/**
* atexit() cleanup handler
*/
static void cleanup()
{
+ hook_t *hook;
+
DESTROY_IF(conftest->suite);
DESTROY_IF(conftest->test);
lib->credmgr->remove_set(lib->credmgr, &conftest->creds->set);
conftest->creds->destroy(conftest->creds);
+ while (conftest->hooks->remove_last(conftest->hooks,
+ (void**)&hook) == SUCCESS)
+ {
+ charon->bus->remove_listener(charon->bus, &hook->listener);
+ hook->destroy(hook);
+ }
+ conftest->hooks->destroy(conftest->hooks);
free(conftest->suite_dir);
free(conftest->test_dir);
free(conftest);
case EOF:
break;
case 'h':
- usage(NULL);
+ usage(stdout);
return 0;
case 'v':
printf("strongSwan %s conftest\n", VERSION);
test_file = optarg;
continue;
default:
- usage("Invalid option.");
+ usage(stderr);
return 1;
}
break;
{
return 1;
}
+ if (!load_hooks())
+ {
+ return 1;
+ }
/* set up thread specific handlers */
action.sa_handler = segv_handler;
--- /dev/null
+/*
+ * Copyright (C) 2010 Martin Willi
+ * Copyright (C) 2010 revosec AG
+ *
+ * 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 the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+/**
+ * @defgroup hook hook
+ * @{ @ingroup hooks
+ */
+
+#ifndef HOOK_H_
+#define HOOK_H_
+
+typedef struct hook_t hook_t;
+
+#include <daemon.h>
+#include <conftest.h>
+
+/**
+ * Hook providing interface.
+ */
+struct hook_t {
+
+ /**
+ * Implements listener_t.
+ */
+ listener_t listener;
+
+ /**
+ * Destroy a hook_t.
+ */
+ void (*destroy)(hook_t *this);
+};
+
+#endif /** HOOK_H_ @}*/