[Pkg-gnupg-commit] [libassuan] 108/437: * assuan-socket-server.c (accept_connection_bottom): Save the pid of the peer if it is available. * assuan-socket-connect.c (assuan_socket_connect): Do not save the dummy SERVED_PID arg. * assuan-pipe-connect.c (do_finish): Don't wait if the pid is 0. (assuan_pipe_connect2): Store the parents pid in the environment of the child. * assuan-pipe-server.c (assuan_init_pipe_server): Initialize the peer's pid from the environment. * assuan-connect.c (assuan_get_pid): Do not return 0 as a PID.

Eric Dorland eric at moszumanska.debian.org
Fri May 22 05:33:30 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 93595de1ede96dedfcaa93c7442536a2cde0b6ef
Author: Werner Koch <wk at gnupg.org>
Date:   Wed Apr 21 14:42:17 2004 +0000

    * assuan-socket-server.c (accept_connection_bottom): Save the pid
    of the peer if it is available.
    * assuan-socket-connect.c (assuan_socket_connect): Do not save the
    dummy SERVED_PID arg.
    * assuan-pipe-connect.c (do_finish): Don't wait if the pid is 0.
    (assuan_pipe_connect2): Store the parents pid in the environment
    of the child.
    * assuan-pipe-server.c (assuan_init_pipe_server): Initialize the
    peer's pid from the environment.
    * assuan-connect.c (assuan_get_pid): Do not return 0 as a PID.
---
 NEWS                        |  2 ++
 THANKS                      |  4 ++++
 src/ChangeLog               | 13 +++++++++++++
 src/assuan-connect.c        |  3 ++-
 src/assuan-defs.h           |  6 +-----
 src/assuan-pipe-connect.c   | 12 +++++++++++-
 src/assuan-pipe-server.c    | 10 +++++++++-
 src/assuan-socket-connect.c |  1 -
 src/assuan-socket-server.c  |  9 +++++----
 9 files changed, 47 insertions(+), 13 deletions(-)

diff --git a/NEWS b/NEWS
index 9cb87b7..72987f7 100644
--- a/NEWS
+++ b/NEWS
@@ -1,6 +1,8 @@
 Noteworthy changes in version 0.6.5
 ------------------------------------------------
 
+ * Pass the clien's pid to a pipe server.
+
 
 Noteworthy changes in version 0.6.4 (2004-02-20)
 ------------------------------------------------
diff --git a/THANKS b/THANKS
index 8b13789..400d392 100644
--- a/THANKS
+++ b/THANKS
@@ -1 +1,5 @@
 
+
+Michael Nottebrock        michaelnottebrock at gmx.net
+
+
diff --git a/src/ChangeLog b/src/ChangeLog
index c84b161..4fc95a7 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,16 @@
+2004-04-21  Werner Koch  <wk at gnupg.org>
+
+	* assuan-socket-server.c (accept_connection_bottom): Save the pid
+	of the peer if it is available.
+	* assuan-socket-connect.c (assuan_socket_connect): Do not save the
+	dummy SERVED_PID arg.
+	* assuan-pipe-connect.c (do_finish): Don't wait if the pid is 0.
+	(assuan_pipe_connect2): Store the parents pid in the environment
+	of the child.
+	* assuan-pipe-server.c (assuan_init_pipe_server): Initialize the
+	peer's pid from the environment.
+	* assuan-connect.c (assuan_get_pid): Do not return 0 as a PID.
+
 2004-04-19  Werner Koch  <wk at gnupg.org>
 
 	* assuan-socket-server.c, assuan-socket-connect.c: Includes
diff --git a/src/assuan-connect.c b/src/assuan-connect.c
index 009aaab..cedaa66 100644
--- a/src/assuan-connect.c
+++ b/src/assuan-connect.c
@@ -47,8 +47,9 @@ assuan_disconnect (ASSUAN_CONTEXT ctx)
     }
 }
 
+/* Return the PID of the perr or -1 if not known. */
 pid_t
 assuan_get_pid (ASSUAN_CONTEXT ctx)
 {
-  return ctx ? ctx->pid : -1;
+  return (ctx && ctx->pid)? ctx->pid : -1;
 }
diff --git a/src/assuan-defs.h b/src/assuan-defs.h
index e2d9811..608c05a 100644
--- a/src/assuan-defs.h
+++ b/src/assuan-defs.h
@@ -90,14 +90,10 @@ struct assuan_context_s
 
   int pipe_mode;  /* We are in pipe mode, i.e. we can handle just one
                      connection and must terminate then */
-  pid_t pid;	  /* In pipe mode, the pid of the child server process.  
-                     In socket mode, the pid of the server */
+  pid_t pid;	  /* The the pid of the peer. */
   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 */
-
   /* Used for Unix domain sockets.  */
   struct sockaddr_un myaddr;
   struct sockaddr_un serveraddr;
diff --git a/src/assuan-pipe-connect.c b/src/assuan-pipe-connect.c
index 9c41aa9..ffb0202 100644
--- a/src/assuan-pipe-connect.c
+++ b/src/assuan-pipe-connect.c
@@ -78,7 +78,7 @@ do_finish (ASSUAN_CONTEXT ctx)
       close (ctx->outbound.fd);
       ctx->outbound.fd = -1;
     }
-  if (ctx->pid != -1)
+  if (ctx->pid != -1 && ctx->pid)
     {
       waitpid (ctx->pid, NULL, 0);  /* FIXME Check return value.  */
       ctx->pid = -1;
@@ -111,6 +111,7 @@ assuan_pipe_connect2 (ASSUAN_CONTEXT *ctx, const char *name, char *const argv[],
   AssuanError err;
   int rp[2];
   int wp[2];
+  char mypidstr[50];
 
   if (!ctx || !name || !argv || !argv[0])
     return ASSUAN_Invalid_Value;
@@ -131,6 +132,8 @@ assuan_pipe_connect2 (ASSUAN_CONTEXT *ctx, const char *name, char *const argv[],
       /* FIXME: This is not MT safe */
     }
 
+  sprintf (mypidstr, "%lu", (unsigned long)getpid ());
+
   if (pipe (rp) < 0)
     return ASSUAN_General_Error;
 
@@ -239,6 +242,13 @@ assuan_pipe_connect2 (ASSUAN_CONTEXT *ctx, const char *name, char *const argv[],
         }
       errno = 0;
 
+      /* We store our parents pid in the environment so that the
+         execed assuan server is able to read the actual pid of the
+         client.  The server can't use getppid becuase it might have
+         been double forked before the assuan server has been
+         initialized. */
+      setenv ("_assuan_pipe_connect_pid", mypidstr, 1);
+
       execv (name, argv); 
       /* oops - use the pipe to tell the parent about it */
       snprintf (errbuf, sizeof(errbuf)-1, "ERR %d can't exec `%s': %.50s\n",
diff --git a/src/assuan-pipe-server.c b/src/assuan-pipe-server.c
index ba269b0..7b2d643 100644
--- a/src/assuan-pipe-server.c
+++ b/src/assuan-pipe-server.c
@@ -69,7 +69,6 @@ _assuan_new_context (ASSUAN_CONTEXT *r_ctx)
   ctx->io = &io;
 
   ctx->listen_fd = -1;
-  ctx->client_pid = (pid_t)-1;
   /* Use the pipe server handler as a default.  */
   ctx->deinit_handler = deinit_pipe_server;
   ctx->accept_handler = accept_connection;
@@ -94,11 +93,20 @@ assuan_init_pipe_server (ASSUAN_CONTEXT *r_ctx, int filedes[2])
   if (!rc)
     {
       ASSUAN_CONTEXT ctx = *r_ctx;
+      const char *s;
+      unsigned long ul;
 
       ctx->is_server = 1;
       ctx->inbound.fd = filedes[0];
       ctx->outbound.fd = filedes[1];
       ctx->pipe_mode = 1;
+
+      s = getenv ("_assuan_pipe_connect_pid");
+      if (s && (ul=strtoul (s, NULL, 10)) && ul)
+        ctx->pid = (pid_t)ul;
+      else
+        ctx->pid = (pid_t)-1;
+
     }
   return rc;
 }
diff --git a/src/assuan-socket-connect.c b/src/assuan-socket-connect.c
index 6ce3f9c..cbe4df8 100644
--- a/src/assuan-socket-connect.c
+++ b/src/assuan-socket-connect.c
@@ -82,7 +82,6 @@ assuan_socket_connect (ASSUAN_CONTEXT *r_ctx,
   err = _assuan_new_context (&ctx); 
   if (err)
       return err;
-  ctx->pid = server_pid; /* save it in case we need it later */
   ctx->deinit_handler = do_deinit;
   ctx->finish_handler = do_finish;
 
diff --git a/src/assuan-socket-server.c b/src/assuan-socket-server.c
index d92e6e4..e81ee50 100644
--- a/src/assuan-socket-server.c
+++ b/src/assuan-socket-server.c
@@ -34,14 +34,16 @@ accept_connection_bottom (ASSUAN_CONTEXT ctx)
 {
   int fd = ctx->connected_fd;
 
-  ctx->client_pid = (pid_t)-1;
 #ifdef HAVE_SO_PEERCRED
   {
+    /* This overrides any already set PID if the function returns a
+       valid one. */
     struct ucred cr; 
     int cl = sizeof cr;
 
-    if ( !getsockopt (fd, SOL_SOCKET, SO_PEERCRED, &cr, &cl) ) 
-      ctx->client_pid = cr.pid;
+    if ( !getsockopt (fd, SOL_SOCKET, SO_PEERCRED, &cr, &cl)
+         && cr.pid != (pid_t)-1 && cr.pid ) 
+      ctx->pid = cr.pid;
   }
 #endif
 
@@ -68,7 +70,6 @@ accept_connection (ASSUAN_CONTEXT ctx)
   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)
     {

-- 
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