diff options
author | Marcus Brinkmann <mb@g10code.com> | 2009-11-19 23:57:35 +0000 |
---|---|---|
committer | Marcus Brinkmann <mb@g10code.com> | 2009-11-19 23:57:35 +0000 |
commit | cb53f862ff2a864da822d1c16d3df6a02157608b (patch) | |
tree | 7e2dc047048fe7053ef795f275537f899c986092 /src | |
parent | 7cceb82f044654adccc7c998c979eca5af0f903b (diff) | |
download | libassuan-cb53f862ff2a864da822d1c16d3df6a02157608b.tar.gz |
2009-11-19 Marcus Brinkmann <marcus@g10code.de>
* Makefile.am (common_sources): Remove assuan-connect.c and add
client.c.
* client.c, server.c: New file.
* assuan-defs.h (_assuan_disconnect): Remove.
(struct assuan_context_s): Remove members deinit_handler.
(_assuan_client_release, _assuan_client_finish)
(_assuan_server_finish, _assuan_server_release): New.
* assuan-socket-server.c (accept_connection_bottom): Use
ASSUAN_INVALID_PID, not -1.
(finish_connection, deinit_socket_server): Remove.
(assuan_init_socket_server): Use _assuan_server_release.
* assuan-socket-connect.c (do_finish, do_deinit): Remove.
(assuan_socket_connect): Use _assuan_client_release.
* assuan-pipe-connect.c (do_finish, do_deinit): Remove.
(pipe_connect): Update deinitialization.
(socketpair_connect): Here as well.
* context.c (assuan_get_pid): New from ...
* assuan-connect.c (assuan_get_pid): ... here. Remove this file.
* assuan-pipe-server.c (_assuan_deinit_server, accept_connection)
(deinit_pipe_server, finish_connection): Remove unused function.
* assuan-listen.c (assuan_accept): Check CTX->accept_handler
before calling. Initialize RC. Do not call finish handler for
pipe server.
* assuan-uds.c (_assuan_uds_deinit): Do not call finish handler.
Diffstat (limited to 'src')
-rw-r--r-- | src/ChangeLog | 27 | ||||
-rw-r--r-- | src/Makefile.am | 9 | ||||
-rw-r--r-- | src/assuan-defs.h | 8 | ||||
-rw-r--r-- | src/assuan-listen.c | 13 | ||||
-rw-r--r-- | src/assuan-pipe-connect.c | 41 | ||||
-rw-r--r-- | src/assuan-pipe-server.c | 50 | ||||
-rw-r--r-- | src/assuan-socket-connect.c | 30 | ||||
-rw-r--r-- | src/assuan-socket-server.c | 46 | ||||
-rw-r--r-- | src/assuan-uds.c | 2 | ||||
-rw-r--r-- | src/client.c | 60 | ||||
-rw-r--r-- | src/context.c | 10 | ||||
-rw-r--r-- | src/server.c (renamed from src/assuan-connect.c) | 64 |
12 files changed, 168 insertions, 192 deletions
diff --git a/src/ChangeLog b/src/ChangeLog index d309e58..e84c344 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,30 @@ +2009-11-19 Marcus Brinkmann <marcus@g10code.de> + + * Makefile.am (common_sources): Remove assuan-connect.c and add + client.c. + * client.c, server.c: New file. + * assuan-defs.h (_assuan_disconnect): Remove. + (struct assuan_context_s): Remove members deinit_handler. + (_assuan_client_release, _assuan_client_finish) + (_assuan_server_finish, _assuan_server_release): New. + * assuan-socket-server.c (accept_connection_bottom): Use + ASSUAN_INVALID_PID, not -1. + (finish_connection, deinit_socket_server): Remove. + (assuan_init_socket_server): Use _assuan_server_release. + * assuan-socket-connect.c (do_finish, do_deinit): Remove. + (assuan_socket_connect): Use _assuan_client_release. + * assuan-pipe-connect.c (do_finish, do_deinit): Remove. + (pipe_connect): Update deinitialization. + (socketpair_connect): Here as well. + * context.c (assuan_get_pid): New from ... + * assuan-connect.c (assuan_get_pid): ... here. Remove this file. + * assuan-pipe-server.c (_assuan_deinit_server, accept_connection) + (deinit_pipe_server, finish_connection): Remove unused function. + * assuan-listen.c (assuan_accept): Check CTX->accept_handler + before calling. Initialize RC. Do not call finish handler for + pipe server. + * assuan-uds.c (_assuan_uds_deinit): Do not call finish handler. + 2009-11-10 Marcus Brinkmann <marcus@g10code.de> * assuan-defs.h (struct assuan_context_s): Rename diff --git a/src/Makefile.am b/src/Makefile.am index 0d6ec0b..b5b9f52 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -36,17 +36,14 @@ endif common_sources = \ assuan-defs.h \ - assuan.c \ - context.c \ - system.c \ - debug.c debug.h \ - conversion.c \ + assuan.c context.c system.c \ + debug.c debug.h conversion.c \ + client.c server.c \ assuan-error.c \ assuan-buffer.c \ assuan-handler.c \ assuan-inquire.c \ assuan-listen.c \ - assuan-connect.c \ assuan-client.c \ assuan-pipe-server.c \ assuan-socket-server.c \ diff --git a/src/assuan-defs.h b/src/assuan-defs.h index 00a3652..e4883a9 100644 --- a/src/assuan-defs.h +++ b/src/assuan-defs.h @@ -182,7 +182,6 @@ struct assuan_context_s int pendingfdscount; /* Number of received descriptors. */ } uds; - void (*deinit_handler)(assuan_context_t); gpg_error_t (*accept_handler)(assuan_context_t); void (*finish_handler)(assuan_context_t); @@ -364,7 +363,12 @@ int putc_unlocked (int c, FILE *stream); #endif -void _assuan_disconnect (assuan_context_t ctx); +void _assuan_client_finish (assuan_context_t ctx); +void _assuan_client_release (assuan_context_t ctx); + +void _assuan_server_finish (assuan_context_t ctx); +void _assuan_server_release (assuan_context_t ctx); + /* Encode the C formatted string SRC and return the malloc'ed result. */ char *_assuan_encode_c_string (assuan_context_t ctx, const char *src); diff --git a/src/assuan-listen.c b/src/assuan-listen.c index 2f6c8a3..5261c0f 100644 --- a/src/assuan-listen.c +++ b/src/assuan-listen.c @@ -72,7 +72,7 @@ assuan_set_hello_line (assuan_context_t ctx, const char *line) gpg_error_t assuan_accept (assuan_context_t ctx) { - gpg_error_t rc; + gpg_error_t rc = 0; const char *p, *pend; if (!ctx) @@ -80,11 +80,14 @@ assuan_accept (assuan_context_t ctx) if (ctx->pipe_mode > 1) return -1; /* second invocation for pipemode -> terminate */ - ctx->finish_handler (ctx); + if (! ctx->pipe_mode) + { + ctx->finish_handler (ctx); - rc = ctx->accept_handler (ctx); - if (rc) - return rc; + rc = ctx->accept_handler (ctx); + if (rc) + return rc; + } /* Send the hello. */ p = ctx->hello_line; diff --git a/src/assuan-pipe-connect.c b/src/assuan-pipe-connect.c index 9e214a8..74f85f2 100644 --- a/src/assuan-pipe-connect.c +++ b/src/assuan-pipe-connect.c @@ -84,36 +84,6 @@ fix_signals (void) } -static void -do_finish (assuan_context_t ctx) -{ - if (ctx->inbound.fd != ASSUAN_INVALID_FD) - { - _assuan_close (ctx, ctx->inbound.fd); - if (ctx->inbound.fd == ctx->outbound.fd) - ctx->outbound.fd = ASSUAN_INVALID_FD; - ctx->inbound.fd = ASSUAN_INVALID_FD; - } - if (ctx->outbound.fd != ASSUAN_INVALID_FD) - { - _assuan_close (ctx, ctx->outbound.fd); - ctx->outbound.fd = ASSUAN_INVALID_FD; - } - if (ctx->pid != ASSUAN_INVALID_PID && ctx->pid) - { - _assuan_waitpid (ctx, ctx->pid, ctx->flags.no_waitpid, NULL, 0); - ctx->pid = ASSUAN_INVALID_PID; - } -} - - -static void -do_deinit (assuan_context_t ctx) -{ - do_finish (ctx); -} - - /* Helper for pipe_connect. */ static gpg_error_t initial_handshake (assuan_context_t ctx) @@ -220,16 +190,15 @@ pipe_connect (assuan_context_t ctx, _assuan_close (ctx, rp[1]); _assuan_close (ctx, wp[0]); - ctx->engine.release = _assuan_disconnect; + ctx->engine.release = _assuan_client_release; ctx->engine.readfnc = _assuan_simple_read; ctx->engine.writefnc = _assuan_simple_write; ctx->engine.sendfd = NULL; ctx->engine.receivefd = NULL; + ctx->finish_handler = _assuan_client_finish; ctx->pipe_mode = 1; ctx->inbound.fd = rp[0]; /* Our inbound is read end of read pipe. */ ctx->outbound.fd = wp[1]; /* Our outbound is write end of write pipe. */ - ctx->deinit_handler = do_deinit; - ctx->finish_handler = do_finish; ctx->pid = pid; rc = initial_handshake (ctx); @@ -370,11 +339,11 @@ socketpair_connect (assuan_context_t ctx, _assuan_close (ctx, fds[1]); + ctx->engine.release = _assuan_client_release; + ctx->finish_handler = _assuan_client_finish; ctx->pipe_mode = 1; ctx->inbound.fd = fds[0]; ctx->outbound.fd = fds[0]; - ctx->deinit_handler = _assuan_uds_deinit; - ctx->finish_handler = do_finish; _assuan_init_uds_io (ctx); err = initial_handshake (ctx); @@ -424,7 +393,7 @@ assuan_pipe_connect (assuan_context_t ctx, TRACE2 (ctx, ASSUAN_LOG_CTX, "assuan_pipe_connect_ext", ctx, "name=%s,flags=0x%x", name ? name : "(null)", flags); - if ((flags & 1)) + if (flags & ASSUAN_PIPE_CONNECT_FDPASSING) { #ifdef HAVE_W32_SYSTEM return _assuan_error (ctx, GPG_ERR_NOT_IMPLEMENTED); diff --git a/src/assuan-pipe-server.c b/src/assuan-pipe-server.c index 7849fa5..56c3947 100644 --- a/src/assuan-pipe-server.c +++ b/src/assuan-pipe-server.c @@ -34,26 +34,6 @@ #include "assuan-defs.h" -static void -deinit_pipe_server (assuan_context_t ctx) -{ - /* nothing to do for this simple server */ -} - -static gpg_error_t -accept_connection (assuan_context_t ctx) -{ - /* This is a NOP for a pipe server */ - return 0; -} - -static void -finish_connection (assuan_context_t ctx) -{ - /* This is a NOP for a pipe server */ -} - - /* Returns true if atoi(S) denotes a valid socket. */ #ifndef HAVE_W32_SYSTEM static int @@ -114,7 +94,7 @@ assuan_init_pipe_server (assuan_context_t ctx, int filedes[2]) #endif ctx->is_server = 1; - ctx->engine.release = deinit_pipe_server; + ctx->engine.release = _assuan_server_release; ctx->engine.readfnc = _assuan_simple_read; ctx->engine.writefnc = _assuan_simple_write; ctx->engine.sendfd = NULL; @@ -126,35 +106,13 @@ assuan_init_pipe_server (assuan_context_t ctx, int filedes[2]) 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->accept_handler = NULL; + ctx->finish_handler = _assuan_server_finish; ctx->inbound.fd = infd; ctx->outbound.fd = outfd; if (is_usd) - { - _assuan_init_uds_io (ctx); - ctx->deinit_handler = _assuan_uds_deinit; - } + _assuan_init_uds_io (ctx); return 0; } - - -void -_assuan_deinit_server (assuan_context_t 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; -} diff --git a/src/assuan-socket-connect.c b/src/assuan-socket-connect.c index 6515631..7ec8799 100644 --- a/src/assuan-socket-connect.c +++ b/src/assuan-socket-connect.c @@ -52,29 +52,6 @@ + strlen ((ptr)->sun_path)) #endif - -static void -do_finish (assuan_context_t ctx) -{ - if (ctx->inbound.fd != ASSUAN_INVALID_FD) - { - _assuan_close (ctx, ctx->inbound.fd); - ctx->inbound.fd = ASSUAN_INVALID_FD; - } - if (ctx->outbound.fd != ASSUAN_INVALID_FD) - { - _assuan_close (ctx, ctx->outbound.fd); - ctx->outbound.fd = ASSUAN_INVALID_FD; - } -} - - -static void -do_deinit (assuan_context_t ctx) -{ - do_finish (ctx); -} - /* Make a connection to the Unix domain socket NAME and return a new Assuan context in CTX. SERVER_PID is currently not used but may @@ -130,17 +107,16 @@ assuan_socket_connect (assuan_context_t ctx, const char *name, return _assuan_error (ctx, GPG_ERR_ASS_CONNECT_FAILED); } - ctx->engine.release = _assuan_disconnect; + ctx->engine.release = _assuan_client_release; ctx->engine.readfnc = _assuan_simple_read; ctx->engine.writefnc = _assuan_simple_write; ctx->engine.sendfd = NULL; ctx->engine.receivefd = NULL; - ctx->deinit_handler = ((flags&1))? _assuan_uds_deinit : do_deinit; - ctx->finish_handler = do_finish; + ctx->finish_handler = _assuan_client_finish; ctx->inbound.fd = fd; ctx->outbound.fd = fd; - if (flags & 1) + if (flags & ASSUAN_SOCKET_CONNECT_FDPASSING) _assuan_init_uds_io (ctx); /* initial handshake */ diff --git a/src/assuan-socket-server.c b/src/assuan-socket-server.c index 2dbfa9f..a4431fa 100644 --- a/src/assuan-socket-server.c +++ b/src/assuan-socket-server.c @@ -61,7 +61,7 @@ accept_connection_bottom (assuan_context_t ctx) /* This overrides any already set PID if the function returns a valid one. */ - if (cr.pid != (pid_t)-1 && cr.pid) + if (cr.pid != ASSUAN_INVALID_PID && cr.pid) ctx->pid = cr.pid; } } @@ -107,37 +107,6 @@ accept_connection (assuan_context_t ctx) } -static void -finish_connection (assuan_context_t ctx) -{ - if (ctx->inbound.fd != ASSUAN_INVALID_FD) - { - _assuan_close (ctx, ctx->inbound.fd); - ctx->inbound.fd = ASSUAN_INVALID_FD; - } - if (ctx->outbound.fd != ASSUAN_INVALID_FD) - { - _assuan_close (ctx, ctx->outbound.fd); - ctx->outbound.fd = ASSUAN_INVALID_FD; - } -} - - -static void -deinit_socket_server (assuan_context_t ctx) -{ - finish_connection (ctx); - - _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; -} - - /* Flag bits: 0 - use sendmsg/recvmsg to allow descriptor passing 1 - FD has already been accepted. @@ -152,13 +121,13 @@ assuan_init_socket_server (assuan_context_t ctx, assuan_fd_t fd, if (rc) return rc; - ctx->engine.release = deinit_socket_server; + ctx->engine.release = _assuan_server_release; ctx->engine.readfnc = _assuan_simple_read; ctx->engine.writefnc = _assuan_simple_write; ctx->engine.sendfd = NULL; ctx->engine.receivefd = NULL; ctx->is_server = 1; - if (flags & 2) + if (flags & ASSUAN_SOCKET_SERVER_ACCEPTED) ctx->pipe_mode = 1; /* We want a second accept to indicate EOF. */ ctx->input_fd = ASSUAN_INVALID_FD; ctx->output_fd = ASSUAN_INVALID_FD; @@ -166,7 +135,7 @@ assuan_init_socket_server (assuan_context_t ctx, assuan_fd_t fd, ctx->inbound.fd = ASSUAN_INVALID_FD; ctx->outbound.fd = ASSUAN_INVALID_FD; - if ((flags & 2)) + if (flags & ASSUAN_SOCKET_SERVER_ACCEPTED) { ctx->listen_fd = ASSUAN_INVALID_FD; ctx->connected_fd = fd; @@ -176,13 +145,12 @@ assuan_init_socket_server (assuan_context_t ctx, assuan_fd_t fd, ctx->listen_fd = fd; ctx->connected_fd = ASSUAN_INVALID_FD; } - ctx->deinit_handler = (flags & 1)? _assuan_uds_deinit:deinit_socket_server; - ctx->accept_handler = ((flags & 2) + ctx->accept_handler = ((flags & ASSUAN_SOCKET_SERVER_ACCEPTED) ? accept_connection_bottom : accept_connection); - ctx->finish_handler = finish_connection; + ctx->finish_handler = _assuan_server_finish; - if ((flags & 1)) + if (flags & ASSUAN_SOCKET_SERVER_FDPASSING) _assuan_init_uds_io (ctx); rc = _assuan_register_std_commands (ctx); diff --git a/src/assuan-uds.c b/src/assuan-uds.c index 4b9988f..1a5dec7 100644 --- a/src/assuan-uds.c +++ b/src/assuan-uds.c @@ -266,8 +266,6 @@ _assuan_uds_close_fds (assuan_context_t ctx) void _assuan_uds_deinit (assuan_context_t ctx) { - /* First call the finish_handler which should close descriptors etc. */ - ctx->finish_handler (ctx); _assuan_uds_close_fds (ctx); } diff --git a/src/client.c b/src/client.c new file mode 100644 index 0000000..685a941 --- /dev/null +++ b/src/client.c @@ -0,0 +1,60 @@ +/* client.c - Functions common to all clients. + Copyright (C) 2009 Free Software Foundation, Inc. + + This file is part of Assuan. + + Assuan is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of + the License, or (at your option) any later version. + + Assuan is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this program; if not, see <http://www.gnu.org/licenses/>. + */ + + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "assuan-defs.h" +#include "debug.h" + +void +_assuan_client_finish (assuan_context_t ctx) +{ + if (ctx->inbound.fd != ASSUAN_INVALID_FD) + { + _assuan_close (ctx, ctx->inbound.fd); + if (ctx->inbound.fd == ctx->outbound.fd) + ctx->outbound.fd = ASSUAN_INVALID_FD; + ctx->inbound.fd = ASSUAN_INVALID_FD; + } + if (ctx->outbound.fd != ASSUAN_INVALID_FD) + { + _assuan_close (ctx, ctx->outbound.fd); + ctx->outbound.fd = ASSUAN_INVALID_FD; + } + if (ctx->pid != ASSUAN_INVALID_PID && ctx->pid) + { + _assuan_waitpid (ctx, ctx->pid, ctx->flags.no_waitpid, NULL, 0); + ctx->pid = ASSUAN_INVALID_PID; + } + + _assuan_uds_deinit (ctx); +} + + +/* Disconnect and release the context CTX. */ +void +_assuan_client_release (assuan_context_t ctx) +{ + assuan_write_line (ctx, "BYE"); + + _assuan_client_finish (ctx); +} diff --git a/src/context.c b/src/context.c index 9e010aa..42ec439 100644 --- a/src/context.c +++ b/src/context.c @@ -171,6 +171,16 @@ assuan_set_error (assuan_context_t ctx, gpg_error_t err, const char *text) } +/* Return the PID of the peer or ASSUAN_INVALID_PID if not known. + This function works in some situations where assuan_get_ucred + fails. */ +pid_t +assuan_get_pid (assuan_context_t ctx) +{ + return (ctx && ctx->pid) ? ctx->pid : ASSUAN_INVALID_PID; +} + + /* Return user credentials. For getting the pid of the peer the assuan_get_pid is usually better suited. */ gpg_error_t diff --git a/src/assuan-connect.c b/src/server.c index 44c92d3..6cc75ae 100644 --- a/src/assuan-connect.c +++ b/src/server.c @@ -1,5 +1,5 @@ -/* assuan-connect.c - Establish a connection (client) - Copyright (C) 2001, 2002, 2009 Free Software Foundation, Inc. +/* server.c - Interfaces for all assuan servers. + Copyright (C) 2009 Free Software Foundation, Inc. This file is part of Assuan. @@ -17,34 +17,49 @@ License along with this program; if not, see <http://www.gnu.org/licenses/>. */ + #ifdef HAVE_CONFIG_H #include <config.h> #endif -#include <stdlib.h> -#include <stdio.h> -#include <string.h> -#include <signal.h> -#include <unistd.h> -#include <errno.h> -#include <sys/types.h> -#ifndef HAVE_W32_SYSTEM -#include <sys/wait.h> -#endif - #include "assuan-defs.h" +#include "debug.h" + -/* Disconnect and release the context CTX. */ + +/* Disconnect and release the context CTX. */ void -_assuan_disconnect (assuan_context_t ctx) +_assuan_server_finish (assuan_context_t ctx) { - assuan_write_line (ctx, "BYE"); - ctx->finish_handler (ctx); - ctx->finish_handler = NULL; - ctx->deinit_handler (ctx); - ctx->deinit_handler = NULL; + if (ctx->inbound.fd != ASSUAN_INVALID_FD) + { + _assuan_close (ctx, ctx->inbound.fd); + if (ctx->inbound.fd == ctx->outbound.fd) + ctx->outbound.fd = ASSUAN_INVALID_FD; + ctx->inbound.fd = ASSUAN_INVALID_FD; + } + if (ctx->outbound.fd != ASSUAN_INVALID_FD) + { + _assuan_close (ctx, ctx->outbound.fd); + ctx->outbound.fd = ASSUAN_INVALID_FD; + } + if (ctx->pid != ASSUAN_INVALID_PID && ctx->pid) + { + _assuan_waitpid (ctx, ctx->pid, ctx->flags.no_waitpid, NULL, 0); + ctx->pid = ASSUAN_INVALID_PID; + } + + _assuan_uds_deinit (ctx); _assuan_inquire_release (ctx); +} + + +void +_assuan_server_release (assuan_context_t ctx) +{ + _assuan_server_finish (ctx); + _assuan_free (ctx, ctx->hello_line); ctx->hello_line = NULL; _assuan_free (ctx, ctx->okay_line); @@ -52,12 +67,3 @@ _assuan_disconnect (assuan_context_t ctx) _assuan_free (ctx, ctx->cmdtbl); ctx->cmdtbl = NULL; } - - -/* Return the PID of the peer or -1 if not known. This function works - in some situations where assuan_get_ucred fails. */ -pid_t -assuan_get_pid (assuan_context_t ctx) -{ - return (ctx && ctx->pid) ? ctx->pid : -1; -} |