1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-03-12 08:58:20 +03:00

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.
This commit is contained in:
Lennart Poettering 2023-11-10 16:30:30 +01:00 committed by Luca Boccassi
parent 1a292659f2
commit aa25e19b47
7 changed files with 64 additions and 54 deletions

View File

@ -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. */

View 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;
}

View 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) {

View 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");

View 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',

View File

@ -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);
}

View File

@ -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);
}