[svn] GnuPG - r5403 - in trunk: common tools

svn author wk cvs at cvs.gnupg.org
Mon Aug 23 21:26:06 CEST 2010


Author: wk
Date: 2010-08-23 21:26:05 +0200 (Mon, 23 Aug 2010)
New Revision: 5403

Modified:
   trunk/common/ChangeLog
   trunk/common/estream.c
   trunk/common/estream.h
   trunk/common/exechelp-w32.c
   trunk/common/exechelp-w32ce.c
   trunk/common/init.c
   trunk/tools/ChangeLog
   trunk/tools/Makefile.am
   trunk/tools/gpgconf-comp.c
Log:
gpgconf does now work for Wince.


Modified: trunk/common/ChangeLog
===================================================================
--- trunk/common/ChangeLog	2010-08-23 16:27:10 UTC (rev 5402)
+++ trunk/common/ChangeLog	2010-08-23 19:26:05 UTC (rev 5403)
@@ -1,3 +1,15 @@
+2010-08-23  Werner Koch  <wk at g10code.com>
+
+	* exechelp-w32ce.c: Rewrite all spawn stuff.
+
+	* exechelp-w32.c (close_all_fds) [W32]: Make it a dummy function.
+
+	* estream.c (es_onclose): New.
+	(notify_list_t, onclose): New.
+	(struct estream_internal): Add field ONCLOSE.
+	(es_initialize, es_deinitialize): Manage new field.
+	(do_close): Call onclose notify functions.
+
 2010-08-20  Werner Koch  <wk at g10code.com>
 
 	* exechelp-w32.c (create_inheritable_pipe): Change arg to HANDLE.

Modified: trunk/tools/ChangeLog
===================================================================
--- trunk/tools/ChangeLog	2010-08-23 16:27:10 UTC (rev 5402)
+++ trunk/tools/ChangeLog	2010-08-23 19:26:05 UTC (rev 5403)
@@ -1,3 +1,17 @@
+2010-08-23  Werner Koch  <wk at g10code.com>
+
+	* gpgconf-comp.c (GPGNAME) [W32CE]: s/gpg2/gpg/.
+	(get_config_filename) [W32CE]: Adjust absolute file name check.
+
+	* gpgconf-comp.c (retrieve_options_from_program)
+	(retrieve_options_from_file, retrieve_options_from_program)
+	(copy_file, gc_process_gpgconf_conf): Do not use es_ferror after a
+	failed es_fclose.  Note that the stream is in any case invalid
+	after calling es_fclose and that es_fclose does set ERRNO.
+
+	* Makefile.am (maybe_commonpth_libs): New.
+	(gpgconf_LDADD): Use it.
+
 2010-08-20  Werner Koch  <wk at g10code.com>
 
 	* gpgconf-comp.c (collect_error_output): Remove extra CRs.

Modified: trunk/common/estream.c
===================================================================
--- trunk/common/estream.c	2010-08-23 16:27:10 UTC (rev 5402)
+++ trunk/common/estream.c	2010-08-23 19:26:05 UTC (rev 5403)
@@ -202,8 +202,16 @@
 
 #define ES_DEFAULT_OPEN_MODE (S_IRUSR | S_IWUSR)
 
+/* A linked list to hold notification functions. */
+struct notify_list_s
+{
+  struct notify_list_s *next;
+  void (*fnc) (estream_t, void*); /* The notification function.  */
+  void *fnc_value;                /* The value to be passed to FNC.  */
+};
+typedef struct notify_list_s *notify_list_t;
+
 /* An internal stream object.  */
-
 struct estream_internal
 {
   unsigned char buffer[BUFFER_BLOCK_SIZE];
@@ -230,6 +238,7 @@
   unsigned int stdstream_fd:2;   /* 0, 1 or 2 for a standard stream.  */
   unsigned int printable_fname_inuse: 1;  /* es_fname_get has been used.  */
   size_t print_ntotal;           /* Bytes written from in print_writer. */
+  notify_list_t onclose;         /* On close notify function list.  */
 };
 
 
@@ -1534,6 +1543,7 @@
   stream->intern->deallocate_buffer = 0;
   stream->intern->printable_fname = NULL;
   stream->intern->printable_fname_inuse = 0;
+  stream->intern->onclose = NULL;
 
   stream->data_len = 0;
   stream->data_offset = 0;
@@ -1568,6 +1578,12 @@
   mem_free (stream->intern->printable_fname);
   stream->intern->printable_fname = NULL;
   stream->intern->printable_fname_inuse = 0;
+  while (stream->intern->onclose)
+    {
+      notify_list_t tmp = stream->intern->onclose->next;
+      mem_free (stream->intern->onclose);
+      stream->intern->onclose = tmp;
+    }
 
   return err;
 }
@@ -1637,6 +1653,16 @@
   if (stream)
     {
       es_list_remove (stream, with_locked_list);
+      while (stream->intern->onclose)
+        {
+          notify_list_t tmp = stream->intern->onclose->next;
+
+          if (stream->intern->onclose->fnc)
+            stream->intern->onclose->fnc (stream,
+                                          stream->intern->onclose->fnc_value);
+          mem_free (stream->intern->onclose);
+          stream->intern->onclose = tmp;
+        }
       err = es_deinitialize (stream);
       mem_free (stream->intern);
       mem_free (stream);
@@ -1647,6 +1673,34 @@
   return err;
 }
 
+
+/* This worker function is called with a locked stream.  */
+static int
+do_onclose (estream_t stream, int mode,
+            void (*fnc) (estream_t, void*), void *fnc_value)
+{
+  notify_list_t item;
+
+  if (!mode)
+    {
+      for (item = stream->intern->onclose; item; item = item->next)
+        if (item->fnc && item->fnc == fnc && item->fnc_value == fnc_value)
+          item->fnc = NULL; /* Disable this notification.  */
+    }
+  else
+    {
+      item = mem_alloc (sizeof *item);
+      if (!item)
+        return -1;
+      item->fnc = fnc;
+      item->fnc_value = fnc_value;
+      item->next = stream->intern->onclose;
+      stream->intern->onclose = item;
+    }
+  return 0;
+}
+
+
 /* Try to read BYTES_TO_READ bytes FROM STREAM into BUFFER in
    unbuffered-mode, storing the amount of bytes read in
    *BYTES_READ.  */
@@ -2760,6 +2814,7 @@
 void
 _es_set_std_fd (int no, int fd)
 {
+  fprintf (stderr, "es_set_std_fd(%d, %d)\n", no, fd);
   ESTREAM_LIST_LOCK;
   if (no >= 0 && no < 3 && !custom_std_fds_valid[no])
     {
@@ -2908,7 +2963,35 @@
 }
 
 
+/* Register or unregister a close notification function for STREAM.
+   FNC is the function to call and FNC_VALUE the value passed as
+   second argument.  To register the notification the value for MODE
+   must be 1.  If mode is 0 the function tries to remove or disable an
+   already registered notification; for this to work the value of FNC
+   and FNC_VALUE must be the same as with the registration and
+   FNC_VALUE must be a unique value.  No error will be returned if
+   MODE is 0.  Unregistered should only be used in the error case
+   because it may not remove memory internall allocated for the
+   onclose handler.
+
+   The notification will be called right before the stream is closed.
+   It may not call any estream function for STREAM, neither direct nor
+   indirectly. */
 int
+es_onclose (estream_t stream, int mode,
+            void (*fnc) (estream_t, void*), void *fnc_value)
+{
+  int err;
+
+  ESTREAM_LOCK (stream);
+  err = do_onclose (stream, mode, fnc, fnc_value);
+  ESTREAM_UNLOCK (stream);
+  
+  return err;
+}
+
+
+int
 es_fileno_unlocked (estream_t stream)
 {
   es_syshd_t syshd;

Modified: trunk/common/estream.h
===================================================================
--- trunk/common/estream.h	2010-08-23 16:27:10 UTC (rev 5402)
+++ trunk/common/estream.h	2010-08-23 19:26:05 UTC (rev 5403)
@@ -87,6 +87,7 @@
 #define es_freopen            _ESTREAM_PREFIX(es_freopen)
 #define es_fopencookie        _ESTREAM_PREFIX(es_fopencookie)
 #define es_fclose             _ESTREAM_PREFIX(es_fclose)
+#define es_onclose            _ESTREAM_PREFIX(es_onclose)
 #define es_fileno             _ESTREAM_PREFIX(es_fileno)
 #define es_fileno_unlocked    _ESTREAM_PREFIX(es_fileno_unlocked)
 #define es_flockfile          _ESTREAM_PREFIX(es_flockfile)
@@ -281,6 +282,8 @@
 			  const char *ES__RESTRICT mode,
 			  es_cookie_io_functions_t functions);
 int es_fclose (estream_t stream);
+int es_onclose (estream_t stream, int mode,
+                void (*fnc) (estream_t, void*), void *fnc_value);
 int es_fileno (estream_t stream);
 int es_fileno_unlocked (estream_t stream);
 int es_syshd (estream_t stream, es_syshd_t *syshd);

Modified: trunk/common/exechelp-w32.c
===================================================================
--- trunk/common/exechelp-w32.c	2010-08-23 16:27:10 UTC (rev 5402)
+++ trunk/common/exechelp-w32.c	2010-08-23 19:26:05 UTC (rev 5403)
@@ -94,43 +94,12 @@
 }
 
 
-/* Close all file descriptors starting with descriptor FIRST.  If
-   EXCEPT is not NULL, it is expected to be a list of file descriptors
-   which shall not be closed.  This list shall be sorted in ascending
-   order with the end marked by -1.  */
+/* Under Windows this is a dummy function.  */
 void
 close_all_fds (int first, int *except)
 {
-  int max_fd = get_max_fds ();
-  int fd, i, except_start;
-
-  if (except)
-    {
-      except_start = 0;
-      for (fd=first; fd < max_fd; fd++)
-        {
-          for (i=except_start; except[i] != -1; i++)
-            {
-              if (except[i] == fd)
-                {
-                  /* If we found the descriptor in the exception list
-                     we can start the next compare run at the next
-                     index because the exception list is ordered.  */
-                except_start = i + 1;
-                break;
-                }
-            }
-          if (except[i] == -1)
-            close (fd);
-        }
-    }
-  else
-    {
-      for (fd=first; fd < max_fd; fd++)
-        close (fd);
-    }
-
-  gpg_err_set_errno (0);
+  (void)first;
+  (void)except;
 }
 
 

Modified: trunk/common/exechelp-w32ce.c
===================================================================
--- trunk/common/exechelp-w32ce.c	2010-08-23 16:27:10 UTC (rev 5402)
+++ trunk/common/exechelp-w32ce.c	2010-08-23 19:26:05 UTC (rev 5403)
@@ -78,7 +78,8 @@
 struct feeder_thread_parms
 {
   estream_t stream;
-  int fd;
+  volatile int stream_valid;
+  HANDLE hd;
   int direction;
 };
 
@@ -89,71 +90,106 @@
 {
   struct feeder_thread_parms *parm = arg;
   char buffer[4096];
+  int rc;
 
   if (parm->direction)
     {
-      size_t nread;
+      size_t nread = 0;
       DWORD nwritten;
 
-      while (!es_read (parm->stream, buffer, sizeof buffer, &nread))
+      log_debug ("feeder_thread estream->pipe: stream=%p pipe=%p\n",
+                 parm->stream, parm->hd);
+      while (parm->stream_valid
+             && !es_read (parm->stream, buffer, sizeof buffer, &nread))
         {
           do
             {
-              if (!WriteFile (fd_to_handle (parm->fd), 
-                              buffer, nread, &nwritten, NULL))
+              pth_enter ();
+              rc = WriteFile (parm->hd, buffer, nread, &nwritten, NULL);
+              pth_leave ();
+              if (!rc)
                 {
-                  log_debug ("feeder(%d): WriteFile error: rc=%d\n",
-                             parm->fd, (int)GetLastError ());
+                  log_debug ("feeder(%p): WriteFile error: rc=%d\n",
+                             parm->hd, (int)GetLastError ());
                   goto leave;
                 }
               nread -= nwritten;
             }
           while (nread);
         }
-      if (nread)
-        log_debug ("feeder(%d): es_read error: %s\n",
-                   parm->fd, strerror (errno));
+      if (!parm->stream_valid)
+        log_debug ("feeder(%p): closed by other thread\n", parm->hd);
+      else if (nread)
+        log_debug ("feeder(%p): es_read error: %s\n",
+                   parm->hd, strerror (errno));
     }
   else
     {
-      DWORD nread;
+      DWORD nread = 0;
       size_t nwritten;
 
-      while (ReadFile (fd_to_handle (parm->fd),
-                       buffer, sizeof buffer, &nread, NULL) && nread)
+      log_debug ("feeder_thread pipe->estream: stream=%p pipe=%p\n",
+                 parm->stream, parm->hd);
+      while ( (pth_enter (),
+               (rc = ReadFile (parm->hd, buffer, sizeof buffer, &nread, NULL)),
+               pth_leave (),
+               rc) && nread)
         {
-          do 
+          log_debug ("feeder_thread pipe->estream: read %d bytes\n",
+                     (int)nread);
+          do
             {
-              if (es_write (parm->stream, buffer, nread, &nwritten))
+              if (parm->stream_valid
+                  && es_write (parm->stream, buffer, nread, &nwritten))
                 {
-                  log_debug ("feeder(%d): es_write error: %s\n",
-                             parm->fd, strerror (errno));
+                  log_debug ("feeder(%p): es_write error: %s\n",
+                             parm->hd, strerror (errno));
                   goto leave;
                 }
+              log_debug ("feeder_thread pipe->estream: es_wrote %d bytes\n",
+                         (int)nwritten);
               nread -= nwritten;
             }
-          while (nread);
+          while (nread && parm->stream_valid);
         }
-      if (nread)
-        log_debug ("feeder(%d): ReadFile error: rc=%d\n",
-                   parm->fd, (int)GetLastError ());
+      if (!parm->stream_valid)
+        log_debug ("feeder(%p): closed by other thread\n", parm->hd);
+      else if (nread)
+        log_debug ("feeder(%p): ReadFile error: rc=%d\n",
+                   parm->hd, (int)GetLastError ());
       else
-        log_debug ("feeder(%d): eof\n", parm->fd);
+        log_debug ("feeder(%p): eof\n", parm->hd);
     }
 
 leave:
-  CloseHandle (fd_to_handle (parm->fd));
+  log_debug ("feeder(%p): waiting for es_fclose\n", parm->hd);
+  while (parm->stream_valid)
+    pth_yield (NULL);
+  log_debug ("feeder(%p): about to close the pipe handle\n", parm->hd);
+  CloseHandle (parm->hd);
+  log_debug ("feeder(%p): pipe handle closed\n", parm->hd);
   xfree (parm);
   return NULL;
 }
 #endif /*USE_GNU_PTH*/
 
+#ifdef USE_GNU_PTH      
+static void
+feeder_onclose_notification (estream_t stream, void *opaque)
+{
+  struct feeder_thread_parms *parm = opaque;
+  (void)stream;
+  log_debug ("feeder(%p): received onclose note\n", parm->hd);
+  parm->stream_valid = 0;
+}
+#endif /*USE_GNU_PTH*/
+
 /* Fire up a thread to copy data between STREAM and a pipe's
    descriptor FD.  With DIRECTION set to true the copy takes place
    from the stream to the pipe, otherwise from the pipe to the
    stream.  */
 static gpg_error_t
-start_feeder (estream_t stream, int fd, int direction)
+start_feeder (estream_t stream, HANDLE hd, int direction)
 {
 #ifdef USE_GNU_PTH      
   gpg_error_t err;
@@ -164,19 +200,27 @@
   if (!parm)
     return gpg_error_from_syserror ();
   parm->stream = stream;
-  parm->fd = fd;
+  parm->stream_valid = 1;
+  parm->hd = hd;
   parm->direction = direction;
+
+  if (es_onclose (stream, 1, feeder_onclose_notification, parm))
+    {
+      err = gpg_error_from_syserror ();
+      xfree (parm);
+      return err;
+    }
   
   tattr = pth_attr_new ();
   pth_attr_set (tattr, PTH_ATTR_JOINABLE, 0);
   pth_attr_set (tattr, PTH_ATTR_STACK_SIZE, 64*1024);
   pth_attr_set (tattr, PTH_ATTR_NAME, "exec-feeder");
   
-  log_error ("spawning new feeder(%p, %d, %d)\n", stream, fd, direction);
+  log_debug ("spawning new feeder(%p, %p, %d)\n", stream, hd, direction);
   if(!pth_spawn (tattr, feeder_thread, parm))
     {
       err = gpg_error_from_syserror ();
-      log_error ("error spawning feeder: %s\n", gpg_strerror (err));
+      es_onclose (stream, 0, feeder_onclose_notification, parm);
       xfree (parm);
     }
   else
@@ -186,7 +230,7 @@
   return err;
 #else
   (void)stream;
-  (void)fd;
+  (void)hd;
   (void)direction;
   return gpg_error (GPG_ERR_NOT_IMPLEMENTED);  /* No Pth.  */
 #endif
@@ -214,50 +258,19 @@
 }
 
 
-/* Close all file descriptors starting with descriptor FIRST.  If
-   EXCEPT is not NULL, it is expected to be a list of file descriptors
-   which shall not be closed.  This list shall be sorted in ascending
-   order with the end marked by -1.  */
+/* Under Windows this is a dummy function.  */
 void
 close_all_fds (int first, int *except)
 {
-  int max_fd = get_max_fds ();
-  int fd, i, except_start;
-
-  if (except)
-    {
-      except_start = 0;
-      for (fd=first; fd < max_fd; fd++)
-        {
-          for (i=except_start; except[i] != -1; i++)
-            {
-              if (except[i] == fd)
-                {
-                  /* If we found the descriptor in the exception list
-                     we can start the next compare run at the next
-                     index because the exception list is ordered.  */
-                except_start = i + 1;
-                break;
-                }
-            }
-          if (except[i] == -1)
-            close (fd);
-        }
-    }
-  else
-    {
-      for (fd=first; fd < max_fd; fd++)
-        close (fd);
-    }
-
-  gpg_err_set_errno (0);
+  (void)first;
+  (void)except;
 }
 
 
 /* Returns an array with all currently open file descriptors.  The end
    of the array is marked by -1.  The caller needs to release this
    array using the *standard free* and not with xfree.  This allow the
-   use of this fucntion right at startup even before libgcrypt has
+   use of this function right at startup even before libgcrypt has
    been initialized.  Returns NULL on error and sets ERRNO
    accordingly.  */
 int *
@@ -335,9 +348,7 @@
    CMDLINE gets the address of a newly allocated string.  */
 static int
 build_w32_commandline (const char * const *argv,
-		       int fd0, int fd0_isnull,
-                       int fd1, int fd1_isnull,
-                       int fd2, int fd2_isnull,
+		       int rvid0, int rvid1, int rvid2,
                        char **cmdline)
 {
   int i, n;
@@ -347,30 +358,24 @@
 
   p = fdbuf;
   *p = 0;
-  if (fd0)
-    {
-      if (fd0_isnull)
-        strcpy (p, "-&S0=null ");
-      else
-        snprintf (p, 25, "-&S0=%d ", fd0);
-      p += strlen (p);
-    }
-  if (fd1)
-    {
-      if (fd1_isnull)
-        strcpy (p, "-&S1=null ");
-      else
-        snprintf (p, 25, "-&S1=%d ", fd1);
-      p += strlen (p);
-    }
-  if (fd2)
-    {
-      if (fd2_isnull)
-        strcpy (p, "-&S2=null ");
-      else
-        snprintf (p, 25, "-&S2=%d ", fd2);
-      p += strlen (p);
-    }
+
+  if (rvid0)
+    snprintf (p, 25, "-&S0=%d ", rvid0);
+  else
+    strcpy (p, "-&S0=null ");
+  p += strlen (p);
+
+  if (rvid1)
+    snprintf (p, 25, "-&S1=%d ", rvid1);
+  else
+    strcpy (p, "-&S1=null ");
+  p += strlen (p);
+
+  if (rvid2)
+    snprintf (p, 25, "-&S2=%d ", rvid2);
+  else
+    strcpy (p, "-&S2=null ");
+  p += strlen (p);
   
   *cmdline = NULL;
   n = strlen (fdbuf);
@@ -492,94 +497,149 @@
                      estream_t *r_errfp,
                      pid_t *pid)
 {
-#if 0
   gpg_error_t err;
   PROCESS_INFORMATION pi = {NULL };
   char *cmdline;
-  int inpipe[2], outpipe[2], errpipe[2];
+  es_syshd_t syshd;
+  struct {
+    HANDLE hd;
+    int rvid;
+  } inpipe = {INVALID_HANDLE_VALUE, 0};
+  struct {
+    HANDLE hd;
+    int rvid;
+  } outpipe = {INVALID_HANDLE_VALUE, 0};
+  struct {
+    HANDLE hd;
+    int rvid;
+  } errpipe = {INVALID_HANDLE_VALUE, 0};
+  estream_t outfp = NULL;
+  estream_t errfp = NULL;
 
   (void)preexec;
   (void)flags;
   
   /* Setup return values.  */
-  *statusfile = NULL;
-  *pid = (pid_t)(-1);
+  if (r_outfp)
+    *r_outfp = NULL;
+  if (r_errfp)
+    *r_errfp = NULL;
+  *pid = (pid_t)(-1); /* Always required.  */
 
-  /* A NULL INFILE or OUTFILE is only used by gpgtar thus we don't
-     need to implement this for CE.  */
-  if (!infile || !outfile)
-    return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
+  log_debug ("%s: enter\n", __func__);
+  if (infp)
+    {
+      es_fflush (infp);
+      es_rewind (infp);
 
-  es_fflush (infile);
-  es_rewind (infile);
+      /* Create a pipe to copy our infile to the stdin of the child
+         process.  On success inpipe.hd is owned by the feeder.  */
+      inpipe.hd = _assuan_w32ce_prepare_pipe (&inpipe.rvid, 1);
+      if (inpipe.hd == INVALID_HANDLE_VALUE)
+        {
+          log_error ("_assuan_w32ce_prepare_pipe failed: %s\n",
+                     w32_strerror (-1));
+          gpg_err_set_errno (EIO);
+          return gpg_error_from_syserror ();
+        }
+      log_debug ("%s: inpipe %p created; hd=%p rvid=%d\n", __func__,
+                 infp, inpipe.hd, inpipe.rvid);
+      err = start_feeder (infp, inpipe.hd, 1);
+      if (err)
+        {
+          log_error ("error spawning feeder: %s\n", gpg_strerror (err));
+          CloseHandle (inpipe.hd);
+          return err;
+        }
+      inpipe.hd = INVALID_HANDLE_VALUE; /* Now owned by the feeder.  */
+      log_debug ("%s: inpipe %p created; feeder started\n", __func__,
+                 infp);
+    }
 
-  /* Create a pipe to copy our infile to the stdin of the child
-     process.  On success inpipe[1] is owned by the feeder.  */
-  err = create_inheritable_pipe (inpipe, 0);
-  if (err)
+  if (r_outfp)
     {
-      log_error (_("error creating a pipe: %s\n"), gpg_strerror (err));
-      return err;
+      /* Create a pipe to make the stdout of the child process
+         available as a stream.  */
+      outpipe.hd = _assuan_w32ce_prepare_pipe (&outpipe.rvid, 0);
+      if (outpipe.hd == INVALID_HANDLE_VALUE)
+        {
+          log_error ("_assuan_w32ce_prepare_pipe failed: %s\n",
+                     w32_strerror (-1));
+          gpg_err_set_errno (EIO);
+          /* Fixme release other stuff/kill feeder.  */
+          return gpg_error_from_syserror ();
+        }
+      syshd.type = ES_SYSHD_HANDLE;
+      syshd.u.handle = outpipe.hd;
+      err = 0;
+      outfp = es_sysopen (&syshd, "r");
+      if (!outfp)
+        {
+          err = gpg_err_make (errsource, gpg_err_code_from_syserror ());
+          log_error ("error opening pipe stream: %s\n", gpg_strerror (err));
+          CloseHandle (outpipe.hd);
+          return err;
+        }
+      log_debug ("%s: outpipe %p created; hd=%p rvid=%d\n", __func__,
+                 outfp, outpipe.hd, outpipe.rvid);
+      outpipe.hd = INVALID_HANDLE_VALUE; /* Now owned by the OUTFP.  */
     }
-  err = start_feeder (infile, inpipe[1], 1);
-  if (err)
-    {
-      log_error (_("error spawning feeder: %s\n"), gpg_strerror (err));
-      CloseHandle (fd_to_handle (inpipe[1]));
-      return err;
-    }
 
-  /* Create a pipe to copy stdout of the child process to our
-     outfile. On success outpipe[0] is owned by the feeded.  */
-  err = create_inheritable_pipe (outpipe, 1);
-  if (err)
+  if (r_errfp)
     {
-      log_error (_("error creating a pipe: %s\n"), gpg_strerror (err));
-      return err;
+      /* Create a pipe to make the stderr of the child process
+         available as a stream.  */
+      errpipe.hd = _assuan_w32ce_prepare_pipe (&errpipe.rvid, 0);
+      if (errpipe.hd == INVALID_HANDLE_VALUE)
+        {
+          log_error ("_assuan_w32ce_prepare_pipe failed: %s\n",
+                     w32_strerror (-1));
+          gpg_err_set_errno (EIO);
+          /* Fixme release other stuff/kill feeder.  */
+          return gpg_error_from_syserror ();
+        }
+      syshd.type = ES_SYSHD_HANDLE;
+      syshd.u.handle = errpipe.hd;
+      err = 0;
+      errfp = es_sysopen (&syshd, "r");
+      if (!errfp)
+        {
+          err = gpg_err_make (errsource, gpg_err_code_from_syserror ());
+          log_error ("error opening pipe stream: %s\n", gpg_strerror (err));
+          CloseHandle (errpipe.hd);
+          return err;
+        }
+      log_debug ("%s: errpipe %p created; hd=%p rvid=%d\n", __func__,
+                 errfp, errpipe.hd, errpipe.rvid);
+      errpipe.hd = INVALID_HANDLE_VALUE; /* Now owned by the ERRFP.  */
     }
-  err = start_feeder (outfile, outpipe[0], 0);
-  if (err)
-    {
-      log_error (_("error spawning feeder: %s\n"), gpg_strerror (err));
-      CloseHandle (fd_to_handle (outpipe[0]));
-      return err;
-    }
 
 
-  /* Create a pipe for use with stderr of the child process.  */
-  err = create_inheritable_pipe (errpipe, 1);
-  if (err)
-    {
-      log_error (_("error creating a pipe: %s\n"), gpg_strerror (err));
-      return err;
-    }
 
   /* Build the command line.  */
-  err = build_w32_commandline (argv,
-                               inpipe[0], 0,
-                               outpipe[1], 0,
-                               errpipe[1], 0,
+  err = build_w32_commandline (argv, inpipe.rvid, outpipe.rvid, errpipe.rvid,
                                &cmdline);
   if (err)
     {
-      CloseHandle (fd_to_handle (errpipe[0]));
+      /* Fixme release other stuff/kill feeder.  */
+      CloseHandle (errpipe.hd);
       return err; 
     }
 
-  
   log_debug ("CreateProcess, path=`%s' cmdline=`%s'\n", pgmname, cmdline);
   if (!create_process (pgmname, cmdline, &pi))
     {
       log_error ("CreateProcess failed: %s\n", w32_strerror (-1));
       xfree (cmdline);
-      CloseHandle (fd_to_handle (errpipe[0]));
+      /* Fixme release other stuff/kill feeder.  */
+      CloseHandle (errpipe.hd);
       return gpg_error (GPG_ERR_GENERAL);
     }
   xfree (cmdline);
   cmdline = NULL;
 
   /* Note: The other end of the pipe is a rendezvous id and thus there
-     is no need to close.  */
+     is no need for a close.  */
 
   log_debug ("CreateProcess ready: hProcess=%p hThread=%p"
              " dwProcessID=%d dwThreadId=%d\n",
@@ -591,20 +651,12 @@
   ResumeThread (pi.hThread);
   CloseHandle (pi.hThread); 
 
-  *statusfile = es_fdopen (handle_to_fd (errpipe[0]), "r");
-  if (!*statusfile)
-    {
-      err = gpg_error_from_syserror ();
-      log_error (_("can't fdopen pipe for reading: %s\n"), gpg_strerror (err));
-      CloseHandle (pi.hProcess);
-      return err;
-    }
-
+  if (r_outfp)
+    *r_outfp = outfp;
+  if (r_errfp)
+    *r_errfp = errfp;
   *pid = handle_to_pid (pi.hProcess);
   return 0;
-#else
-  return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
-#endif
 }
 
 
@@ -632,7 +684,7 @@
     return gpg_error (GPG_ERR_NOT_SUPPORTED);
 
   /* Build the command line.  */
-  err = build_w32_commandline (argv, -1, 1, -1, 1, -1, 1, &cmdline);
+  err = build_w32_commandline (argv, 0, 0, 0, &cmdline);
   if (err)
     return err; 
 
@@ -755,7 +807,7 @@
   (void)envp;
   
   /* Build the command line.  */
-  err = build_w32_commandline (argv, -1, 1, -1, 1, -1, 1, &cmdline);
+  err = build_w32_commandline (argv, 0, 0, 0, &cmdline);
   if (err)
     return err; 
 

Modified: trunk/common/init.c
===================================================================
--- trunk/common/init.c	2010-08-23 16:27:10 UTC (rev 5402)
+++ trunk/common/init.c	2010-08-23 19:26:05 UTC (rev 5403)
@@ -118,9 +118,18 @@
   (void)argvp;
 #endif
 
+  /* Access the standard estreams as early as possible.  If we don't
+     do this the original stdio streams may have been closed when
+     _es_get_std_stream is first use and in turn it would connect to
+     the bit bucket.  */
+  {
+    int i;
+    for (i=0; i < 3; i++)
+      (void)_es_get_std_stream (i);
+  }
+
   /* --version et al shall use estream as well.  */
   argparse_register_outfnc (writestring_via_estream);
-
 }
 
 
@@ -191,6 +200,6 @@
         argv[i] = NULL;
     }
 
-
+  
 }
 #endif /*HAVE_W32CE_SYSTEM*/

Modified: trunk/tools/Makefile.am
===================================================================
--- trunk/tools/Makefile.am	2010-08-23 16:27:10 UTC (rev 5402)
+++ trunk/tools/Makefile.am	2010-08-23 19:26:05 UTC (rev 5403)
@@ -67,7 +67,15 @@
 
 common_libs = $(libcommon) ../gl/libgnu.a
 commonpth_libs = $(libcommonpth) ../gl/libgnu.a
+
+# Some modules require PTH under W32CE.
 if HAVE_W32CE_SYSTEM
+maybe_commonpth_libs = $(commonpth_libs)
+else
+maybe_commonpth_libs = $(common_libs)
+endif
+
+if HAVE_W32CE_SYSTEM
 pwquery_libs = 
 else
 pwquery_libs = ../common/libsimple-pwquery.a
@@ -85,8 +93,8 @@
 
 # common sucks in gpg-error, will they, nil they (some compilers
 # do not eliminate the supposed-to-be-unused-inline-functions).
-gpgconf_LDADD = $(common_libs) $(opt_libassuan_libs) \
-                $(LIBINTL) $(GPG_ERROR_LIBS) $(NETLIBS) \
+gpgconf_LDADD = $(maybe_commonpth_libs) $(opt_libassuan_libs) \
+                $(LIBINTL) $(GPG_ERROR_LIBS) $(PTH_LIBS) $(NETLIBS) \
 	        $(LIBICONV) $(W32SOCKLIBS)
 
 gpgparsemail_SOURCES = gpgparsemail.c rfc822parse.c rfc822parse.h

Modified: trunk/tools/gpgconf-comp.c
===================================================================
--- trunk/tools/gpgconf-comp.c	2010-08-23 16:27:10 UTC (rev 5402)
+++ trunk/tools/gpgconf-comp.c	2010-08-23 19:26:05 UTC (rev 5403)
@@ -54,9 +54,8 @@
 
 /* There is a problem with gpg 1.4 under Windows: --gpgconf-list
    returns a plain filename without escaping.  As long as we have not
-   fixed that we need to use gpg2 - it might actually be better to use
-   gpg2 in any case.  */
-#ifdef HAVE_W32_SYSTEM
+   fixed that we need to use gpg2.  */
+#if defined(HAVE_W32_SYSTEM) && !defined(HAVE_W32CE_SYSTEM)
 #define GPGNAME "gpg2"
 #else
 #define GPGNAME "gpg"
@@ -1799,7 +1798,9 @@
   else
     filename = "";
 
-#ifdef HAVE_DOSISH_SYSTEM
+#if HAVE_W32CE_SYSTEM
+  if (!(filename[0] == '/' || filename[0] == '\\'))
+#elif defined(HAVE_DOSISH_SYSTEM)
   if (!(filename[0] 
         && filename[1] == ':'
         && (filename[2] == '/' || filename[2] == '\\')))
@@ -1916,7 +1917,7 @@
     }
   if (length < 0 || es_ferror (outfp))
     gc_error (1, errno, "error reading from %s", pgmname);
-  if (es_fclose (outfp) && es_ferror (outfp))
+  if (es_fclose (outfp))
     gc_error (1, errno, "error closing %s", pgmname);
 
   err = gnupg_wait_process (pgmname, pid, 1, &exitcode);
@@ -2018,7 +2019,7 @@
 
       if (length < 0 || es_ferror (config))
 	gc_error (1, errno, "error reading from %s", config_filename);
-      if (es_fclose (config) && es_ferror (config))
+      if (es_fclose (config))
 	gc_error (1, errno, "error closing %s", config_filename);
     }
 
@@ -2098,7 +2099,7 @@
   if (config_option->flags & GC_OPT_FLAG_NO_CHANGE)
     list_option->flags |= GC_OPT_FLAG_NO_CHANGE;
 
-  if (list_file && fclose (list_file) && ferror (list_file))
+  if (list_file && fclose (list_file))
     gc_error (1, errno, "error closing %s", list_filename);
   xfree (line);
 }
@@ -2328,9 +2329,9 @@
       return -1;
     }
 
-  if (fclose (dst) && ferror (dst))
+  if (fclose (dst))
     gc_error (1, errno, "error closing %s", dst_name);
-  if (fclose (src) && ferror (src))
+  if (fclose (src))
     gc_error (1, errno, "error closing %s", src_name);
 
   return 0;
@@ -3623,7 +3624,7 @@
       gc_error (0, errno, "error reading from `%s'", fname);
       result = -1;
     }
-  if (fclose (config) && ferror (config))
+  if (fclose (config))
     gc_error (0, errno, "error closing `%s'", fname);
 
   xfree (line);





More information about the Gnupg-commits mailing list