| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/vt.h> |
| 4 |
#include <stdio.h> |
| 5 |
#include <sys/ioctl.h> |
| 6 |
|
| 7 |
#include "sd-journal.h" |
| 8 |
|
| 9 |
#include "alloc-util.h" |
| 10 |
#include "build.h" |
| 11 |
#include "fd-util.h" |
| 12 |
#include "fileio.h" |
| 13 |
#include "format-table.h" |
| 14 |
#include "io-util.h" |
| 15 |
#include "log.h" |
| 16 |
#include "logs-show.h" |
| 17 |
#include "main-func.h" |
| 18 |
#include "options.h" |
| 19 |
#include "parse-argument.h" |
| 20 |
#include "pretty-print.h" |
| 21 |
#include "qrcode-util.h" |
| 22 |
#include "signal-util.h" |
| 23 |
#include "stdio-util.h" |
| 24 |
#include "terminal-util.h" |
| 25 |
|
| 26 |
static bool arg_continuous = false; |
| 27 |
static char *arg_tty = NULL; |
| 28 |
|
| 29 |
STATIC_DESTRUCTOR_REGISTER(arg_tty, freep); |
| 30 |
|
| 31 |
static int help(void) { |
| 32 |
_cleanup_free_ char *link = NULL; |
| 33 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 34 |
int r; |
| 35 |
|
| 36 |
r = terminal_urlify_man("systemd-bsod", "8", &link); |
| 37 |
if (r < 0) |
| 38 |
return log_oom(); |
| 39 |
|
| 40 |
r = option_parser_get_help_table(&options); |
| 41 |
if (r < 0) |
| 42 |
return r; |
| 43 |
|
| 44 |
printf("%s [OPTIONS...]\n\n" |
| 45 |
"%sFilter the journal to fetch the first message from the current boot with an\n" |
| 46 |
"emergency log level and display it as a string and a QR code.%s\n" |
| 47 |
"\n%sOptions:%s\n", |
| 48 |
program_invocation_short_name, |
| 49 |
ansi_highlight(), |
| 50 |
ansi_normal(), |
| 51 |
ansi_underline(), |
| 52 |
ansi_normal()); |
| 53 |
|
| 54 |
r = table_print_or_warn(options); |
| 55 |
if (r < 0) |
| 56 |
return r; |
| 57 |
|
| 58 |
printf("\nSee the %s for details.\n", link); |
| 59 |
return 0; |
| 60 |
} |
| 61 |
|
| 62 |
static int acquire_first_emergency_log_message(char **ret) { |
| 63 |
_cleanup_(sd_journal_closep) sd_journal *j = NULL; |
| 64 |
_cleanup_free_ char *message = NULL; |
| 65 |
const void *d; |
| 66 |
size_t l; |
| 67 |
int r; |
| 68 |
|
| 69 |
assert(ret); |
| 70 |
|
| 71 |
r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY | (arg_continuous ? 0 : SD_JOURNAL_ASSUME_IMMUTABLE)); |
| 72 |
if (r < 0) |
| 73 |
return log_error_errno(r, "Failed to open journal: %m"); |
| 74 |
|
| 75 |
r = add_match_this_boot(j, NULL); |
| 76 |
if (r < 0) |
| 77 |
return log_error_errno(r, "Failed to add boot ID filter: %m"); |
| 78 |
|
| 79 |
r = sd_journal_add_match(j, "_UID=0", SIZE_MAX); |
| 80 |
if (r < 0) |
| 81 |
return log_error_errno(r, "Failed to add User ID filter: %m"); |
| 82 |
|
| 83 |
assert_cc(0 == LOG_EMERG); |
| 84 |
r = sd_journal_add_match(j, "PRIORITY=0", SIZE_MAX); |
| 85 |
if (r < 0) |
| 86 |
return log_error_errno(r, "Failed to add Emergency filter: %m"); |
| 87 |
|
| 88 |
r = sd_journal_seek_head(j); |
| 89 |
if (r < 0) |
| 90 |
return log_error_errno(r, "Failed to seek to start of journal: %m"); |
| 91 |
|
| 92 |
for (;;) { |
| 93 |
r = sd_journal_next(j); |
| 94 |
if (r < 0) |
| 95 |
return log_error_errno(r, "Failed to read next journal entry: %m"); |
| 96 |
if (r > 0) |
| 97 |
break; |
| 98 |
|
| 99 |
if (!arg_continuous) { |
| 100 |
log_debug("No emergency level entries in the journal"); |
| 101 |
*ret = NULL; |
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
r = sd_journal_wait(j, UINT64_MAX); |
| 106 |
if (r < 0) |
| 107 |
return log_error_errno(r, "Failed to wait for changes: %m"); |
| 108 |
} |
| 109 |
|
| 110 |
r = sd_journal_get_data(j, "MESSAGE", &d, &l); |
| 111 |
if (r < 0) |
| 112 |
return log_error_errno(r, "Failed to read journal message: %m"); |
| 113 |
|
| 114 |
message = memdup_suffix0((const char*)d + strlen("MESSAGE="), l - strlen("MESSAGE=")); |
| 115 |
if (!message) |
| 116 |
return log_oom(); |
| 117 |
|
| 118 |
*ret = TAKE_PTR(message); |
| 119 |
|
| 120 |
return 0; |
| 121 |
} |
| 122 |
|
| 123 |
static int find_next_free_vt(int fd, int *ret_free_vt, int *ret_original_vt) { |
| 124 |
struct vt_stat terminal_status; |
| 125 |
|
| 126 |
assert(fd >= 0); |
| 127 |
assert(ret_free_vt); |
| 128 |
assert(ret_original_vt); |
| 129 |
|
| 130 |
if (ioctl(fd, VT_GETSTATE, &terminal_status) < 0) |
| 131 |
return -errno; |
| 132 |
|
| 133 |
for (size_t i = 0; i < sizeof(terminal_status.v_state) * 8; i++) |
| 134 |
if ((terminal_status.v_state & (1 << i)) == 0) { |
| 135 |
*ret_free_vt = i + 1; |
| 136 |
*ret_original_vt = terminal_status.v_active; |
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
return -ENOTTY; |
| 141 |
} |
| 142 |
|
| 143 |
static int display_emergency_message_fullscreen(const char *message) { |
| 144 |
int r, free_vt = 0, original_vt = 0; |
| 145 |
unsigned qr_code_start_row = 1, qr_code_start_column = 1; |
| 146 |
char ttybuf[STRLEN("/dev/tty") + DECIMAL_STR_MAX(int) + 1]; |
| 147 |
_cleanup_close_ int fd = -EBADF; |
| 148 |
_cleanup_fclose_ FILE *f = NULL; |
| 149 |
char read_character_buffer = '\0'; |
| 150 |
struct winsize w = { |
| 151 |
.ws_col = 80, |
| 152 |
.ws_row = 25, |
| 153 |
}; |
| 154 |
const char *tty; |
| 155 |
|
| 156 |
assert(message); |
| 157 |
|
| 158 |
if (arg_tty) |
| 159 |
tty = arg_tty; |
| 160 |
else { |
| 161 |
fd = open_terminal("/dev/tty1", O_RDWR|O_NOCTTY|O_CLOEXEC); |
| 162 |
if (fd < 0) |
| 163 |
return log_error_errno(fd, "Failed to open %s: %m", "/dev/tty1"); |
| 164 |
|
| 165 |
r = find_next_free_vt(fd, &free_vt, &original_vt); |
| 166 |
if (r < 0) |
| 167 |
return log_error_errno(r, "Failed to find a free VT: %m"); |
| 168 |
|
| 169 |
xsprintf(ttybuf, "/dev/tty%d", free_vt); |
| 170 |
tty = ttybuf; |
| 171 |
|
| 172 |
fd = safe_close(fd); |
| 173 |
} |
| 174 |
|
| 175 |
fd = open_terminal(tty, O_RDWR|O_NOCTTY|O_CLOEXEC); |
| 176 |
if (fd < 0) |
| 177 |
return log_error_errno(fd, "Failed to open %s: %m", tty); |
| 178 |
|
| 179 |
if (ioctl(fd, TIOCGWINSZ, &w) < 0) |
| 180 |
log_warning_errno(errno, "Failed to fetch tty size, ignoring: %m"); |
| 181 |
|
| 182 |
if (free_vt > 0 && ioctl(fd, VT_ACTIVATE, free_vt) < 0) |
| 183 |
return log_error_errno(errno, "Failed to activate /dev/tty%i, ignoring: %m", free_vt); |
| 184 |
|
| 185 |
r = loop_write(fd, ANSI_BACKGROUND_BLUE ANSI_HOME_CLEAR, SIZE_MAX); |
| 186 |
if (r < 0) |
| 187 |
log_warning_errno(r, "Failed to clear terminal, ignoring: %m"); |
| 188 |
|
| 189 |
r = terminal_set_cursor_position(fd, 2, 4); |
| 190 |
if (r < 0) |
| 191 |
log_warning_errno(r, "Failed to move terminal cursor position, ignoring: %m"); |
| 192 |
|
| 193 |
r = loop_write(fd, "The current boot has failed!", SIZE_MAX); |
| 194 |
if (r < 0) { |
| 195 |
log_error_errno(r, "Failed to write to terminal: %m"); |
| 196 |
goto cleanup; |
| 197 |
} |
| 198 |
|
| 199 |
qr_code_start_row = w.ws_row * 3U / 5U; |
| 200 |
qr_code_start_column = w.ws_col * 3U / 4U; |
| 201 |
r = terminal_set_cursor_position(fd, 4, 4); |
| 202 |
if (r < 0) |
| 203 |
log_warning_errno(r, "Failed to move terminal cursor position, ignoring: %m"); |
| 204 |
|
| 205 |
r = loop_write(fd, message, SIZE_MAX); |
| 206 |
if (r < 0) { |
| 207 |
log_error_errno(r, "Failed to write emergency message to terminal: %m"); |
| 208 |
goto cleanup; |
| 209 |
} |
| 210 |
|
| 211 |
r = fdopen_independent(fd, "r+", &f); |
| 212 |
if (r < 0) { |
| 213 |
r = log_error_errno(errno, "Failed to open output file: %m"); |
| 214 |
goto cleanup; |
| 215 |
} |
| 216 |
|
| 217 |
r = print_qrcode_full(f, "Scan the error message", |
| 218 |
message, qr_code_start_row, qr_code_start_column, w.ws_col, w.ws_row, |
| 219 |
/* check_tty= */ false); |
| 220 |
if (r < 0) |
| 221 |
log_warning_errno(r, "QR code could not be printed, ignoring: %m"); |
| 222 |
|
| 223 |
r = terminal_set_cursor_position(fd, w.ws_row - 1, w.ws_col * 2U / 5U); |
| 224 |
if (r < 0) |
| 225 |
log_warning_errno(r, "Failed to move terminal cursor position, ignoring: %m"); |
| 226 |
|
| 227 |
r = loop_write(fd, ANSI_BACKGROUND_BLUE "Press any key to exit...", SIZE_MAX); |
| 228 |
if (r < 0) { |
| 229 |
log_error_errno(r, "Failed to write to terminal: %m"); |
| 230 |
goto cleanup; |
| 231 |
} |
| 232 |
|
| 233 |
r = read_one_char(f, &read_character_buffer, USEC_INFINITY, /* echo= */ true, /* need_nl= */ NULL); |
| 234 |
if (r < 0 && r != -EINTR) |
| 235 |
log_warning_errno(r, "Failed to read character, ignoring: %m"); |
| 236 |
|
| 237 |
r = 0; |
| 238 |
|
| 239 |
cleanup: |
| 240 |
if (original_vt > 0 && ioctl(fd, VT_ACTIVATE, original_vt) < 0) |
| 241 |
log_warning_errno(errno, "Failed to switch back to original VT /dev/tty%i: %m", original_vt); |
| 242 |
|
| 243 |
return r; |
| 244 |
} |
| 245 |
|
| 246 |
static int parse_argv(int argc, char *argv[]) { |
| 247 |
assert(argc >= 0); |
| 248 |
assert(argv); |
| 249 |
|
| 250 |
OptionParser opts = { argc, argv }; |
| 251 |
int r; |
| 252 |
|
| 253 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 254 |
switch (c) { |
| 255 |
|
| 256 |
OPTION_COMMON_HELP: |
| 257 |
return help(); |
| 258 |
|
| 259 |
OPTION_COMMON_VERSION: |
| 260 |
return version(); |
| 261 |
|
| 262 |
OPTION('c', "continuous", NULL, "Continuously wait for changes in the journal"): |
| 263 |
arg_continuous = true; |
| 264 |
break; |
| 265 |
|
| 266 |
OPTION_LONG("tty", "TTY", "Specify path to TTY to use"): |
| 267 |
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_tty); |
| 268 |
if (r < 0) |
| 269 |
return r; |
| 270 |
break; |
| 271 |
} |
| 272 |
|
| 273 |
if (option_parser_get_n_args(&opts) > 0) |
| 274 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 275 |
"%s takes no argument.", |
| 276 |
program_invocation_short_name); |
| 277 |
return 1; |
| 278 |
} |
| 279 |
|
| 280 |
static int run(int argc, char *argv[]) { |
| 281 |
/* Don't use SA_RESTART here, as we don't want to restart syscalls on signal |
| 282 |
* to get out of read_one_char() when needed */ |
| 283 |
static const struct sigaction nop_sigaction = { |
| 284 |
.sa_handler = nop_signal_handler, |
| 285 |
.sa_flags = 0, |
| 286 |
}; |
| 287 |
_cleanup_free_ char *message = NULL; |
| 288 |
int r; |
| 289 |
|
| 290 |
log_setup(); |
| 291 |
|
| 292 |
r = parse_argv(argc, argv); |
| 293 |
if (r <= 0) |
| 294 |
return r; |
| 295 |
|
| 296 |
journal_browse_prepare(); |
| 297 |
|
| 298 |
r = acquire_first_emergency_log_message(&message); |
| 299 |
if (r < 0) |
| 300 |
return r; |
| 301 |
|
| 302 |
if (!message) { |
| 303 |
log_debug("No emergency-level entries"); |
| 304 |
return 0; |
| 305 |
} |
| 306 |
|
| 307 |
assert_se(sigaction_many(&nop_sigaction, SIGTERM, SIGINT) >= 0); |
| 308 |
|
| 309 |
return display_emergency_message_fullscreen(message); |
| 310 |
} |
| 311 |
|
| 312 |
DEFINE_MAIN_FUNCTION(run); |