ID_KEY_ID = 11
};
+/**
+ * Old pluto id format
+ *
+ * @deprecated Do not use any more, only here for pluto.
+ */
+// struct id {
+// /** ID_* value, pluto pendant to id_type_t */
+// int kind;
+// /** ID_IPV4_ADDR, ID_IPV6_ADDR */
+// ip_address ip_addr;
+// /** ID_FQDN, ID_USER_FQDN (with @) */
+// /** ID_KEY_ID, ID_DER_ASN_DN */
+// chunk_t name;
+// };
+
/**
* String mappings for id_type_t.
*/
*/
char *(*get_string) (identification_t *this);
+ /**
+ * @brief Get the id in the format used in pluto.
+ *
+ * We do this in pluto style here, which means no memory
+ * is allocated.
+ *
+ * @param this the identification_t object
+ * @return string
+ */
+ // void (*get_pluto_id) (identification_t *this, struct id *pluto_id);
+
/**
* @brief Check if two identification_t objects are equal.
*
*/
identification_t * identification_create_from_string(id_type_t type, char *string);
-
/**
* @brief Creates an identification_t object from an encoded chunk.
*
*/
identification_t * identification_create_from_encoding(id_type_t type, chunk_t encoded);
+/**
+ * @brief Creates an identification_t object from the old pluto id format.
+ *
+ * Pluto uses struct id for identification stuff. Since we need to convert from
+ * this format to our identification_t, we need this special constructor.
+ *
+ * @param id old pluto format id
+ * @return identification_t object
+ *
+ * @ingroup utils
+ */
+// identification_t * identification_create_from_pluto_id(struct id *pluto_id);
+
#endif /* IDENTIFICATION_H_ */
#include <netinet/in.h>
#include <arpa/inet.h>
#include <dlfcn.h>
+#include <unistd.h>
#include "leak_detective.h"
#include <types.h>
+#include <utils/logger_manager.h>
#ifdef LEAK_DETECTIVE
*/
#define MEMORY_HEADER_MAGIC 0xF1367ADF
+/**
+ * logger for the leak detective
+ */
+logger_t *logger;
static void install_hooks(void);
static void uninstall_hooks(void);
*/
void *old_malloc_hook, *old_realloc_hook, *old_free_hook;
-
+/**
+ * Mutex to exclusivly uninstall hooks, access heap list
+ */
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+void (*__malloc_initialize_hook) (void) = install_hooks;
/**
* log stack frames queried by backtrace()
* TODO: Dump symbols of static functions!!!
*/
-void log_stack_frames(void *stack_frames, int stack_frame_count)
+void log_stack_frames(void **stack_frames, int stack_frame_count)
{
char **strings;
size_t i;
strings = backtrace_symbols (stack_frames, stack_frame_count);
- printf(" dumping %d stack frames.\n", stack_frame_count);
+ logger->log(logger, ERROR, " dumping %d stack frame addresses.", stack_frame_count);
for (i = 0; i < stack_frame_count; i++)
{
- printf (" %s\n", strings[i]);
+ logger->log(logger, ERROR, " %s", strings[i]);
}
free (strings);
}
-void (*__malloc_initialize_hook) (void) = install_hooks;
-
/**
* Installs the malloc hooks, enables leak detection
*/
void install_hooks()
{
+ logger = logger_manager->get_logger(logger_manager, LEAK_DETECT);
old_malloc_hook = __malloc_hook;
old_realloc_hook = __realloc_hook;
old_free_hook = __free_hook;
pthread_mutex_unlock(&mutex);
/* TODO: Since we get a lot of theses from the pthread lib, its deactivated for now... */
return;
- printf("freeing of invalid memory (%p)\n", ptr);
+ logger->log(logger, ERROR, "freeing of invalid memory (%p)", ptr);
stack_frame_count = backtrace(stack_frames, STACK_FRAMES_COUNT);
log_stack_frames(stack_frames, stack_frame_count);
- kill(0, SIGSEGV);
+ kill(getpid(), SIGKILL);
return;
}
/* remove magic from hdr */
}
if (hdr->magic != MEMORY_HEADER_MAGIC)
{
- printf("reallocation of invalid memory (%p)\n", old);
+ logger->log(logger, ERROR, "reallocation of invalid memory (%p)", old);
stack_frame_count = backtrace(stack_frames, STACK_FRAMES_COUNT);
log_stack_frames(stack_frames, stack_frame_count);
- kill(0, SIGSEGV);
+ kill(getpid(), SIGKILL);
return NULL;
}
for (hdr = first_header.next; hdr != NULL; hdr = hdr->next)
{
- printf("Leak (%d bytes at %p)\n", hdr->bytes, hdr + 1);
+ logger->log(logger, ERROR, "Leak (%d bytes at %p)", hdr->bytes, hdr + 1);
log_stack_frames(hdr->stack_frames, hdr->stack_frame_count);
leaks++;
}
switch (leaks)
{
case 0:
- printf("No leaks detected\n");
+ logger->log(logger, CONTROL, "No leaks detected");
break;
case 1:
- printf("One leak detected\n");
+ logger->log(logger, ERROR, "One leak detected");
break;
default:
- printf("%d leaks detected\n", leaks);
+ logger->log(logger, ERROR, "%d leaks detected", leaks);
break;
}
}
handle = dlopen("libc.so.6", RTLD_LAZY);
if (handle == NULL)
{
- kill(0, SIGSEGV);
+ install_hooks();
+ pthread_mutex_unlock(&mutex);
+ kill(getpid(), SIGSEGV);
}
_inet_ntoa = dlsym(handle, "inet_ntoa");
if (_inet_ntoa == NULL)
{
- kill(0, SIGSEGV);
+ dlclose(handle);
+ install_hooks();
+ pthread_mutex_unlock(&mutex);
+ kill(getpid(), SIGSEGV);
}
result = _inet_ntoa(in);
dlclose(handle);
handle = dlopen("libpthread.so.0", RTLD_LAZY);
if (handle == NULL)
{
- kill(0, SIGSEGV);
+ install_hooks();
+ pthread_mutex_unlock(&mutex);
+ kill(getpid(), SIGSEGV);
}
_pthread_create = dlsym(handle, "pthread_create");
if (_pthread_create == NULL)
{
- kill(0, SIGSEGV);
+ dlclose(handle);
+ install_hooks();
+ pthread_mutex_unlock(&mutex);
+ kill(getpid(), SIGSEGV);
}
result = _pthread_create(__threadp, __attr, __start_routine, __arg);
dlclose(handle);
handle = dlopen("libc.so.6", RTLD_LAZY);
if (handle == NULL)
{
- kill(0, SIGSEGV);
+ install_hooks();
+ pthread_mutex_unlock(&mutex);
+ kill(getpid(), SIGSEGV);
}
_mktime = dlsym(handle, "mktime");
if (_mktime == NULL)
{
- kill(0, SIGSEGV);
+ dlclose(handle);
+ install_hooks();
+ pthread_mutex_unlock(&mutex);
+ kill(getpid(), SIGSEGV);
}
result = _mktime(tm);
dlclose(handle);