From 78e9d8bfe0421bd046a33153c8e299c0bdaba649 Mon Sep 17 00:00:00 2001 From: Ben Kibbey Date: Mon, 25 Feb 2008 22:09:55 -0500 Subject: [PATCH] Use a pth_msgport_t event instead of signals in child_thread() to send status messages to the client. This removes the SIGUSR2 and SIGALRM signal events. --- src/common.h | 2 ++ src/pwmd.c | 90 ++++++++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 62 insertions(+), 30 deletions(-) diff --git a/src/common.h b/src/common.h index 0d880dce..c17e4447 100644 --- a/src/common.h +++ b/src/common.h @@ -92,6 +92,8 @@ struct client_thread_s { gint fd; pth_event_t ev; pth_t keepalive_tid; + pth_msgport_t msg; + gchar *msg_name; struct client_s *cl; }; diff --git a/src/pwmd.c b/src/pwmd.c index 89fd704c..63967750 100644 --- a/src/pwmd.c +++ b/src/pwmd.c @@ -285,19 +285,20 @@ void send_status_all(status_msg_t which) for (t = g_slist_length(cn_thread_list), i = 0; i < t; i++) { struct client_thread_s *cn = g_slist_nth_data(cn_thread_list, i); + pth_msgport_t m; if (cn->tid == pth_self()) { send_status(cn->cl->ctx, which); continue; } - switch (which) { - case STATUS_CACHE: - pth_raise(cn->tid, SIGUSR2); - break; - case STATUS_CONFIG: - pth_raise(cn->tid, SIGALRM); - break; + m = pth_msgport_find(cn->msg_name); + + if (m) { + pth_message_t *msg = g_malloc0(sizeof(pth_message_t)); + + msg->m_data = (status_msg_t *)which; + pth_msgport_put(m, msg); } } @@ -381,6 +382,20 @@ static void cleanup_cb(void *arg) g_free(cl); pth_event_isolate(cn->ev); pth_event_free(cn->ev, PTH_FREE_THIS); + + if (cn->msg_name) + g_free(cn->msg_name); + + if (cn->msg) { + while (pth_msgport_pending(cn->msg) > 0) { + pth_message_t *m = pth_msgport_get(cn->msg); + + g_free(m); + } + + pth_msgport_destroy(cn->msg); + } + cn_thread_list = g_slist_remove(cn_thread_list, cn); g_free(cn); pth_mutex_release(&cn_mutex); @@ -394,7 +409,7 @@ static void *client_thread(void *data) { struct client_thread_s *thd = data; gint fd = thd->fd; - pth_event_t ev; + pth_event_t ev, msg_ev; #ifdef WITH_PINENTRY pth_event_t pinentry_ev = NULL; #endif @@ -422,6 +437,19 @@ static void *client_thread(void *data) thd->cl = cl; cl->thd = thd; pth_cleanup_push(cleanup_cb, thd); + thd->msg_name = g_strdup_printf("%p", thd->tid); + + if (!thd->msg_name) { + log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM)); + goto fail; + } + + thd->msg = pth_msgport_create(thd->msg_name); + + if (!thd->msg) { + log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM)); + goto fail; + } /* * This is a "child" thread. Don't catch any signals. Let the master @@ -430,10 +458,6 @@ static void *client_thread(void *data) sigfillset(&set); pth_sigmask(SIG_BLOCK, &set, NULL); sigemptyset(&set); - /* Sends a CACHE status message to the client. */ - sigaddset(&set, SIGUSR2); - /* Send a CONFIG status message. */ - sigaddset(&set, SIGALRM); /* Terminates this thread. Raised from keepalive_thread(). */ sigaddset(&set, SIGQUIT); @@ -460,6 +484,8 @@ static void *client_thread(void *data) } ev = pth_event(PTH_EVENT_FD|PTH_UNTIL_FD_READABLE, cl->fd); + msg_ev = pth_event(PTH_EVENT_MSG, thd->msg); + pth_event_concat(ev, msg_ev, NULL); attr = pth_attr_new(); pth_attr_init(attr); pth_attr_set(attr, PTH_ATTR_NAME, "keepalive"); @@ -476,24 +502,6 @@ static void *client_thread(void *data) if (sig > 0) { switch (sig) { - case SIGALRM: - rc = send_status(cl->ctx, STATUS_CONFIG); - - if (rc) { - log_write("%s", gpg_strerror(rc)); - goto done; - } - - break; - case SIGUSR2: - rc = send_status(cl->ctx, STATUS_CACHE); - - if (rc) { - log_write("%s", gpg_strerror(rc)); - goto done; - } - - break; case SIGQUIT: /* pth_raise() from keepalive_thread(). */ goto done; @@ -503,6 +511,28 @@ static void *client_thread(void *data) } pth_event_isolate(ev); + pth_event_isolate(msg_ev); + + if (pth_event_occurred(msg_ev)) { + pth_event_free(msg_ev, PTH_FREE_THIS); + + while (pth_msgport_pending(thd->msg) > 0) { + pth_message_t *m = pth_msgport_get(thd->msg); + status_msg_t n = (status_msg_t)m->m_data; + + rc = send_status(cl->ctx, n); + g_free(m); + + if (rc) { + log_write("%s", gpg_strerror(rc)); + goto done; + } + } + + msg_ev = pth_event(PTH_EVENT_MSG, thd->msg); + } + + pth_event_concat(ev, msg_ev, NULL); if (pth_event_occurred(ev)) { rc = assuan_process_next(cl->ctx); -- 2.11.4.GIT