diff options
author | Marcus Brinkmann <mb@g10code.com> | 2001-11-19 13:09:50 +0100 |
---|---|---|
committer | Marcus Brinkmann <mb@g10code.com> | 2001-11-19 13:09:50 +0100 |
commit | 2b2a800a77596a7108cbbe71d7ac5c1f2728cafb (patch) | |
tree | 0d053a44f7ecca95ab05092ce8f07dd9a4fa46cb /assuan | |
parent | gpgsm --verify does now work like gpg including the (diff) | |
download | gnupg2-2b2a800a77596a7108cbbe71d7ac5c1f2728cafb.tar.xz gnupg2-2b2a800a77596a7108cbbe71d7ac5c1f2728cafb.zip |
* assuan-connect.c (assuan_pipe_connect): New function.
(assuan_pipe_disconnect): Likewise.
* assuan-defs.h (struct assuan_context_s): New member PID.
Diffstat (limited to 'assuan')
-rw-r--r-- | assuan/assuan-connect.c | 106 | ||||
-rw-r--r-- | assuan/assuan-defs.h | 2 |
2 files changed, 108 insertions, 0 deletions
diff --git a/assuan/assuan-connect.c b/assuan/assuan-connect.c index d1c87fc2b..294a26cef 100644 --- a/assuan/assuan-connect.c +++ b/assuan/assuan-connect.c @@ -22,6 +22,112 @@ #include <stdlib.h> #include <stdio.h> #include <string.h> +#include <signal.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/wait.h> #include "assuan-defs.h" +/* Connect to a server over a pipe, creating the assuan context and + returning it in CTX. The server filename is NAME, the argument + vector in ARGV. If NAME is NULL, the first element in ARGV is + used. */ +AssuanError +assuan_pipe_connect (ASSUAN_CONTEXT *ctx, const char *name, char *const argv[]) +{ + static int fixed_signals = 0; + AssuanError err; + int rp[2]; + int wp[2]; + int fd[2]; + + if (!argv || !argv[0]) + return ASSUAN_General_Error; + + if (!name) + name = argv[0]; + + if (!fixed_signals) + { + struct sigaction act; + + sigaction (SIGPIPE, NULL, &act); + if (act.sa_handler == SIG_DFL) + { + act.sa_handler = SIG_IGN; + sigemptyset (&act.sa_mask); + act.sa_flags = 0; + sigaction (SIGPIPE, &act, NULL); + } + fixed_signals = 1; + /* FIXME: This is not MT safe */ + } + + if (pipe (rp) < 0) + return ASSUAN_General_Error; + + if (pipe (wp) < 0) + { + close (rp[0]); + close (rp[1]); + return ASSUAN_General_Error; + } + + fd[0] = rp[0]; /* Our inbound is read end of read pipe. */ + fd[1] = wp[1]; /* Our outbound is write end of write pipe. */ + + err = assuan_init_pipe_server (ctx, fd); /* FIXME: Common code should be factored out. */ + if (err) + { + close (rp[0]); + close (rp[1]); + close (wp[0]); + close (wp[1]); + return err; + } + + (*ctx)->pid = fork (); + if ((*ctx)->pid < 0) + { + close (rp[0]); + close (rp[1]); + close (wp[0]); + close (wp[1]); + assuan_deinit_pipe_server (*ctx); /* FIXME: Common code should be factored out. */ + return ASSUAN_General_Error; + } + + if ((*ctx)->pid == 0) + { + close (rp[0]); + close (wp[1]); + if (rp[1] != STDOUT_FILENO) + { + dup2 (rp[1], STDOUT_FILENO); /* Child's outbound is write end of read pipe. */ + close (rp[1]); + } + if (wp[0] != STDIN_FILENO) + { + dup2 (wp[0], STDIN_FILENO); /* Child's inbound is read end of write pipe. */ + close (wp[0]); + } + execv (name, argv); + _exit (1); + } + + close (rp[1]); + close (wp[0]); + _assuan_read_line (*ctx); /* FIXME: Handshake. */ + return 0; +} + +void +assuan_pipe_disconnect (ASSUAN_CONTEXT ctx) +{ + _assuan_write_line (ctx, "BYE"); + close (ctx->inbound.fd); + close (ctx->outbound.fd); + waitpid (ctx->pid, NULL, 0); /* FIXME Check return value. */ + assuan_deinit_pipe_server (ctx); +} diff --git a/assuan/assuan-defs.h b/assuan/assuan-defs.h index d3e0439dc..72f6eb422 100644 --- a/assuan/assuan-defs.h +++ b/assuan/assuan-defs.h @@ -21,6 +21,7 @@ #ifndef ASSUAN_DEFS_H #define ASSUAN_DEFS_H +#include <sys/types.h> #include "assuan.h" #define LINELENGTH 1002 /* 1000 + [CR,]LF */ @@ -50,6 +51,7 @@ 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. */ struct cmdtbl_s *cmdtbl; size_t cmdtbl_used; /* used entries */ |