From 17055e1c995367e60f45ee73fa1a39e7a09efd59 Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Wed, 10 May 2023 13:24:43 +0900 Subject: w32: Fix the semantics of sending FD, it's Windows HANDLE. * src/assuan-handler.c (w32_handler_sendfd): It's Windows HANDLE. * src/system-w32.c (get_file_handle, w32_fdpass_send): Likewise. * tests/fdpassing.c (cmd_echo): Received FD is Windows HANDLE. (client): Use Windows HANDLE for assuan_sendfd. -- GnuPG-bug-id: 6236 Signed-off-by: NIIBE Yutaka --- src/assuan-handler.c | 20 +++++--------------- src/system-w32.c | 21 +++++++-------------- tests/fdpassing.c | 21 +++++++++++++++++++++ 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/assuan-handler.c b/src/assuan-handler.c index 37f84b6..a04ab4e 100644 --- a/src/assuan-handler.c +++ b/src/assuan-handler.c @@ -27,9 +27,6 @@ #include #include #include -#if HAVE_W32_SYSTEM || HAVE_W64_SYSTEM -#include -#endif #include "assuan-defs.h" #include "debug.h" @@ -359,10 +356,11 @@ std_handler_output (assuan_context_t ctx, char *line) } -#if HAVE_W32_SYSTEM || HAVE_W64_SYSTEM +#ifdef HAVE_W32_SYSTEM /* - * The command used by a client to send FD. That is, from the viewpoint - * of handling this command, it is to _receive_ a file handle. + * The command used by a client to send an FD. That is, from the + * viewpoint of handling this command, it is to _receive_ a file + * handle. */ static const char w32_help_sendfd[] = "SENDFD \n" @@ -375,7 +373,6 @@ w32_handler_sendfd (assuan_context_t ctx, char *line) gpg_error_t err = 0; char *endp; intptr_t file_handle; - int fd; #if HAVE_W64_SYSTEM file_handle = strtoull (line, &endp, 16); @@ -389,14 +386,7 @@ w32_handler_sendfd (assuan_context_t ctx, char *line) return PROCESS_DONE (ctx, err); } - fd = _open_osfhandle ((intptr_t)file_handle, _O_RDWR); - if (fd < 0) - { - CloseHandle ((HANDLE)file_handle); - err = GPG_ERR_ASSUAN; - } - - ctx->uds.pendingfds[ctx->uds.pendingfdscount++] = (assuan_fd_t)fd; + ctx->uds.pendingfds[ctx->uds.pendingfdscount++] = (assuan_fd_t)file_handle; return PROCESS_DONE (ctx, err); } #endif diff --git a/src/system-w32.c b/src/system-w32.c index b7a14c8..28d3564 100644 --- a/src/system-w32.c +++ b/src/system-w32.c @@ -167,19 +167,17 @@ __assuan_close (assuan_context_t ctx, assuan_fd_t fd) -/* Get a file HANDLE to send, from POSIX fd. */ +/* Get a file HANDLE for other end to send, from MY_HANDLE. */ static gpg_error_t -get_file_handle (int fd, int process_id, HANDLE *r_handle) +get_file_handle (assuan_fd_t my_handle, int process_id, HANDLE *r_handle) { - HANDLE prochandle, handle, newhandle; - - handle = (void *)_get_osfhandle (fd); + HANDLE prochandle, newhandle; prochandle = OpenProcess (PROCESS_DUP_HANDLE, FALSE, process_id); if (!prochandle) return gpg_error (GPG_ERR_ASS_PARAMETER);/*FIXME: error*/ - if (!DuplicateHandle (GetCurrentProcess (), handle, prochandle, &newhandle, + if (!DuplicateHandle (GetCurrentProcess (), my_handle, prochandle, &newhandle, 0, TRUE, DUPLICATE_SAME_ACCESS)) { CloseHandle (prochandle); @@ -191,21 +189,16 @@ get_file_handle (int fd, int process_id, HANDLE *r_handle) } -/* Send a FD (which means POSIX fd) to the peer. */ +/* Send an FD (which means Windows HANDLE) to the peer. */ gpg_error_t w32_fdpass_send (assuan_context_t ctx, assuan_fd_t fd) { char fdpass_msg[256]; int res; - int fd0; /* POSIX fd */ - intptr_t fd_converted_to_integer; HANDLE file_handle; gpg_error_t err; - fd_converted_to_integer = (intptr_t)fd; - fd0 = (int)fd_converted_to_integer; /* Bit pattern is possibly truncated. */ - - err = get_file_handle (fd0, ctx->process_id, &file_handle); + err = get_file_handle (fd, ctx->process_id, &file_handle); if (err) return err; @@ -221,7 +214,7 @@ w32_fdpass_send (assuan_context_t ctx, assuan_fd_t fd) } -/* Receive a HANDLE from the peer and turn it into a FD (POSIX fd). */ +/* Receive a HANDLE from the peer and turn it into an FD. */ gpg_error_t w32_fdpass_recv (assuan_context_t ctx, assuan_fd_t *fd) { diff --git a/tests/fdpassing.c b/tests/fdpassing.c index 3caa4b2..438c8c5 100644 --- a/tests/fdpassing.c +++ b/tests/fdpassing.c @@ -30,6 +30,7 @@ # include # include # include +#include #else # include # include @@ -54,12 +55,24 @@ cmd_echo (assuan_context_t ctx, char *line) int c; FILE *fp; int nbytes; +#ifdef HAVE_W32_SYSTEM + HANDLE file_handle; +#endif log_info ("got ECHO command (%s)\n", line); +#if HAVE_W32_SYSTEM + file_handle = assuan_get_input_fd (ctx); + if (file_handle == ASSUAN_INVALID_FD) + return gpg_error (GPG_ERR_ASS_NO_INPUT); + fd = _open_osfhandle ((intptr_t)file_handle, _O_RDONLY); + if (fd < 0) + return gpg_error (GPG_ERR_ASS_NO_INPUT); +#else fd = (int)assuan_get_input_fd (ctx); if (fd == -1) return gpg_error (GPG_ERR_ASS_NO_INPUT); +#endif fp = fdopen (fd, "r"); if (!fp) { @@ -234,6 +247,9 @@ client (assuan_context_t ctx, const char *fname) int rc; FILE *fp; int i; +#if HAVE_W32_SYSTEM + HANDLE file_handle; +#endif log_info ("client started. Servers's pid is %ld\n", (long)assuan_get_pid (ctx)); @@ -248,7 +264,12 @@ client (assuan_context_t ctx, const char *fname) return -1; } +#ifdef HAVE_W32_SYSTEM + file_handle = (HANDLE)_get_osfhandle (fileno (fp)); + rc = assuan_sendfd (ctx, file_handle); +#else rc = assuan_sendfd (ctx, (assuan_fd_t)fileno (fp)); +#endif if (rc) { fclose (fp); -- cgit v1.2.1