summaryrefslogtreecommitdiff
path: root/src/libstrongswan/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstrongswan/utils')
-rw-r--r--src/libstrongswan/utils/leak_detective.c15
-rw-r--r--src/libstrongswan/utils/leak_detective.h18
-rw-r--r--src/libstrongswan/utils/settings.c20
-rw-r--r--src/libstrongswan/utils/test.c53
-rw-r--r--src/libstrongswan/utils/test.h34
5 files changed, 84 insertions, 56 deletions
diff --git a/src/libstrongswan/utils/leak_detective.c b/src/libstrongswan/utils/leak_detective.c
index 82eadcb97..af29e2100 100644
--- a/src/libstrongswan/utils/leak_detective.c
+++ b/src/libstrongswan/utils/leak_detective.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2013 Tobias Brunner
+ * Copyright (C) 2013-2014 Tobias Brunner
* Copyright (C) 2006-2013 Martin Willi
* Hochschule fuer Technik Rapperswil
*
@@ -973,17 +973,20 @@ leak_detective_t *leak_detective_create()
},
);
+ if (getenv("LEAK_DETECTIVE_DISABLE") != NULL)
+ {
+ free(this);
+ return NULL;
+ }
+
lock = spinlock_create();
thread_disabled = thread_value_create(NULL);
init_static_allocations();
- if (getenv("LEAK_DETECTIVE_DISABLE") == NULL)
+ if (register_hooks())
{
- if (register_hooks())
- {
- enable_leak_detective();
- }
+ enable_leak_detective();
}
return &this->public;
}
diff --git a/src/libstrongswan/utils/leak_detective.h b/src/libstrongswan/utils/leak_detective.h
index 3fd0b8c93..ca70067d4 100644
--- a/src/libstrongswan/utils/leak_detective.h
+++ b/src/libstrongswan/utils/leak_detective.h
@@ -50,9 +50,7 @@ typedef void (*leak_detective_summary_cb_t)(void* user, int count, size_t bytes,
int whitelisted);
/**
- * Leak detective finds leaks and bad frees using malloc hooks.
- *
- * Currently leaks are reported to stderr on destruction.
+ * Leak detective finds leaks and invalid frees using malloc hooks.
*
* @todo Build an API for leak detective, allowing leak enumeration, statistics
* and dynamic whitelisting.
@@ -62,13 +60,12 @@ struct leak_detective_t {
/**
* Report leaks to the registered callback functions.
*
- * @param detailed TRUE to resolve line/filename of leak (slow)
+ * @param detailed TRUE to resolve line/filename of leaks (slow)
*/
void (*report)(leak_detective_t *this, bool detailed);
/**
- * Report current memory usage to out.
- * Set callback functions invoked during a report().
+ * Set callback functions invoked when report() is called.
*
* @param cb callback invoked for each detected leak
* @param scb summary callback invoked at end of report
@@ -78,11 +75,11 @@ struct leak_detective_t {
leak_detective_summary_cb_t scb, void *user);
/**
- * Report current memory usage using a callbacks.
+ * Report current memory usage using callback functions.
*
* @param cb callback invoked for each allocation
* @param scb summary callback invoked at end of usage report
- * @param user user data supplied to callbacks
+ * @param user user data to supply to callbacks
*/
void (*usage)(leak_detective_t *this, leak_detective_report_cb_t cb,
leak_detective_summary_cb_t scb, void *user);
@@ -109,7 +106,10 @@ struct leak_detective_t {
};
/**
- * Create a leak_detective instance.
+ * Create a leak_detective instance, unless the LEAK_DETECTIVE_DISABLE
+ * environment variable is set.
+ *
+ * @return leak detective instance
*/
leak_detective_t *leak_detective_create();
diff --git a/src/libstrongswan/utils/settings.c b/src/libstrongswan/utils/settings.c
index 490490a1e..cf34fd1cf 100644
--- a/src/libstrongswan/utils/settings.c
+++ b/src/libstrongswan/utils/settings.c
@@ -1224,7 +1224,16 @@ static bool parse_file(linked_list_t *contents, char *file, int level,
{
if (errno == ENOENT)
{
- DBG2(DBG_LIB, "'%s' does not exist, ignored", file);
+#ifdef STRONGSWAN_CONF
+ if (streq(file, STRONGSWAN_CONF))
+ {
+ DBG2(DBG_LIB, "'%s' does not exist, ignored", file);
+ }
+ else
+#endif
+ {
+ DBG1(DBG_LIB, "'%s' does not exist, ignored", file);
+ }
return TRUE;
}
DBG1(DBG_LIB, "failed to stat '%s': %s", file, strerror(errno));
@@ -1244,8 +1253,8 @@ static bool parse_file(linked_list_t *contents, char *file, int level,
fseek(fd, 0, SEEK_END);
len = ftell(fd);
rewind(fd);
- text = malloc(len + 1);
- text[len] = '\0';
+ text = malloc(len + 2);
+ text[len] = text[len + 1] = '\0';
if (fread(text, 1, len, fd) != len)
{
free(text);
@@ -1287,7 +1296,7 @@ static bool parse_files(linked_list_t *contents, char *file, int level,
if (!strlen(pattern))
{
- DBG2(DBG_LIB, "empty include pattern, ignored");
+ DBG1(DBG_LIB, "empty include pattern, ignored");
return TRUE;
}
@@ -1318,7 +1327,7 @@ static bool parse_files(linked_list_t *contents, char *file, int level,
status = glob(pat, GLOB_ERR, NULL, &buf);
if (status == GLOB_NOMATCH)
{
- DBG2(DBG_LIB, "no files found matching '%s', ignored", pat);
+ DBG1(DBG_LIB, "no files found matching '%s', ignored", pat);
}
else if (status != 0)
{
@@ -1509,4 +1518,3 @@ settings_t *settings_create(char *file)
return &this->public;
}
-
diff --git a/src/libstrongswan/utils/test.c b/src/libstrongswan/utils/test.c
index 7de5a7661..624ac4b34 100644
--- a/src/libstrongswan/utils/test.c
+++ b/src/libstrongswan/utils/test.c
@@ -22,29 +22,46 @@
*/
hashtable_t *testable_functions;
+/**
+ * The function that actually initializes the hash table above. Provided
+ * by the test runner.
+ */
+void testable_functions_create() __attribute__((weak));
+
/*
* Described in header.
*/
void testable_function_register(char *name, void *fn)
{
- if (testable_functions)
+ bool old = FALSE;
+
+ if (!testable_functions_create)
+ { /* not linked to the test runner */
+ return;
+ }
+ else if (!fn && !testable_functions)
+ { /* ignore as testable_functions has already been destroyed */
+ return;
+ }
+
+ if (lib && lib->leak_detective)
+ {
+ old = lib->leak_detective->set_state(lib->leak_detective, FALSE);
+ }
+ if (!testable_functions)
+ {
+ testable_functions_create();
+ }
+ if (fn)
+ {
+ testable_functions->put(testable_functions, name, fn);
+ }
+ else
+ {
+ testable_functions->remove(testable_functions, name);
+ }
+ if (lib && lib->leak_detective)
{
- bool old = FALSE;
- if (lib->leak_detective)
- {
- old = lib->leak_detective->set_state(lib->leak_detective, FALSE);
- }
- if (fn)
- {
- testable_functions->put(testable_functions, name, fn);
- }
- else
- {
- testable_functions->remove(testable_functions, name);
- }
- if (lib->leak_detective)
- {
- lib->leak_detective->set_state(lib->leak_detective, old);
- }
+ lib->leak_detective->set_state(lib->leak_detective, old);
}
}
diff --git a/src/libstrongswan/utils/test.h b/src/libstrongswan/utils/test.h
index 5b7289244..a1b2a2d9b 100644
--- a/src/libstrongswan/utils/test.h
+++ b/src/libstrongswan/utils/test.h
@@ -51,7 +51,7 @@ void testable_function_register(char *name, void *fn);
* @param fn function to register
*/
#define EXPORT_FUNCTION_FOR_TESTS(ns, fn) \
-static void testable_function_register_##fn() __attribute__ ((constructor(2000))); \
+static void testable_function_register_##fn() __attribute__ ((constructor)); \
static void testable_function_register_##fn() \
{ \
testable_function_register(#ns "/" #fn, fn); \
@@ -65,32 +65,32 @@ static void testable_function_unregister_##fn() \
/**
* Import a registered function so that it can be called from tests.
*
- * @note If the imported function is static (or no conflicting header files
- * are included) ret can be prefixed with static to declare the function static.
- *
- * @note We allocate an arbitrary amount of stack space, hopefully enough for
- * all arguments.
- *
* @param ns namespace of the function
* @param name name of the function
* @param ret return type of the function
* @param ... arguments of the function
*/
#define IMPORT_FUNCTION_FOR_TESTS(ns, name, ret, ...) \
-ret name(__VA_ARGS__) \
-{ \
- void (*fn)() = NULL; \
+static ret (*TEST_##ns##name)(__VA_ARGS__);
+
+/**
+ * Call a registered function from tests.
+ *
+ * @param ns namespace of the function
+ * @param name name of the function
+ * @param ... arguments for the function
+ */
+#define TEST_FUNCTION(ns, name, ...) \
+({ \
if (testable_functions) \
{ \
- fn = testable_functions->get(testable_functions, #ns "/" #name); \
+ TEST_##ns##name = testable_functions->get(testable_functions, #ns "/" #name); \
} \
- if (fn) \
+ if (!TEST_##ns##name) \
{ \
- void *args = __builtin_apply_args(); \
- __builtin_return(__builtin_apply(fn, args, 16*sizeof(void*))); \
+ test_fail_msg(__FILE__, __LINE__, "function " #name " (" #ns ") not found"); \
} \
- test_fail_msg(__FILE__, __LINE__, "function " #name " (" #ns ") not found"); \
- __builtin_return(NULL); \
-}
+ TEST_##ns##name(__VA_ARGS__); \
+})
#endif /** TEST_H_ @}*/