+#ifdef HAVE_LIBCURL
+#include <curl/curl.h>
+
+#ifdef LIBCURL_USE_OPENSSL
+#include <openssl/crypto.h>
+static GMutex **openssl_mutex_array;
+static void openssl_lock_callback(int mode, int type, const char *file, int line)
+{
+ (void)file;
+ (void)line;
+ if (mode & CRYPTO_LOCK) {
+ g_mutex_lock(openssl_mutex_array[type]);
+ }
+ else {
+ g_mutex_unlock(openssl_mutex_array[type]);
+ }
+}
+
+static void
+init_ssl(void)
+{
+ int i;
+
+ openssl_mutex_array = g_new0(GMutex *, CRYPTO_num_locks());
+
+ for (i=0; i<CRYPTO_num_locks(); i++) {
+ openssl_mutex_array[i] = g_mutex_new();
+ }
+ CRYPTO_set_locking_callback(openssl_lock_callback);
+
+}
+
+#else /* LIBCURL_USE_OPENSSL */
+#if defined LIBCURL_USE_GNUTLS
+
+#include <gcrypt.h>
+#include <errno.h>
+
+GCRY_THREAD_OPTION_PTHREAD_IMPL;
+static void
+init_ssl(void)
+{
+ gcry_control(GCRYCTL_SET_THREAD_CBS);
+}
+
+#else /* LIBCURL_USE_GNUTLS */
+
+static void
+init_ssl(void)
+{
+}
+#endif /* LIBCURL_USE_GNUTLS */
+#endif /* LIBCURL_USE_OPENSSL */
+
+#else /* HAVE_LIBCURL */
+static void
+init_ssl(void)
+{
+}
+#endif /* HAVE_LIBCURL */
+
+void
+glib_init(void) {
+ static gboolean did_glib_init = FALSE;
+ if (did_glib_init) return;
+ did_glib_init = TRUE;
+
+ /* set up libcurl */
+#ifdef HAVE_LIBCURL
+# ifdef G_THREADS_ENABLED
+# if (GLIB_MAJOR_VERSION < 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 31))
+ g_assert(!g_thread_supported()); /* assert threads aren't initialized yet */
+# endif
+# endif
+ g_assert(curl_global_init(CURL_GLOBAL_ALL) == 0);
+#endif
+
+ /* do a version check */
+#if GLIB_CHECK_VERSION(2,6,0)
+ {
+ const char *glib_err = glib_check_version(GLIB_MAJOR_VERSION,
+ GLIB_MINOR_VERSION,
+ GLIB_MICRO_VERSION);
+ if (glib_err) {
+ error(_("%s: Amanda was compiled with glib-%d.%d.%d"), glib_err,
+ GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
+ exit(1); /* glib_init may be called before error handling is set up */
+ }
+ }
+#endif
+
+ /* Initialize glib's type system. On glib >= 2.24, this will initialize
+ * threads, so it must be done after curl is initialized. */
+ g_type_init();
+
+ /* And set up glib's threads */
+#if defined(G_THREADS_ENABLED) && !defined(G_THREADS_IMPL_NONE)
+ if (!g_thread_supported())
+ g_thread_init(NULL);
+#endif
+
+ /* initialize ssl */
+ init_ssl();
+
+}
+