$OpenBSD: patch-agent_gpg-agent_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.

--- agent/gpg-agent.c.orig	Fri Nov 18 12:26:38 2016
+++ agent/gpg-agent.c	Fri Nov 18 12:26:33 2016
@@ -715,7 +715,31 @@ finalize_rereadable_options (void)
 }
 
 
+static void
+thread_init_once (void)
+{
+  static int npth_initialized = 0;
 
+  if (!npth_initialized)
+    {
+      npth_initialized++;
+      npth_init ();
+    }
+  gpgrt_set_syscall_clamp (npth_unprotect, npth_protect);
+}
+
+static void
+initialize_modules (void)
+{
+  thread_init_once ();
+  assuan_set_system_hooks (ASSUAN_SYSTEM_NPTH);
+  initialize_module_cache ();
+  initialize_module_call_pinentry ();
+  initialize_module_call_scd ();
+  initialize_module_trustlist ();
+}
+
+
 /* The main entry point.  */
 int
 main (int argc, char **argv )
@@ -762,14 +786,11 @@ main (int argc, char **argv )
   i18n_init ();
   init_common_subsystems (&argc, &argv);
 
-  npth_init ();
-
   malloc_hooks.malloc = gcry_malloc;
   malloc_hooks.realloc = gcry_realloc;
   malloc_hooks.free = gcry_free;
   assuan_set_malloc_hooks (&malloc_hooks);
   assuan_set_gpg_err_source (GPG_ERR_SOURCE_DEFAULT);
-  assuan_set_system_hooks (ASSUAN_SYSTEM_NPTH);
   assuan_sock_init ();
   setup_libassuan_logging (&opt.debug);
 
@@ -1051,16 +1072,12 @@ main (int argc, char **argv )
       exit (1);
     }
 
-  initialize_module_cache ();
-  initialize_module_call_pinentry ();
-  initialize_module_call_scd ();
-  initialize_module_trustlist ();
-
   /* Try to create missing directories. */
   create_directories ();
 
   if (debug_wait && pipe_server)
     {
+      thread_init_once ();
       log_debug ("waiting for debugger - my pid is %u .....\n",
                  (unsigned int)getpid());
       gnupg_sleep (debug_wait);
@@ -1167,6 +1184,8 @@ main (int argc, char **argv )
       /* This is the simple pipe based server */
       ctrl_t ctrl;
 
+      initialize_modules ();
+
       ctrl = xtrycalloc (1, sizeof *ctrl);
       if (!ctrl)
         {
@@ -1369,6 +1388,8 @@ main (int argc, char **argv )
       /*
          This is the child
        */
+
+      initialize_modules ();
 
       /* Detach from tty and put process into a new session */
       if (!nodetach )
