[git] GpgEX - branch, master, updated. gpgex-1.0.0-4-gda8aeee

by Werner Koch cvs at cvs.gnupg.org
Wed Mar 5 14:00:23 CET 2014


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GnupG extension for the Windows Explorer".

The branch, master has been updated
       via  da8aeee958e8943aad57a4b178e2f39a3897e4c6 (commit)
      from  4b230c7604ba61fec0d6ee78390065207dd6c606 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit da8aeee958e8943aad57a4b178e2f39a3897e4c6
Author: Werner Koch <wk at gnupg.org>
Date:   Wed Mar 5 13:37:35 2014 +0100

    Lock the spawning of a new UI server.
    
    * src/exechelp.c (gpgex_lock_spawning): New.
    (gpgex_unlock_spawning): New.
    * src/client.cc (uiserver_connect): Use the new lock functions.
    --
    
    The locking is required to be sure that only one UI server is started.
    The lock code has been taken from GnuPG (common/asshelp.c) of which
    g10 Code is the sole author.

diff --git a/NEWS b/NEWS
index 23c5e30..5415c9a 100644
--- a/NEWS
+++ b/NEWS
@@ -1,6 +1,9 @@
 Noteworthy changes for version 1.0.1 (unreleased)
 -------------------------------------------------
 
+* Avoid double starting of the UI-server.
+
+
 Noteworthy changes for version 1.0.0 (2013-07-30)
 -------------------------------------------------
 
diff --git a/src/client.cc b/src/client.cc
index 37fe7ef..8350181 100644
--- a/src/client.cc
+++ b/src/client.cc
@@ -1,5 +1,5 @@
 /* client.cc - gpgex assuan client implementation
-   Copyright (C) 2007, 2008, 2013 g10 Code GmbH
+   Copyright (C) 2007, 2008, 2013, 2014 g10 Code GmbH
 
    This file is part of GpgEX.
 
@@ -295,6 +295,7 @@ uiserver_connect (assuan_context_t *ctx, HWND hwnd)
   gpg_error_t rc;
   const char *socket_name = NULL;
   pid_t pid;
+  lock_spawn_t lock;
 
   TRACE_BEG (DEBUG_ASSUAN, "client_t::uiserver_connect", ctx);
 
@@ -320,19 +321,26 @@ uiserver_connect (assuan_context_t *ctx, HWND hwnd)
 
       (void) TRACE_LOG ("UI server not running, starting it");
 
-      rc = gpgex_spawn_detached (default_uiserver_cmdline ());
-      if (rc)
-	return TRACE_GPGERR (rc);
+      /* Now try to connect again with the spawn lock taken.  */
+      if (!(rc = gpgex_lock_spawning (&lock))
+          && assuan_socket_connect (*ctx, socket_name, -1, 0))
+        {
+          rc = gpgex_spawn_detached (default_uiserver_cmdline ());
+          if (!rc)
+            {
+              /* Give it a bit of time to start up and try a couple of
+                 times.  */
+              for (count = 0; count < 10; count++)
+                {
+                  Sleep (1000);
+                  rc = assuan_socket_connect (*ctx, socket_name, -1, 0);
+                  if (!rc)
+                    break;
+                }
+            }
 
-      /* Give it a bit of time to start up and try a couple of
-	 times.  */
-      for (count = 0; count < 10; count++)
-	{
-	  Sleep (1000);
-	  rc = assuan_socket_connect (*ctx, socket_name, -1, 0);
-	  if (!rc)
-	    break;
-	}
+        }
+      gpgex_unlock_spawning (&lock);
     }
 
   if (! rc)
diff --git a/src/exechelp.c b/src/exechelp.c
index 47e3e27..7ce28ba 100644
--- a/src/exechelp.c
+++ b/src/exechelp.c
@@ -1,5 +1,5 @@
 /* exechelp.c - fork and exec helpers
- * Copyright (C) 2004, 2007 g10 Code GmbH
+ * Copyright (C) 2004, 2007, 2014 g10 Code GmbH
  *
  * This file is part of GpgEX.
  *
@@ -37,7 +37,62 @@
 /* Define to 1 do enable debugging.  */
 #define DEBUG_W32_SPAWN 0
 
+
 

+
+/* Lock a spawning process.  The caller needs to provide the address
+   of a variable to store the lock information and the name or the
+   process.  */
+gpg_error_t
+gpgex_lock_spawning (lock_spawn_t *lock)
+{
+  int waitrc;
+  int timeout = 5;
+
+  _TRACE (DEBUG_ASSUAN, "gpgex_lock_spawning", lock);
+
+  *lock = CreateMutexW (NULL, FALSE, L"spawn_gnupg_uiserver_sentinel");
+  if (!*lock)
+    {
+      TRACE_LOG1 ("failed to create the spawn mutex: rc=%d", GetLastError ());
+      return gpg_error (GPG_ERR_GENERAL);
+    }
+
+ retry:
+  waitrc = WaitForSingleObject (*lock, 1000);
+  if (waitrc == WAIT_OBJECT_0)
+    return 0;
+
+  if (waitrc == WAIT_TIMEOUT && timeout)
+    {
+      timeout--;
+      goto retry;
+    }
+  if (waitrc == WAIT_TIMEOUT)
+    TRACE_LOG ("error waiting for the spawn mutex: timeout");
+  else
+    TRACE_LOG2 ("error waiting for the spawn mutex: (code=%d) rc=%d",
+                waitrc, GetLastError ());
+  return gpg_error (GPG_ERR_GENERAL);
+}
+
+
+/* Unlock the spawning process.  */
+void
+gpgex_unlock_spawning (lock_spawn_t *lock)
+{
+  if (*lock)
+    {
+      _TRACE (DEBUG_ASSUAN, "gpgex_unlock_spawning", lock);
+
+      if (!ReleaseMutex (*lock))
+        TRACE_LOG1 ("failed to release the spawn mutex: rc=%d", GetLastError());
+      CloseHandle (*lock);
+      *lock = NULL;
+    }
+}
+
+
 /* Fork and exec the program with /dev/null as stdin, stdout and
    stderr.  Returns 0 on success or an error code.  */
 gpg_error_t
diff --git a/src/exechelp.h b/src/exechelp.h
index a08855d..389da37 100644
--- a/src/exechelp.h
+++ b/src/exechelp.h
@@ -30,9 +30,15 @@ extern "C" {
 #endif
 #endif
 
+#define lock_spawn_t HANDLE
+
+gpg_error_t gpgex_lock_spawning (lock_spawn_t *lock);
+void gpgex_unlock_spawning (lock_spawn_t *lock);
+
 /* Fork and exec CMDLINE with /dev/null as stdin, stdout and stderr.
    Returns 0 on success or an error code.  */
 gpg_error_t gpgex_spawn_detached (const char *cmdline);
+
 #ifdef __cplusplus
 #if 0
 {

-----------------------------------------------------------------------

Summary of changes:
 NEWS           |    3 +++
 src/client.cc  |   34 ++++++++++++++++++++-------------
 src/exechelp.c |   57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 src/exechelp.h |    6 ++++++
 4 files changed, 86 insertions(+), 14 deletions(-)


hooks/post-receive
-- 
GnupG extension for the Windows Explorer
http://git.gnupg.org




More information about the Gnupg-commits mailing list