]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
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 | 17 | static const char* const show_status_table[_SHOW_STATUS_MAX] = { |
36cf4507 | 18 | [SHOW_STATUS_NO] = "no", |
0d066dd1 | 19 | [SHOW_STATUS_ERROR] = "error", |
36cf4507 | 20 | [SHOW_STATUS_AUTO] = "auto", |
7a293242 | 21 | [SHOW_STATUS_TEMPORARY] = "temporary", |
36cf4507 | 22 | [SHOW_STATUS_YES] = "yes", |
7a293242 YW |
23 | }; |
24 | ||
25 | DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(show_status, ShowStatus, SHOW_STATUS_YES); | |
26 | ||
15bd9a28 | 27 | int parse_show_status(const char *v, ShowStatus *ret) { |
7a293242 | 28 | ShowStatus s; |
15bd9a28 | 29 | |
15bd9a28 LP |
30 | assert(ret); |
31 | ||
7a293242 YW |
32 | s = show_status_from_string(v); |
33 | if (s < 0 || s == SHOW_STATUS_TEMPORARY) | |
34 | return -EINVAL; | |
15bd9a28 | 35 | |
7a293242 | 36 | *ret = s; |
15bd9a28 LP |
37 | return 0; |
38 | } | |
b8faf2ec | 39 | |
a885727a | 40 | int status_vprintf(const char *status, ShowStatusFlags flags, const char *format, va_list ap) { |
b8faf2ec LP |
41 | static const char status_indent[] = " "; /* "[" STATUS "] " */ |
42 | _cleanup_free_ char *s = NULL; | |
43 | _cleanup_close_ int fd = -1; | |
82554307 | 44 | struct iovec iovec[7] = {}; |
b8faf2ec LP |
45 | int n = 0; |
46 | static bool prev_ephemeral; | |
47 | ||
48 | assert(format); | |
49 | ||
50 | /* This is independent of logging, as status messages are | |
51 | * optional and go exclusively to the console. */ | |
52 | ||
53 | if (vasprintf(&s, format, ap) < 0) | |
54 | return log_oom(); | |
55 | ||
8ae2c630 LP |
56 | /* Before you ask: yes, on purpose we open/close the console for each status line we write individually. This |
57 | * is a good strategy to avoid PID 1 getting killed by the kernel's SAK concept (it doesn't fix this entirely, | |
58 | * but minimizes the time window the kernel might end up killing PID 1 due to SAK). It also makes things easier | |
59 | * for us so that we don't have to recover from hangups and suchlike triggered on the console. */ | |
60 | ||
b8faf2ec LP |
61 | fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC); |
62 | if (fd < 0) | |
63 | return fd; | |
64 | ||
a885727a | 65 | if (FLAGS_SET(flags, SHOW_STATUS_ELLIPSIZE)) { |
b8faf2ec LP |
66 | char *e; |
67 | size_t emax, sl; | |
68 | int c; | |
69 | ||
70 | c = fd_columns(fd); | |
71 | if (c <= 0) | |
72 | c = 80; | |
73 | ||
74 | sl = status ? sizeof(status_indent)-1 : 0; | |
75 | ||
76 | emax = c - sl - 1; | |
77 | if (emax < 3) | |
78 | emax = 3; | |
79 | ||
80 | e = ellipsize(s, emax, 50); | |
ccfc08d4 LP |
81 | if (e) |
82 | free_and_replace(s, e); | |
b8faf2ec LP |
83 | } |
84 | ||
85 | if (prev_ephemeral) | |
82554307 | 86 | iovec[n++] = IOVEC_MAKE_STRING(ANSI_REVERSE_LINEFEED "\r" ANSI_ERASE_TO_END_OF_LINE); |
b8faf2ec LP |
87 | |
88 | if (status) { | |
89 | if (!isempty(status)) { | |
e6a7ec4b LP |
90 | iovec[n++] = IOVEC_MAKE_STRING("["); |
91 | iovec[n++] = IOVEC_MAKE_STRING(status); | |
92 | iovec[n++] = IOVEC_MAKE_STRING("] "); | |
b8faf2ec | 93 | } else |
e6a7ec4b | 94 | iovec[n++] = IOVEC_MAKE_STRING(status_indent); |
b8faf2ec LP |
95 | } |
96 | ||
e6a7ec4b | 97 | iovec[n++] = IOVEC_MAKE_STRING(s); |
82554307 T |
98 | iovec[n++] = IOVEC_MAKE_STRING("\n"); |
99 | ||
a885727a | 100 | if (prev_ephemeral && !FLAGS_SET(flags, SHOW_STATUS_EPHEMERAL)) |
82554307 | 101 | iovec[n++] = IOVEC_MAKE_STRING(ANSI_ERASE_TO_END_OF_LINE); |
a885727a | 102 | prev_ephemeral = FLAGS_SET(flags, SHOW_STATUS_EPHEMERAL) ; |
b8faf2ec LP |
103 | |
104 | if (writev(fd, iovec, n) < 0) | |
105 | return -errno; | |
106 | ||
107 | return 0; | |
108 | } | |
109 | ||
a885727a | 110 | int status_printf(const char *status, ShowStatusFlags flags, const char *format, ...) { |
b8faf2ec LP |
111 | va_list ap; |
112 | int r; | |
113 | ||
114 | assert(format); | |
115 | ||
116 | va_start(ap, format); | |
a885727a | 117 | r = status_vprintf(status, flags, format, ap); |
b8faf2ec LP |
118 | va_end(ap); |
119 | ||
120 | return r; | |
121 | } | |
36cf4507 ZJS |
122 | |
123 | static const char* const status_unit_format_table[_STATUS_UNIT_FORMAT_MAX] = { | |
124 | [STATUS_UNIT_FORMAT_NAME] = "name", | |
125 | [STATUS_UNIT_FORMAT_DESCRIPTION] = "description", | |
126 | }; | |
127 | ||
128 | DEFINE_STRING_TABLE_LOOKUP(status_unit_format, StatusUnitFormat); |