$OpenBSD: patch-dirmngr_dirmngr_c,v 1.1 2016/11/18 11:30:53 ajacoutot Exp $

From eda17649f8bd3b8ce7bfc00a3c11cbcae63c845d Mon Sep 17 00:00:00 2001
From: NIIBE Yutaka <gniibe@fsij.org>
Date: Tue, 4 Oct 2016 09:01:13 +0900
Subject: [PATCH] agent, dirmngr, scd: npth_init must be after fork.

From fc0b392e766af8127094e8b529d25abb84ad1d65 Mon Sep 17 00:00:00 2001
From: NIIBE Yutaka <gniibe@fsij.org>
Date: Fri, 7 Oct 2016 10:45:22 +0900
Subject: [PATCH] agent, dirmngr, scd: Fix init_common_subsystems.

--- dirmngr/dirmngr.c.orig	Fri Nov 18 12:26:43 2016
+++ dirmngr/dirmngr.c	Fri Nov 18 12:26:33 2016
@@ -636,6 +636,23 @@ pid_suffix_callback (unsigned long *r_suffix)
 #endif /*!HAVE_W32_SYSTEM*/
 
 
+static void
+thread_init (void)
+{
+  npth_init ();
+  gpgrt_set_syscall_clamp (npth_unprotect, npth_protect);
+
+  /* Now with NPth running we can set the logging callback.  Our
+     windows implementation does not yet feature the NPth TLS
+     functions.  */
+#ifndef HAVE_W32_SYSTEM
+  if (npth_key_create (&my_tlskey_current_fd, NULL) == 0)
+    if (npth_setspecific (my_tlskey_current_fd, NULL) == 0)
+      log_set_pid_suffix_cb (pid_suffix_callback);
+#endif /*!HAVE_W32_SYSTEM*/
+}
+
+
 int
 main (int argc, char **argv)
 {
@@ -669,8 +686,6 @@ main (int argc, char **argv)
   i18n_init ();
   init_common_subsystems (&argc, &argv);
 
-  npth_init ();
-
   gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
 
  /* Check that the libraries are suitable.  Do it here because
@@ -711,15 +726,6 @@ main (int argc, char **argv)
   if (shell && strlen (shell) >= 3 && !strcmp (shell+strlen (shell)-3, "csh") )
     csh_style = 1;
 
-    /* Now with NPth running we can set the logging callback.  Our
-     windows implementation does not yet feature the NPth TLS
-     functions.  */
-#ifndef HAVE_W32_SYSTEM
-  if (npth_key_create (&my_tlskey_current_fd, NULL) == 0)
-    if (npth_setspecific (my_tlskey_current_fd, NULL) == 0)
-      log_set_pid_suffix_cb (pid_suffix_callback);
-#endif /*!HAVE_W32_SYSTEM*/
-
   /* Reset rereadable options to default values. */
   parse_rereadable_options (NULL, 0);
 
@@ -970,6 +976,7 @@ main (int argc, char **argv)
       ldap_wrapper_launch_thread ();
 #endif /*USE_LDAP*/
 
+      thread_init ();
       cert_cache_init ();
       crl_cache_init ();
       start_command_handler (ASSUAN_INVALID_FD);
@@ -1168,6 +1175,7 @@ main (int argc, char **argv)
       ldap_wrapper_launch_thread ();
 #endif /*USE_LDAP*/
 
+      thread_init ();
       cert_cache_init ();
       crl_cache_init ();
       handle_connections (fd);
@@ -1195,6 +1203,7 @@ main (int argc, char **argv)
 #if USE_LDAP
       ldap_wrapper_launch_thread ();
 #endif /*USE_LDAP*/
+      thread_init ();
       cert_cache_init ();
       crl_cache_init ();
       if (!argc)
@@ -1220,6 +1229,7 @@ main (int argc, char **argv)
 #if USE_LDAP
       ldap_wrapper_launch_thread ();
 #endif /*USE_LDAP*/
+      thread_init ();
       cert_cache_init ();
       crl_cache_init ();
       rc = crl_fetch (&ctrlbuf, argv[0], &reader);
