[svn] assuan - r352 - in trunk: . doc src tests

svn author wk cvs at cvs.gnupg.org
Thu Jan 28 16:31:26 CET 2010


Author: wk
Date: 2010-01-28 16:31:25 +0100 (Thu, 28 Jan 2010)
New Revision: 352

Added:
   trunk/src/sysutils.c
Modified:
   trunk/ChangeLog
   trunk/configure.ac
   trunk/doc/assuan.texi
   trunk/src/ChangeLog
   trunk/src/Makefile.am
   trunk/src/assuan-defs.h
   trunk/src/assuan-handler.c
   trunk/src/assuan-logging.c
   trunk/src/assuan-socket-server.c
   trunk/src/assuan-socket.c
   trunk/src/assuan.h
   trunk/src/system.c
   trunk/tests/Makefile.am
   trunk/tests/common.h
Log:
Cleanups for W32CE.
Implement getenv replacement for W32CE.


Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2010-01-22 18:55:16 UTC (rev 351)
+++ trunk/ChangeLog	2010-01-28 15:31:25 UTC (rev 352)
@@ -1,3 +1,11 @@
+2010-01-27  Werner Koch  <wk at g10code.com>
+
+	* tests/common.h (SOCKET2HANDLE, HANDLE2SOCKET): New.
+
+2010-01-26  Werner Koch  <wk at g10code.com>
+
+	* configure.ac (NETLIBS) [W32CE]: Use -lws2.
+
 2010-01-22  Werner Koch  <wk at g10code.com>
 
 	* configure.ac: Require libgpg-error 1.8.

Modified: trunk/src/ChangeLog
===================================================================
--- trunk/src/ChangeLog	2010-01-22 18:55:16 UTC (rev 351)
+++ trunk/src/ChangeLog	2010-01-28 15:31:25 UTC (rev 352)
@@ -1,3 +1,24 @@
+2010-01-28  Werner Koch  <wk at g10code.com>
+
+	* assuan.h: Remove ranges in list of copyright years.
+	(getenv) [W32CE]: Provide macro.
+	* sysutils.c: New.
+	(_assuan_sysutils_blurb): New.
+	(_assuan_getenv): new.
+	* assuan-logging.c: Call _assuan_sysutils_blurb.
+
+2010-01-27  Werner Koch  <wk at g10code.com>
+
+	* assuan-socket.c (_assuan_sock_bind): Replace remove by DeleteFile.
+
+	* assuan-handler.c (assuan_get_active_fds) [W32CE]: Remove use of
+	_get_osfhandle.
+	* assuan.h (assuan_fd_from_posix_fd) [__MINGW32CE__]: Ditto.
+	* system.c (assuan_fdopen): Ditto.
+	(__assuan_spawn) [W32CE]: Do not use GetPriorityClass.
+
+	* assuan-defs.h (getpid) [W32CE]: New.
+
 2010-01-22  Werner Koch  <wk at g10code.com>
 
 	* setenv.c [W32CE]: Make it a dummy.
@@ -691,7 +712,7 @@
 2007-10-04  Werner Koch  <wk at g10code.com>
 
 	* mkerrors: Map EAGAIN to GPG_ERR_EAGAIN for read and write
-	errors. 
+	errors.
 
 2007-10-02  Werner Koch  <wk at g10code.com>
 
@@ -707,7 +728,7 @@
 	* assuan.h (ASSUAN_INT2FD, ASSUAN_FD2INT): New.
 
 	* assuan-socket.c: Rewritten.
-	(assuan_sock_new, assuan_sock_connect, assuan_sock_bind) 
+	(assuan_sock_new, assuan_sock_connect, assuan_sock_bind)
 	(assuan_sock_get_nonce, assuan_sock_check_nonce): New APIs.
 
 	* assuan-io.c (_assuan_simple_read, _assuan_simple_write):

Modified: trunk/configure.ac
===================================================================
--- trunk/configure.ac	2010-01-22 18:55:16 UTC (rev 351)
+++ trunk/configure.ac	2010-01-28 15:31:25 UTC (rev 352)
@@ -199,7 +199,12 @@
 AC_SUBST(NETLIBS)
 
 if test "$have_w32_system" = yes; then
-   NETLIBS="-lws2_32 $NETLIBS"
+   if test "$have_w32ce_system" = yes; then
+     NETLIBS="-lws2 $NETLIBS"
+   else
+     # FIXME: Check why we need to use ws2_32 and document that.
+     NETLIBS="-lws2_32 $NETLIBS"
+   fi
 fi
 
 

Modified: trunk/doc/assuan.texi
===================================================================
--- trunk/doc/assuan.texi	2010-01-22 18:55:16 UTC (rev 351)
+++ trunk/doc/assuan.texi	2010-01-28 15:31:25 UTC (rev 352)
@@ -1889,7 +1889,7 @@
 of the socket wrapper functions are used.
 @end deftypefun
 
- at deftypefun gpg_error_t assuan_sock_init (void)
+ at deftypefun gpg_error_t assuan_sock_deinit (void)
 Deinitialize the socket wrappers.
 @end deftypefun
 
@@ -1933,11 +1933,18 @@
         @w{assuan_fd_t @var{fd}}, @
         @w{assuan_sock_nonce_t *@var{nonce}})
 
-On Windows this is used by the server after an accept to read the nonce
-from the client and compare it with the saved @var{nonce}.  If this
-function fails the server should immediatly drop the connection.  To
-keep the code readable this may also be used on POSIX system; it is a
-dummy function then.  See also @code{assuan_set_sock_nonce}.
+If the option @code{ASSUAN_SOCKET_SERVER_ACCEPTED} has been used,
+Libassuan has no way to check the nonce of the server.  Thus an explicit
+check of the saved nonce using this function is required.  If this
+function fails the server should immediately drop the connection.  This
+function may not be used if Libassuan does the accept call itself
+(i.e. @code{ASSUAN_SOCKET_SERVER_ACCEPTED} has not been used) because
+in this case Libassuan calls this function internally.  See also
+ at code{assuan_set_sock_nonce}.
+
+Actually this mechanism is only required on Windows but for cleanness of
+code it may be used on POSIX systems as well, where this function is
+a nop.
 @end deftypefun
 
 

Modified: trunk/src/Makefile.am
===================================================================
--- trunk/src/Makefile.am	2010-01-22 18:55:16 UTC (rev 351)
+++ trunk/src/Makefile.am	2010-01-28 15:31:25 UTC (rev 352)
@@ -37,7 +37,7 @@
 common_sources = \
 	assuan-defs.h \
 	assuan.c context.c system.c \
-	debug.c debug.h conversion.c \
+	debug.c debug.h conversion.c sysutils.c \
 	client.c server.c \
 	assuan-error.c \
 	assuan-buffer.c \

Modified: trunk/src/assuan-defs.h
===================================================================
--- trunk/src/assuan-defs.h	2010-01-22 18:55:16 UTC (rev 351)
+++ trunk/src/assuan-defs.h	2010-01-28 15:31:25 UTC (rev 352)
@@ -335,6 +335,16 @@
 #define funopen(a,r,w,s,c) _assuan_funopen ((a), (r), (w), (s), (c))
 #endif /*HAVE_FOPENCOOKIE*/
 
+/*-- sysutils.c --*/
+const char *_assuan_sysutils_blurb (void);
+
+#ifdef HAVE_W32CE_SYSTEM
+#define getpid() GetCurrentProcessId ()
+char *_assuan_getenv (const char *name);
+#define getenv(a) _assuan_getenv ((a))
+#endif
+
+
 /* Prototypes for replacement functions.  */
 #ifndef HAVE_MEMRCHR
 void *memrchr (const void *block, int c, size_t size);

Modified: trunk/src/assuan-handler.c
===================================================================
--- trunk/src/assuan-handler.c	2010-01-22 18:55:16 UTC (rev 351)
+++ trunk/src/assuan-handler.c	2010-01-28 15:31:25 UTC (rev 352)
@@ -811,7 +811,9 @@
       if (ctx->outbound.fd != ASSUAN_INVALID_FD)
         fdarray[n++] = ctx->outbound.fd;
       if (ctx->outbound.data.fp)
-#ifdef HAVE_W32_SYSTEM
+#if defined(HAVE_W32CE_SYSTEM)
+        fdarray[n++] = (void*)fileno (ctx->outbound.data.fp);
+#elif defined(HAVE_W32_SYSTEM)
         fdarray[n++] = (void*)_get_osfhandle (fileno (ctx->outbound.data.fp));
 #else
         fdarray[n++] = fileno (ctx->outbound.data.fp);

Modified: trunk/src/assuan-logging.c
===================================================================
--- trunk/src/assuan-logging.c	2010-01-22 18:55:16 UTC (rev 351)
+++ trunk/src/assuan-logging.c	2010-01-28 15:31:25 UTC (rev 352)
@@ -65,6 +65,8 @@
   flagstr = getenv ("ASSUAN_DEBUG");
   if (flagstr)
     log_cats = atoi (flagstr);
+
+  _assuan_sysutils_blurb (); /* Make sure this code gets linked in.  */
 }
 
 

Modified: trunk/src/assuan-socket-server.c
===================================================================
--- trunk/src/assuan-socket-server.c	2010-01-22 18:55:16 UTC (rev 351)
+++ trunk/src/assuan-socket-server.c	2010-01-28 15:31:25 UTC (rev 352)
@@ -46,6 +46,8 @@
 {
   assuan_fd_t fd = ctx->connected_fd;
 
+  TRACE (ctx, ASSUAN_LOG_SYSIO, "accept_connection_bottom", ctx);
+
   ctx->peercred_valid = 0;
 #ifdef HAVE_SO_PEERCRED
   {
@@ -90,12 +92,17 @@
   struct sockaddr_un clnt_addr;
   socklen_t len = sizeof clnt_addr;
 
+  TRACE1 (ctx, ASSUAN_LOG_SYSIO, "accept_connection", ctx, 
+         "listen_fd=0x%x", ctx->listen_fd);
+
   fd = SOCKET2HANDLE(accept (HANDLE2SOCKET(ctx->listen_fd), 
                              (struct sockaddr*)&clnt_addr, &len ));
   if (fd == ASSUAN_INVALID_FD)
     {
       return _assuan_error (ctx, gpg_err_code_from_syserror ());
     }
+  TRACE1 (ctx, ASSUAN_LOG_SYSIO, "accept_connection", ctx, 
+          "fd->0x%x", fd);
   if (_assuan_sock_check_nonce (ctx, fd, &ctx->listen_nonce))
     {
       _assuan_close (ctx, fd);

Modified: trunk/src/assuan-socket.c
===================================================================
--- trunk/src/assuan-socket.c	2010-01-22 18:55:16 UTC (rev 351)
+++ trunk/src/assuan-socket.c	2010-01-28 15:31:25 UTC (rev 352)
@@ -38,6 +38,7 @@
 #include <assert.h>
 
 #include "assuan-defs.h"
+#include "debug.h"
 
 /* Hacks for Slowaris.  */
 #ifndef PF_LOCAL
@@ -268,7 +269,7 @@
         {
           int save_e = errno;
           fclose (fp);
-          remove (unaddr->sun_path);
+          DeleteFile (unaddr->sun_path);
           gpg_err_set_errno (save_e);
           return rc;
         }
@@ -313,7 +314,7 @@
     }
   else
     {
-      nonce->length = 42; /* Arbitrary valuie to detect unitialized nonce. */
+      nonce->length = 42; /* Arbitrary value to detect unitialized nonce. */
       nonce->nonce[0] = 42;
     }
 #else

Modified: trunk/src/assuan.h
===================================================================
--- trunk/src/assuan.h	2010-01-22 18:55:16 UTC (rev 351)
+++ trunk/src/assuan.h	2010-01-28 15:31:25 UTC (rev 352)
@@ -1,5 +1,6 @@
 /* assuan.h - Definitions for the Assuan IPC library
-   Copyright (C) 2001-2003, 2005, 2007-2009 Free Software Foundation, Inc.
+   Copyright (C) 2001, 2002, 2003, 2005, 2007, 2008, 2009,
+                 2010  Free Software Foundation, Inc.
 
    This file is part of Assuan.
 
@@ -88,10 +89,14 @@
 static inline assuan_fd_t
 assuan_fd_from_posix_fd (int fd)
 {
+#ifdef __MINGW32CE__
+    return (assuan_fd_t)(fd);
+#else
   if (fd < 0)
     return ASSUAN_INVALID_FD;
   else
     return (assuan_fd_t) _get_osfhandle (fd);
+#endif
 }
 #else
 typedef int assuan_fd_t;
@@ -109,7 +114,7 @@
 
 /* Assuan features an emulation of Unix domain sockets based on a
    local TCP connections.  To implement access permissions based on
-   file permissions a nonce is used which is expected by th server as
+   file permissions a nonce is used which is expected by the server as
    the first bytes received.  This structure is used by the server to
    save the nonce created initially by bind.  On POSIX systems this is
    a dummy operation. */  

Modified: trunk/src/system.c
===================================================================
--- trunk/src/system.c	2010-01-22 18:55:16 UTC (rev 351)
+++ trunk/src/system.c	2010-01-28 15:31:25 UTC (rev 352)
@@ -50,7 +50,11 @@
 assuan_fdopen (int fd)
 {
 #ifdef HAVE_W32_SYSTEM
+#ifdef HAVE_W32CE_SYSTEM
+  assuan_fd_t ifd = (assuan_fd_t)fd;
+#else
   assuan_fd_t ifd = (assuan_fd_t) _get_osfhandle (fd);
+#endif
   assuan_fd_t ofd;
 
   if (! DuplicateHandle(GetCurrentProcess(), ifd, 
@@ -199,6 +203,11 @@
   sec_attr.nLength = sizeof (sec_attr);
   sec_attr.bInheritHandle = FALSE;
 
+#ifdef HAVE_W32CE_SYSTEM
+# warning Implement a CreatePipe Replacement.  
+      gpg_err_set_errno (EIO);
+      return -1;
+#else
   if (! CreatePipe (&rh, &wh, &sec_attr, 0))
     {
       TRACE1 (ctx, ASSUAN_LOG_SYSIO, "__assuan_pipe", ctx,
@@ -206,6 +215,7 @@
       gpg_err_set_errno (EIO);
       return -1;
     }
+#endif
 
   if (! DuplicateHandle (GetCurrentProcess(), (inherit_idx == 0) ? rh : wh,
 			 GetCurrentProcess(), &th, 0,
@@ -663,7 +673,9 @@
                       TRUE,                 /* Inherit handles.  */
                       (CREATE_DEFAULT_ERROR_MODE
                        | ((flags & 128)? DETACHED_PROCESS : 0)
+#ifndef HAVE_W32CE_SYSTEM
                        | GetPriorityClass (GetCurrentProcess ())
+#endif
                        | CREATE_SUSPENDED), /* Creation flags.  */
                       NULL,                 /* Environment.  */
                       NULL,                 /* Use current drive/directory.  */

Added: trunk/src/sysutils.c
===================================================================
--- trunk/src/sysutils.c	                        (rev 0)
+++ trunk/src/sysutils.c	2010-01-28 15:31:25 UTC (rev 352)
@@ -0,0 +1,130 @@
+/* sysutils.c - System utilities
+   Copyright (C) 2010 Free Software Foundation, Inc.
+
+   This file is part of Assuan.
+
+   Assuan is free software; you can redistribute it and/or modify it
+   under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of
+   the License, or (at your option) any later version.
+
+   Assuan is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+#ifdef HAVE_W32_SYSTEM
+#include <windows.h>
+#endif /*HAVE_W32_SYSTEM*/
+
+#include "assuan-defs.h"
+
+
+/* This is actually a dummy function to make sure that is module is
+   not empty.  Sokme compilers barf on that.  */
+const char *
+_assuan_sysutils_blurb (void)
+{
+  static const char blurb[] = 
+    "\n\n"
+    "This is Libassuan - The GnuPG IPC Library\n"
+    "Copyright 2000, 2002, 2003, 2004, 2007, 2008, 2009,\n"
+    "          2010 Free Software Foundation, Inc.\n"
+    "\n\n";
+  return blurb;
+}
+
+
+/* Return a string from the Win32 Registry or NULL in case of error.
+   The returned string is allocated using a plain malloc and thus the
+   caller needs to call the standard free().  The string is looked up
+   under HKEY_LOCAL_MACHINE.  */
+#ifdef HAVE_W32CE_SYSTEM
+static char *
+w32_read_registry (const wchar_t *dir, const wchar_t *name)
+{
+  HKEY handle;
+  DWORD n, nbytes;
+  wchar_t *buffer = NULL;
+  char *result = NULL;
+  
+  if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &handle))
+    return NULL; /* No need for a RegClose, so return immediately. */
+
+  nbytes = 1;
+  if (RegQueryValueEx (handle, name, 0, NULL, NULL, &nbytes))
+    goto leave;
+  buffer = malloc ((n=nbytes+2));
+  if (!buffer)
+    goto leave;
+  if (RegQueryValueEx (handle, name, 0, NULL, (PBYTE)buffer, &n))
+    {
+      free (buffer);
+      buffer = NULL;
+      goto leave;
+    }
+  
+  n = WideCharToMultiByte (CP_UTF8, 0, buffer, nbytes, NULL, 0, NULL, NULL);
+  if (n < 0 || (n+1) <= 0)
+    goto leave;
+  result = malloc (n+1);
+  if (!result)
+    goto leave;
+  n = WideCharToMultiByte (CP_UTF8, 0, buffer, nbytes, result, n, NULL, NULL);
+  if (n < 0)
+    {
+      free (result);
+      result = NULL;
+      goto leave;
+    }
+  result[n] = 0;
+
+ leave:
+  free (buffer);
+  RegCloseKey (handle);
+  return result;
+}
+#endif /*HAVE_W32CE_SYSTEM*/
+
+
+
+#ifdef HAVE_W32CE_SYSTEM
+/* Replacement for getenv which takes care of the our use of getenv.
+   The code is not thread safe but we expect it to work in all cases
+   because it is called for the first time early enough.  */
+char *
+_assuan_getenv (const char *name)
+{
+  static int initialized;
+  static char *val_debug;
+  static char *val_full_logging;
+
+  if (!initialized)
+    {
+      val_debug = w32_read_registry (L"\\Software\\GNU\\libassuan",
+                                     L"debug");
+      val_full_logging = w32_read_registry (L"\\Software\\GNU\\libassuan",
+                                            L"full_logging");
+      initialized = 1;
+    }
+
+
+  if (!strcmp (name, "ASSUAN_DEBUG"))
+    return val_debug;
+  else if (!strcmp (name, "ASSUAN_FULL_LOGGING"))
+    return val_full_logging;
+  else
+    return NULL;
+}
+#endif /*HAVE_W32CE_SYSTEM*/

Modified: trunk/tests/Makefile.am
===================================================================
--- trunk/tests/Makefile.am	2010-01-22 18:55:16 UTC (rev 351)
+++ trunk/tests/Makefile.am	2010-01-28 15:31:25 UTC (rev 352)
@@ -25,7 +25,7 @@
 BUILT_SOURCES = 
 CLEANFILES = 
 
-TESTS =
+TESTS = ce-server
 
 if USE_DESCRIPTOR_PASSING
 TESTS += fdpassing
@@ -35,5 +35,5 @@
 
 noinst_HEADERS = common.h
 noinst_PROGRAMS = $(TESTS)
-LDADD =  ../src/libassuan.la  $(NETLIBS) $(GPG_ERROR_LIBS)
+LDADD = ../src/libassuan.la  $(NETLIBS) $(GPG_ERROR_LIBS)
 

Modified: trunk/tests/common.h
===================================================================
--- trunk/tests/common.h	2010-01-22 18:55:16 UTC (rev 351)
+++ trunk/tests/common.h	2010-01-28 15:31:25 UTC (rev 352)
@@ -19,19 +19,33 @@
 
 #include <stdarg.h>
 
+#ifdef HAVE_W32CE_SYSTEM
+#define getpid() GetCurrentProcessId ()
+#define getenv(a) (NULL)
+#endif
 
+#if HAVE_W32_SYSTEM
+#define SOCKET2HANDLE(s) ((void *)(s))
+#define HANDLE2SOCKET(h) ((unsigned int)(h))
+#else
+#define SOCKET2HANDLE(s) (s)
+#define HANDLE2SOCKET(h) (h)
+#endif
+
+
 static const char *log_prefix;
 static int errorcount;
 static int verbose;
 static int debug;
 
-
 void *
 xmalloc (size_t n)
 {
   char *p = malloc (n);
   if (!p)
     {
+      if (log_prefix)
+        fprintf (stderr, "%s[%u]: ", log_prefix, (unsigned int)getpid ());
       fprintf (stderr, "out of core\n");
       exit (1);
     }
@@ -44,6 +58,8 @@
   char *p = calloc (n, m);
   if (!p)
     {
+      if (log_prefix)
+        fprintf (stderr, "%s[%u]: ", log_prefix, (unsigned int)getpid ());
       fprintf (stderr, "out of core\n");
       exit (1);
     }
@@ -69,6 +85,13 @@
 }
 
 
+const char *
+log_get_prefix (void)
+{
+  return log_prefix? log_prefix:"";
+}
+
+
 void
 log_info (const char *format, ...)
 {




More information about the Gnupg-commits mailing list