Index: uspace/lib/c/generic/private/fibril.h
===================================================================
--- uspace/lib/c/generic/private/fibril.h	(revision df2b4ce7dfd9a9460beae9c79d6b15ab4f89a620)
+++ uspace/lib/c/generic/private/fibril.h	(revision 6a57b934ac1bab03632d1a7c5ef5d52e622314b8)
@@ -70,5 +70,12 @@
 	fibril_owner_info_t *waits_for;
 	fibril_event_t *sleep_event;
+
+	list_t exit_hooks;
 };
+
+typedef struct {
+	link_t link;
+	void (*func)(void);
+} fibril_hook_t;
 
 extern fibril_t *fibril_alloc(void);
Index: uspace/lib/c/generic/thread/fibril.c
===================================================================
--- uspace/lib/c/generic/thread/fibril.c	(revision df2b4ce7dfd9a9460beae9c79d6b15ab4f89a620)
+++ uspace/lib/c/generic/thread/fibril.c	(revision 6a57b934ac1bab03632d1a7c5ef5d52e622314b8)
@@ -198,4 +198,8 @@
 	if (fibril->is_freeable) {
 		tls_free(fibril->tcb);
+		list_foreach_safe(fibril->exit_hooks, cur, _next) {
+			fibril_hook_t *hook = list_get_instance(cur, fibril_hook_t, link);
+			free(hook);
+		}
 		free(fibril);
 	}
@@ -562,4 +566,6 @@
 	fibril->arg = arg;
 
+	list_initialize(&fibril->exit_hooks);
+
 	context_create_t sctx = {
 		.fn = _fibril_main,
@@ -855,4 +861,8 @@
 	(void) retval;
 
+	list_foreach(fibril_self()->exit_hooks, link, fibril_hook_t, hook) {
+		hook->func();
+	}
+
 	fibril_t *f = _ready_list_pop_nonblocking(false);
 	if (!f)
@@ -918,4 +928,15 @@
 }
 
+errno_t fibril_add_exit_hook(void (*hook)(void))
+{
+	fibril_hook_t *h = malloc(sizeof(fibril_hook_t));
+	if (!h)
+		return ENOMEM;
+
+	h->func = hook;
+	list_append(&h->link, &fibril_self()->exit_hooks);
+	return EOK;
+}
+
 errno_t fibril_ipc_wait(ipc_call_t *call, const struct timespec *expires)
 {
Index: uspace/lib/c/include/fibril.h
===================================================================
--- uspace/lib/c/include/fibril.h	(revision df2b4ce7dfd9a9460beae9c79d6b15ab4f89a620)
+++ uspace/lib/c/include/fibril.h	(revision 6a57b934ac1bab03632d1a7c5ef5d52e622314b8)
@@ -74,4 +74,7 @@
 extern __noreturn void fibril_exit(long);
 
+/** Add a function to be called after fibril exits, just before it is destroyed */
+extern errno_t fibril_add_exit_hook(void (*)(void));
+
 __HELENOS_DECLS_END;
 
