1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2014 Lennart Poettering
6 #include "alloc-util.h"
9 #include "parse-util.h"
10 #include "show-status.h"
11 #include "string-util.h"
12 #include "terminal-util.h"
15 int parse_show_status(const char *v
, ShowStatus
*ret
) {
21 if (streq(v
, "auto")) {
22 *ret
= SHOW_STATUS_AUTO
;
30 *ret
= r
? SHOW_STATUS_YES
: SHOW_STATUS_NO
;
34 int status_vprintf(const char *status
, bool ellipse
, bool ephemeral
, const char *format
, va_list ap
) {
35 static const char status_indent
[] = " "; /* "[" STATUS "] " */
36 _cleanup_free_
char *s
= NULL
;
37 _cleanup_close_
int fd
= -1;
38 struct iovec iovec
[6] = {};
40 static bool prev_ephemeral
;
44 /* This is independent of logging, as status messages are
45 * optional and go exclusively to the console. */
47 if (vasprintf(&s
, format
, ap
) < 0)
50 /* Before you ask: yes, on purpose we open/close the console for each status line we write individually. This
51 * is a good strategy to avoid PID 1 getting killed by the kernel's SAK concept (it doesn't fix this entirely,
52 * but minimizes the time window the kernel might end up killing PID 1 due to SAK). It also makes things easier
53 * for us so that we don't have to recover from hangups and suchlike triggered on the console. */
55 fd
= open_terminal("/dev/console", O_WRONLY
|O_NOCTTY
|O_CLOEXEC
);
68 sl
= status
? sizeof(status_indent
)-1 : 0;
74 e
= ellipsize(s
, emax
, 50);
82 iovec
[n
++] = IOVEC_MAKE_STRING("\r" ANSI_ERASE_TO_END_OF_LINE
);
83 prev_ephemeral
= ephemeral
;
86 if (!isempty(status
)) {
87 iovec
[n
++] = IOVEC_MAKE_STRING("[");
88 iovec
[n
++] = IOVEC_MAKE_STRING(status
);
89 iovec
[n
++] = IOVEC_MAKE_STRING("] ");
91 iovec
[n
++] = IOVEC_MAKE_STRING(status_indent
);
94 iovec
[n
++] = IOVEC_MAKE_STRING(s
);
96 iovec
[n
++] = IOVEC_MAKE_STRING("\n");
98 if (writev(fd
, iovec
, n
) < 0)
104 int status_printf(const char *status
, bool ellipse
, bool ephemeral
, const char *format
, ...) {
110 va_start(ap
, format
);
111 r
= status_vprintf(status
, ellipse
, ephemeral
, format
, ap
);