Debian Codegraph @

src/shared/plymouth-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "fd-util.h"
5 #include "io-util.h"
6 #include "log.h"
7 #include "plymouth-util.h"
8 #include "socket-util.h"
9
10 int plymouth_connect(int flags) {
11 static const union sockaddr_union sa = {
12 .un.sun_family = AF_UNIX,
13 .un.sun_path = "\0/org/freedesktop/plymouthd",
14 };
15 _cleanup_close_ int fd = -EBADF;
16
17 fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|flags, 0);
18 if (fd < 0)
19 return -errno;
20
21 if (connect(fd, &sa.sa, sockaddr_un_len(&sa.un)) < 0)
22 return -errno;
23
24 return TAKE_FD(fd);
25 }
26
27 int plymouth_send_raw(const void *raw, size_t size, int flags) {
28 _cleanup_close_ int fd = -EBADF;
29
30 fd = plymouth_connect(flags);
31 if (fd < 0)
32 return fd;
33
34 return loop_write(fd, raw, size);
35 }
36
37 int plymouth_send_msg(const char *text, bool pause_spinner) {
38 _cleanup_free_ char *plymouth_message = NULL;
39 int c, r;
40
41 assert(text);
42 assert(strlen(text) < UCHAR_MAX);
43
44 c = asprintf(&plymouth_message,
45 "M\x02%c%s%c"
46 "%c%c", /* pause/resume spinner */
47 (int) strlen(text) + 1, text, '\x00',
48 pause_spinner ? 'A' : 'a', '\x00');
49 if (c < 0)
50 return log_oom();
51
52 r = plymouth_send_raw(plymouth_message, c, SOCK_NONBLOCK);
53 if (r < 0)
54 return log_full_errno(ERRNO_IS_NO_PLYMOUTH(r) ? LOG_DEBUG : LOG_WARNING, r,
55 "Failed to communicate with plymouth, ignoring: %m");
56
57 return 0;
58 }
59
60 int plymouth_hide_splash(void) {
61 int r;
62
63 r = plymouth_send_raw("H\0", 2, SOCK_NONBLOCK);
64 if (r < 0)
65 return log_full_errno(ERRNO_IS_NO_PLYMOUTH(r) ? LOG_DEBUG : LOG_WARNING, r,
66 "Failed to communicate with plymouth: %m");
67
68 return 0;
69 }