From f561e2cafe166c4665f49bcb627204c49388c0af Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Sun, 1 Mar 2020 13:36:59 -0700 Subject: [PATCH] Store the event base in the client closure. Explicitly passing the event base removes the need to set a default base. --- logsrvd/logsrvd.c | 45 ++++++++++++++++++++++----------------------- logsrvd/logsrvd.h | 1 + logsrvd/sendlog.c | 23 ++++++++++++----------- logsrvd/sendlog.h | 1 + 4 files changed, 36 insertions(+), 34 deletions(-) diff --git a/logsrvd/logsrvd.c b/logsrvd/logsrvd.c index c4434d956..54135a46d 100644 --- a/logsrvd/logsrvd.c +++ b/logsrvd/logsrvd.c @@ -109,7 +109,7 @@ connection_closure_free(struct connection_closure *closure) sudo_ev_free(closure->read_ev); sudo_ev_free(closure->write_ev); #if defined(HAVE_OPENSSL) - sudo_ev_free(closure->ssl_accept_ev); + sudo_ev_free(closure->ssl_accept_ev); #endif iolog_details_free(&closure->details); free(closure->read_buf.data); @@ -117,7 +117,7 @@ connection_closure_free(struct connection_closure *closure) free(closure); if (shutting_down && TAILQ_EMPTY(&connections)) - sudo_ev_loopbreak(NULL); + sudo_ev_loopbreak(closure->evbase); } debug_return; @@ -273,7 +273,7 @@ handle_accept(AcceptMessage *msg, struct connection_closure *closure) /* Send log ID to client for restarting connections. */ if (!fmt_log_id_message(closure->details.iolog_path, &closure->write_buf)) debug_return_bool(false); - if (sudo_ev_add(NULL, closure->write_ev, + if (sudo_ev_add(closure->evbase, closure->write_ev, logsrvd_conf_get_sock_timeout(), false) == -1) { sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, "unable to add server write event"); @@ -356,7 +356,7 @@ handle_exit(ExitMessage *msg, struct connection_closure *closure) /* No more data, command exited. */ closure->state = EXITED; - sudo_ev_del(NULL, closure->read_ev); + sudo_ev_del(closure->evbase, closure->read_ev); sudo_debug_printf(SUDO_DEBUG_INFO, "%s: elapsed time: %lld, %ld", __func__, (long long)closure->elapsed_time.tv_sec, @@ -371,7 +371,7 @@ handle_exit(ExitMessage *msg, struct connection_closure *closure) } /* Schedule the final commit point event immediately. */ - if (sudo_ev_add(NULL, closure->commit_ev, &tv, false) == -1) { + if (sudo_ev_add(closure->evbase, closure->commit_ev, &tv, false) == -1) { sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, "unable to add commit point event"); debug_return_bool(false); @@ -399,9 +399,9 @@ handle_restart(RestartMessage *msg, struct connection_closure *closure) /* XXX - structured error message so client can send from beginning */ if (!fmt_error_message(closure->errstr, &closure->write_buf)) debug_return_bool(false); - sudo_ev_del(NULL, closure->read_ev); - if (sudo_ev_add(NULL, closure->write_ev, - logsrvd_conf_get_sock_timeout(), false) == -1) { + sudo_ev_del(closure->evbase, closure->read_ev); + if (sudo_ev_add(closure->evbase, closure->write_ev, + logsrvd_conf_get_sock_timeout(), false) == -1) { sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, "unable to add server write event"); debug_return_bool(false); @@ -462,7 +462,7 @@ handle_iobuf(int iofd, IoBuffer *msg, struct connection_closure *closure) /* Schedule a commit point in 10 sec if one is not already pending. */ if (!ISSET(closure->commit_ev->flags, SUDO_EVQ_INSERTED)) { struct timespec tv = { ACK_FREQUENCY, 0 }; - if (sudo_ev_add(NULL, closure->commit_ev, &tv, false) == -1) { + if (sudo_ev_add(closure->evbase, closure->commit_ev, &tv, false) == -1) { sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, "unable to add commit point event"); debug_return_bool(false); @@ -660,7 +660,7 @@ server_msg_cb(int fd, int what, void *v) /* Delete write event if it was only due to SSL_read(). */ if (closure->temporary_write_event) { closure->temporary_write_event = false; - sudo_ev_del(NULL, closure->write_ev); + sudo_ev_del(closure->evbase, closure->write_ev); } client_msg_cb(fd, what, v); debug_return; @@ -721,7 +721,7 @@ server_msg_cb(int fd, int what, void *v) "%s: finished sending %u bytes to client", __func__, buf->len); buf->off = 0; buf->len = 0; - sudo_ev_del(NULL, closure->write_ev); + sudo_ev_del(closure->evbase, closure->write_ev); if (closure->state == FLUSHED || closure->state == SHUTDOWN || closure->state == ERROR) goto finished; @@ -780,7 +780,7 @@ client_msg_cb(int fd, int what, void *v) "SSL_read returns SSL_ERROR_WANT_WRITE"); if (!sudo_ev_pending(closure->write_ev, SUDO_EV_WRITE, NULL)) { /* Enable a temporary write event. */ - if (sudo_ev_add(NULL, closure->write_ev, + if (sudo_ev_add(closure->evbase, closure->write_ev, logsrvd_conf_get_sock_timeout(), false) == -1) { sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, "unable to add event to queue"); @@ -860,9 +860,9 @@ send_error: if (closure->errstr == NULL) goto finished; if (fmt_error_message(closure->errstr, &closure->write_buf)) { - sudo_ev_del(NULL, closure->read_ev); - if (sudo_ev_add(NULL, closure->write_ev, - logsrvd_conf_get_sock_timeout(), false) == -1) { + sudo_ev_del(closure->evbase, closure->read_ev); + if (sudo_ev_add(closure->evbase, closure->write_ev, + logsrvd_conf_get_sock_timeout(), false) == -1) { sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, "unable to add server write event"); } @@ -900,7 +900,7 @@ server_commit_cb(int unused, int what, void *v) "unable to format ServerMessage (commit point)"); goto bad; } - if (sudo_ev_add(NULL, closure->write_ev, + if (sudo_ev_add(closure->evbase, closure->write_ev, logsrvd_conf_get_sock_timeout(), false) == -1) { sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, "unable to add server write event"); @@ -1226,7 +1226,6 @@ static void tls_handshake_cb(int fd, int what, void *v) { struct connection_closure *closure = v; - struct sudo_event_base *base = closure->ssl_accept_ev->base; debug_decl(tls_handshake_cb, SUDO_DEBUG_UTIL); @@ -1256,7 +1255,7 @@ tls_handshake_cb(int fd, int what, void *v) goto bad; } } - if (sudo_ev_add(base, closure->ssl_accept_ev, + if (sudo_ev_add(closure->evbase, closure->ssl_accept_ev, logsrvd_conf_get_sock_timeout(), false) == -1) { sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, "unable to add ssl_accept_ev to queue"); @@ -1275,7 +1274,7 @@ tls_handshake_cb(int fd, int what, void *v) goto bad; } } - if (sudo_ev_add(base, closure->ssl_accept_ev, + if (sudo_ev_add(closure->evbase, closure->ssl_accept_ev, logsrvd_conf_get_sock_timeout(), false) == -1) { sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, "unable to add ssl_accept_ev to queue"); @@ -1291,7 +1290,7 @@ tls_handshake_cb(int fd, int what, void *v) } /* Enable reader for ClientMessage */ - if (sudo_ev_add(base, closure->read_ev, NULL, false) == -1) { + if (sudo_ev_add(closure->evbase, closure->read_ev, NULL, false) == -1) { sudo_warn(U_("unable to add event to queue")); } @@ -1311,7 +1310,7 @@ bad: * Allocate a new connection closure. */ static struct connection_closure * -connection_closure_alloc(int sock) +connection_closure_alloc(int sock, struct sudo_event_base *base) { struct connection_closure *closure; debug_decl(connection_closure_alloc, SUDO_DEBUG_UTIL); @@ -1321,6 +1320,7 @@ connection_closure_alloc(int sock) closure->iolog_dir_fd = -1; closure->sock = sock; + closure->evbase = base; TAILQ_INSERT_TAIL(&connections, closure, entries); @@ -1368,7 +1368,7 @@ new_connection(int sock, const struct sockaddr *sa, struct sudo_event_base *base debug_decl(new_connection, SUDO_DEBUG_UTIL); - if ((closure = connection_closure_alloc(sock)) == NULL) + if ((closure = connection_closure_alloc(sock, base)) == NULL) goto bad; /* Format and write ServerHello message. */ @@ -1782,7 +1782,6 @@ main(int argc, char *argv[]) if ((evbase = sudo_ev_base_alloc()) == NULL) sudo_fatal(NULL); - sudo_ev_base_setdef(evbase); /* Initialize listeners and TLS context. */ server_setup(evbase); diff --git a/logsrvd/logsrvd.h b/logsrvd/logsrvd.h index 9536d563e..73c95fa21 100644 --- a/logsrvd/logsrvd.h +++ b/logsrvd/logsrvd.h @@ -87,6 +87,7 @@ struct connection_closure { struct timespec elapsed_time; struct connection_buffer read_buf; struct connection_buffer write_buf; + struct sudo_event_base *evbase; struct sudo_event *commit_ev; struct sudo_event *read_ev; struct sudo_event *write_ev; diff --git a/logsrvd/sendlog.c b/logsrvd/sendlog.c index d78a139ac..d79c82d09 100644 --- a/logsrvd/sendlog.c +++ b/logsrvd/sendlog.c @@ -435,7 +435,7 @@ client_closure_free(struct client_closure *closure) * Initialize a new client closure */ static struct client_closure * -client_closure_alloc(int sock, +client_closure_alloc(int sock, struct sudo_event_base *base, struct timespec *elapsed, struct timespec *restart, const char *iolog_id, struct iolog_info *log_info) { @@ -446,6 +446,7 @@ client_closure_alloc(int sock, debug_return_ptr(NULL); closure->sock = sock; + closure->evbase = base; TAILQ_INSERT_TAIL(&connections, closure, entries); @@ -721,7 +722,7 @@ fmt_accept_message(struct client_closure *closure) client_msg.type_case = CLIENT_MESSAGE__TYPE_ACCEPT_MSG; ret = fmt_client_message(&closure->write_buf, &client_msg); if (ret) { - if (sudo_ev_add(NULL, closure->write_ev, NULL, false) == -1) + if (sudo_ev_add(closure->evbase, closure->write_ev, NULL, false) == -1) ret = false; } @@ -763,7 +764,7 @@ fmt_restart_message(struct client_closure *closure) client_msg.type_case = CLIENT_MESSAGE__TYPE_RESTART_MSG; ret = fmt_client_message(&closure->write_buf, &client_msg); if (ret) { - if (sudo_ev_add(NULL, closure->write_ev, NULL, false) == -1) + if (sudo_ev_add(closure->evbase, closure->write_ev, NULL, false) == -1) ret = false; } @@ -1017,7 +1018,7 @@ client_message_completion(struct client_closure *closure) break; case SEND_EXIT: /* Done writing, just waiting for final commit point. */ - sudo_ev_del(NULL, closure->write_ev); + sudo_ev_del(closure->evbase, closure->write_ev); closure->state = CLOSING; break; default: @@ -1185,10 +1186,10 @@ handle_server_message(uint8_t *buf, size_t len, case SERVER_MESSAGE__TYPE_COMMIT_POINT: ret = handle_commit_point(msg->commit_point, closure); if (sudo_timespeccmp(&closure->elapsed, &closure->committed, ==)) { - sudo_ev_del(NULL, closure->read_ev); + sudo_ev_del(closure->evbase, closure->read_ev); closure->state = FINISHED; if (++finished_transmissions == nr_of_conns) - sudo_ev_loopexit(NULL); + sudo_ev_loopexit(closure->evbase); } break; case SERVER_MESSAGE__TYPE_LOG_ID: @@ -1259,7 +1260,7 @@ server_msg_cb(int fd, int what, void *v) "SSL_read returns SSL_ERROR_WANT_WRITE"); if (!sudo_ev_pending(closure->write_ev, SUDO_EV_WRITE, NULL)) { /* Enable a temporary write event. */ - if (sudo_ev_add(NULL, closure->write_ev, NULL, false) == -1) { + if (sudo_ev_add(closure->evbase, closure->write_ev, NULL, false) == -1) { sudo_warnx(U_("unable to add event to queue")); goto bad; } @@ -1345,7 +1346,7 @@ client_msg_cb(int fd, int what, void *v) /* Delete write event if it was only due to SSL_read(). */ if (closure->temporary_write_event) { closure->temporary_write_event = false; - sudo_ev_del(NULL, closure->write_ev); + sudo_ev_del(closure->evbase, closure->write_ev); } server_msg_cb(fd, what, v); debug_return; @@ -1589,7 +1590,6 @@ main(int argc, char *argv[]) if ((evbase = sudo_ev_base_alloc()) == NULL) sudo_fatal(NULL); - sudo_ev_base_setdef(evbase); if (testrun) printf("connecting clients...\n"); @@ -1602,8 +1602,9 @@ main(int argc, char *argv[]) if (!testrun) printf("Connected to %s:%s\n", host, port); - closure = client_closure_alloc(sock, &elapsed, &restart, iolog_id, log_info); - if (!closure) + closure = client_closure_alloc(sock, evbase, &elapsed, &restart, + iolog_id, log_info); + if (closure == NULL) goto bad; /* Open the I/O log files and seek to restart point if there is one. */ diff --git a/logsrvd/sendlog.h b/logsrvd/sendlog.h index a5990505c..ed01b3d3f 100644 --- a/logsrvd/sendlog.h +++ b/logsrvd/sendlog.h @@ -50,6 +50,7 @@ struct client_closure { struct timespec elapsed; struct timespec committed; struct timing_closure timing; + struct sudo_event_base *evbase; struct connection_buffer read_buf; struct connection_buffer write_buf; #if defined(HAVE_OPENSSL)