lintian doesn't like orphan packages with uploaders...
[debian/amanda] / common-src / glib-util.c
index 7ba6702259e4040867a5ff661d83c008610f3333..d0506ac675d8d1c7d90d9b5767de989029ba7366 100644 (file)
@@ -1,9 +1,10 @@
 /*
- * Copyright (c) 2007, 2008, 2009, 2010 Zmanda, Inc.  All Rights Reserved.
+ * Copyright (c) 2007-2012 Zmanda, Inc.  All Rights Reserved.
  *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 as published
- * by the Free Software Foundation.
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
 #ifdef HAVE_LIBCURL
 #include <curl/curl.h>
-#endif
+
+#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) {
@@ -42,7 +100,9 @@ glib_init(void) {
      * is initialized) */
 #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
@@ -70,6 +130,10 @@ glib_init(void) {
     if (!g_thread_supported())
        g_thread_init(NULL);
 #endif
+
+    /* initialize ssl */
+    init_ssl();
+
 }
 
 typedef enum {
@@ -106,42 +170,19 @@ GValue* g_value_unset_copy(const GValue * from, GValue * to) {
     return to;
 }
 
-void g_list_free_full(GList * list) {
-    GList * cur = list;
-
-    while (cur != NULL) {
-        gpointer data = cur->data;
-        amfree(data);
-        cur = g_list_next(cur);
-    }
-
-    g_list_free(list);
-}
-
-void g_slist_free_full(GSList * list) {
+#if (GLIB_MAJOR_VERSION < 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 28))
+void slist_free_full(GSList * list, GDestroyNotify free_fn) {
     GSList * cur = list;
 
     while (cur != NULL) {
         gpointer data = cur->data;
-        amfree(data);
+        free_fn(data);
         cur = g_slist_next(cur);
     }
 
     g_slist_free(list);
 }
-
-void g_slist_free_full_gpointer(gpointer list) {
-    g_slist_free_full((GSList *)list);
-}
-
-void g_queue_free_full(GQueue * queue) {
-    while (!g_queue_is_empty(queue)) {
-        gpointer data;
-        data = g_queue_pop_head(queue);
-        amfree(data);
-    }
-    g_queue_free(queue);
-}
+#endif
 
 void g_ptr_array_free_full(GPtrArray * array) {
     size_t i;