GNUPG-1-9-BRANCH gnupg (9 files)

cvs user wk cvs at cvs.gnupg.org
Sun Dec 19 18:39:33 CET 2004


    Date: Sunday, December 19, 2004 @ 18:44:20
  Author: wk
    Path: /cvs/gnupg/gnupg
     Tag: GNUPG-1-9-BRANCH

Modified: agent/ChangeLog agent/agent.h agent/call-scd.c agent/gpg-agent.c
          agent/query.c jnlib/ChangeLog jnlib/w32-pth.c jnlib/w32-pth.h
          scd/scdaemon.c

* query.c (initialize_module_query): 

* call-scd.c (initialize_module_call_scd): New.

* w32-pth.c (pth_init): Enable debugging depending on env var.
(pth_self): New.
(pth_mutex_release, pth_mutex_acquire): Implemented directly using
the W32 API.


-------------------+
 agent/ChangeLog   |    6 ++
 agent/agent.h     |    2 
 agent/call-scd.c  |   21 +++++++-
 agent/gpg-agent.c |    4 +
 agent/query.c     |   21 +++++++-
 jnlib/ChangeLog   |    3 +
 jnlib/w32-pth.c   |  131 +++++++++++++++++++++++-----------------------------
 jnlib/w32-pth.h   |   12 +---
 scd/scdaemon.c    |    5 +
 9 files changed, 123 insertions(+), 82 deletions(-)


Index: gnupg/agent/ChangeLog
diff -u gnupg/agent/ChangeLog:1.59.2.48 gnupg/agent/ChangeLog:1.59.2.49
--- gnupg/agent/ChangeLog:1.59.2.48	Sat Dec 18 19:53:32 2004
+++ gnupg/agent/ChangeLog	Sun Dec 19 18:44:20 2004
@@ -1,3 +1,9 @@
+2004-12-19  Werner Koch  <wk at g10code.com>
+
+	* query.c (initialize_module_query): 
+
+	* call-scd.c (initialize_module_call_scd): New.
+
 2004-12-18  Werner Koch  <wk at g10code.com>
 
 	* gpg-agent.c (main):  Remove special Pth initialize.
Index: gnupg/agent/agent.h
diff -u gnupg/agent/agent.h:1.32.2.9 gnupg/agent/agent.h:1.32.2.10
--- gnupg/agent/agent.h:1.32.2.9	Sat Dec 18 11:22:10 2004
+++ gnupg/agent/agent.h	Sun Dec 19 18:44:20 2004
@@ -145,6 +145,7 @@
 int agent_key_available (const unsigned char *grip);
 
 /*-- query.c --*/
+void initialize_module_query (void);
 int agent_askpin (ctrl_t ctrl,
                   const char *desc_text, const char *inital_errtext,
                   struct pin_entry_info_s *pininfo);
@@ -212,6 +213,7 @@
 
 
 /*-- call-scd.c --*/
+void initialize_module_call_scd (void);
 int agent_reset_scd (ctrl_t ctrl);
 int agent_card_learn (ctrl_t ctrl,
                       void (*kpinfo_cb)(void*, const char *),
Index: gnupg/agent/call-scd.c
diff -u gnupg/agent/call-scd.c:1.13.2.6 gnupg/agent/call-scd.c:1.13.2.7
--- gnupg/agent/call-scd.c:1.13.2.6	Wed Dec 15 15:15:09 2004
+++ gnupg/agent/call-scd.c	Sun Dec 19 18:44:20 2004
@@ -52,7 +52,7 @@
 
 static ASSUAN_CONTEXT scd_ctx = NULL;
 #ifdef USE_GNU_PTH
-static pth_mutex_t scd_lock = PTH_MUTEX_INIT;
+static pth_mutex_t scd_lock;
 #endif
 /* We need to keep track of the connection currently using the SCD.
    For a pipe server this is all a NOP because the connection will
@@ -153,6 +153,23 @@
 
 
 
+/* This function must be called once to initialize this module.  This
+   has to be done before a second thread is spawned.  We can't do the
+   static initialization because Pth emulation code might not be able
+   to do a static init; in particualr, it is not possible for W32. */
+void
+initialize_module_call_scd (void)
+{
+#ifdef USE_GNU_PTH
+  static int initialized;
+
+  if (!initialized)
+    if (pth_mutex_init (&scd_lock))
+      initialized = 1;
+#endif /*USE_GNU_PTH*/
+}
+
+
 static int 
 unlock_scd (int rc)
 {
@@ -163,7 +180,7 @@
       if (!rc)
         rc = gpg_error (GPG_ERR_INTERNAL);
     }
-#endif
+#endif /*USE_GNU_PTH*/
   return rc;
 }
 
Index: gnupg/agent/gpg-agent.c
diff -u gnupg/agent/gpg-agent.c:1.31.2.26 gnupg/agent/gpg-agent.c:1.31.2.27
--- gnupg/agent/gpg-agent.c:1.31.2.26	Sun Dec 19 14:38:24 2004
+++ gnupg/agent/gpg-agent.c	Sun Dec 19 18:44:20 2004
@@ -627,6 +627,10 @@
       exit (1);
     }
 
+  initialize_module_query ();
+  initialize_module_call_scd ();
+  
+  /* Try to create missing directories. */
   create_directories ();
 
   if (debug_wait && pipe_server)
Index: gnupg/agent/query.c
diff -u gnupg/agent/query.c:1.23.2.6 gnupg/agent/query.c:1.23.2.7
--- gnupg/agent/query.c:1.23.2.6	Fri Dec 17 21:41:50 2004
+++ gnupg/agent/query.c	Sun Dec 19 18:44:20 2004
@@ -51,7 +51,7 @@
 
 static ASSUAN_CONTEXT entry_ctx = NULL;
 #ifdef USE_GNU_PTH
-static pth_mutex_t entry_lock = PTH_MUTEX_INIT;
+static pth_mutex_t entry_lock;
 #endif
 
 /* data to be passed to our callbacks */
@@ -64,6 +64,25 @@
 
 
 
+/* This function must be called once to initialize this module.  This
+   has to be done before a second thread is spawned.  We can't do the
+   static initialization because Pth emulation code might not be able
+   to do a static init; in particualr, it is not possible for W32. */
+void
+initialize_module_query (void)
+{
+#ifdef USE_GNU_PTH
+  static int initialized;
+
+  if (!initialized)
+    if (pth_mutex_init (&entry_lock))
+      initialized = 1;
+#endif /*USE_GNU_PTH*/
+}
+
+
+
+
 /* Unlock the pinentry so that another thread can start one and
    disconnect that pinentry - we do this after the unlock so that a
    stalled pinentry does not block other threads.  Fixme: We should
Index: gnupg/jnlib/ChangeLog
diff -u gnupg/jnlib/ChangeLog:1.3.2.29 gnupg/jnlib/ChangeLog:1.3.2.30
--- gnupg/jnlib/ChangeLog:1.3.2.29	Sun Dec 19 14:38:24 2004
+++ gnupg/jnlib/ChangeLog	Sun Dec 19 18:44:20 2004
@@ -1,6 +1,9 @@
 2004-12-19  Werner Koch  <wk at g10code.com>
 
 	* w32-pth.c (pth_init): Enable debugging depending on env var.
+	(pth_self): New.
+	(pth_mutex_release, pth_mutex_acquire): Implemented directly using
+	the W32 API.
 
 2004-12-18  Werner Koch  <wk at g10code.com>
 
Index: gnupg/jnlib/w32-pth.c
diff -u gnupg/jnlib/w32-pth.c:1.1.2.9 gnupg/jnlib/w32-pth.c:1.1.2.10
--- gnupg/jnlib/w32-pth.c:1.1.2.9	Sun Dec 19 14:38:24 2004
+++ gnupg/jnlib/w32-pth.c	Sun Dec 19 18:44:20 2004
@@ -68,7 +68,6 @@
 static CRITICAL_SECTION pth_shd;
 
 
-
 struct pth_event_s
 {
   struct pth_event_s * next;
@@ -306,8 +305,6 @@
           fprintf (stderr, "%s: pth_write(%d) failed in write: %s\n",
                    log_get_prefix (NULL), fd,
                    w32_strerror (strerr, sizeof strerr));
-          fprintf (stderr, "--> fd = %d, handle = %p, size = %lu\n",
-                   fd, (HANDLE)fd, size);
           n = -1;
         }
       else
@@ -444,107 +441,94 @@
 
 
 int
-pth_mutex_release (pth_mutex_t *hd)
+pth_mutex_release (pth_mutex_t *mutex)
 {
-  if (!hd)
-    return -1;
+  int rc;
+
   implicit_init ();
   enter_pth (__FUNCTION__);
-  if (hd->mx)
+
+  if (!ReleaseMutex (*mutex))
     {
-      CloseHandle (hd->mx);
-      hd->mx = NULL;
+      char strerr[256];
+
+      fprintf (stderr, "%s: pth_release_mutex %p failed: %s\n",
+               log_get_prefix (NULL), *mutex,
+               w32_strerror (strerr, sizeof strerr));
+      rc = FALSE;
     }
-  free (hd);
+  else
+    rc = TRUE;
+
   leave_pth (__FUNCTION__);
-  return 0;
+  return rc;
 }
 
 
 int
-pth_mutex_acquire (pth_mutex_t *hd, int tryonly, pth_event_t ev_extra)
+pth_mutex_acquire (pth_mutex_t *mutex, int tryonly, pth_event_t ev_extra)
 {
+  int code;
+  int rc;
+
   implicit_init ();
   enter_pth (__FUNCTION__);
 
-  if (!hd || !hd->mx)
-    {
-      leave_pth (__FUNCTION__);
-      return FALSE;
-    }
-    
-#if 0
-  /* still not locked, so simply acquire mutex? */
-  if (!(mutex->mx_state & PTH_MUTEX_LOCKED))
-    {
-      mutex->mx_state |= PTH_MUTEX_LOCKED;
-      mutex->mx_count = 1;
-      pth_ring_append(&(pth_current->mutexring), &(mutex->mx_node));
-      pth_debug1("pth_mutex_acquire: immediately locking mutex");
-      return 0;
-    }
-
-  /* already locked by caller? */
-  if (mutex->mx_count >= 1 && mutex->mx_owner == pth_current)
-    {
-      /* recursive lock */
-      mutex->mx_count++;
-      pth_debug1("pth_mutex_acquire: recursive locking");
-      return 0;
-    }
-
-  if (tryonly)
-    {
-      leave_pth (__FUNCTION__);
-      return -1;
-    }
-
-  for (;;)
+  /* FIXME: ev_extra is not yet supported.  */
+  
+  code = WaitForSingleObject (*mutex, INFINITE);
+  switch (code) 
     {
-      ev = pth_event(PTH_EVENT_MUTEX|PTH_MODE_STATIC, &ev_key, mutex);
-      if (ev_extra != NULL)
-        pth_event_concat (ev, ev_extra, NULL);
-      pth_wait (ev);
-      if (ev_extra != NULL)
+      case WAIT_FAILED:
         {
-          pth_event_isolate (ev);
-          if (do_pth_event_status(ev) == PTH_STATUS_PENDING)
-            {
-              leave_pth (__FUNCTION__);
-              return -1;
-            }
+          char strerr[256];
+          
+          fprintf (stderr, "%s: pth_mutex_acquire for %p failed: %s\n",
+                   log_get_prefix (NULL), *mutex,
+                   w32_strerror (strerr, sizeof strerr));
         }
-      if (!(mutex->mx_state & PTH_MUTEX_LOCKED))
+        rc = FALSE;
+        break;
+        
+      case WAIT_OBJECT_0:
+        rc = TRUE;
+        break;
+
+      default:
+        fprintf (stderr, "%s: WaitForSingleObject returned unexpected "
+                 "code %d for mutex %p\n",
+                 log_get_prefix (NULL), code, *mutex);
+        rc = FALSE;
         break;
     }
-#endif
 
-  hd->mx_state |= PTH_MUTEX_LOCKED;
   leave_pth (__FUNCTION__);
-  return TRUE;
+  return rc;
 }
 
 
+
 int
-pth_mutex_init (pth_mutex_t *hd)
+pth_mutex_init (pth_mutex_t *mutex)
 {
   SECURITY_ATTRIBUTES sa;
   
   implicit_init ();
   enter_pth (__FUNCTION__);
 
-  if (hd->mx)
-    {
-      ReleaseMutex (hd->mx);
-      CloseHandle (hd->mx);
-    }
   memset (&sa, 0, sizeof sa);
   sa.bInheritHandle = TRUE;
   sa.lpSecurityDescriptor = NULL;
   sa.nLength = sizeof sa;
-  hd->mx = CreateMutex (&sa, FALSE, NULL);
-  hd->mx_state = PTH_MUTEX_INITIALIZED;
-
+  *mutex = CreateMutex (&sa, FALSE, NULL);
+  if (!*mutex)
+   {
+      free (*mutex);
+      *mutex = NULL;
+      leave_pth (__FUNCTION__);
+      return FALSE;
+    }
+    
   leave_pth (__FUNCTION__);
   return TRUE;
 }
@@ -695,6 +679,12 @@
 }
 
 
+pth_t 
+pth_self (void)
+{
+  return GetCurrentThread ();
+}
+
 int
 pth_join (pth_t hd, void **value)
 {
@@ -1271,9 +1261,6 @@
 
         case PTH_EVENT_MUTEX:
           fprintf (stderr, "pth_wait: add mutex event.\n");
-          hdidx[i++] = pos;
-          waitbuf[pos++] = tmp->u.mx->mx;
-          /* XXX: Use SetEvent(hd->ev) */
           break;
         }
     }
Index: gnupg/jnlib/w32-pth.h
diff -u gnupg/jnlib/w32-pth.h:1.1.2.5 gnupg/jnlib/w32-pth.h:1.1.2.6
--- gnupg/jnlib/w32-pth.h:1.1.2.5	Tue Dec 14 12:25:04 2004
+++ gnupg/jnlib/w32-pth.h	Sun Dec 19 18:44:20 2004
@@ -53,7 +53,9 @@
 /* Mutex values. */
 #define PTH_MUTEX_INITIALIZED  (1<<0)
 #define PTH_MUTEX_LOCKED       (1<<1)
-#define PTH_MUTEX_INIT         {PTH_MUTEX_INITIALIZED}
+
+/* Note: We can't do static initialization, thus we don't define the
+   initializer PTH_MUTEX_INIT.  */
 
 
 #define PTH_KEY_INIT	       (1<<0)
@@ -158,12 +160,7 @@
 
 
 /* The Mutex object.  */
-struct pth_mutex_s 
-{
-  unsigned mx_state;
-  W32_PTH_HANDLE_INTERNAL mx; 
-};
-typedef struct pth_mutex_s pth_mutex_t;
+typedef W32_PTH_HANDLE_INTERNAL pth_mutex_t;
 
 
 /* The Event object.  */
@@ -214,6 +211,7 @@
 int pth_attr_set (pth_attr_t hd, int field, ...);
 
 pth_t pth_spawn (pth_attr_t hd, void *(*func)(void *), void *arg);
+pth_t pth_self (void);
 int pth_join (pth_t hd, void **value);
 int pth_abort (pth_t hd);
 void pth_exit (void *value);
Index: gnupg/scd/scdaemon.c
diff -u gnupg/scd/scdaemon.c:1.12.2.22 gnupg/scd/scdaemon.c:1.12.2.23
--- gnupg/scd/scdaemon.c:1.12.2.22	Sat Dec 18 19:53:31 2004
+++ gnupg/scd/scdaemon.c	Sun Dec 19 18:44:20 2004
@@ -351,12 +351,17 @@
      Note that this will also do the pth_init. */
 #ifndef HAVE_OPENSC
 #ifdef USE_GNU_PTH
+#ifdef HAVE_W32_SYSTEM
+  /* For W32 we need pth.  */
+  pth_init ();
+#else
   err = gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pth);
   if (err)
     {
       log_fatal ("can't register GNU Pth with Libgcrypt: %s\n",
                  gpg_strerror (err));
     }
+#endif
 #endif /*USE_GNU_PTH*/
 #endif /*!HAVE_OPENSC*/
 




More information about the Gnupg-commits mailing list