From: Lennart Poettering Date: Fri, 10 Nov 2023 15:30:30 +0000 (+0100) Subject: util-lib: share plymouth client code X-Git-Tag: v255-rc2~30 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=aa25e19b474bb08ab607b363f83f0626a96c8d85;p=thirdparty%2Fsystemd.git util-lib: share plymouth client code Let's add a new "plymouth-util.c" module with helpers for talking to plymouth. We so far had three places for this, let's unify the code doing this a bit. --- diff --git a/src/basic/constants.h b/src/basic/constants.h index 98816d183c2..6bb5f3c2812 100644 --- a/src/basic/constants.h +++ b/src/basic/constants.h @@ -89,11 +89,6 @@ * in containers so that our children inherit that. */ #define DEFAULT_RLIMIT_MEMLOCK (1024ULL*1024ULL*8ULL) -#define PLYMOUTH_SOCKET { \ - .un.sun_family = AF_UNIX, \ - .un.sun_path = "\0/org/freedesktop/plymouthd", \ - } - /* Path where PID1 listens for varlink subscriptions from systemd-oomd to notify of changes in ManagedOOM settings. */ #define VARLINK_ADDR_PATH_MANAGED_OOM_SYSTEM "/run/systemd/io.systemd.ManagedOOM" /* Path where systemd-oomd listens for varlink connections from user managers to report changes in ManagedOOM settings. */ diff --git a/src/battery-check/battery-check.c b/src/battery-check/battery-check.c index 5d7c6a58ae5..03628c8b921 100644 --- a/src/battery-check/battery-check.c +++ b/src/battery-check/battery-check.c @@ -9,14 +9,14 @@ #include "battery-util.h" #include "build.h" -#include "constants.h" #include "errno-util.h" -#include "glyph-util.h" #include "fd-util.h" +#include "glyph-util.h" #include "io-util.h" #include "log.h" #include "main-func.h" #include "parse-util.h" +#include "plymouth-util.h" #include "pretty-print.h" #include "proc-cmdline.h" #include "socket-util.h" @@ -51,14 +51,8 @@ static int help(void) { return 0; } -static bool ERRNO_IS_NO_PLYMOUTH(int r) { - return IN_SET(abs(r), EAGAIN, ENOENT) || ERRNO_IS_DISCONNECT(r); -} - static int plymouth_send_message(const char *mode, const char *message) { - static const union sockaddr_union sa = PLYMOUTH_SOCKET; _cleanup_free_ char *plymouth_message = NULL; - _cleanup_close_ int fd = -EBADF; int c, r; assert(mode); @@ -72,20 +66,11 @@ static int plymouth_send_message(const char *mode, const char *message) { if (c < 0) return log_oom(); - /* We set SOCK_NONBLOCK here so that we rather drop the - * message than wait for plymouth */ - fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); - if (fd < 0) - return log_warning_errno(errno, "socket() failed: %m"); - - if (connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) - return log_full_errno(ERRNO_IS_NO_PLYMOUTH(errno) ? LOG_DEBUG : LOG_WARNING, errno, - "Failed to connect to plymouth: %m"); - - r = loop_write(fd, plymouth_message, c); + /* We set SOCK_NONBLOCK here so that we rather drop the message than wait for plymouth */ + r = plymouth_send_raw(plymouth_message, c, SOCK_NONBLOCK); if (r < 0) return log_full_errno(ERRNO_IS_NO_PLYMOUTH(r) ? LOG_DEBUG : LOG_WARNING, r, - "Failed to write to plymouth: %m"); + "Failed to communicate with plymouth: %m"); return 0; } diff --git a/src/core/manager.c b/src/core/manager.c index b974898cc7e..fcb7739b136 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -70,6 +70,7 @@ #include "parse-util.h" #include "path-lookup.h" #include "path-util.h" +#include "plymouth-util.h" #include "process-util.h" #include "psi-util.h" #include "ratelimit.h" @@ -3440,10 +3441,8 @@ void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) { } void manager_send_unit_plymouth(Manager *m, Unit *u) { - static const union sockaddr_union sa = PLYMOUTH_SOCKET; _cleanup_free_ char *message = NULL; - _cleanup_close_ int fd = -EBADF; - int n = 0; + int c, r; /* Don't generate plymouth events if the service was already * started and we're just deserializing */ @@ -3459,27 +3458,15 @@ void manager_send_unit_plymouth(Manager *m, Unit *u) { if (!UNIT_VTABLE(u)->notify_plymouth) return; - /* We set SOCK_NONBLOCK here so that we rather drop the - * message then wait for plymouth */ - fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); - if (fd < 0) { - log_error_errno(errno, "socket() failed: %m"); - return; - } - - if (connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) { - if (!IN_SET(errno, EAGAIN, ENOENT) && !ERRNO_IS_DISCONNECT(errno)) - log_error_errno(errno, "connect() failed: %m"); - return; - } - - if (asprintf(&message, "U\002%c%s%n", (int) (strlen(u->id) + 1), u->id, &n) < 0) + c = asprintf(&message, "U\x02%c%s%c", (int) (strlen(u->id) + 1), u->id, '\x00'); + if (c < 0) return (void) log_oom(); - errno = 0; - if (write(fd, message, n + 1) != n + 1) - if (!IN_SET(errno, EAGAIN, ENOENT) && !ERRNO_IS_DISCONNECT(errno)) - log_error_errno(errno, "Failed to write Plymouth message: %m"); + /* We set SOCK_NONBLOCK here so that we rather drop the message then wait for plymouth */ + r = plymouth_send_raw(message, c, SOCK_NONBLOCK); + if (r < 0) + log_full_errno(ERRNO_IS_NO_PLYMOUTH(r) ? LOG_DEBUG : LOG_WARNING, r, + "Failed to communicate with plymouth: %m"); } usec_t manager_get_watchdog(Manager *m, WatchdogType t) { diff --git a/src/shared/ask-password-api.c b/src/shared/ask-password-api.c index a5cf8974db5..0e323f4644e 100644 --- a/src/shared/ask-password-api.c +++ b/src/shared/ask-password-api.c @@ -20,7 +20,6 @@ #include "alloc-util.h" #include "ask-password-api.h" -#include "constants.h" #include "creds-util.h" #include "fd-util.h" #include "fileio.h" @@ -36,6 +35,7 @@ #include "missing_syscall.h" #include "mkdir-label.h" #include "nulstr-util.h" +#include "plymouth-util.h" #include "process-util.h" #include "random-util.h" #include "signal-util.h" @@ -211,7 +211,6 @@ int ask_password_plymouth( const char *flag_file, char ***ret) { - static const union sockaddr_union sa = PLYMOUTH_SOCKET; _cleanup_close_ int fd = -EBADF, notify = -EBADF; _cleanup_free_ char *packet = NULL; ssize_t k; @@ -238,12 +237,9 @@ int ask_password_plymouth( return -errno; } - fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); + fd = plymouth_connect(SOCK_NONBLOCK); if (fd < 0) - return -errno; - - if (connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) - return -errno; + return fd; if (FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED)) { packet = strdup("c"); diff --git a/src/shared/meson.build b/src/shared/meson.build index fc5cc1c59c9..b24a541de5d 100644 --- a/src/shared/meson.build +++ b/src/shared/meson.build @@ -137,6 +137,7 @@ shared_sources = files( 'pcrextend-util.c', 'pe-binary.c', 'pkcs11-util.c', + 'plymouth-util.c', 'pretty-print.c', 'ptyfwd.c', 'qrcode-util.c', diff --git a/src/shared/plymouth-util.c b/src/shared/plymouth-util.c new file mode 100644 index 00000000000..31ab3409311 --- /dev/null +++ b/src/shared/plymouth-util.c @@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "fd-util.h" +#include "io-util.h" +#include "plymouth-util.h" +#include "socket-util.h" + +int plymouth_connect(int flags) { + static const union sockaddr_union sa = { + .un.sun_family = AF_UNIX, + .un.sun_path = "\0/org/freedesktop/plymouthd", + }; + _cleanup_close_ int fd = -EBADF; + + fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|flags, 0); + if (fd < 0) + return -errno; + + if (connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) + return -errno; + + return TAKE_FD(fd); +} + +int plymouth_send_raw(const void *raw, size_t size, int flags) { + _cleanup_close_ int fd = -EBADF; + + fd = plymouth_connect(flags); + if (fd < 0) + return fd; + + return loop_write(fd, raw, size); +} diff --git a/src/shared/plymouth-util.h b/src/shared/plymouth-util.h new file mode 100644 index 00000000000..04aec707b70 --- /dev/null +++ b/src/shared/plymouth-util.h @@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include + +#include "errno-util.h" + +int plymouth_connect(int flags); +int plymouth_send_raw(const void *raw, size_t size, int flags); + +static inline bool ERRNO_IS_NO_PLYMOUTH(int r) { + return IN_SET(abs(r), EAGAIN, ENOENT) || ERRNO_IS_DISCONNECT(r); +}