1
/* assuan-pipe-connect.c - Establish a pipe connection (client)
2
* Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4
* This file is part of Assuan.
6
* Assuan is free software; you can redistribute it and/or modify it
7
* under the terms of the GNU Lesser General Public License as
8
* published by the Free Software Foundation; either version 2.1 of
9
* the License, or (at your option) any later version.
11
* Assuan is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
32
#include <sys/types.h>
35
#include "assuan-defs.h"
37
#ifdef _POSIX_OPEN_MAX
38
#define MAX_OPEN_FDS _POSIX_OPEN_MAX
40
#define MAX_OPEN_FDS 20
43
#ifdef HAVE_JNLIB_LOGGING
44
#include "../jnlib/logging.h"
45
#define LOGERROR1(a,b) log_error ((a), (b))
47
#define LOGERROR1(a,b) fprintf (stderr, (a), (b))
53
writen ( int fd, const char *buffer, size_t length )
57
int nwritten = write (fd, buffer, length);
63
return -1; /* write error */
73
do_finish (ASSUAN_CONTEXT ctx)
75
if (ctx->inbound.fd != -1)
77
close (ctx->inbound.fd);
80
if (ctx->outbound.fd != -1)
82
close (ctx->outbound.fd);
83
ctx->outbound.fd = -1;
87
waitpid (ctx->pid, NULL, 0); /* FIXME Check return value. */
94
do_deinit (ASSUAN_CONTEXT ctx)
101
/* Connect to a server over a pipe, creating the assuan context and
102
returning it in CTX. The server filename is NAME, the argument
103
vector in ARGV. FD_CHILD_LIST is a -1 terminated list of file
104
descriptors not to close in the child. */
106
assuan_pipe_connect (ASSUAN_CONTEXT *ctx, const char *name, char *const argv[],
109
static int fixed_signals = 0;
114
if (!ctx || !name || !argv || !argv[0])
115
return ASSUAN_Invalid_Value;
119
struct sigaction act;
121
sigaction (SIGPIPE, NULL, &act);
122
if (act.sa_handler == SIG_DFL)
124
act.sa_handler = SIG_IGN;
125
sigemptyset (&act.sa_mask);
127
sigaction (SIGPIPE, &act, NULL);
130
/* FIXME: This is not MT safe */
134
return ASSUAN_General_Error;
140
return ASSUAN_General_Error;
143
err = _assuan_new_context (ctx);
152
(*ctx)->pipe_mode = 1;
153
(*ctx)->inbound.fd = rp[0]; /* Our inbound is read end of read pipe. */
154
(*ctx)->outbound.fd = wp[1]; /* Our outbound is write end of write pipe. */
155
(*ctx)->deinit_handler = do_deinit;
156
(*ctx)->finish_handler = do_finish;
158
(*ctx)->pid = fork ();
165
_assuan_release_context (*ctx);
166
return ASSUAN_General_Error;
169
if ((*ctx)->pid == 0)
175
/* Close all files which will not be duped and are not in the
177
n = sysconf (_SC_OPEN_MAX);
180
for (i=0; i < n; i++)
185
while (*fdp != -1 && *fdp != i)
189
if (!(fdp && *fdp != -1)
190
&& i != rp[1] && i != wp[0])
195
/* Dup stderr to /dev/null unless it is in the list of FDs to be
196
passed to the child. */
200
for (; *fdp != -1 && *fdp != STDERR_FILENO; fdp++)
203
if (!fdp || *fdp == -1)
205
int fd = open ("/dev/null", O_WRONLY);
208
LOGERROR1 ("can't open `/dev/null': %s\n", strerror (errno));
211
if (dup2 (fd, STDERR_FILENO) == -1)
213
LOGERROR1 ("dup2(dev/null, 2) failed: %s\n", strerror (errno));
219
/* Dup handles and to stdin/stdout and exec. */
220
if (rp[1] != STDOUT_FILENO)
222
if (dup2 (rp[1], STDOUT_FILENO) == -1)
224
LOGERROR1 ("dup2 failed in child: %s\n", strerror (errno));
229
if (wp[0] != STDIN_FILENO)
231
if (dup2 (wp[0], STDIN_FILENO) == -1)
233
LOGERROR1 ("dup2 failed in child: %s\n", strerror (errno));
240
/* oops - use the pipe to tell the parent about it */
241
snprintf (errbuf, sizeof(errbuf)-1, "ERR %d can't exec `%s': %.50s\n",
242
ASSUAN_Problem_Starting_Server, name, strerror (errno));
243
errbuf[sizeof(errbuf)-1] = 0;
244
writen (1, errbuf, strlen (errbuf));
251
/* initial handshake */
255
err = _assuan_read_from_server (*ctx, &okay, &off);
258
LOGERROR1 ("can't connect server: %s\n", assuan_strerror (err));
262
LOGERROR1 ("can't connect server: `%s'\n", (*ctx)->inbound.line);
263
err = ASSUAN_Connect_Failed;
269
assuan_disconnect (*ctx);