@@ -, +, @@ at 0x48389CB: free (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x48AED9F: havege_destroy (havege.c:197) by 0x10BCA5: error_exit (haveged.c:708) by 0x10B1B7: run_daemon (haveged.c:573) by 0x10B1B7: main (haveged.c:470) Address 0x4845000 is in a rw- anonymous segment memory and is expected to free resources. from terminal with daemon(). ... if (!havege_exit(hptr)) // <- here incorrect pid-based detection happens return; if (0 != (temp=hptr->io_buf)) { hptr->io_buf = 0; free(temp); // <--- here free() happens } --- src/havege.c | 11 +++++++++++ src/havege.h | 8 ++++++++ src/haveged.c | 4 +++- 3 files changed, 22 insertions(+), 1 deletion(-) --- a/src/havege.c +++ a/src/havege.c @@ -178,6 +178,17 @@ H_PTR havege_create( /* RETURN: app state */ havege_ndsetup(h); return h; } + +void havege_reparent( + H_PTR hptr) +{ + H_THREAD *t = (H_THREAD *)hptr->threads; + if (0 == t) + return; /* single-threaded */ + + t->main = getpid(); +} + /** * Destructor. In a multi-collector build, this method should be called from a signal handler * to avoid creating processes. --- a/src/havege.h +++ b/src/havege.h @@ -238,6 +238,14 @@ typedef enum { * H_NOINIT */ H_PTR havege_create(H_PARAMS *params); + +/** + * haveger_create() remembers parent pid and uses it to identify deallocating thread. + * daemonize() forks parent and effectively loses parent thread. + * havege_reparent(void) allows recovering new parent pid before havege_run() is started. + */ +void havege_reparent(H_PTR hptr); + /** * Frees all allocated anchor resources. If the multi-core option is used, this * method should be called from a signal handler to prevent zombie processes. --- a/src/haveged.c +++ b/src/haveged.c @@ -538,8 +538,10 @@ static void run_daemon( /* RETURN: nothing */ anchor_info(h); return; } - if (params->foreground==0) + if (params->foreground==0) { daemonize(); + havege_reparent(handle); + } else printf ("%s starting up\n", params->daemon); if (0 != havege_run(h)) error_exit("Couldn't initialize HAVEGE rng %d", h->error); --