1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
10 #include "parse-util.h"
11 #include "show-status.h"
12 #include "string-table.h"
13 #include "string-util.h"
14 #include "terminal-util.h"
17 static const char* const show_status_table
[_SHOW_STATUS_MAX
] = {
18 [SHOW_STATUS_NO
] = "no",
19 [SHOW_STATUS_AUTO
] = "auto",
20 [SHOW_STATUS_TEMPORARY
] = "temporary",
21 [SHOW_STATUS_YES
] = "yes",
24 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(show_status
, ShowStatus
, SHOW_STATUS_YES
);
26 int parse_show_status(const char *v
, ShowStatus
*ret
) {
31 s
= show_status_from_string(v
);
32 if (s
< 0 || s
== SHOW_STATUS_TEMPORARY
)
39 int status_vprintf(const char *status
, ShowStatusFlags flags
, const char *format
, va_list ap
) {
40 static const char status_indent
[] = " "; /* "[" STATUS "] " */
41 _cleanup_free_
char *s
= NULL
;
42 _cleanup_close_
int fd
= -1;
43 struct iovec iovec
[7] = {};
45 static bool prev_ephemeral
;
49 /* This is independent of logging, as status messages are
50 * optional and go exclusively to the console. */
52 if (vasprintf(&s
, format
, ap
) < 0)
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. */
60 fd
= open_terminal("/dev/console", O_WRONLY
|O_NOCTTY
|O_CLOEXEC
);
64 if (FLAGS_SET(flags
, SHOW_STATUS_ELLIPSIZE
)) {
73 sl
= status
? sizeof(status_indent
)-1 : 0;
79 e
= ellipsize(s
, emax
, 50);
81 free_and_replace(s
, e
);
85 iovec
[n
++] = IOVEC_MAKE_STRING(ANSI_REVERSE_LINEFEED
"\r" ANSI_ERASE_TO_END_OF_LINE
);
88 if (!isempty(status
)) {
89 iovec
[n
++] = IOVEC_MAKE_STRING("[");
90 iovec
[n
++] = IOVEC_MAKE_STRING(status
);
91 iovec
[n
++] = IOVEC_MAKE_STRING("] ");
93 iovec
[n
++] = IOVEC_MAKE_STRING(status_indent
);
96 iovec
[n
++] = IOVEC_MAKE_STRING(s
);
97 iovec
[n
++] = IOVEC_MAKE_STRING("\n");
99 if (prev_ephemeral
&& !FLAGS_SET(flags
, SHOW_STATUS_EPHEMERAL
))
100 iovec
[n
++] = IOVEC_MAKE_STRING(ANSI_ERASE_TO_END_OF_LINE
);
101 prev_ephemeral
= FLAGS_SET(flags
, SHOW_STATUS_EPHEMERAL
) ;
103 if (writev(fd
, iovec
, n
) < 0)
109 int status_printf(const char *status
, ShowStatusFlags flags
, const char *format
, ...) {
115 va_start(ap
, format
);
116 r
= status_vprintf(status
, flags
, format
, ap
);