[Pkg-gnupg-commit] [libassuan] 48/437: * assuan-util.c (assuan_set_io_func): New. * assuan-buffer.c (writen, readline): Use the new functions instead of pth. * assuan-socket-server.c (accept_connection): Don't use the pth_accept - using the assuan included accept code would be a bad idea within Pth so we don't need a replacement function.

Eric Dorland eric at moszumanska.debian.org
Fri May 22 05:33:22 UTC 2015


This is an automated email from the git hooks/post-receive script.

eric pushed a commit to branch master
in repository libassuan.

commit 1e45cc2fb3618cdfcb0b790d563201c6ecb16334
Author: Werner Koch <wk at gnupg.org>
Date:   Thu May 23 09:07:12 2002 +0000

    * assuan-util.c (assuan_set_io_func): New.
    * assuan-buffer.c (writen, readline): Use the new functions
    instead of pth.
    * assuan-socket-server.c (accept_connection): Don't use the
    pth_accept - using the assuan included accept code would be a bad
    idea within Pth so we don't need a replacement function.
---
 src/ChangeLog              | 15 ++++++++++
 src/assuan-buffer.c        | 20 +++++--------
 src/assuan-defs.h          |  5 ++++
 src/assuan-pipe-server.c   |  2 +-
 src/assuan-socket-server.c | 72 ++++++++++++++++++++++++++++++++--------------
 src/assuan-util.c          | 14 +++++++++
 src/assuan.h               |  4 +++
 7 files changed, 97 insertions(+), 35 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 2b95d56..bb74b7c 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,18 @@
+2002-05-23  Werner Koch  <wk at gnupg.org>
+
+	* assuan-util.c (assuan_set_io_func): New.
+	* assuan-buffer.c (writen, readline): Use the new functions
+	instead of pth.
+	* assuan-socket-server.c (accept_connection): Don't use the
+	pth_accept - using the assuan included accept code would be a bad
+	idea within Pth so we don't need a replacement function.
+
+2002-05-22  Werner Koch  <wk at gnupg.org>
+
+	* assuan-socket-server.c (assuan_init_connected_socket_server): New.
+	(accept_connection): Factored most code out to..
+	(accept_connection_bottom): .. new function.
+
 2002-04-04  Werner Koch  <wk at gnupg.org>
 
 	* assuan-buffer.c (my_log_prefix): New.  Use it for all i/o debug
diff --git a/src/assuan-buffer.c b/src/assuan-buffer.c
index 29f9479..da6b201 100644
--- a/src/assuan-buffer.c
+++ b/src/assuan-buffer.c
@@ -25,9 +25,6 @@
 #include <errno.h>
 #include <unistd.h>
 #include <assert.h>
-#ifdef USE_GNU_PTH
-# include <pth.h>
-#endif
 #include "assuan-defs.h"
 
 #ifdef HAVE_JNLIB_LOGGING
@@ -51,11 +48,9 @@ writen ( int fd, const char *buffer, size_t length )
 {
   while (length)
     {
-#ifdef USE_GNU_PTH
-      int nwritten = pth_write (fd, buffer, length);
-#else
-      int nwritten = write (fd, buffer, length);
-#endif
+      int nwritten = _assuan_write_wrapper?
+        _assuan_write_wrapper (fd, buffer, length):
+        write (fd, buffer, length);
       
       if (nwritten < 0)
         {
@@ -80,11 +75,10 @@ readline (int fd, char *buf, size_t buflen, int *r_nread, int *eof)
   *r_nread = 0;
   while (nleft > 0)
     {
-#ifdef USE_GNU_PTH
-      int n = pth_read (fd, buf, nleft);
-#else
-      int n = read (fd, buf, nleft);
-#endif
+      int n = _assuan_read_wrapper?
+        _assuan_read_wrapper (fd, buf, nleft):
+        read (fd, buf, nleft);
+
       if (n < 0)
         {
           if (errno == EINTR)
diff --git a/src/assuan-defs.h b/src/assuan-defs.h
index 6c502bf..5185ad1 100644
--- a/src/assuan-defs.h
+++ b/src/assuan-defs.h
@@ -76,6 +76,7 @@ struct assuan_context_s {
   pid_t pid;	  /* In pipe mode, the pid of the child server process.  
                      In socket mode, the pid of the server */
   int listen_fd;  /* The fd we are listening on (used by socket servers) */
+  int connected_fd; /* helper */
 
   pid_t client_pid; /* for a socket server the PID of the client or -1
                        if not available */
@@ -101,6 +102,7 @@ struct assuan_context_s {
 };
 
 
+
 /*-- assuan-pipe-server.c --*/
 int _assuan_new_context (ASSUAN_CONTEXT *r_ctx);
 void _assuan_release_context (ASSUAN_CONTEXT ctx);
@@ -119,6 +121,9 @@ AssuanError _assuan_read_from_server (ASSUAN_CONTEXT ctx, int *okay, int *off);
 
 
 /*-- assuan-util.c --*/
+extern ssize_t (*_assuan_read_wrapper)(int,void*,size_t);
+extern ssize_t (*_assuan_write_wrapper)(int,const void*,size_t);
+
 void *_assuan_malloc (size_t n);
 void *_assuan_calloc (size_t n, size_t m);
 void *_assuan_realloc (void *p, size_t n);
diff --git a/src/assuan-pipe-server.c b/src/assuan-pipe-server.c
index 5c5d124..07373e1 100644
--- a/src/assuan-pipe-server.c
+++ b/src/assuan-pipe-server.c
@@ -46,7 +46,7 @@ finish_connection (ASSUAN_CONTEXT ctx)
 
 
 /* Create a new context.  Note that the handlers are set up for a pipe
-   server/client - this wau we don't need extra dummy functions */
+   server/client - this way we don't need extra dummy functions */
 int
 _assuan_new_context (ASSUAN_CONTEXT *r_ctx)
 {
diff --git a/src/assuan-socket-server.c b/src/assuan-socket-server.c
index 39dd84a..e4a11b0 100644
--- a/src/assuan-socket-server.c
+++ b/src/assuan-socket-server.c
@@ -25,31 +25,15 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <unistd.h>
-#ifdef USE_GNU_PTH
-# include <pth.h>
-#endif
 
 #include "assuan-defs.h"
 
 static int
-accept_connection (ASSUAN_CONTEXT ctx)
+accept_connection_bottom (ASSUAN_CONTEXT ctx)
 {
-  int fd;
-  struct sockaddr_un clnt_addr;
-  size_t len = sizeof clnt_addr;
+  int fd = ctx->connected_fd;
 
   ctx->client_pid = (pid_t)-1;
-#ifdef USE_GNU_PTH
-  fd = pth_accept (ctx->listen_fd, (struct sockaddr*)&clnt_addr, &len );
-#else
-  fd = accept (ctx->listen_fd, (struct sockaddr*)&clnt_addr, &len );
-#endif
-  if (fd == -1)
-    {
-      ctx->os_errno = errno;
-      return ASSUAN_Accept_Failed;
-    }
-
 #ifdef HAVE_SO_PEERCRED
   {
     struct ucred cr; 
@@ -75,6 +59,26 @@ accept_connection (ASSUAN_CONTEXT ctx)
   return 0;
 }
 
+
+static int
+accept_connection (ASSUAN_CONTEXT ctx)
+{
+  int fd;
+  struct sockaddr_un clnt_addr;
+  size_t len = sizeof clnt_addr;
+
+  ctx->client_pid = (pid_t)-1;
+  fd = accept (ctx->listen_fd, (struct sockaddr*)&clnt_addr, &len );
+  if (fd == -1)
+    {
+      ctx->os_errno = errno;
+      return ASSUAN_Accept_Failed;
+    }
+
+  ctx->connected_fd = fd;
+  return accept_connection_bottom (ctx);
+}
+
 static int
 finish_connection (ASSUAN_CONTEXT ctx)
 {
@@ -116,6 +120,7 @@ assuan_init_socket_server (ASSUAN_CONTEXT *r_ctx, int listen_fd)
   ctx->outbound.fd = -1;
 
   ctx->listen_fd = listen_fd;
+  ctx->connected_fd = -1;
   ctx->deinit_handler = deinit_socket_server;
   ctx->accept_handler = accept_connection;
   ctx->finish_handler = finish_connection;
@@ -128,12 +133,37 @@ assuan_init_socket_server (ASSUAN_CONTEXT *r_ctx, int listen_fd)
   return rc;
 }
 
+/* Initialize a server using the already accepted socket FD. */
+int
+assuan_init_connected_socket_server (ASSUAN_CONTEXT *r_ctx, int fd)
+{
+  ASSUAN_CONTEXT ctx;
+  int rc;
 
+  *r_ctx = NULL;
+  ctx = xtrycalloc (1, sizeof *ctx);
+  if (!ctx)
+    return ASSUAN_Out_Of_Core;
+  ctx->is_server = 1;
+  ctx->pipe_mode = 1; /* we wan't a second accept to indicate EOF */
+  ctx->input_fd = -1;
+  ctx->output_fd = -1;
 
+  ctx->inbound.fd = -1;
+  ctx->outbound.fd = -1;
 
+  ctx->listen_fd = -1;
+  ctx->connected_fd = fd;
+  ctx->deinit_handler = deinit_socket_server;
+  ctx->accept_handler = accept_connection_bottom;
+  ctx->finish_handler = finish_connection;
 
-
-
-
+  rc = _assuan_register_std_commands (ctx);
+  if (rc)
+    xfree (ctx);
+  else
+    *r_ctx = ctx;
+  return rc;
+}
 
 
diff --git a/src/assuan-util.c b/src/assuan-util.c
index 4153ef8..15b9fe5 100644
--- a/src/assuan-util.c
+++ b/src/assuan-util.c
@@ -29,6 +29,9 @@
 #include "../jnlib/logging.h"
 #endif
 
+ssize_t (*_assuan_read_wrapper)(int,void*,size_t) = NULL;
+ssize_t (*_assuan_write_wrapper)(int,const void*,size_t) = NULL;
+
 
 static void *(*alloc_func)(size_t n) = malloc;
 static void *(*realloc_func)(void *p, size_t n) = realloc;
@@ -74,6 +77,17 @@ _assuan_free (void *p)
     free_func (p);
 }
 
+/* For use with Pth it is required to have special read and write
+   functions.  We can't assume an ELF based system so we have to
+   explicitly set them if we are going to use Pth. */
+void
+assuan_set_io_func (ssize_t (*r)(int,void*,size_t),
+                    ssize_t (*w)(int,const void*,size_t))
+{
+  _assuan_read_wrapper = r;
+  _assuan_write_wrapper = w;
+}
+
 
 

 /* Store the error in the context so that the error sending function
diff --git a/src/assuan.h b/src/assuan.h
index a934001..dae8ed7 100644
--- a/src/assuan.h
+++ b/src/assuan.h
@@ -23,6 +23,7 @@
 
 #include <stdio.h>
 #include <sys/types.h>
+#include <unistd.h> /* for ssize_t */
 
 #ifdef __cplusplus
 extern "C" { 
@@ -170,6 +171,7 @@ void assuan_deinit_server (ASSUAN_CONTEXT ctx);
 
 /*-- assuan-socket-server.c --*/
 int assuan_init_socket_server (ASSUAN_CONTEXT *r_ctx, int listen_fd);
+int assuan_init_connected_socket_server (ASSUAN_CONTEXT *r_ctx, int fd);
 
 
 /*-- assuan-pipe-connect.c --*/
@@ -212,6 +214,8 @@ AssuanError assuan_send_data (ASSUAN_CONTEXT ctx,
 void assuan_set_malloc_hooks ( void *(*new_alloc_func)(size_t n),
                                void *(*new_realloc_func)(void *p, size_t n),
                                void (*new_free_func)(void*) );
+void assuan_set_io_func (ssize_t (*r)(int,void*,size_t),
+                         ssize_t (*w)(int,const void*,size_t));
 void assuan_set_log_stream (ASSUAN_CONTEXT ctx, FILE *fp);
 int assuan_set_error (ASSUAN_CONTEXT ctx, int err, const char *text);
 void assuan_set_pointer (ASSUAN_CONTEXT ctx, void *pointer);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-gnupg/libassuan.git



More information about the Pkg-gnupg-commit mailing list