From nobody Mon Sep 16 19:34:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=quarantine dis=quarantine) header.from=suse.com Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1707130664316648.5596501362279; Mon, 5 Feb 2024 02:57:44 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.675832.1051416 (Exim 4.92) (envelope-from ) id 1rWwfE-0001Y9-MM; Mon, 05 Feb 2024 10:57:24 +0000 Received: by outflank-mailman (output) from mailman id 675832.1051416; Mon, 05 Feb 2024 10:57:24 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rWwfE-0001XS-Hu; Mon, 05 Feb 2024 10:57:24 +0000 Received: by outflank-mailman (input) for mailman id 675832; Mon, 05 Feb 2024 10:57:22 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rWwaE-0002wU-HF for xen-devel@lists.xenproject.org; Mon, 05 Feb 2024 10:52:14 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 9ea7bc5c-c414-11ee-8a45-1f161083a0e0; Mon, 05 Feb 2024 11:52:13 +0100 (CET) Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 736251F8BA; Mon, 5 Feb 2024 10:52:13 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 41B7E132DD; Mon, 5 Feb 2024 10:52:13 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id EQSNDt29wGXQNgAAD6G6ig (envelope-from ); Mon, 05 Feb 2024 10:52:13 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 9ea7bc5c-c414-11ee-8a45-1f161083a0e0 From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH v4 23/32] tools/xenstored: move all log-pipe handling into posix.c Date: Mon, 5 Feb 2024 11:49:52 +0100 Message-Id: <20240205105001.24171-24-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20240205105001.24171-1-jgross@suse.com> References: <20240205105001.24171-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spam-Level: Authentication-Results: smtp-out2.suse.de; none X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Spamd-Result: default: False [-4.00 / 50.00]; REPLY(-4.00)[] X-Spam-Score: -4.00 X-Rspamd-Queue-Id: 736251F8BA X-Spam-Flag: NO X-ZM-MESSAGEID: 1707130665152100003 Content-Type: text/plain; charset="utf-8" All of the log-pipe handling is needed only when running as daemon. Move it into posix.c. This requires to have a service function in the main event loop for handling the related requests and one for setting the fds[] array. Use a generic name for those functions, as socket handling can be added to them later, too. Signed-off-by: Juergen Gross Reviewed-by: Julien Grall --- V3: - new patch V4: - rename fds to poll_fds (Julien Grall) --- tools/xenstored/core.c | 69 +++++++++++----------------------------- tools/xenstored/core.h | 11 ++++--- tools/xenstored/minios.c | 14 ++++---- tools/xenstored/posix.c | 47 ++++++++++++++++++++++++++- 4 files changed, 79 insertions(+), 62 deletions(-) diff --git a/tools/xenstored/core.c b/tools/xenstored/core.c index 7b5e1d0c0f..86ab330eaf 100644 --- a/tools/xenstored/core.c +++ b/tools/xenstored/core.c @@ -56,7 +56,7 @@ =20 extern xenevtchn_handle *xce_handle; /* in domain.c */ static int xce_pollfd_idx =3D -1; -static struct pollfd *fds; +struct pollfd *poll_fds; static unsigned int current_array_size; static unsigned int nr_fds; static unsigned int delayed_requests; @@ -69,8 +69,6 @@ char **orig_argv; LIST_HEAD(connections); int tracefd =3D -1; bool keep_orphans =3D false; -int reopen_log_pipe[2]; -static int reopen_log_pipe0_pollfd_idx =3D -1; char *tracefile =3D NULL; static struct hashtable *nodes; unsigned int trace_flags =3D TRACE_OBJ | TRACE_IO; @@ -145,18 +143,6 @@ void trace_destroy(const void *data, const char *type) trace("obj: DESTROY %s %p\n", type, data); } =20 -/** - * Signal handler for SIGHUP, which requests that the trace log is reopened - * (in the main loop). A single byte is written to reopen_log_pipe, to aw= aken - * the poll() in the main loop. - */ -static void trigger_reopen_log(int signal __attribute__((unused))) -{ - char c =3D 'A'; - int dummy; - dummy =3D write(reopen_log_pipe[1], &c, 1); -} - void close_log(void) { if (tracefd >=3D 0) @@ -467,7 +453,7 @@ static bool conn_can_write(struct connection *conn) } =20 /* This function returns index inside the array if succeed, -1 if fail */ -static int set_fd(int fd, short events) +int set_fd(int fd, short events) { int ret; if (current_array_size < nr_fds + 1) { @@ -479,18 +465,18 @@ static int set_fd(int fd, short events) */ newsize =3D ROUNDUP(nr_fds + 1, 8); =20 - new_fds =3D realloc(fds, sizeof(struct pollfd)*newsize); + new_fds =3D realloc(poll_fds, sizeof(struct pollfd)*newsize); if (!new_fds) goto fail; - fds =3D new_fds; + poll_fds =3D new_fds; =20 - memset(&fds[0] + current_array_size, 0, + memset(&poll_fds[0] + current_array_size, 0, sizeof(struct pollfd ) * (newsize-current_array_size)); current_array_size =3D newsize; } =20 - fds[nr_fds].fd =3D fd; - fds[nr_fds].events =3D events; + poll_fds[nr_fds].fd =3D fd; + poll_fds[nr_fds].events =3D events; ret =3D nr_fds; nr_fds++; =20 @@ -505,18 +491,16 @@ static void initialize_fds(int *p_sock_pollfd_idx, in= t *ptimeout) struct connection *conn; uint64_t msecs; =20 - if (fds) - memset(fds, 0, sizeof(struct pollfd) * current_array_size); + if (poll_fds) + memset(poll_fds, 0, sizeof(struct pollfd) * current_array_size); nr_fds =3D 0; =20 /* In case of delayed requests pause for max 1 second. */ *ptimeout =3D delayed_requests ? 1000 : -1; =20 + set_special_fds(); if (sock !=3D -1) *p_sock_pollfd_idx =3D set_fd(sock, POLLIN|POLLPRI); - if (reopen_log_pipe[0] !=3D -1) - reopen_log_pipe0_pollfd_idx =3D - set_fd(reopen_log_pipe[0], POLLIN|POLLPRI); =20 if (xce_handle !=3D NULL) xce_pollfd_idx =3D set_fd(xenevtchn_fd(xce_handle), @@ -2324,12 +2308,12 @@ static bool socket_can_process(struct connection *c= onn, int mask) if (conn->pollfd_idx =3D=3D -1) return false; =20 - if (fds[conn->pollfd_idx].revents & ~(POLLIN | POLLOUT)) { + if (poll_fds[conn->pollfd_idx].revents & ~(POLLIN | POLLOUT)) { talloc_free(conn); return false; } =20 - return (fds[conn->pollfd_idx].revents & mask); + return (poll_fds[conn->pollfd_idx].revents & mask); } =20 static bool socket_can_write(struct connection *conn) @@ -2897,8 +2881,6 @@ int main(int argc, char *argv[]) =20 talloc_enable_null_tracking(); =20 - init_pipe(reopen_log_pipe); - /* Listen to hypervisor. */ if (!live_update) { domain_init(-1); @@ -2913,7 +2895,6 @@ int main(int argc, char *argv[]) xprintf =3D trace; #endif =20 - signal(SIGHUP, trigger_reopen_log); if (tracefile) tracefile =3D talloc_strdup(NULL, tracefile); =20 @@ -2934,43 +2915,29 @@ int main(int argc, char *argv[]) for (;;) { struct connection *conn, *next; =20 - if (poll(fds, nr_fds, timeout) < 0) { + if (poll(poll_fds, nr_fds, timeout) < 0) { if (errno =3D=3D EINTR) continue; barf_perror("Poll failed"); } =20 - if (reopen_log_pipe0_pollfd_idx !=3D -1) { - if (fds[reopen_log_pipe0_pollfd_idx].revents - & ~POLLIN) { - close(reopen_log_pipe[0]); - close(reopen_log_pipe[1]); - init_pipe(reopen_log_pipe); - } else if (fds[reopen_log_pipe0_pollfd_idx].revents - & POLLIN) { - char c; - if (read(reopen_log_pipe[0], &c, 1) !=3D 1) - barf_perror("read failed"); - reopen_log(); - } - reopen_log_pipe0_pollfd_idx =3D -1; - } + handle_special_fds(); =20 if (sock_pollfd_idx !=3D -1) { - if (fds[sock_pollfd_idx].revents & ~POLLIN) { + if (poll_fds[sock_pollfd_idx].revents & ~POLLIN) { barf_perror("sock poll failed"); break; - } else if (fds[sock_pollfd_idx].revents & POLLIN) { + } else if (poll_fds[sock_pollfd_idx].revents & POLLIN) { accept_connection(sock); sock_pollfd_idx =3D -1; } } =20 if (xce_pollfd_idx !=3D -1) { - if (fds[xce_pollfd_idx].revents & ~POLLIN) { + if (poll_fds[xce_pollfd_idx].revents & ~POLLIN) { barf_perror("xce_handle poll failed"); break; - } else if (fds[xce_pollfd_idx].revents & POLLIN) { + } else if (poll_fds[xce_pollfd_idx].revents & POLLIN) { handle_event(); xce_pollfd_idx =3D -1; } diff --git a/tools/xenstored/core.h b/tools/xenstored/core.h index 0de2d8a26e..44c4d0f8b8 100644 --- a/tools/xenstored/core.h +++ b/tools/xenstored/core.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -361,6 +362,8 @@ extern int dom0_event; extern int priv_domid; extern bool keep_orphans; =20 +extern struct pollfd *poll_fds; + extern unsigned int timeout_watch_event_msec; =20 /* Get internal time in milliseconds. */ @@ -387,15 +390,15 @@ evtchn_port_t get_xenbus_evtchn(void); void early_init(bool live_update, bool dofork, const char *pidfile); void late_init(bool live_update); =20 +int set_fd(int fd, short events); +void set_special_fds(void); +void handle_special_fds(void); + void init_sockets(void); -extern int reopen_log_pipe[2]; =20 /* Close stdin/stdout/stderr to complete daemonize */ void finish_daemonize(void); =20 -/* Open a pipe for signal handling */ -void init_pipe(int reopen_log_pipe[2]); - #ifndef NO_SOCKETS extern const struct interface_funcs socket_funcs; #endif diff --git a/tools/xenstored/minios.c b/tools/xenstored/minios.c index 45bb0440be..3208169187 100644 --- a/tools/xenstored/minios.c +++ b/tools/xenstored/minios.c @@ -24,12 +24,6 @@ void finish_daemonize(void) { } =20 -void init_pipe(int reopen_log_pipe[2]) -{ - reopen_log_pipe[0] =3D -1; - reopen_log_pipe[1] =3D -1; -} - evtchn_port_t get_xenbus_evtchn(void) { return dom0_event; @@ -53,3 +47,11 @@ void early_init(bool live_update, bool dofork, const cha= r *pidfile) void late_init(bool live_update) { } + +void set_special_fds(void) +{ +} + +void handle_special_fds(void) +{ +} diff --git a/tools/xenstored/posix.c b/tools/xenstored/posix.c index 97c8dcaba3..1ea8475293 100644 --- a/tools/xenstored/posix.c +++ b/tools/xenstored/posix.c @@ -33,6 +33,9 @@ #include "core.h" #include "osdep.h" =20 +static int reopen_log_pipe0_pollfd_idx =3D -1; +static int reopen_log_pipe[2]; + static void write_pidfile(const char *pidfile) { char buf[100]; @@ -82,6 +85,19 @@ static void daemonize(void) umask(0); } =20 +/* + * Signal handler for SIGHUP, which requests that the trace log is reopened + * (in the main loop). A single byte is written to reopen_log_pipe, to aw= aken + * the poll() in the main loop. + */ +static void trigger_reopen_log(int signal __attribute__((unused))) +{ + char c =3D 'A'; + int dummy; + + dummy =3D write(reopen_log_pipe[1], &c, 1); +} + void finish_daemonize(void) { int devnull =3D open("/dev/null", O_RDWR); @@ -93,7 +109,7 @@ void finish_daemonize(void) close(devnull); } =20 -void init_pipe(int reopen_log_pipe[2]) +static void init_pipe(void) { int flags; unsigned int i; @@ -183,9 +199,38 @@ void early_init(bool live_update, bool dofork, const c= har *pidfile) =20 /* Don't kill us with SIGPIPE. */ signal(SIGPIPE, SIG_IGN); + signal(SIGHUP, trigger_reopen_log); =20 if (!live_update) init_sockets(); + + init_pipe(); +} + +void set_special_fds(void) +{ + if (reopen_log_pipe[0] !=3D -1) + reopen_log_pipe0_pollfd_idx =3D + set_fd(reopen_log_pipe[0], POLLIN|POLLPRI); +} + +void handle_special_fds(void) +{ + if (reopen_log_pipe0_pollfd_idx !=3D -1) { + if (poll_fds[reopen_log_pipe0_pollfd_idx].revents & ~POLLIN) { + close(reopen_log_pipe[0]); + close(reopen_log_pipe[1]); + init_pipe(); + } else if (poll_fds[reopen_log_pipe0_pollfd_idx].revents & + POLLIN) { + char c; + + if (read(reopen_log_pipe[0], &c, 1) !=3D 1) + barf_perror("read failed"); + reopen_log(); + } + reopen_log_pipe0_pollfd_idx =3D -1; + } } =20 void late_init(bool live_update) --=20 2.35.3