diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/assuan-listen.c | 2 | ||||
-rw-r--r-- | src/assuan-pipe-connect.c | 26 | ||||
-rw-r--r-- | src/assuan-pipe-server.c | 7 | ||||
-rw-r--r-- | src/assuan-socket-connect.c | 26 | ||||
-rw-r--r-- | src/assuan-socket-server.c | 4 | ||||
-rw-r--r-- | src/client.c | 2 | ||||
-rw-r--r-- | src/system-w32.c | 76 |
7 files changed, 135 insertions, 8 deletions
diff --git a/src/assuan-listen.c b/src/assuan-listen.c index 6755d59..93560ff 100644 --- a/src/assuan-listen.c +++ b/src/assuan-listen.c @@ -117,7 +117,7 @@ assuan_accept (assuan_context_t ctx) else { static char const okstr[] = "OK Pleased to meet you"; - pid_t apid = assuan_get_pid (ctx); + pid_t apid = getpid (); if (apid != ASSUAN_INVALID_PID) { char tmpbuf[50]; diff --git a/src/assuan-pipe-connect.c b/src/assuan-pipe-connect.c index 13ea3de..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); diff --git a/src/assuan-pipe-server.c b/src/assuan-pipe-server.c index 8da54ad..7977b83 100644 --- a/src/assuan-pipe-server.c +++ b/src/assuan-pipe-server.c @@ -82,8 +82,11 @@ assuan_init_pipe_server (assuan_context_t ctx, assuan_fd_t filedes[2]) return TRACE_ERR (rc); #ifdef HAVE_W32_SYSTEM - infd = filedes[0]; - outfd = filedes[1]; + if (filedes) + { + infd = filedes[0]; + outfd = filedes[1]; + } #else s = getenv ("_assuan_connection_fd"); if (s && *s && is_valid_socket (s)) diff --git a/src/assuan-socket-connect.c b/src/assuan-socket-connect.c index 019a41c..457edfe 100644 --- a/src/assuan-socket-connect.c +++ b/src/assuan-socket-connect.c @@ -115,8 +115,12 @@ _assuan_connect_finalize (assuan_context_t ctx, assuan_fd_t fd, ctx->max_accepts = -1; ctx->flags.is_socket = 1; +#ifdef HAVE_W32_SYSTEM + ctx->engine.sendfd = w32_fdpass_send; +#else if (flags & ASSUAN_SOCKET_CONNECT_FDPASSING) _assuan_init_uds_io (ctx); +#endif /* initial handshake */ { @@ -127,7 +131,27 @@ _assuan_connect_finalize (assuan_context_t ctx, assuan_fd_t fd, if (err) TRACE1 (ctx, ASSUAN_LOG_SYSIO, "assuan_socket_connect", ctx, "can't connect to server: %s\n", gpg_strerror (err)); - else if (response != ASSUAN_RESPONSE_OK) + else if (response == ASSUAN_RESPONSE_OK) + { + 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 { char *sname = _assuan_encode_c_string (ctx, ctx->inbound.line); if (sname) diff --git a/src/assuan-socket-server.c b/src/assuan-socket-server.c index a8330a8..f5fe038 100644 --- a/src/assuan-socket-server.c +++ b/src/assuan-socket-server.c @@ -238,8 +238,12 @@ assuan_init_socket_server (assuan_context_t ctx, assuan_fd_t fd, : accept_connection); ctx->finish_handler = _assuan_server_finish; +#ifdef HAVE_W32_SYSTEM + ctx->engine.receivefd = w32_fdpass_recv; +#else if (flags & ASSUAN_SOCKET_SERVER_FDPASSING) _assuan_init_uds_io (ctx); +#endif rc = _assuan_register_std_commands (ctx); if (rc) diff --git a/src/client.c b/src/client.c index dcb2a1a..1746788 100644 --- a/src/client.c +++ b/src/client.c @@ -51,7 +51,7 @@ _assuan_client_finish (assuan_context_t ctx) if (ctx->pid != ASSUAN_INVALID_PID && ctx->pid) { if (!ctx->flags.is_socket) - _assuan_waitpid (ctx, ctx->pid, ctx->flags.no_waitpid, NULL, 0); + _assuan_waitpid (ctx, ctx->pid, ctx->flags.no_waitpid, NULL, 0); ctx->pid = ASSUAN_INVALID_PID; } diff --git a/src/system-w32.c b/src/system-w32.c index 76ff2a0..1392a1e 100644 --- a/src/system-w32.c +++ b/src/system-w32.c @@ -167,6 +167,10 @@ __assuan_close (assuan_context_t ctx, assuan_fd_t fd) +/* To encode/decode file HANDLE, we use FDPASS_FORMAT */ +#define FDPASS_FORMAT "%p" +#define FDPASS_MSG_SIZE (sizeof (uintptr_t)*2 + 1) + /* Get a file HANDLE to send, from POSIX fd. */ static gpg_error_t get_file_handle (int fd, int server_pid, HANDLE *r_handle) @@ -220,6 +224,30 @@ w32_fdpass_send (assuan_context_t ctx, assuan_fd_t fd) return err; } +static int +process_fdpass_msg (const char *fdpass_msg, size_t msglen, int *r_fd) +{ + void *file_handle; + int res; + int fd; + + *r_fd = -1; + + res = sscanf (fdpass_msg, FDPASS_FORMAT, &file_handle); + if (res != 1) + return -1; + + fd = _open_osfhandle ((intptr_t)file_handle, _O_RDWR); + if (fd < 0) + { + CloseHandle (file_handle); + return -1; + } + + *r_fd = fd; + return 0; +} + /* Receive a HANDLE from the peer and turn it into a FD (POSIX fd). */ gpg_error_t @@ -252,7 +280,53 @@ __assuan_read (assuan_context_t ctx, assuan_fd_t fd, void *buffer, size_t size) if (ctx->flags.is_socket) { + fd_set fds; int tries = 3; + fd_set efds; + + FD_ZERO (&fds); + FD_SET (HANDLE2SOCKET (fd), &fds); + FD_ZERO (&efds); + FD_SET (HANDLE2SOCKET (fd), &efds); + res = select (0, &fds, NULL, &efds, NULL); + if (res < 0) + { + gpg_err_set_errno (EIO); + return -1; + } + else if (FD_ISSET (HANDLE2SOCKET (fd), &efds)) + { + int fd_recv; + char fdpass_msg[FDPASS_MSG_SIZE]; + + /* the message of ! */ + res = recv (HANDLE2SOCKET (fd), fdpass_msg, sizeof (fdpass_msg), MSG_OOB); + if (res < 0) + { + gpg_err_set_errno (EIO); + return -1; + } + + /* the body of message */ + res = recv (HANDLE2SOCKET (fd), fdpass_msg, sizeof (fdpass_msg), 0); + if (res < 0) + { + gpg_err_set_errno (EIO); + return -1; + } + + res = process_fdpass_msg (fdpass_msg, res, &fd_recv); + if (res < 0) + { + gpg_err_set_errno (EIO); + return -1; + } + + ctx->uds.pendingfds[ctx->uds.pendingfdscount++] = (assuan_fd_t)fd_recv; + TRACE1 (ctx, ASSUAN_LOG_SYSIO, "__assuan_read", ctx, + "received fd: %d", fd_recv); + /* Fall through */ + } again: ec = 0; @@ -266,8 +340,6 @@ __assuan_read (assuan_context_t ctx, assuan_fd_t fd, void *buffer, size_t size) layer then needs to take care of EAGAIN. No need to specify a timeout - the socket is not expected to be in blocking mode. */ - fd_set fds; - FD_ZERO (&fds); FD_SET (HANDLE2SOCKET (fd), &fds); select (0, &fds, NULL, NULL, NULL); |