[svn] dirmngr - r284 - in trunk: . jnlib src

svn author marcus cvs at cvs.gnupg.org
Tue Feb 12 02:13:45 CET 2008


Author: marcus
Date: 2008-02-12 02:13:44 +0100 (Tue, 12 Feb 2008)
New Revision: 284

Modified:
   trunk/NEWS
   trunk/jnlib/ChangeLog
   trunk/jnlib/logging.c
   trunk/src/ChangeLog
   trunk/src/dirmngr_ldap.c
   trunk/src/exechelp.c
   trunk/src/ldap.c
Log:
jnlib/
2008-02-12  Marcus Brinkmann  <marcus at g10code.de>

	* logging.c (do_logv): Flush the logstream.

src/
2008-02-12  Marcus Brinkmann  <marcus at g10code.de>

	* ldap.c (pth_close) [! HAVE_W32_SYSTEM]: New macro.
	(struct wrapper_context_s): New member log_ev.
	(destroy_wrapper): Check FDs for != -1 rather than != 0.  Use
	pth_close instead of close.  Free CTX->log_ev.
	(ldap_wrapper_thread): Rewritten to use pth_wait instead of
	select.  Also use pth_read instead of read and pth_close instead
	of close.
	(ldap_wrapper): Initialize CTX->log_ev.
	(reader_callback): Use pth_close instead of close.
	* exechelp.c (create_inheritable_pipe) [HAVE_W32_SYSTEM]: Removed.
	(dirmngr_spawn_process) [HAVE_W32_SYSTEM]: Use pth_pipe instead.
	* dirmngr_ldap.c [HAVE_W32_SYSTEM]: Include <fcntl.h>.
	(main) [HAVE_W32_SYSTEM]: Set mode of stdout to binary.


Modified: trunk/jnlib/ChangeLog
===================================================================
--- trunk/jnlib/ChangeLog	2008-02-01 11:26:55 UTC (rev 283)
+++ trunk/jnlib/ChangeLog	2008-02-12 01:13:44 UTC (rev 284)
@@ -1,3 +1,7 @@
+2008-02-12  Marcus Brinkmann  <marcus at g10code.de>
+
+	* logging.c (do_logv): Flush the logstream.
+
 2007-08-16  Werner Koch  <wk at g10code.com>
 
 	* utf8conv.c (jnlib_iconv): Cast arg passed to iconv.

Modified: trunk/src/ChangeLog
===================================================================
--- trunk/src/ChangeLog	2008-02-01 11:26:55 UTC (rev 283)
+++ trunk/src/ChangeLog	2008-02-12 01:13:44 UTC (rev 284)
@@ -1,3 +1,19 @@
+2008-02-12  Marcus Brinkmann  <marcus at g10code.de>
+
+	* ldap.c (pth_close) [! HAVE_W32_SYSTEM]: New macro.
+	(struct wrapper_context_s): New member log_ev.
+	(destroy_wrapper): Check FDs for != -1 rather than != 0.  Use
+	pth_close instead of close.  Free CTX->log_ev.
+	(ldap_wrapper_thread): Rewritten to use pth_wait instead of
+	select.  Also use pth_read instead of read and pth_close instead
+	of close.
+	(ldap_wrapper): Initialize CTX->log_ev.
+	(reader_callback): Use pth_close instead of close.
+	* exechelp.c (create_inheritable_pipe) [HAVE_W32_SYSTEM]: Removed.
+	(dirmngr_spawn_process) [HAVE_W32_SYSTEM]: Use pth_pipe instead.
+	* dirmngr_ldap.c [HAVE_W32_SYSTEM]: Include <fcntl.h>.
+	(main) [HAVE_W32_SYSTEM]: Set mode of stdout to binary.
+
 2008-02-01  Werner Koch  <wk at g10code.com>
 
 	* ldap.c: Remove all ldap headers as they are unused.

Modified: trunk/NEWS
===================================================================
--- trunk/NEWS	2008-02-01 11:26:55 UTC (rev 283)
+++ trunk/NEWS	2008-02-12 01:13:44 UTC (rev 284)
@@ -3,9 +3,10 @@
 
  * New option --url for the LOOKUP command and dirmngr-client.
 
- * The LOOKUP command does now alsos consult the local cache.  New
+ * The LOOKUP command does now also consults the local cache.  New
    option --cache-only for it and --local for dirmngr-client.
 
+ * Port to Windows completed.
 
 Noteworthy changes in version 1.0.1 (2007-08-16)
 ------------------------------------------------

Modified: trunk/jnlib/logging.c
===================================================================
--- trunk/jnlib/logging.c	2008-02-01 11:26:55 UTC (rev 283)
+++ trunk/jnlib/logging.c	2008-02-12 01:13:44 UTC (rev 284)
@@ -504,6 +504,8 @@
         putc('\n', logstream );
       abort();
     }
+
+  fflush (logstream);
 }
 
 static void

Modified: trunk/src/dirmngr_ldap.c
===================================================================
--- trunk/src/dirmngr_ldap.c	2008-02-01 11:26:55 UTC (rev 283)
+++ trunk/src/dirmngr_ldap.c	2008-02-12 01:13:44 UTC (rev 284)
@@ -34,6 +34,7 @@
 #ifdef HAVE_W32_SYSTEM
 #include <winsock2.h>
 #include <winldap.h>
+#include <fcntl.h>
 #include "ldap-url.h"
 #else
 /* For OpenLDAP, to enable the API that we're using. */
@@ -185,6 +186,11 @@
   char *p;
   int only_search_timeout = 0;
 
+#ifdef HAVE_W32_SYSTEM
+  /* Yeah, right.  Sigh.  */
+  _setmode (_fileno (stdout), _O_BINARY);
+#endif
+
   set_strusage (my_strusage);
   log_set_prefix ("dirmngr_ldap", JNLIB_LOG_WITH_PREFIX); 
   

Modified: trunk/src/exechelp.c
===================================================================
--- trunk/src/exechelp.c	2008-02-01 11:26:55 UTC (rev 283)
+++ trunk/src/exechelp.c	2008-02-12 01:13:44 UTC (rev 284)
@@ -1,5 +1,5 @@
 /* exechelp.c - fork and exec helpers
- *      Copyright (C) 2004, 2007 g10 Code GmbH
+ * Copyright (C) 2004, 2007, 2008 g10 Code GmbH
  *
  * This file is part of DirMngr.
  *
@@ -132,40 +132,6 @@
 #endif /*HAVE_W32_SYSTEM*/
 
 
-#ifdef HAVE_W32_SYSTEM
-/* Create pipe where the write end is inheritable.  */
-static int
-create_inheritable_pipe (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(), w,
-                        GetCurrentProcess(), &h, 0,
-                        TRUE, DUPLICATE_SAME_ACCESS ))
-    {
-      log_error ("DuplicateHandle failed: %s\n", w32_strerror (-1));
-      CloseHandle (r);
-      CloseHandle (w);
-      return -1;
-    }
-  CloseHandle (w);
-  w = h;
-
-  filedes[0] = handle_to_fd (r);
-  filedes[1] = handle_to_fd (w);
-  return 0;
-}
-#endif /*HAVE_W32_SYSTEM*/
-
-
 #ifndef HAVE_W32_SYSTEM
 /* The exec core used right after the fork. This will never return. */
 static void
@@ -267,7 +233,7 @@
     return err; 
 
   /* Create a pipe.  */
-  if (create_inheritable_pipe (rp_stdout))
+  if (pth_pipe (rp_stdout, 1))
     {
       err = gpg_error (GPG_ERR_GENERAL);
       log_error (_("error creating a pipe: %s\n"), gpg_strerror (err));
@@ -275,7 +241,7 @@
       return err;
     }
 
-  if (create_inheritable_pipe (rp_stderr))
+  if (pth_pipe (rp_stderr, 1))
     {
       err = gpg_error (GPG_ERR_GENERAL);
       log_error (_("error creating a pipe: %s\n"), gpg_strerror (err));

Modified: trunk/src/ldap.c
===================================================================
--- trunk/src/ldap.c	2008-02-01 11:26:55 UTC (rev 283)
+++ trunk/src/ldap.c	2008-02-12 01:13:44 UTC (rev 284)
@@ -1,6 +1,6 @@
 /* ldap.c - LDAP access
- *      Copyright (C) 2002 Klarälvdalens Datakonsult AB
- *      Copyright (C) 2003, 2004, 2005, 2007 g10 Code GmbH
+ * Copyright (C) 2002 Klarälvdalens Datakonsult AB
+ * Copyright (C) 2003, 2004, 2005, 2007, 2008 g10 Code GmbH
  *
  * This file is part of DirMngr.
  *
@@ -38,8 +38,11 @@
 
 #if HAVE_W32_SYSTEM
 #define setenv(a,b,c) SetEnvironmentVariable ((a),(b))
+#else
+#define pth_close(fd) close(fd)
 #endif
 
+
 /* In case sysconf does not return a value we need to have a limit. */
 #ifdef _POSIX_OPEN_MAX
 #define MAX_OPEN_FDS _POSIX_OPEN_MAX
@@ -81,6 +84,7 @@
   gpg_error_t fd_error; /* Set to the gpg_error of the last read error
                            if any.  */
   int log_fd;   /* Connected with stderr of the ldap wrapper.  */
+  pth_event_t log_ev;
   ctrl_t ctrl;  /* Connection data. */
   int ready;    /* Internally used to mark to be removed contexts. */
   ksba_reader_t reader; /* The ksba reader object or NULL. */
@@ -166,9 +170,11 @@
     dirmngr_kill_process (ctx->pid);
   ksba_reader_release (ctx->reader);
   if (ctx->fd)
-    close (ctx->fd);
+    pth_close (ctx->fd);
   if (ctx->log_fd)
-    close (ctx->log_fd);
+    pth_close (ctx->log_fd);
+  if (ctx->log_ev)
+    pth_event_free (ctx->log_ev, PTH_FREE_THIS);
   xfree (ctx->line);
   xfree (ctx);
 }
@@ -239,57 +245,69 @@
 void *
 ldap_wrapper_thread (void *dummy)
 {
-  fd_set read_fds;
-  int nfds, n;
-  struct timeval tv;
-  struct wrapper_context_s *ctx, *ctx_prev;
+  int nfds;
+  int n;
+  struct wrapper_context_s *ctx;
+  struct wrapper_context_s *ctx_prev;
   char line[256];
   time_t current_time;
 
   (void)dummy;
 
+
   for (;;)
     {
-      FD_ZERO (&read_fds);
-      
-      for (nfds = -1, ctx = wrapper_list; ctx; ctx = ctx->next)
+      pth_event_t timeout_ev;
+
+      timeout_ev = pth_event (PTH_EVENT_TIME, pth_timeout (1, 0));
+      if (! timeout_ev)
+	{
+          log_error (_("pth_event failed: %s\n"), strerror (errno));
+          pth_sleep (10);
+	  continue;
+	}
+
+      for (ctx = wrapper_list; ctx; ctx = ctx->next)
         {
           if (ctx->log_fd != -1)
             {
-              FD_SET (ctx->log_fd, &read_fds);
-              if (ctx->log_fd > nfds)
-                nfds = ctx->log_fd;
+	      pth_event_isolate (ctx->log_ev);
+	      pth_event_concat (timeout_ev, ctx->log_ev, NULL);
             }
         }
-      nfds++;
 
-      tv.tv_sec = 1;
-      tv.tv_usec = 0;
-      nfds = pth_select (nfds, &read_fds, NULL, NULL, &tv);
-      if ( nfds < 0 )
+      /* Note that the read FDs are actually handles.  Thus, we can
+	 not use pth_select, but have to use pth_wait.  */
+      nfds = pth_wait (timeout_ev);
+      if (nfds < 0)
         {
-          log_error (_("select failed: %s\n"), strerror (errno));
+          pth_event_free (timeout_ev, PTH_FREE_THIS);
+          log_error (_("pth_wait failed: %s\n"), strerror (errno));
           pth_sleep (10);
-          continue;
+	  continue;
         }
+      if (pth_event_status (timeout_ev) == PTH_STATUS_OCCURRED)
+	nfds--;
+      pth_event_free (timeout_ev, PTH_FREE_THIS);
 
       current_time = time (NULL);
       if (current_time > INACTIVITY_TIMEOUT)
         current_time -= INACTIVITY_TIMEOUT;
 
       /* Note that there is no need to lock the list because we always
-         add entries at the head and thus traversing the list will
-         even work if we have a context switch in waitpid (which should
-         anyway only happen with Pth's hard system call mapping).   */
+         add entries at the head (with a pending event status) and
+         thus traversing the list will even work if we have a context
+         switch in waitpid (which should anyway only happen with Pth's
+         hard system call mapping).  */
       for (ctx = wrapper_list; ctx; ctx = ctx->next)
         {
           /* Check whether there is any logging to be done. */
-          if (nfds && ctx->log_fd != -1 && FD_ISSET (ctx->log_fd, &read_fds))
+          if (nfds && ctx->log_fd != -1
+	      && pth_event_status (ctx->log_ev) == PTH_STATUS_OCCURRED)
             {
-              /* Note that we do not need to use pth_read here because
-                 we already know that the read won't block. */
+              /* We must use the pth_read function for pipes, always.  */
               do 
-                n = read (ctx->log_fd, line, sizeof line - 1);
+                n = pth_read (ctx->log_fd, line, sizeof line - 1);
               while (n < 0 && errno == EINTR);
               if (n < 0)
                 {
@@ -300,8 +318,10 @@
               else if (!n) /* EOF */
                 {
                   print_log_line (ctx, NULL);
-                  close (ctx->log_fd);
+                  pth_close (ctx->log_fd);
                   ctx->log_fd = -1;
+                  pth_event_free (ctx->log_ev, PTH_FREE_THIS);
+                  ctx->log_ev = NULL;
                 }
               else 
                 {
@@ -397,7 +417,7 @@
           {
             fd = ctx->fd;
             ctx->fd = -1;
-            close (fd);
+            pth_close (fd);
           }
         if (ctx->ctrl)
           {
@@ -464,16 +484,16 @@
       pth_event_t evt;
       gpg_error_t err;
 
-      evt = pth_event (PTH_EVENT_TIME, pth_timeout (1, 0) );
-      n = pth_read_ev ( ctx->fd, buffer, nleft, evt);
-      if (n < 0 && evt && pth_event_occurred (evt) )
+      evt = pth_event (PTH_EVENT_TIME, pth_timeout (1, 0));
+      n = pth_read_ev (ctx->fd, buffer, nleft, evt);
+      if (n < 0 && evt && pth_event_occurred (evt))
         {
           n = 0;
           err = dirmngr_tick (ctx->ctrl);
           if (err)
             {
               ctx->fd_error = err;
-              close (ctx->fd);
+              pth_close (ctx->fd);
               ctx->fd = -1;
               if (evt)
                 pth_event_free (evt, PTH_FREE_THIS);
@@ -484,7 +504,7 @@
       else if (n < 0)
         {
           ctx->fd_error = gpg_error_from_errno (errno);
-          close (ctx->fd);
+          pth_close (ctx->fd);
           ctx->fd = -1;
           if (evt)
             pth_event_free (evt, PTH_FREE_THIS);
@@ -613,6 +633,12 @@
   ctx->printable_pid = (int) pid;
   ctx->fd = fd_out;
   ctx->log_fd = fd_err;
+  ctx->log_ev = pth_event (PTH_EVENT_FD | PTH_UNTIL_FD_READABLE, fd_err);
+  if (! ctx->log_ev)
+    {
+      xfree (ctx);
+      return gpg_error_from_syserror ();
+    }
   ctx->ctrl = ctrl;
   ctrl->refcount++;
   ctx->stamp = time (NULL);




More information about the Gnupg-commits mailing list