From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from atuin.qyliss.net (localhost [IPv6:::1]) by atuin.qyliss.net (Postfix) with ESMTP id 291841C8F0; Fri, 03 Oct 2025 21:43:39 +0000 (UTC) Received: by atuin.qyliss.net (Postfix, from userid 993) id 60C7D1C83C; Fri, 03 Oct 2025 21:43:37 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on atuin.qyliss.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DMARC_PASS,FREEMAIL_FROM,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=4.0.1 Received: from mail-yx1-xb132.google.com (mail-yx1-xb132.google.com [IPv6:2607:f8b0:4864:20::b132]) by atuin.qyliss.net (Postfix) with ESMTPS id AD6241C836 for ; Fri, 03 Oct 2025 21:43:35 +0000 (UTC) Received: by mail-yx1-xb132.google.com with SMTP id 956f58d0204a3-6354a4b4871so3461762d50.2 for ; Fri, 03 Oct 2025 14:43:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1759527814; x=1760132614; darn=spectrum-os.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=vXzUZh/hYSswSbbzR5n/UhD8D5CCL1g8yROUcOX8+Ls=; b=GDFBQrVNRm5LK16ek/mEVVA9mEwLOIIDBHss/Onh/BZJKprcO2d2yMPiMJhNEx2GxP RGcGmG2g/dLwlBi0orrzPxyp5vegWNiWuzZRLMv9AbNYV0guVE/6zHe/vY03zubaDu5X JG1SeamzBxGxmJpua/KfqPcESWTORMtl0gd1wrpBi9K2C/cCTxVl9SCoevMX2bEbfSTT TCur82AOAFAKcK/xKSJOIkfvim0N7UWQfU8jL4o/XkslXxV7/pPERvtHi2BddKuNP4XV u8QqnzCj00gwrLkOEv1iMLrEW+WlvGIFEcnViI/bkHyCi2+nrSsACYsI2a4+UwiAUekK Y+SQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759527814; x=1760132614; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vXzUZh/hYSswSbbzR5n/UhD8D5CCL1g8yROUcOX8+Ls=; b=qNoMOh7bqyThWnFsjhMcqHCLUp09hYS/gv8VstSVO7zLCg02WbJbl6Eeg1uEIRYmV4 3LB8/ssFKLEKjXKuAdOQWM07rh1f3w3qi0crNqKPEZGFTYa+4UCKSzB8t3f4aI3UNF+g 4R/d7eBNVEDw4sUiAyc2COtXZ/sK/utOA3KQ/j9/7Ej0qZBTKanxQMN6WomkOlkj/nA6 CaBhV+8fvrLpoq00ICdbwrOdzHYa8d+BUORLIT8rHXxLhZuYXy3MZ/Ycn/rsSGE1YlHv fBOa1672Cxnay+wNjjuh4U1XKwCaTyS3+vLvnNs3h8JpHjLj9OY1ZjnZbKoyrE19CQZK rvJg== X-Gm-Message-State: AOJu0Yww0p9L8kG+deq4kND3s2bCzAXa2vQtcb/agfZkGfAYZIuQdHPV jMVsluHEqq9laeSenxUjmEeDZrtghdtkL1icqiDJrclmaM5Svgp/ntQbjrpzzCWy X-Gm-Gg: ASbGncuqXYGITeFXkojFtlkgVl/qq0vnJuDZm+NLZLT1UyTaujQid91CjZ0DPme73G3 Rbh0pmg+u2gpYk/0RfwZY8FyKpCTwlSvWal0TiOCJWCrI0aIAnel+knZ8esctZ9ACjGPjGZjUwo YKWGedD4gxNMAGJ/ymWGoGQ00BwWmbfLfO3hjI0+ibF3NUjDUad6VuUGakfhkYTaXQ/yorjK55l 00zwGRu1dxk9WrF5IjYMur71FPak0qXCjYbccUwNcX00QcnHhIkg4HvhAOEuR/5XIzrIVOiX+aZ M11PoaPrBFPZjya8JFuRbVrNHB7m9BNzFLwu/ubKJkRSIZkisps/kdsyQ/OQi8H0YjgXMkzlqLk KtvHkmIQFKf/QPtvWwRjFpK6ta3VwXNFbKB4Q/DqYJtngsbJQvUdvk7G3A0/8V9zsIGLiE2J2v7 /H4ba+4wfFlnR6w5XVkw8W3bVZf/WenZ5PGHWAKLCsJHFnYK/safTbtokqLdj43vP1PuI= X-Google-Smtp-Source: AGHT+IGuSr/VEybW8c2XXHjSruhvSTEJyWykj1sHnu7ZUYCr6ETSLgGT8+qfFgdXaaen0EwbRkMDJQ== X-Received: by 2002:a05:690e:18e:b0:5fc:53ab:a49c with SMTP id 956f58d0204a3-63b9a05d196mr2453146d50.11.1759527814123; Fri, 03 Oct 2025 14:43:34 -0700 (PDT) Received: from localhost.localdomain (h96-60-249-169.cncrtn.broadband.dynamic.tds.net. [96.60.249.169]) by smtp.gmail.com with UTF8SMTPSA id 00721157ae682-77fa13b612asm5852467b3.65.2025.10.03.14.43.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Oct 2025 14:43:33 -0700 (PDT) From: Demi Marie Obenour Date: Fri, 03 Oct 2025 17:42:55 -0400 Subject: [PATCH v4 1/2] tools: Add adapter tool for services using sd_notify MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20251003-udev-v4-1-7d7344b14d11@gmail.com> References: <20251003-udev-v4-0-7d7344b14d11@gmail.com> In-Reply-To: <20251003-udev-v4-0-7d7344b14d11@gmail.com> To: Spectrum OS Development X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1759527774; l=5815; i=demiobenour@gmail.com; s=20250729; h=from:subject:message-id; bh=qZkMxTt5Xwm9d8hsaa/Nnr/bbStPd1iXQuiNpnx3Upg=; b=oaFbWwJoSSfAcdeb54wff1nixdiEdxC5WycBd0tvthhnO4NXpUcIaWNxCsir0Nhb7zyndkPtI Pp3KgrsEZx3BtEyFYvaH7LKE9rvmpPLwgt3+lMdQ9CWzJPH0cYJh3jW X-Developer-Key: i=demiobenour@gmail.com; a=ed25519; pk=X57Q4/YQDj9t4SBeKaDwvXYKB6quZJVx/DE2Ly2out0= Message-ID-Hash: 3EONEJMQTKP2VHOMFYVJPVOQ4NTYOL4K X-Message-ID-Hash: 3EONEJMQTKP2VHOMFYVJPVOQ4NTYOL4K X-MailFrom: demiobenour@gmail.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-devel.spectrum-os.org-0; header-match-devel.spectrum-os.org-1; header-match-devel.spectrum-os.org-2; header-match-devel.spectrum-os.org-3; header-match-devel.spectrum-os.org-4; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Demi Marie Obenour , Alyssa Ross X-Mailman-Version: 3.3.9 Precedence: list List-Id: Patches and low-level development discussion Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: This adapts programs using sd_notify for use with s6 readiness notification. stdin and stdout are hard-coded for simplicity. Signed-off-by: Demi Marie Obenour --- systemd readiness notification has two strict advantages over the s6 version: 1. It allows reliable reloading. 2. It allows providing a status message that the service manager can show in status output. s6 would actually benefit from both of these features. --- Changes since v1: - Hard-code file descriptors. - Run wrapper as background process. - Massively reduce code size. - Use // instead of /* */ for comments. - Check that the notification FD is a pipe and that the listening socket is a socket. - Rely on s6-ipc-socketbinder to create the listening socket. - Do not unlink the listening socket. --- tools/default.nix | 1 + tools/meson.build | 1 + tools/sd-notify-adapter/meson.build | 4 + tools/sd-notify-adapter/sd-notify-adapter.c | 114 ++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+) diff --git a/tools/default.nix b/tools/default.nix index 2c6846c80073e7b64fb7a19488103f6cf97a4420..4971e8bf6d84163b665ba0fb2af570cafa3171f5 100644 --- a/tools/default.nix +++ b/tools/default.nix @@ -77,6 +77,7 @@ stdenv.mkDerivation (finalAttrs: { ./lsvm ./start-vmm ./subprojects + ./sd-notify-adapter ] ++ lib.optionals driverSupport [ ./xdp-forwarder ])); diff --git a/tools/meson.build b/tools/meson.build index 186008dbc9dd2b63adbce7475c375fb0de5c2c6a..5d0ae81042fd3d77646594500f32cb1d48a6af0c 100644 --- a/tools/meson.build +++ b/tools/meson.build @@ -27,6 +27,7 @@ if get_option('host') subdir('lsvm') subdir('start-vmm') + subdir('sd-notify-adapter') endif if get_option('app') diff --git a/tools/sd-notify-adapter/meson.build b/tools/sd-notify-adapter/meson.build new file mode 100644 index 0000000000000000000000000000000000000000..6032a3a7704d49cae0655b43d0189444d3b15e4d --- /dev/null +++ b/tools/sd-notify-adapter/meson.build @@ -0,0 +1,4 @@ +# SPDX-License-Identifier: ISC +# SPDX-FileCopyrightText: 2025 Demi Marie Obenour + +executable('sd-notify-adapter', 'sd-notify-adapter.c', install: true) diff --git a/tools/sd-notify-adapter/sd-notify-adapter.c b/tools/sd-notify-adapter/sd-notify-adapter.c new file mode 100644 index 0000000000000000000000000000000000000000..10f4e05eb602491540a792c7fb5620d66d5bb989 --- /dev/null +++ b/tools/sd-notify-adapter/sd-notify-adapter.c @@ -0,0 +1,114 @@ +// SPDX-License-Identifier: MIT +// SPDX-FileCopyrightText: 2025 Demi Marie Obenour + +#define _GNU_SOURCE 1 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#define ARRAY_SIZE(s) (sizeof(s)/sizeof(s[0])) + +enum { + socket_fd, + notification_fd, +}; + +#define READY "READY=1" +#define READY_SIZE (sizeof(READY) - 1) + +static void process_notification(struct iovec *const msg) +{ + ssize_t first_recv_size = recv(socket_fd, msg->iov_base, msg->iov_len, + MSG_DONTWAIT | MSG_TRUNC | MSG_PEEK); + if (first_recv_size == -1) { + if (errno == EINTR) + return; // signal caught + if (errno == EAGAIN || errno == EWOULDBLOCK) + return; // spurious wakeup + err(EXIT_FAILURE, "recv from notification socket"); + } + assert(first_recv_size >= 0); + size_t size = (size_t)first_recv_size; + if (size == 0) + return; // avoid arithmetic on NULL pointer + if (size > msg->iov_len) { + msg->iov_base = realloc(msg->iov_base, size); + if (msg->iov_base == NULL) + err(EXIT_FAILURE, "allocation failure"); + msg->iov_len = size; + } + ssize_t second_recv_size = recv(socket_fd, msg->iov_base, msg->iov_len, + MSG_CMSG_CLOEXEC | MSG_TRUNC); + if (second_recv_size == -1) { + if (errno == EINTR) + return; + err(EXIT_FAILURE, "recv from notification socket"); + } + assert(first_recv_size == second_recv_size); + for (char *next, *cursor = msg->iov_base, *end = cursor + size; + cursor != NULL; cursor = (next == NULL ? NULL : next + 1)) { + next = memchr(cursor, '\n', (size_t)(end - cursor)); + size_t message_size = (size_t)((next == NULL ? end : next) - cursor); + if (message_size == READY_SIZE && + memcmp(cursor, READY, READY_SIZE) == 0) { + ssize_t write_size = write(notification_fd, "\n", 1); + if (write_size != 1) + err(EXIT_FAILURE, "writing to notification descriptor"); + exit(0); + } + } +} + +int main(int argc, char **) +{ + if (argc != 1) + errx(EXIT_FAILURE, "stdin is listening socket, stdout is notification pipe"); + // Main event loop. + struct iovec v = { + .iov_base = NULL, + .iov_len = 0, + }; + for (;;) { + struct pollfd p[] = { + { + .fd = socket_fd, + .events = POLLIN, + .revents = 0, + }, + { + .fd = notification_fd, + .events = 0, + .revents = 0, + }, + }; + int r = poll(p, ARRAY_SIZE(p), -1); + if (r < 0) { + if (errno == EINTR) + continue; + err(EXIT_FAILURE, "poll"); + } + if (p[0].revents) { + if (p[0].revents & POLLERR) + errx(EXIT_FAILURE, "unexpected POLLERR"); + if (p[0].revents & POLLIN) + process_notification(&v); + } + if (p[1].revents) + errx(EXIT_FAILURE, "s6 closed its pipe before the child was ready"); + } +} -- 2.51.0