diff options
Diffstat (limited to 'src/assuan-pipe-server.c')
-rw-r--r-- | src/assuan-pipe-server.c | 180 |
1 files changed, 74 insertions, 106 deletions
diff --git a/src/assuan-pipe-server.c b/src/assuan-pipe-server.c index a732db7..3c30d48 100644 --- a/src/assuan-pipe-server.c +++ b/src/assuan-pipe-server.c @@ -47,48 +47,10 @@ accept_connection (assuan_context_t ctx) return 0; } -static gpg_error_t +static void finish_connection (assuan_context_t ctx) { /* This is a NOP for a pipe server */ - return 0; -} - -/* Create a new context. Note that the handlers are set up for a pipe - server/client - this way we don't need extra dummy functions */ -gpg_error_t -_assuan_new_context (assuan_context_t *r_ctx) -{ - static struct assuan_io io = { _assuan_simple_read, - _assuan_simple_write, - 0, 0 }; - - assuan_context_t ctx; - gpg_error_t rc; - - *r_ctx = NULL; - ctx = _assuan_calloc (1, sizeof *ctx); - if (!ctx) - return _assuan_error (gpg_err_code_from_syserror ()); - ctx->input_fd = ASSUAN_INVALID_FD; - ctx->output_fd = ASSUAN_INVALID_FD; - - ctx->inbound.fd = ASSUAN_INVALID_FD; - ctx->outbound.fd = ASSUAN_INVALID_FD; - ctx->io = &io; - - ctx->listen_fd = ASSUAN_INVALID_FD; - /* Use the pipe server handler as a default. */ - ctx->deinit_handler = deinit_pipe_server; - ctx->accept_handler = accept_connection; - ctx->finish_handler = finish_connection; - - rc = _assuan_register_std_commands (ctx); - if (rc) - _assuan_free (ctx); - else - *r_ctx = ctx; - return rc; } @@ -107,86 +69,92 @@ is_valid_socket (const char *s) gpg_error_t -assuan_init_pipe_server (assuan_context_t *r_ctx, int filedes[2]) +assuan_init_pipe_server (assuan_context_t ctx, int filedes[2]) { - int rc; + const char *s; + unsigned long ul; + gpg_error_t rc; + assuan_fd_t infd = ASSUAN_INVALID_FD; + assuan_fd_t outfd = ASSUAN_INVALID_FD; + int is_usd = 0; + static struct assuan_io io = { _assuan_simple_read, _assuan_simple_write, + 0, 0 }; - rc = _assuan_new_context (r_ctx); - if (!rc) - { - assuan_context_t ctx = *r_ctx; - const char *s; - unsigned long ul; + rc = _assuan_register_std_commands (ctx); + if (rc) + return rc; - ctx->is_server = 1; #ifdef HAVE_W32_SYSTEM - /* MS Windows has so many different types of handle that one - needs to tranlsate them at many place forth and back. Also - make sure that the file descriptors are in binary mode. */ - setmode (filedes[0], O_BINARY); - setmode (filedes[1], O_BINARY); - ctx->inbound.fd = (void*)_get_osfhandle (filedes[0]); - ctx->outbound.fd = (void*)_get_osfhandle (filedes[1]); + /* MS Windows has so many different types of handle that one needs + to tranlsate them at many place forth and back. Also make sure + that the file descriptors are in binary mode. */ + setmode (filedes[0], O_BINARY); + setmode (filedes[1], O_BINARY); + infd = (void*)_get_osfhandle (filedes[0]); + outfd = (void*)_get_osfhandle (filedes[1]); #else - s = getenv ("_assuan_connection_fd"); - if (s && *s && is_valid_socket (s) ) - { - /* Well, we are called with an bi-directional file - descriptor. Prepare for using sendmsg/recvmsg. In this - case we ignore the passed file descriptors. */ - ctx->inbound.fd = ctx->outbound.fd = atoi (s); - _assuan_init_uds_io (ctx); - ctx->deinit_handler = _assuan_uds_deinit; - } - else if (filedes && filedes[0] != ASSUAN_INVALID_FD - && filedes[1] != ASSUAN_INVALID_FD ) - { - /* Standard pipe server. */ - ctx->inbound.fd = filedes[0]; - ctx->outbound.fd = filedes[1]; - } - else - { - _assuan_release_context (*r_ctx); - *r_ctx = NULL; - return _assuan_error (GPG_ERR_ASS_SERVER_START); - } -#endif - ctx->pipe_mode = 1; - - s = getenv ("_assuan_pipe_connect_pid"); - if (s && (ul=strtoul (s, NULL, 10)) && ul) - ctx->pid = (pid_t)ul; - else - ctx->pid = (pid_t)-1; + s = getenv ("_assuan_connection_fd"); + if (s && *s && is_valid_socket (s)) + { + /* Well, we are called with an bi-directional file descriptor. + Prepare for using sendmsg/recvmsg. In this case we ignore + the passed file descriptors. */ + infd = atoi (s); + outfd = atoi (s); + is_usd = 1; } - return rc; -} + else if (filedes && filedes[0] != ASSUAN_INVALID_FD + && filedes[1] != ASSUAN_INVALID_FD ) + { + /* Standard pipe server. */ + infd = filedes[0]; + outfd = filedes[1]; + } + else + return _assuan_error (ctx, GPG_ERR_ASS_SERVER_START); +#endif + ctx->is_server = 1; + ctx->engine.release = deinit_pipe_server; + ctx->pipe_mode = 1; -void -_assuan_release_context (assuan_context_t ctx) -{ - if (ctx) + s = getenv ("_assuan_pipe_connect_pid"); + if (s && (ul=strtoul (s, NULL, 10)) && ul) + ctx->pid = (pid_t)ul; + else + ctx->pid = (pid_t)-1; + ctx->accept_handler = accept_connection; + ctx->finish_handler = finish_connection; + ctx->deinit_handler = deinit_pipe_server; + ctx->inbound.fd = infd; + ctx->outbound.fd = outfd; + + if (is_usd) { - _assuan_inquire_release (ctx); - _assuan_free (ctx->hello_line); - _assuan_free (ctx->okay_line); - _assuan_free (ctx->cmdtbl); - _assuan_free (ctx); + _assuan_init_uds_io (ctx); + ctx->deinit_handler = _assuan_uds_deinit; } + else + ctx->io = &io; + + return 0; } + void -assuan_deinit_server (assuan_context_t ctx) +_assuan_deinit_server (assuan_context_t ctx) { - if (ctx) - { - /* We use this function pointer to avoid linking other server - when not needed but still allow for a generic deinit function. */ - ctx->deinit_handler (ctx); - ctx->deinit_handler = NULL; - _assuan_release_context (ctx); - } + /* We use this function pointer to avoid linking other server when + not needed but still allow for a generic deinit function. */ + ctx->deinit_handler (ctx); + ctx->deinit_handler = NULL; + + _assuan_inquire_release (ctx); + _assuan_free (ctx, ctx->hello_line); + ctx->hello_line = NULL; + _assuan_free (ctx, ctx->okay_line); + ctx->okay_line = NULL; + _assuan_free (ctx, ctx->cmdtbl); + ctx->cmdtbl = NULL; } |