]>
Commit | Line | Data |
---|---|---|
15bd9a28 LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2014 Lennart Poettering | |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
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/>. | |
18 | ***/ | |
19 | ||
b5efdb8a | 20 | #include "alloc-util.h" |
8b43440b | 21 | #include "fd-util.h" |
afc5dbf3 | 22 | #include "io-util.h" |
6bedfcbb LP |
23 | #include "parse-util.h" |
24 | #include "show-status.h" | |
07630cea | 25 | #include "string-util.h" |
8b43440b | 26 | #include "terminal-util.h" |
15bd9a28 LP |
27 | #include "util.h" |
28 | ||
29 | int parse_show_status(const char *v, ShowStatus *ret) { | |
30 | int r; | |
31 | ||
32 | assert(v); | |
33 | assert(ret); | |
34 | ||
35 | if (streq(v, "auto")) { | |
36 | *ret = SHOW_STATUS_AUTO; | |
37 | return 0; | |
38 | } | |
39 | ||
40 | r = parse_boolean(v); | |
41 | if (r < 0) | |
42 | return r; | |
43 | ||
44 | *ret = r ? SHOW_STATUS_YES : SHOW_STATUS_NO; | |
45 | return 0; | |
46 | } | |
b8faf2ec LP |
47 | |
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] = {}; | |
53 | int n = 0; | |
54 | static bool prev_ephemeral; | |
55 | ||
56 | assert(format); | |
57 | ||
58 | /* This is independent of logging, as status messages are | |
59 | * optional and go exclusively to the console. */ | |
60 | ||
61 | if (vasprintf(&s, format, ap) < 0) | |
62 | return log_oom(); | |
63 | ||
8ae2c630 LP |
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. */ | |
68 | ||
b8faf2ec LP |
69 | fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC); |
70 | if (fd < 0) | |
71 | return fd; | |
72 | ||
73 | if (ellipse) { | |
74 | char *e; | |
75 | size_t emax, sl; | |
76 | int c; | |
77 | ||
78 | c = fd_columns(fd); | |
79 | if (c <= 0) | |
80 | c = 80; | |
81 | ||
82 | sl = status ? sizeof(status_indent)-1 : 0; | |
83 | ||
84 | emax = c - sl - 1; | |
85 | if (emax < 3) | |
86 | emax = 3; | |
87 | ||
88 | e = ellipsize(s, emax, 50); | |
89 | if (e) { | |
90 | free(s); | |
91 | s = e; | |
92 | } | |
93 | } | |
94 | ||
95 | if (prev_ephemeral) | |
e6a7ec4b | 96 | iovec[n++] = IOVEC_MAKE_STRING("\r" ANSI_ERASE_TO_END_OF_LINE); |
b8faf2ec LP |
97 | prev_ephemeral = ephemeral; |
98 | ||
99 | if (status) { | |
100 | if (!isempty(status)) { | |
e6a7ec4b LP |
101 | iovec[n++] = IOVEC_MAKE_STRING("["); |
102 | iovec[n++] = IOVEC_MAKE_STRING(status); | |
103 | iovec[n++] = IOVEC_MAKE_STRING("] "); | |
b8faf2ec | 104 | } else |
e6a7ec4b | 105 | iovec[n++] = IOVEC_MAKE_STRING(status_indent); |
b8faf2ec LP |
106 | } |
107 | ||
e6a7ec4b | 108 | iovec[n++] = IOVEC_MAKE_STRING(s); |
b8faf2ec | 109 | if (!ephemeral) |
e6a7ec4b | 110 | iovec[n++] = IOVEC_MAKE_STRING("\n"); |
b8faf2ec LP |
111 | |
112 | if (writev(fd, iovec, n) < 0) | |
113 | return -errno; | |
114 | ||
115 | return 0; | |
116 | } | |
117 | ||
118 | int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) { | |
119 | va_list ap; | |
120 | int r; | |
121 | ||
122 | assert(format); | |
123 | ||
124 | va_start(ap, format); | |
125 | r = status_vprintf(status, ellipse, ephemeral, format, ap); | |
126 | va_end(ap); | |
127 | ||
128 | return r; | |
129 | } |