[svn] GnuPG - r5297 - trunk/common

svn author wk cvs at cvs.gnupg.org
Mon Mar 22 15:22:41 CET 2010


Author: wk
Date: 2010-03-22 15:22:41 +0100 (Mon, 22 Mar 2010)
New Revision: 5297

Modified:
   trunk/common/ChangeLog
   trunk/common/exechelp.c
Log:
Code cleanup.


Modified: trunk/common/ChangeLog
===================================================================
--- trunk/common/ChangeLog	2010-03-22 12:46:05 UTC (rev 5296)
+++ trunk/common/ChangeLog	2010-03-22 14:22:41 UTC (rev 5297)
@@ -1,5 +1,12 @@
 2010-03-22  Werner Koch  <wk at g10code.com>
 
+	* exechelp.c (create_inheritable_pipe_r)
+	(create_inheritable_pipe_w): Fold both into ...
+	(create_inheritable_pipe): .. New.  Change callers to use this.
+	(gnupg_create_inbound_pipe, gnupg_create_outbound_pipe): Factor
+	code out to ...
+	(do_create_pipe): .. New.
+
 	* init.c (parse_std_file_handles): Change to use rendezvous ids.
 
 2010-03-15  Werner Koch  <wk at g10code.com>

Modified: trunk/common/exechelp.c
===================================================================
--- trunk/common/exechelp.c	2010-03-22 12:46:05 UTC (rev 5296)
+++ trunk/common/exechelp.c	2010-03-22 14:22:41 UTC (rev 5297)
@@ -250,7 +250,7 @@
     p = stpcpy (p, "\"\"");
   else if (strpbrk (string, " \t\n\v\f\""))
     {
-      /* Need top do some kind of quoting.  */
+      /* Need to do some kind of quoting.  */
       p = stpcpy (p, "\"");
       for (s=string; *s; s++)
         {
@@ -311,9 +311,10 @@
 
 
 #ifdef HAVE_W32_SYSTEM
-/* Create  pipe where the write end is inheritable.  */
+/* Create pipe where one end is inheritable: With an INHERIT_IDX of 0
+   the read end is inheritable, with 1 the write end is inheritable.  */
 static int
-create_inheritable_pipe_w (int filedes[2])
+create_inheritable_pipe (int filedes[2], int inherit_idx)
 {
   HANDLE r, w, h;
   SECURITY_ATTRIBUTES sec_attr;
@@ -325,7 +326,7 @@
   if (!CreatePipe (&r, &w, &sec_attr, 0))
     return -1;
 
-  if (!DuplicateHandle (GetCurrentProcess(), w,
+  if (!DuplicateHandle (GetCurrentProcess(), inherit_idx? w : r,
                         GetCurrentProcess(), &h, 0,
                         TRUE, DUPLICATE_SAME_ACCESS ))
     {
@@ -334,39 +335,17 @@
       CloseHandle (w);
       return -1;
     }
-  CloseHandle (w);
-  w = h;
 
-  filedes[0] = handle_to_fd (r);
-  filedes[1] = handle_to_fd (w);
-  return 0;
-}
-
-/* Create  pipe where the read end is inheritable.  */
-static int
-create_inheritable_pipe_r (int filedes[2])
-{
-  HANDLE r, w, h;
-  SECURITY_ATTRIBUTES sec_attr;
-
-  memset (&sec_attr, 0, sizeof sec_attr );
-  sec_attr.nLength = sizeof sec_attr;
-  sec_attr.bInheritHandle = FALSE;
-    
-  if (!CreatePipe (&r, &w, &sec_attr, 0))
-    return -1;
-
-  if (!DuplicateHandle (GetCurrentProcess(), r,
-                        GetCurrentProcess(), &h, 0,
-                        TRUE, DUPLICATE_SAME_ACCESS ))
+  if (inherit_idx)
     {
-      log_error ("DuplicateHandle failed: %s\n", w32_strerror (-1));
-      CloseHandle (r);
       CloseHandle (w);
-      return -1;
+      w = h;
     }
-  CloseHandle (r);
-  r = h;
+  else
+    {
+      CloseHandle (r);
+      r = h;
+    }
 
   filedes[0] = handle_to_fd (r);
   filedes[1] = handle_to_fd (w);
@@ -454,10 +433,8 @@
 #endif /*!HAVE_W32_SYSTEM*/
 
 
-/* Portable function to create a pipe.  Under Windows the write end is
-   inheritable.  */
-gpg_error_t
-gnupg_create_inbound_pipe (int filedes[2])
+static gpg_error_t
+do_create_pipe (int filedes[2], int inherit_idx)
 {
   gpg_error_t err = 0;
 #if HAVE_W32_SYSTEM
@@ -465,7 +442,7 @@
 
   filedes[0] = filedes[1] = -1;
   err = gpg_error (GPG_ERR_GENERAL);
-  if (!create_inheritable_pipe_w (fds))
+  if (!create_inheritable_pipe (fds, inherit_idx))
     {
       filedes[0] = _open_osfhandle (fds[0], 0);
       if (filedes[0] == -1)
@@ -497,48 +474,21 @@
   return err;
 }
 
+/* Portable function to create a pipe.  Under Windows the write end is
+   inheritable.  */
+gpg_error_t
+gnupg_create_inbound_pipe (int filedes[2])
+{
+  return do_create_pipe (filedes, 1);
+}
 
+
 /* Portable function to create a pipe.  Under Windows the read end is
    inheritable.  */
 gpg_error_t
 gnupg_create_outbound_pipe (int filedes[2])
 {
-  gpg_error_t err = 0;
-#if HAVE_W32_SYSTEM
-  int fds[2];
-
-  filedes[0] = filedes[1] = -1;
-  err = gpg_error (GPG_ERR_GENERAL);
-  if (!create_inheritable_pipe_r (fds))
-    {
-      filedes[0] = _open_osfhandle (fds[0], 0);
-      if (filedes[0] == -1)
-        {
-          log_error ("failed to translate osfhandle %p\n", (void*)fds[0]);
-          CloseHandle (fd_to_handle (fds[1]));
-        }
-      else 
-        {
-          filedes[1] = _open_osfhandle (fds[1], 1);
-          if (filedes[1] == -1)
-            {
-              log_error ("failed to translate osfhandle %p\n", (void*)fds[1]);
-              close (filedes[0]);
-              filedes[0] = -1;
-              CloseHandle (fd_to_handle (fds[1]));
-            }
-          else
-            err = 0;
-        }
-    }
-#else
-  if (pipe (filedes) == -1)
-    {
-      err = gpg_error_from_syserror ();
-      filedes[0] = filedes[1] = -1;
-    }
-#endif
-  return err;
+  return do_create_pipe (filedes, 0);
 }
 
 
@@ -606,7 +556,7 @@
     return err; 
 
   /* Create a pipe.  */
-  if (create_inheritable_pipe_w (rp))
+  if (create_inheritable_pipe (rp, 1))
     {
       err = gpg_error (GPG_ERR_GENERAL);
       log_error (_("error creating a pipe: %s\n"), gpg_strerror (err));




More information about the Gnupg-commits mailing list