]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/show-status.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / core / show-status.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2014 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include "alloc-util.h"
22 #include "fd-util.h"
23 #include "io-util.h"
24 #include "parse-util.h"
25 #include "show-status.h"
26 #include "string-util.h"
27 #include "terminal-util.h"
28 #include "util.h"
29
30 int parse_show_status(const char *v, ShowStatus *ret) {
31 int r;
32
33 assert(v);
34 assert(ret);
35
36 if (streq(v, "auto")) {
37 *ret = SHOW_STATUS_AUTO;
38 return 0;
39 }
40
41 r = parse_boolean(v);
42 if (r < 0)
43 return r;
44
45 *ret = r ? SHOW_STATUS_YES : SHOW_STATUS_NO;
46 return 0;
47 }
48
49 int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) {
50 static const char status_indent[] = " "; /* "[" STATUS "] " */
51 _cleanup_free_ char *s = NULL;
52 _cleanup_close_ int fd = -1;
53 struct iovec iovec[6] = {};
54 int n = 0;
55 static bool prev_ephemeral;
56
57 assert(format);
58
59 /* This is independent of logging, as status messages are
60 * optional and go exclusively to the console. */
61
62 if (vasprintf(&s, format, ap) < 0)
63 return log_oom();
64
65 /* Before you ask: yes, on purpose we open/close the console for each status line we write individually. This
66 * is a good strategy to avoid PID 1 getting killed by the kernel's SAK concept (it doesn't fix this entirely,
67 * but minimizes the time window the kernel might end up killing PID 1 due to SAK). It also makes things easier
68 * for us so that we don't have to recover from hangups and suchlike triggered on the console. */
69
70 fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
71 if (fd < 0)
72 return fd;
73
74 if (ellipse) {
75 char *e;
76 size_t emax, sl;
77 int c;
78
79 c = fd_columns(fd);
80 if (c <= 0)
81 c = 80;
82
83 sl = status ? sizeof(status_indent)-1 : 0;
84
85 emax = c - sl - 1;
86 if (emax < 3)
87 emax = 3;
88
89 e = ellipsize(s, emax, 50);
90 if (e) {
91 free(s);
92 s = e;
93 }
94 }
95
96 if (prev_ephemeral)
97 iovec[n++] = IOVEC_MAKE_STRING("\r" ANSI_ERASE_TO_END_OF_LINE);
98 prev_ephemeral = ephemeral;
99
100 if (status) {
101 if (!isempty(status)) {
102 iovec[n++] = IOVEC_MAKE_STRING("[");
103 iovec[n++] = IOVEC_MAKE_STRING(status);
104 iovec[n++] = IOVEC_MAKE_STRING("] ");
105 } else
106 iovec[n++] = IOVEC_MAKE_STRING(status_indent);
107 }
108
109 iovec[n++] = IOVEC_MAKE_STRING(s);
110 if (!ephemeral)
111 iovec[n++] = IOVEC_MAKE_STRING("\n");
112
113 if (writev(fd, iovec, n) < 0)
114 return -errno;
115
116 return 0;
117 }
118
119 int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) {
120 va_list ap;
121 int r;
122
123 assert(format);
124
125 va_start(ap, format);
126 r = status_vprintf(status, ellipse, ephemeral, format, ap);
127 va_end(ap);
128
129 return r;
130 }