2007-07-13 Marcus Brinkmann <marcus@g10code.de>

* Revert the "close_notify_handler" returns int stuff.  Always
	close in the _gpgme_io_close implementations.
	* engine-gpgsm.c (status_handler): Try to terminate the connection
	in case of error.
	* w32-io.c (_gpgme_io_read): Return C->error_code in ERRNO.
	(_gpgme_io_write): Likewise.
This commit is contained in:
Marcus Brinkmann 2007-07-13 01:57:02 +00:00
parent c4bb93ec28
commit 5d25b54f30
7 changed files with 30 additions and 36 deletions

View File

@ -1,5 +1,12 @@
2007-07-13 Marcus Brinkmann <marcus@g10code.de> 2007-07-13 Marcus Brinkmann <marcus@g10code.de>
* Revert the "close_notify_handler" returns int stuff. Always
close in the _gpgme_io_close implementations.
* engine-gpgsm.c (status_handler): Try to terminate the connection
in case of error.
* w32-io.c (_gpgme_io_read): Return C->error_code in ERRNO.
(_gpgme_io_write): Likewise.
* priv-io.h (_gpgme_io_set_close_notify): Change type of HANDLER * priv-io.h (_gpgme_io_set_close_notify): Change type of HANDLER
to _gpgme_close_notify_handler. to _gpgme_close_notify_handler.
(_gpgme_close_notify_handler): New type. (_gpgme_close_notify_handler): New type.

View File

@ -112,7 +112,7 @@ gpgsm_get_req_version (void)
} }
static int static void
close_notify_handler (int fd, void *opaque) close_notify_handler (int fd, void *opaque)
{ {
engine_gpgsm_t gpgsm = opaque; engine_gpgsm_t gpgsm = opaque;
@ -124,9 +124,6 @@ close_notify_handler (int fd, void *opaque)
(*gpgsm->io_cbs.remove) (gpgsm->status_cb.tag); (*gpgsm->io_cbs.remove) (gpgsm->status_cb.tag);
gpgsm->status_cb.fd = -1; gpgsm->status_cb.fd = -1;
gpgsm->status_cb.tag = NULL; gpgsm->status_cb.tag = NULL;
/* We do not want to close the status FD, as it is controled by
Assuan. */
return 0;
} }
else if (gpgsm->input_cb.fd == fd) else if (gpgsm->input_cb.fd == fd)
{ {
@ -149,7 +146,6 @@ close_notify_handler (int fd, void *opaque)
gpgsm->message_cb.fd = -1; gpgsm->message_cb.fd = -1;
gpgsm->message_cb.tag = NULL; gpgsm->message_cb.tag = NULL;
} }
return 1;
} }
@ -287,8 +283,6 @@ gpgsm_cancel (void *engine)
if (!gpgsm) if (!gpgsm)
return gpg_error (GPG_ERR_INV_VALUE); return gpg_error (GPG_ERR_INV_VALUE);
if (gpgsm->status_cb.fd != -1)
_gpgme_io_close (gpgsm->status_cb.fd);
if (gpgsm->input_cb.fd != -1) if (gpgsm->input_cb.fd != -1)
_gpgme_io_close (gpgsm->input_cb.fd); _gpgme_io_close (gpgsm->input_cb.fd);
if (gpgsm->output_cb.fd != -1) if (gpgsm->output_cb.fd != -1)
@ -827,6 +821,8 @@ status_handler (void *opaque, int fd)
err = gpg_error (GPG_ERR_GENERAL); err = gpg_error (GPG_ERR_GENERAL);
DEBUG2 ("fd %d: ERR line - mapped to: %s\n", DEBUG2 ("fd %d: ERR line - mapped to: %s\n",
fd, err? gpg_strerror (err):"ok"); fd, err? gpg_strerror (err):"ok");
/* Try our best to terminate the connection friendly. */
assuan_write_line (gpgsm->assuan_ctx, "BYE");
} }
else if (linelen >= 2 else if (linelen >= 2
&& line[0] == 'O' && line[1] == 'K' && line[0] == 'O' && line[1] == 'K'
@ -846,7 +842,6 @@ status_handler (void *opaque, int fd)
gpgsm->colon.any = 0; gpgsm->colon.any = 0;
err = gpgsm->colon.fnc (gpgsm->colon.fnc_value, NULL); err = gpgsm->colon.fnc (gpgsm->colon.fnc_value, NULL);
} }
_gpgme_io_close (gpgsm->status_cb.fd);
DEBUG2 ("fd %d: OK line - final status: %s\n", DEBUG2 ("fd %d: OK line - final status: %s\n",
fd, err? gpg_strerror (err):"ok"); fd, err? gpg_strerror (err):"ok");
return err; return err;
@ -990,8 +985,7 @@ start (engine_gpgsm_t gpgsm, const char *command)
/* We used to duplicate the file descriptor so that we do not /* We used to duplicate the file descriptor so that we do not
disturb Assuan. But this gets in the way of the Handle-to-Thread disturb Assuan. But this gets in the way of the Handle-to-Thread
mapping in w32-io.c, so instead we just share the file descriptor mapping in w32-io.c, so instead we just share the file descriptor
*carefully*, by avoiding to close it ourselves (this is achieved by but leave it to Assuan to close it. */
returning 0 from the close_notify_handler for this descriptor). */
gpgsm->status_cb.fd = fdlist[0]; gpgsm->status_cb.fd = fdlist[0];
if (_gpgme_io_set_close_notify (gpgsm->status_cb.fd, if (_gpgme_io_set_close_notify (gpgsm->status_cb.fd,

View File

@ -139,8 +139,6 @@ _gpgme_io_pipe (int filedes[2], int inherit_idx)
int int
_gpgme_io_close (int fd) _gpgme_io_close (int fd)
{ {
int really_close = 1;
if (fd == -1) if (fd == -1)
return -1; return -1;
/* First call the notify handler. */ /* First call the notify handler. */
@ -149,16 +147,13 @@ _gpgme_io_close (int fd)
{ {
if (notify_table[fd].handler) if (notify_table[fd].handler)
{ {
really_close = notify_table[fd].handler (fd, notify_table[fd].value); notify_table[fd].handler (fd, notify_table[fd].value);
notify_table[fd].handler = NULL; notify_table[fd].handler = NULL;
notify_table[fd].value = NULL; notify_table[fd].value = NULL;
} }
} }
/* Then do the close. */ /* Then do the close. */
if (really_close) return close (fd);
return close (fd);
return 0;
} }

View File

@ -47,7 +47,7 @@ int _gpgme_io_read (int fd, void *buffer, size_t count);
int _gpgme_io_write (int fd, const void *buffer, size_t count); int _gpgme_io_write (int fd, const void *buffer, size_t count);
int _gpgme_io_pipe (int filedes[2], int inherit_idx); int _gpgme_io_pipe (int filedes[2], int inherit_idx);
int _gpgme_io_close (int fd); int _gpgme_io_close (int fd);
typedef int (*_gpgme_close_notify_handler_t) (int,void*); typedef void (*_gpgme_close_notify_handler_t) (int,void*);
int _gpgme_io_set_close_notify (int fd, _gpgme_close_notify_handler_t handler, int _gpgme_io_set_close_notify (int fd, _gpgme_close_notify_handler_t handler,
void *value); void *value);
int _gpgme_io_set_nonblocking (int fd); int _gpgme_io_set_nonblocking (int fd);

View File

@ -141,7 +141,7 @@ gpg_io_event (void *engine, gpgme_event_io_t type, void *type_data)
} }
static int static void
close_notify_handler (int fd, void *opaque) close_notify_handler (int fd, void *opaque)
{ {
engine_gpg_t gpg = opaque; engine_gpg_t gpg = opaque;
@ -183,7 +183,6 @@ close_notify_handler (int fd, void *opaque)
} }
} }
} }
return 1;
} }
/* If FRONT is true, push at the front of the list. Use this for /* If FRONT is true, push at the front of the list. Use this for

View File

@ -271,7 +271,6 @@ int
_gpgme_io_close (int fd) _gpgme_io_close (int fd)
{ {
GIOChannel *chan; GIOChannel *chan;
int really_close = 1;
if (fd < 0 || fd >= MAX_SLAFD) if (fd < 0 || fd >= MAX_SLAFD)
{ {
@ -283,24 +282,21 @@ _gpgme_io_close (int fd)
DEBUG1 ("closing fd %d", fd); DEBUG1 ("closing fd %d", fd);
if (notify_table[fd].handler) if (notify_table[fd].handler)
{ {
really_close = notify_table[fd].handler (fd, notify_table[fd].value); notify_table[fd].handler (fd, notify_table[fd].value);
notify_table[fd].handler = NULL; notify_table[fd].handler = NULL;
notify_table[fd].value = NULL; notify_table[fd].value = NULL;
} }
/* Then do the close. */ /* Then do the close. */
if (really_close) chan = giochannel_table[fd];
if (chan)
{ {
chan = giochannel_table[fd]; g_io_channel_shutdown (chan, 1, NULL);
if (chan) g_io_channel_unref (chan);
{ giochannel_table[fd] = NULL;
g_io_channel_shutdown (chan, 1, NULL);
g_io_channel_unref (chan);
giochannel_table[fd] = NULL;
}
else
_close (fd);
} }
else
_close (fd);
return 0; return 0;
} }

View File

@ -374,6 +374,7 @@ _gpgme_io_read ( int fd, void *buffer, size_t count )
DEBUG2 ("fd %d: about to read %d bytes\n", fd, (int)count ); DEBUG2 ("fd %d: about to read %d bytes\n", fd, (int)count );
if ( !c ) { if ( !c ) {
DEBUG0 ( "no reader thread\n"); DEBUG0 ( "no reader thread\n");
errno = EBADF;
return -1; return -1;
} }
if (c->eof_shortcut) { if (c->eof_shortcut) {
@ -402,6 +403,7 @@ _gpgme_io_read ( int fd, void *buffer, size_t count )
return 0; return 0;
} }
DEBUG1 ("fd %d: read error", fd ); DEBUG1 ("fd %d: read error", fd );
errno = c->error_code;
return -1; return -1;
} }
@ -631,11 +633,12 @@ _gpgme_io_write ( int fd, const void *buffer, size_t count )
_gpgme_debug (2, "fd %d: write `%.*s'\n", fd, (int) count, buffer); _gpgme_debug (2, "fd %d: write `%.*s'\n", fd, (int) count, buffer);
if ( !c ) { if ( !c ) {
DEBUG0 ( "no writer thread\n"); DEBUG0 ( "no writer thread\n");
errno = EBADF;
return -1; return -1;
} }
LOCK (c->mutex); LOCK (c->mutex);
if ( c->nbytes ) { /* bytes are pending for send */ if ( !c->error && c->nbytes ) { /* bytes are pending for send */
/* Reset the is_empty event. Better safe than sorry. */ /* Reset the is_empty event. Better safe than sorry. */
if (!ResetEvent (c->is_empty)) if (!ResetEvent (c->is_empty))
DEBUG1 ("ResetEvent failed: ec=%d", (int)GetLastError ()); DEBUG1 ("ResetEvent failed: ec=%d", (int)GetLastError ());
@ -650,6 +653,7 @@ _gpgme_io_write ( int fd, const void *buffer, size_t count )
if ( c->error) { if ( c->error) {
UNLOCK (c->mutex); UNLOCK (c->mutex);
DEBUG1 ("fd %d: write error", fd ); DEBUG1 ("fd %d: write error", fd );
errno = c->error_code;
return -1; return -1;
} }
@ -729,7 +733,6 @@ _gpgme_io_close ( int fd )
int i; int i;
_gpgme_close_notify_handler_t handler = NULL; _gpgme_close_notify_handler_t handler = NULL;
void *value = NULL; void *value = NULL;
int really_close = 1;
if ( fd == -1 ) if ( fd == -1 )
return -1; return -1;
@ -750,9 +753,9 @@ _gpgme_io_close ( int fd )
} }
UNLOCK (notify_table_lock); UNLOCK (notify_table_lock);
if (handler) if (handler)
really_close = handler (fd, value); handler (fd, value);
if ( really_close && !CloseHandle (fd_to_handle (fd)) ) { if ( !CloseHandle (fd_to_handle (fd)) ) {
DEBUG2 ("CloseHandle for fd %d failed: ec=%d\n", DEBUG2 ("CloseHandle for fd %d failed: ec=%d\n",
fd, (int)GetLastError ()); fd, (int)GetLastError ());
return -1; return -1;