1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Lennart Poettering
8 #include "alloc-util.h"
11 #include "parse-util.h"
12 #include "show-status.h"
13 #include "string-util.h"
14 #include "terminal-util.h"
17 int parse_show_status(const char *v
, ShowStatus
*ret
) {
23 if (streq(v
, "auto")) {
24 *ret
= SHOW_STATUS_AUTO
;
32 *ret
= r
? SHOW_STATUS_YES
: SHOW_STATUS_NO
;
36 int status_vprintf(const char *status
, bool ellipse
, bool ephemeral
, const char *format
, va_list ap
) {
37 static const char status_indent
[] = " "; /* "[" STATUS "] " */
38 _cleanup_free_
char *s
= NULL
;
39 _cleanup_close_
int fd
= -1;
40 struct iovec iovec
[6] = {};
42 static bool prev_ephemeral
;
46 /* This is independent of logging, as status messages are
47 * optional and go exclusively to the console. */
49 if (vasprintf(&s
, format
, ap
) < 0)
52 /* Before you ask: yes, on purpose we open/close the console for each status line we write individually. This
53 * is a good strategy to avoid PID 1 getting killed by the kernel's SAK concept (it doesn't fix this entirely,
54 * but minimizes the time window the kernel might end up killing PID 1 due to SAK). It also makes things easier
55 * for us so that we don't have to recover from hangups and suchlike triggered on the console. */
57 fd
= open_terminal("/dev/console", O_WRONLY
|O_NOCTTY
|O_CLOEXEC
);
70 sl
= status
? sizeof(status_indent
)-1 : 0;
76 e
= ellipsize(s
, emax
, 50);
84 iovec
[n
++] = IOVEC_MAKE_STRING("\r" ANSI_ERASE_TO_END_OF_LINE
);
85 prev_ephemeral
= ephemeral
;
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
);
98 iovec
[n
++] = IOVEC_MAKE_STRING("\n");
100 if (writev(fd
, iovec
, n
) < 0)
106 int status_printf(const char *status
, bool ellipse
, bool ephemeral
, const char *format
, ...) {
112 va_start(ap
, format
);
113 r
= status_vprintf(status
, ellipse
, ephemeral
, format
, ap
);