Because we use atexit() to cleanup after ourselves, this will cause a
problem if we have been dynamically loaded and then unloaded again: the
atexit() handler may no longer be there.
Most modern atexit() implementations can handle this, however there are
still difficulties if libssl gets unloaded before libcrypto, because of
the atexit() callback that libcrypto makes to libssl.
The most robust solution seems to be to ensure that libcrypto and libssl
never unload. This is done by simply deliberately leaking a dlopen()
reference to them.
Reviewed-by: Tim Hudson <tjh@openssl.org>
#include <stdlib.h>
#include <assert.h>
#include <internal/thread_once.h>
+#include <internal/dso.h>
static int stopped = 0;
return 0;
OPENSSL_cpuid_setup();
base_inited = 1;
+
+ /*
+ * Deliberately leak a reference to ourselves. This will force the library
+ * to remain loaded until the atexit() handler is run a process exit.
+ */
+ {
+ DSO *dso = NULL;
+
+ dso = DSO_dsobyaddr(&base_inited, DSO_FLAG_NO_UNLOAD_ON_FREE);
+ DSO_free(dso);
+ }
+
return 1;
}
{
OPENSSL_INIT_STOP *newhand;
+ /*
+ * Deliberately leak a reference to the handler. This will force the
+ * library/code containing the handler to remain loaded until we run the
+ * atexit handler.
+ */
+ {
+ DSO *dso = NULL;
+ union {
+ void *sym;
+ void (*func)(void);
+ } handlersym;
+
+ handlersym.func = handler;
+
+ dso = DSO_dsobyaddr(handlersym.sym, DSO_FLAG_NO_UNLOAD_ON_FREE);
+ DSO_free(dso);
+ }
+
newhand = OPENSSL_malloc(sizeof(*newhand));
if (newhand == NULL)
return 0;