summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNIIBE Yutaka <gniibe@fsij.org>2022-10-28 12:19:39 +0900
committerNIIBE Yutaka <gniibe@fsij.org>2022-10-28 12:19:39 +0900
commitf85726db85685a732143fb705db6b9c423aeccb5 (patch)
tree5ee81456fa6bf736d2fe3520a8323db1e319a744
parent905369a8dfeeea30989e526f99346771c827070c (diff)
downloadlibassuan-f85726db85685a732143fb705db6b9c423aeccb5.tar.gz
fdpassing using pipe works on Windows.
./fdpassing.exe --with-exec --debug --verbose Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
-rw-r--r--src/assuan-pipe-connect.c30
-rw-r--r--src/assuan-pipe-server.c4
-rw-r--r--src/system-w32.c16
-rw-r--r--tests/fdpassing.c20
4 files changed, 46 insertions, 24 deletions
diff --git a/src/assuan-pipe-connect.c b/src/assuan-pipe-connect.c
index a2223cd..fc56334 100644
--- a/src/assuan-pipe-connect.c
+++ b/src/assuan-pipe-connect.c
@@ -104,7 +104,31 @@ initial_handshake (assuan_context_t ctx)
if (err)
TRACE1 (ctx, ASSUAN_LOG_SYSIO, "initial_handshake", ctx,
"can't connect server: %s", gpg_strerror (err));
- else if (response != ASSUAN_RESPONSE_OK)
+ else if (response == ASSUAN_RESPONSE_OK)
+ {
+#ifdef HAVE_W32_SYSTEM
+ const char *line = ctx->inbound.line + off;
+ int pid = ASSUAN_INVALID_PID;
+
+ /* Parse the message: OK ..., process %i */
+ line = strchr (line, ',');
+ if (line)
+ {
+ line = strchr (line + 1, ' ');
+ if (line)
+ {
+ line = strchr (line + 1, ' ');
+ if (line)
+ pid = atoi (line + 1);
+ }
+ }
+ if (pid != ASSUAN_INVALID_PID)
+ ctx->pid = pid;
+#else
+ ;
+#endif
+ }
+ else
{
TRACE1 (ctx, ASSUAN_LOG_SYSIO, "initial_handshake", ctx,
"can't connect server: `%s'", ctx->inbound.line);
@@ -209,7 +233,11 @@ pipe_connect (assuan_context_t ctx,
ctx->engine.release = _assuan_client_release;
ctx->engine.readfnc = _assuan_simple_read;
ctx->engine.writefnc = _assuan_simple_write;
+#ifdef HAVE_W32_SYSTEM
+ ctx->engine.sendfd = w32_fdpass_send;
+#else
ctx->engine.sendfd = NULL;
+#endif
ctx->engine.receivefd = NULL;
ctx->finish_handler = _assuan_client_finish;
ctx->max_accepts = 1;
diff --git a/src/assuan-pipe-server.c b/src/assuan-pipe-server.c
index 03b754c..7977b83 100644
--- a/src/assuan-pipe-server.c
+++ b/src/assuan-pipe-server.c
@@ -118,7 +118,11 @@ assuan_init_pipe_server (assuan_context_t ctx, assuan_fd_t filedes[2])
ctx->engine.readfnc = _assuan_simple_read;
ctx->engine.writefnc = _assuan_simple_write;
ctx->engine.sendfd = NULL;
+#ifdef HAVE_W32_SYSTEM
+ ctx->engine.receivefd = w32_fdpass_recv;
+#else
ctx->engine.receivefd = NULL;
+#endif
ctx->max_accepts = 1;
s = getenv ("_assuan_pipe_connect_pid");
diff --git a/src/system-w32.c b/src/system-w32.c
index 037a924..dfd2ba2 100644
--- a/src/system-w32.c
+++ b/src/system-w32.c
@@ -235,7 +235,6 @@ gpg_error_t
w32_fdpass_send (assuan_context_t ctx, assuan_fd_t fd)
{
char fdpass_msg[256];
- size_t msglen;
int res;
int fd0; /* POSIX fd */
intptr_t fd_converted_to_integer;
@@ -249,20 +248,6 @@ w32_fdpass_send (assuan_context_t ctx, assuan_fd_t fd)
if (err)
return err;
-#if 0
- res = snprintf (fdpass_msg, sizeof (fdpass_msg), FDPASS_FORMAT, file_handle);
- if (res < 0)
- {
- CloseHandle (file_handle);
- return gpg_error (GPG_ERR_ASS_PARAMETER);/*FIXME: error*/
- }
-
- msglen = (size_t)res + 1; /* Including NUL. */
-
- res = send (HANDLE2SOCKET (ctx->outbound.fd), "!", 1, MSG_OOB);
- res = send (HANDLE2SOCKET (ctx->outbound.fd), fdpass_msg, msglen, 0);
- return 0;
-#else
res = snprintf (fdpass_msg, sizeof (fdpass_msg), "SENDFD %p", file_handle);
if (res < 0)
{
@@ -272,7 +257,6 @@ w32_fdpass_send (assuan_context_t ctx, assuan_fd_t fd)
err = assuan_transact (ctx, fdpass_msg, NULL, NULL, NULL, NULL, NULL, NULL);
return err;
-#endif
}
static int
diff --git a/tests/fdpassing.c b/tests/fdpassing.c
index 83ae62d..6323a41 100644
--- a/tests/fdpassing.c
+++ b/tests/fdpassing.c
@@ -60,11 +60,11 @@ cmd_echo (assuan_context_t ctx, char *line)
nbytes = 0;
while ( (c=getc (fp)) != -1)
{
- putc (c, stdout);
+ putc (c, stderr);
nbytes++;
}
- fflush (stdout);
- log_info ("done printing %d bytes to stdout\n", nbytes);
+ fflush (stderr);
+ log_info ("done printing %d bytes to stderr\n", nbytes);
fclose (fp);
return 0;
@@ -102,14 +102,18 @@ server (void)
{
int rc;
assuan_context_t ctx;
+ assuan_fd_t filedes[2];
log_info ("server started\n");
+ filedes[0] = _get_osfhandle (0);
+ filedes[1] = _get_osfhandle (1);
+
rc = assuan_new (&ctx);
if (rc)
log_fatal ("assuan_new failed: %s\n", gpg_strerror (rc));
- rc = assuan_init_pipe_server (ctx, NULL);
+ rc = assuan_init_pipe_server (ctx, filedes);
if (rc)
log_fatal ("assuan_init_pipe_server failed: %s\n", gpg_strerror (rc));
@@ -117,7 +121,7 @@ server (void)
if (rc)
log_fatal ("register_commands failed: %s\n", gpg_strerror(rc));
- assuan_set_log_stream (ctx, stderr);
+ // assuan_set_log_stream (ctx, stderr);
for (;;)
{
@@ -276,11 +280,11 @@ main (int argc, char **argv)
{
const char *loc;
- no_close_fds[0] = 2;
+ no_close_fds[0] = _get_osfhandle (fileno (stderr));
no_close_fds[1] = -1;
if (with_exec)
{
- arglist[0] = "fdpassing";
+ arglist[0] = "fdpassing.exe";
arglist[1] = "--server";
arglist[2] = verbose? "--verbose":NULL;
arglist[3] = NULL;
@@ -290,6 +294,8 @@ main (int argc, char **argv)
if (err)
log_fatal ("assuan_new failed: %s\n", gpg_strerror (err));
+ assuan_set_log_stream (ctx, stderr);
+
err = assuan_pipe_connect (ctx, with_exec? "./fdpassing.exe":NULL,
with_exec ? arglist : &loc,
no_close_fds, NULL, NULL, 1);