]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/show-status.c
headers: remove unneeded includes from util.h
[thirdparty/systemd.git] / src / core / show-status.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
15bd9a28 2
ca78ad1d
ZJS
3#include <fcntl.h>
4#include <sys/stat.h>
5#include <sys/types.h>
6
b5efdb8a 7#include "alloc-util.h"
8b43440b 8#include "fd-util.h"
afc5dbf3 9#include "io-util.h"
6bedfcbb
LP
10#include "parse-util.h"
11#include "show-status.h"
7a293242 12#include "string-table.h"
07630cea 13#include "string-util.h"
8b43440b 14#include "terminal-util.h"
15bd9a28
LP
15#include "util.h"
16
bee38b5c 17static const char* const show_status_table[_SHOW_STATUS_MAX] = {
7a293242
YW
18 [SHOW_STATUS_NO] = "no",
19 [SHOW_STATUS_AUTO] = "auto",
20 [SHOW_STATUS_TEMPORARY] = "temporary",
21 [SHOW_STATUS_YES] = "yes",
22};
23
24DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(show_status, ShowStatus, SHOW_STATUS_YES);
25
15bd9a28 26int parse_show_status(const char *v, ShowStatus *ret) {
7a293242 27 ShowStatus s;
15bd9a28 28
15bd9a28
LP
29 assert(ret);
30
7a293242
YW
31 s = show_status_from_string(v);
32 if (s < 0 || s == SHOW_STATUS_TEMPORARY)
33 return -EINVAL;
15bd9a28 34
7a293242 35 *ret = s;
15bd9a28
LP
36 return 0;
37}
b8faf2ec 38
a885727a 39int status_vprintf(const char *status, ShowStatusFlags flags, const char *format, va_list ap) {
b8faf2ec
LP
40 static const char status_indent[] = " "; /* "[" STATUS "] " */
41 _cleanup_free_ char *s = NULL;
42 _cleanup_close_ int fd = -1;
82554307 43 struct iovec iovec[7] = {};
b8faf2ec
LP
44 int n = 0;
45 static bool prev_ephemeral;
46
47 assert(format);
48
49 /* This is independent of logging, as status messages are
50 * optional and go exclusively to the console. */
51
52 if (vasprintf(&s, format, ap) < 0)
53 return log_oom();
54
8ae2c630
LP
55 /* Before you ask: yes, on purpose we open/close the console for each status line we write individually. This
56 * is a good strategy to avoid PID 1 getting killed by the kernel's SAK concept (it doesn't fix this entirely,
57 * but minimizes the time window the kernel might end up killing PID 1 due to SAK). It also makes things easier
58 * for us so that we don't have to recover from hangups and suchlike triggered on the console. */
59
b8faf2ec
LP
60 fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
61 if (fd < 0)
62 return fd;
63
a885727a 64 if (FLAGS_SET(flags, SHOW_STATUS_ELLIPSIZE)) {
b8faf2ec
LP
65 char *e;
66 size_t emax, sl;
67 int c;
68
69 c = fd_columns(fd);
70 if (c <= 0)
71 c = 80;
72
73 sl = status ? sizeof(status_indent)-1 : 0;
74
75 emax = c - sl - 1;
76 if (emax < 3)
77 emax = 3;
78
79 e = ellipsize(s, emax, 50);
ccfc08d4
LP
80 if (e)
81 free_and_replace(s, e);
b8faf2ec
LP
82 }
83
84 if (prev_ephemeral)
82554307 85 iovec[n++] = IOVEC_MAKE_STRING(ANSI_REVERSE_LINEFEED "\r" ANSI_ERASE_TO_END_OF_LINE);
b8faf2ec
LP
86
87 if (status) {
88 if (!isempty(status)) {
e6a7ec4b
LP
89 iovec[n++] = IOVEC_MAKE_STRING("[");
90 iovec[n++] = IOVEC_MAKE_STRING(status);
91 iovec[n++] = IOVEC_MAKE_STRING("] ");
b8faf2ec 92 } else
e6a7ec4b 93 iovec[n++] = IOVEC_MAKE_STRING(status_indent);
b8faf2ec
LP
94 }
95
e6a7ec4b 96 iovec[n++] = IOVEC_MAKE_STRING(s);
82554307
T
97 iovec[n++] = IOVEC_MAKE_STRING("\n");
98
a885727a 99 if (prev_ephemeral && !FLAGS_SET(flags, SHOW_STATUS_EPHEMERAL))
82554307 100 iovec[n++] = IOVEC_MAKE_STRING(ANSI_ERASE_TO_END_OF_LINE);
a885727a 101 prev_ephemeral = FLAGS_SET(flags, SHOW_STATUS_EPHEMERAL) ;
b8faf2ec
LP
102
103 if (writev(fd, iovec, n) < 0)
104 return -errno;
105
106 return 0;
107}
108
a885727a 109int status_printf(const char *status, ShowStatusFlags flags, const char *format, ...) {
b8faf2ec
LP
110 va_list ap;
111 int r;
112
113 assert(format);
114
115 va_start(ap, format);
a885727a 116 r = status_vprintf(status, flags, format, ap);
b8faf2ec
LP
117 va_end(ap);
118
119 return r;
120}