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-13 14:17:05 +00:00
committed by Luca Boccassi
parent 1a292659f2
commit aa25e19b47
7 changed files with 64 additions and 54 deletions
+3 -7
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");
+1
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',
+33
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);
}
+13
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);
}