| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "fd-util.h" |
| 7 |
#include "iovec-util.h" |
| 8 |
#include "log.h" |
| 9 |
#include "parse-util.h" |
| 10 |
#include "show-status.h" |
| 11 |
#include "string-table.h" |
| 12 |
#include "string-util.h" |
| 13 |
#include "terminal-util.h" |
| 14 |
|
| 15 |
static const char* const show_status_table[_SHOW_STATUS_MAX] = { |
| 16 |
[SHOW_STATUS_NO] = "no", |
| 17 |
[SHOW_STATUS_ERROR] = "error", |
| 18 |
[SHOW_STATUS_AUTO] = "auto", |
| 19 |
[SHOW_STATUS_TEMPORARY] = "temporary", |
| 20 |
[SHOW_STATUS_YES] = "yes", |
| 21 |
}; |
| 22 |
|
| 23 |
DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(show_status, ShowStatus, SHOW_STATUS_YES); |
| 24 |
|
| 25 |
int parse_show_status(const char *s, ShowStatus *ret) { |
| 26 |
ShowStatus status; |
| 27 |
|
| 28 |
assert(ret); |
| 29 |
|
| 30 |
status = show_status_from_string(s); |
| 31 |
if (status < 0 || status == SHOW_STATUS_TEMPORARY) |
| 32 |
return -EINVAL; |
| 33 |
|
| 34 |
*ret = status; |
| 35 |
return 0; |
| 36 |
} |
| 37 |
|
| 38 |
int status_vprintf(const char *status, ShowStatusFlags flags, const char *format, va_list ap) { |
| 39 |
static const char status_indent[] = " "; /* "[" STATUS "] " */ |
| 40 |
static bool prev_ephemeral = false; |
| 41 |
static int dumb = -1; |
| 42 |
|
| 43 |
_cleanup_free_ char *s = NULL; |
| 44 |
_cleanup_close_ int fd = -EBADF; |
| 45 |
struct iovec iovec[7] = {}; |
| 46 |
int n = 0; |
| 47 |
|
| 48 |
assert(format); |
| 49 |
|
| 50 |
if (dumb < 0) |
| 51 |
dumb = getenv_terminal_is_dumb(); |
| 52 |
|
| 53 |
/* This is independent of logging, as status messages are |
| 54 |
* optional and go exclusively to the console. */ |
| 55 |
|
| 56 |
if (vasprintf(&s, format, ap) < 0) |
| 57 |
return log_oom(); |
| 58 |
|
| 59 |
/* Before you ask: yes, on purpose we open/close the console for each status line we write individually. This |
| 60 |
* is a good strategy to avoid PID 1 getting killed by the kernel's SAK concept (it doesn't fix this entirely, |
| 61 |
* but minimizes the time window the kernel might end up killing PID 1 due to SAK). It also makes things easier |
| 62 |
* for us so that we don't have to recover from hangups and suchlike triggered on the console. */ |
| 63 |
|
| 64 |
fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC); |
| 65 |
if (fd < 0) |
| 66 |
return fd; |
| 67 |
|
| 68 |
if (FLAGS_SET(flags, SHOW_STATUS_ELLIPSIZE) && !dumb) { |
| 69 |
char *e; |
| 70 |
size_t emax, sl; |
| 71 |
int c; |
| 72 |
|
| 73 |
c = fd_columns(fd); |
| 74 |
if (c <= 0) { |
| 75 |
const char *env = getenv("COLUMNS"); |
| 76 |
if (env) |
| 77 |
(void) safe_atoi(env, &c); |
| 78 |
|
| 79 |
if (c <= 0) |
| 80 |
c = 80; |
| 81 |
} |
| 82 |
|
| 83 |
sl = status ? strlen(status_indent) : 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_and_replace(s, e); |
| 92 |
} |
| 93 |
|
| 94 |
if (prev_ephemeral && !dumb) |
| 95 |
iovec[n++] = IOVEC_MAKE_STRING(ANSI_REVERSE_LINEFEED "\r" ANSI_ERASE_TO_END_OF_LINE); |
| 96 |
|
| 97 |
if (status) { |
| 98 |
if (!isempty(status)) { |
| 99 |
iovec[n++] = IOVEC_MAKE_STRING("["); |
| 100 |
iovec[n++] = IOVEC_MAKE_STRING(status); |
| 101 |
iovec[n++] = IOVEC_MAKE_STRING("] "); |
| 102 |
} else |
| 103 |
iovec[n++] = IOVEC_MAKE_STRING(status_indent); |
| 104 |
} |
| 105 |
|
| 106 |
iovec[n++] = IOVEC_MAKE_STRING(s); |
| 107 |
/* use CRNL instead of just NL, to be robust towards TTYs in raw mode. If we're writing to a dumb |
| 108 |
* terminal, use NL as CRNL might be interpreted as a double newline. */ |
| 109 |
iovec[n++] = IOVEC_MAKE_STRING(dumb ? "\n" : "\r\n"); |
| 110 |
|
| 111 |
if (prev_ephemeral && !FLAGS_SET(flags, SHOW_STATUS_EPHEMERAL) && !dumb) |
| 112 |
iovec[n++] = IOVEC_MAKE_STRING(ANSI_ERASE_TO_END_OF_LINE); |
| 113 |
prev_ephemeral = FLAGS_SET(flags, SHOW_STATUS_EPHEMERAL); |
| 114 |
|
| 115 |
if (writev(fd, iovec, n) < 0) |
| 116 |
return -errno; |
| 117 |
|
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
int status_printf(const char *status, ShowStatusFlags flags, const char *format, ...) { |
| 122 |
va_list ap; |
| 123 |
int r; |
| 124 |
|
| 125 |
assert(format); |
| 126 |
|
| 127 |
va_start(ap, format); |
| 128 |
r = status_vprintf(status, flags, format, ap); |
| 129 |
va_end(ap); |
| 130 |
|
| 131 |
return r; |
| 132 |
} |
| 133 |
|
| 134 |
static const char* const status_unit_format_table[_STATUS_UNIT_FORMAT_MAX] = { |
| 135 |
[STATUS_UNIT_FORMAT_NAME] = "name", |
| 136 |
[STATUS_UNIT_FORMAT_DESCRIPTION] = "description", |
| 137 |
[STATUS_UNIT_FORMAT_COMBINED] = "combined", |
| 138 |
}; |
| 139 |
|
| 140 |
DEFINE_STRING_TABLE_LOOKUP(status_unit_format, StatusUnitFormat); |