]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
util-lib: share plymouth client code
authorLennart Poettering <lennart@poettering.net>
Fri, 10 Nov 2023 15:30:30 +0000 (16:30 +0100)
committerLuca Boccassi <luca.boccassi@gmail.com>
Mon, 13 Nov 2023 14:17:05 +0000 (14:17 +0000)
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.

src/basic/constants.h
src/battery-check/battery-check.c
src/core/manager.c
src/shared/ask-password-api.c
src/shared/meson.build
src/shared/plymouth-util.c [new file with mode: 0644]
src/shared/plymouth-util.h [new file with mode: 0644]

index 98816d183c22afef3e97ede338f8c7c5fd9c4e9b..6bb5f3c28127ea394859b7411e907bc3a1103c94 100644 (file)
  * 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. */
index 5d7c6a58ae53a2ae765cac444d5eb0cea69971bb..03628c8b921494db79ab083fea66b5dc0d37490b 100644 (file)
@@ -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;
 }
index b974898cc7efbed77f13bd30b71398156b47f2ac..fcb7739b13605391f90c5367b40ee5532ff1a4a7 100644 (file)
@@ -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) {
index a5cf8974db54b02be7ec1c64d63341d79ec8e5c2..0e323f4644e576274a1840896264147b5a81df23 100644 (file)
@@ -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");
index fc5cc1c59c9d37a85e34f9f5d3cd8d744dec7f9d..b24a541de5d0bf64b1d6e09dc9b06549746e5411 100644 (file)
@@ -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 (file)
index 0000000..31ab340
--- /dev/null
@@ -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 (file)
index 0000000..04aec70
--- /dev/null
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+#include <sys/types.h>
+
+#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);
+}