From b915cd016946ffb5798fc1b6aa7663a2f1ffba7b Mon Sep 17 00:00:00 2001 From: Ben Kibbey Date: Sun, 16 May 2010 16:01:55 -0400 Subject: [PATCH] When waiting for the file mutex to be acquired also wait for an FD event to determine whether the client disconnected while waiting for the lock. This fixes sending the STATUS_CLIENTS message to other connected clients rather than having to wait for another connection/disconnection. --- src/commands.c | 2 +- src/lock.h | 21 ++++++++++++++++----- src/pinentry.c | 2 +- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/commands.c b/src/commands.c index ff3b2e14..6cb89a03 100644 --- a/src/commands.c +++ b/src/commands.c @@ -156,7 +156,7 @@ gpg_error_t lock_file_mutex(struct client_s *client) #endif } else - MUTEX_TRYLOCK(client->ctx, m, rc); + MUTEX_TRYLOCK(client, m, rc); if (!rc) client->has_lock = TRUE; diff --git a/src/lock.h b/src/lock.h index 91445e8e..87e97deb 100644 --- a/src/lock.h +++ b/src/lock.h @@ -51,18 +51,29 @@ assert(0); \ } -#define MUTEX_TRYLOCK(ctx, m, rc) \ +#define MUTEX_LOCK_EV(client, m, rc) { \ + pth_event_t ev = pth_event(PTH_EVENT_FD|PTH_UNTIL_FD_READABLE, \ + client->thd->fd); \ + MUTEX_LOCK_DEBUG(m) \ + if (!pth_mutex_acquire(m, FALSE, ev)) { \ + rc = gpg_error_from_errno(errno); \ + log_write("%s(%i): LOCK: %s", __FILE__, __LINE__, pwmd_strerror(rc));\ + } \ + pth_event_free(ev, PTH_FREE_ALL); \ +} + +#define MUTEX_TRYLOCK(client, m, rc) \ if (!pth_mutex_acquire(m, TRUE, NULL)) { \ if (errno == EBUSY) { \ - if (ctx) {\ - rc = send_status(ctx, STATUS_LOCKED, NULL); \ + if (client && client->ctx) {\ + rc = send_status(client->ctx, STATUS_LOCKED, NULL); \ if (!rc) { \ - MUTEX_LOCK(m); \ + MUTEX_LOCK_EV(client, m, rc); \ } \ } \ } \ else { \ - log_write("%s(%i): %s: LOCK", __FILE__, __LINE__, __FUNCTION__); \ + log_write("%s(%i): %s: LOCK errno=%i", __FILE__, __LINE__, __FUNCTION__, errno); \ assert(0); \ } \ } \ diff --git a/src/pinentry.c b/src/pinentry.c index 157ed37e..349f848a 100644 --- a/src/pinentry.c +++ b/src/pinentry.c @@ -569,7 +569,7 @@ gpg_error_t lock_pin_mutex(struct client_s *client) { gpg_error_t rc = 0; - MUTEX_TRYLOCK(client->ctx, &pin_mutex, rc); + MUTEX_TRYLOCK(client, &pin_mutex, rc); if (!rc) client->pinentry->has_lock = TRUE; -- 2.11.4.GIT