2 This file is part of systemd.
4 Copyright 2014 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include "alloc-util.h"
23 #include "parse-util.h"
24 #include "show-status.h"
25 #include "string-util.h"
26 #include "terminal-util.h"
29 int parse_show_status(const char *v
, ShowStatus
*ret
) {
35 if (streq(v
, "auto")) {
36 *ret
= SHOW_STATUS_AUTO
;
44 *ret
= r
? SHOW_STATUS_YES
: SHOW_STATUS_NO
;
48 int status_vprintf(const char *status
, bool ellipse
, bool ephemeral
, const char *format
, va_list ap
) {
49 static const char status_indent
[] = " "; /* "[" STATUS "] " */
50 _cleanup_free_
char *s
= NULL
;
51 _cleanup_close_
int fd
= -1;
52 struct iovec iovec
[6] = {};
54 static bool prev_ephemeral
;
58 /* This is independent of logging, as status messages are
59 * optional and go exclusively to the console. */
61 if (vasprintf(&s
, format
, ap
) < 0)
64 /* Before you ask: yes, on purpose we open/close the console for each status line we write individually. This
65 * is a good strategy to avoid PID 1 getting killed by the kernel's SAK concept (it doesn't fix this entirely,
66 * but minimizes the time window the kernel might end up killing PID 1 due to SAK). It also makes things easier
67 * for us so that we don't have to recover from hangups and suchlike triggered on the console. */
69 fd
= open_terminal("/dev/console", O_WRONLY
|O_NOCTTY
|O_CLOEXEC
);
82 sl
= status
? sizeof(status_indent
)-1 : 0;
88 e
= ellipsize(s
, emax
, 50);
96 iovec
[n
++] = IOVEC_MAKE_STRING("\r" ANSI_ERASE_TO_END_OF_LINE
);
97 prev_ephemeral
= ephemeral
;
100 if (!isempty(status
)) {
101 iovec
[n
++] = IOVEC_MAKE_STRING("[");
102 iovec
[n
++] = IOVEC_MAKE_STRING(status
);
103 iovec
[n
++] = IOVEC_MAKE_STRING("] ");
105 iovec
[n
++] = IOVEC_MAKE_STRING(status_indent
);
108 iovec
[n
++] = IOVEC_MAKE_STRING(s
);
110 iovec
[n
++] = IOVEC_MAKE_STRING("\n");
112 if (writev(fd
, iovec
, n
) < 0)
118 int status_printf(const char *status
, bool ellipse
, bool ephemeral
, const char *format
, ...) {
124 va_start(ap
, format
);
125 r
= status_vprintf(status
, ellipse
, ephemeral
, format
, ap
);