Blame SOURCES/libgcrypt-1.5.3-ath-reinstall.patch

241384
diff -up libgcrypt-1.5.3/src/ath.c.ath-reinstall libgcrypt-1.5.3/src/ath.c
241384
--- libgcrypt-1.5.3/src/ath.c.ath-reinstall	2013-07-25 11:10:04.000000000 +0200
241384
+++ libgcrypt-1.5.3/src/ath.c	2017-02-28 14:37:15.267668432 +0100
241384
@@ -36,7 +36,7 @@
241384
 #include <errno.h>
241384
 
241384
 #include "ath.h"
241384
-
241384
+#include "g10lib.h"
241384
 
241384
 
241384
 /* The interface table.  */
241384
@@ -45,6 +45,13 @@ static struct ath_ops ops;
241384
 /* True if we should use the external callbacks.  */
241384
 static int ops_set;
241384
 
241384
+struct lock_list
241384
+{
241384
+  ath_mutex_t *lock;
241384
+  struct lock_list *next;
241384
+};
241384
+
241384
+static struct lock_list *reinstallable_locks;
241384
 
241384
 /* For the dummy interface.  */
241384
 #define MUTEX_UNLOCKED	((ath_mutex_t) 0)
241384
@@ -62,6 +69,50 @@ static int ops_set;
241384
 /* The lock we take while checking for lazy lock initialization.  */
241384
 static ath_mutex_t check_init_lock = ATH_MUTEX_INITIALIZER;
241384
 
241384
+static void
241384
+add_reinstallable_lock(ath_mutex_t *lock)
241384
+{
241384
+  struct lock_list *ll, *new, **ptr;
241384
+
241384
+  new = gcry_calloc(1, sizeof(*new));
241384
+  if (!new)
241384
+    abort();
241384
+
241384
+  for (ll = reinstallable_locks, ptr = &reinstallable_locks; ll != NULL; ptr = &ll->next, ll = ll->next)
241384
+    {
241384
+      if (ll->lock == lock)
241384
+        {
241384
+          gcry_free(new);
241384
+          return;
241384
+        }
241384
+    }
241384
+
241384
+  new->lock = lock;
241384
+  *ptr = new;
241384
+}
241384
+
241384
+static void
241384
+remove_reinstallable_lock(ath_mutex_t *lock)
241384
+{
241384
+  struct lock_list *ll, **ptr;
241384
+
241384
+  for (ll = reinstallable_locks, ptr = &reinstallable_locks; ll != NULL; ptr = &ll->next, ll = ll->next)
241384
+    {
241384
+      if (ll->lock == lock)
241384
+        {
241384
+          *ptr = ll->next;
241384
+          gcry_free(ll);
241384
+          /* we do not store duplicates */
241384
+          return;
241384
+        }
241384
+    }
241384
+
241384
+#ifndef NDEBUG
241384
+    /* lock not found, should not happen */
241384
+    abort();
241384
+#endif
241384
+}
241384
+
241384
 int
241384
 ath_init (void)
241384
 {
241384
@@ -85,7 +136,9 @@ ath_init (void)
241384
 gpg_err_code_t
241384
 ath_install (struct ath_ops *ath_ops, int check_only)
241384
 {
241384
-  if (check_only)
241384
+  gpg_err_code_t err = 0;
241384
+
241384
+  if (check_only && ops_set)
241384
     {
241384
       unsigned int option = 0;
241384
 
241384
@@ -119,7 +172,25 @@ ath_install (struct ath_ops *ath_ops, in
241384
   else
241384
     ops_set = 0;
241384
 
241384
-  return 0;
241384
+  if (ops_set && reinstallable_locks)
241384
+    {
241384
+      struct lock_list *ll;
241384
+
241384
+      ath_init();
241384
+      for (ll = reinstallable_locks; ll != NULL;)
241384
+        {
241384
+          struct lock_list *prev;
241384
+
241384
+          if (ath_mutex_init(ll->lock))
241384
+            err = GPG_ERR_NOT_SUPPORTED;
241384
+          prev = ll;
241384
+          ll = ll->next;
241384
+          gcry_free(prev);
241384
+        }
241384
+      reinstallable_locks = NULL;
241384
+    }
241384
+
241384
+  return err;
241384
 }
241384
 
241384
 
241384
@@ -143,6 +214,8 @@ ath_mutex_init (ath_mutex_t *lock)
241384
 {
241384
   if (ops_set)
241384
     return mutex_init (lock, 0);
241384
+  else
241384
+    add_reinstallable_lock(lock);
241384
 
241384
 #ifndef NDEBUG
241384
   *lock = MUTEX_UNLOCKED;
241384
@@ -168,6 +241,8 @@ ath_mutex_destroy (ath_mutex_t *lock)
241384
       (*ops.mutex_unlock) (&check_init_lock);
241384
       return (*ops.mutex_destroy) (lock);
241384
     }
241384
+  else
241384
+    remove_reinstallable_lock(lock);
241384
 
241384
 #ifndef NDEBUG
241384
   assert (*lock == MUTEX_UNLOCKED);