1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
6 #include "parse-util.h"
7 #include "show-status.h"
8 #include "string-table.h"
9 #include "string-util.h"
10 #include "terminal-util.h"
13 static const char* const show_status_table
[_SHOW_STATUS_MAX
] = {
14 [SHOW_STATUS_NO
] = "no",
15 [SHOW_STATUS_AUTO
] = "auto",
16 [SHOW_STATUS_TEMPORARY
] = "temporary",
17 [SHOW_STATUS_YES
] = "yes",
20 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(show_status
, ShowStatus
, SHOW_STATUS_YES
);
22 int parse_show_status(const char *v
, ShowStatus
*ret
) {
27 s
= show_status_from_string(v
);
28 if (s
< 0 || s
== SHOW_STATUS_TEMPORARY
)
35 int status_vprintf(const char *status
, bool ellipse
, bool ephemeral
, const char *format
, va_list ap
) {
36 static const char status_indent
[] = " "; /* "[" STATUS "] " */
37 _cleanup_free_
char *s
= NULL
;
38 _cleanup_close_
int fd
= -1;
39 struct iovec iovec
[7] = {};
41 static bool prev_ephemeral
;
45 /* This is independent of logging, as status messages are
46 * optional and go exclusively to the console. */
48 if (vasprintf(&s
, format
, ap
) < 0)
51 /* Before you ask: yes, on purpose we open/close the console for each status line we write individually. This
52 * is a good strategy to avoid PID 1 getting killed by the kernel's SAK concept (it doesn't fix this entirely,
53 * but minimizes the time window the kernel might end up killing PID 1 due to SAK). It also makes things easier
54 * for us so that we don't have to recover from hangups and suchlike triggered on the console. */
56 fd
= open_terminal("/dev/console", O_WRONLY
|O_NOCTTY
|O_CLOEXEC
);
69 sl
= status
? sizeof(status_indent
)-1 : 0;
75 e
= ellipsize(s
, emax
, 50);
83 iovec
[n
++] = IOVEC_MAKE_STRING(ANSI_REVERSE_LINEFEED
"\r" ANSI_ERASE_TO_END_OF_LINE
);
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
);
95 iovec
[n
++] = IOVEC_MAKE_STRING("\n");
97 if (prev_ephemeral
&& !ephemeral
)
98 iovec
[n
++] = IOVEC_MAKE_STRING(ANSI_ERASE_TO_END_OF_LINE
);
99 prev_ephemeral
= ephemeral
;
101 if (writev(fd
, iovec
, n
) < 0)
107 int status_printf(const char *status
, bool ellipse
, bool ephemeral
, const char *format
, ...) {
113 va_start(ap
, format
);
114 r
= status_vprintf(status
, ellipse
, ephemeral
, format
, ap
);