summaryrefslogtreecommitdiffstats
path: root/src/journal/bsod.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/journal/bsod.c123
1 files changed, 71 insertions, 52 deletions
diff --git a/src/journal/bsod.c b/src/journal/bsod.c
index a88cb66..b2889f0 100644
--- a/src/journal/bsod.c
+++ b/src/journal/bsod.c
@@ -17,6 +17,7 @@
#include "log.h"
#include "logs-show.h"
#include "main-func.h"
+#include "parse-argument.h"
#include "pretty-print.h"
#include "qrcode-util.h"
#include "sigbus.h"
@@ -25,6 +26,9 @@
#include "terminal-util.h"
static bool arg_continuous = false;
+static char *arg_tty = NULL;
+
+STATIC_DESTRUCTOR_REGISTER(arg_tty, freep);
static int help(void) {
_cleanup_free_ char *link = NULL;
@@ -34,19 +38,22 @@ static int help(void) {
if (r < 0)
return log_oom();
- printf("%s\n\n"
- "%sFilter the journal to fetch the first message from the\n"
- "current boot with an emergency log level and displays it\n"
- "as a string and a QR code.\n\n%s"
+ printf("%1$s [OPTIONS...]\n\n"
+ "%5$sFilter the journal to fetch the first message from the current boot with an%6$s\n"
+ "%5$semergency log level and display it as a string and a QR code.%6$s\n"
+ "\n%3$sOptions:%4$s\n"
" -h --help Show this help\n"
" --version Show package version\n"
" -c --continuous Make systemd-bsod wait continuously\n"
" for changes in the journal\n"
- "\nSee the %s for details.\n",
+ " --tty=TTY Specify path to TTY to use\n"
+ "\nSee the %2$s for details.\n",
program_invocation_short_name,
- ansi_highlight(),
+ link,
+ ansi_underline(),
ansi_normal(),
- link);
+ ansi_highlight(),
+ ansi_normal());
return 0;
}
@@ -60,22 +67,22 @@ static int acquire_first_emergency_log_message(char **ret) {
assert(ret);
- r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
+ r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY | (arg_continuous ? 0 : SD_JOURNAL_ASSUME_IMMUTABLE));
if (r < 0)
return log_error_errno(r, "Failed to open journal: %m");
r = add_match_this_boot(j, NULL);
if (r < 0)
- return log_warning_errno(r, "Failed to add boot ID filter: %m");
+ return log_error_errno(r, "Failed to add boot ID filter: %m");
- r = sd_journal_add_match(j, "_UID=0", 0);
+ r = sd_journal_add_match(j, "_UID=0", SIZE_MAX);
if (r < 0)
- return log_warning_errno(r, "Failed to add User ID filter: %m");
+ return log_error_errno(r, "Failed to add User ID filter: %m");
assert_cc(0 == LOG_EMERG);
- r = sd_journal_add_match(j, "PRIORITY=0", 0);
+ r = sd_journal_add_match(j, "PRIORITY=0", SIZE_MAX);
if (r < 0)
- return log_warning_errno(r, "Failed to add Emergency filter: %m");
+ return log_error_errno(r, "Failed to add Emergency filter: %m");
r = sd_journal_seek_head(j);
if (r < 0)
@@ -103,7 +110,7 @@ static int acquire_first_emergency_log_message(char **ret) {
if (r < 0)
return log_error_errno(r, "Failed to read journal message: %m");
- message = memdup_suffix0((const char*)d + STRLEN("MESSAGE="), l - STRLEN("MESSAGE="));
+ message = memdup_suffix0((const char*)d + strlen("MESSAGE="), l - strlen("MESSAGE="));
if (!message)
return log_oom();
@@ -124,18 +131,18 @@ static int find_next_free_vt(int fd, int *ret_free_vt, int *ret_original_vt) {
for (size_t i = 0; i < sizeof(terminal_status.v_state) * 8; i++)
if ((terminal_status.v_state & (1 << i)) == 0) {
- *ret_free_vt = i;
+ *ret_free_vt = i + 1;
*ret_original_vt = terminal_status.v_active;
return 0;
}
- return log_error_errno(SYNTHETIC_ERRNO(ENOTTY), "No free VT found: %m");
+ return -ENOTTY;
}
static int display_emergency_message_fullscreen(const char *message) {
- int r, ret = 0, free_vt = 0, original_vt = 0;
+ int r, free_vt = 0, original_vt = 0;
unsigned qr_code_start_row = 1, qr_code_start_column = 1;
- char tty[STRLEN("/dev/tty") + DECIMAL_STR_MAX(int) + 1];
+ char ttybuf[STRLEN("/dev/tty") + DECIMAL_STR_MAX(int) + 1];
_cleanup_close_ int fd = -EBADF;
_cleanup_fclose_ FILE *stream = NULL;
char read_character_buffer = '\0';
@@ -143,30 +150,36 @@ static int display_emergency_message_fullscreen(const char *message) {
.ws_col = 80,
.ws_row = 25,
};
+ const char *tty;
assert(message);
- fd = open_terminal("/dev/tty1", O_RDWR|O_NOCTTY|O_CLOEXEC);
- if (fd < 0)
- return log_error_errno(fd, "Failed to open tty1: %m");
+ if (arg_tty)
+ tty = arg_tty;
+ else {
+ fd = open_terminal("/dev/tty1", O_RDWR|O_NOCTTY|O_CLOEXEC);
+ if (fd < 0)
+ return log_error_errno(fd, "Failed to open /dev/tty1: %m");
- r = find_next_free_vt(fd, &free_vt, &original_vt);
- if (r < 0)
- return log_error_errno(r, "Failed to find a free VT: %m");
+ r = find_next_free_vt(fd, &free_vt, &original_vt);
+ if (r < 0)
+ return log_error_errno(r, "Failed to find a free VT: %m");
- xsprintf(tty, "/dev/tty%d", free_vt + 1);
+ xsprintf(ttybuf, "/dev/tty%d", free_vt);
+ tty = ttybuf;
- r = open_terminal(tty, O_RDWR|O_NOCTTY|O_CLOEXEC);
- if (r < 0)
- return log_error_errno(fd, "Failed to open tty: %m");
+ fd = safe_close(fd);
+ }
- close_and_replace(fd, r);
+ fd = open_terminal(tty, O_RDWR|O_NOCTTY|O_CLOEXEC);
+ if (fd < 0)
+ return log_error_errno(fd, "Failed to open %s: %m", tty);
if (ioctl(fd, TIOCGWINSZ, &w) < 0)
log_warning_errno(errno, "Failed to fetch tty size, ignoring: %m");
- if (ioctl(fd, VT_ACTIVATE, free_vt + 1) < 0)
- return log_error_errno(errno, "Failed to activate tty: %m");
+ if (free_vt > 0 && ioctl(fd, VT_ACTIVATE, free_vt) < 0)
+ return log_error_errno(errno, "Failed to activate /dev/tty%i, ignoring: %m", free_vt);
r = loop_write(fd, ANSI_BACKGROUND_BLUE ANSI_HOME_CLEAR, SIZE_MAX);
if (r < 0)
@@ -178,7 +191,7 @@ static int display_emergency_message_fullscreen(const char *message) {
r = loop_write(fd, "The current boot has failed!", SIZE_MAX);
if (r < 0) {
- ret = log_warning_errno(r, "Failed to write to terminal: %m");
+ log_error_errno(r, "Failed to write to terminal: %m");
goto cleanup;
}
@@ -190,13 +203,13 @@ static int display_emergency_message_fullscreen(const char *message) {
r = loop_write(fd, message, SIZE_MAX);
if (r < 0) {
- ret = log_warning_errno(r, "Failed to write emergency message to terminal: %m");
+ log_error_errno(r, "Failed to write emergency message to terminal: %m");
goto cleanup;
}
r = fdopen_independent(fd, "r+", &stream);
if (r < 0) {
- ret = log_error_errno(errno, "Failed to open output file: %m");
+ r = log_error_errno(errno, "Failed to open output file: %m");
goto cleanup;
}
@@ -208,37 +221,41 @@ static int display_emergency_message_fullscreen(const char *message) {
if (r < 0)
log_warning_errno(r, "Failed to move terminal cursor position, ignoring: %m");
- r = loop_write(fd, "Press any key to exit...", SIZE_MAX);
+ r = loop_write(fd, ANSI_BACKGROUND_BLUE "Press any key to exit...", SIZE_MAX);
if (r < 0) {
- ret = log_warning_errno(r, "Failed to write to terminal: %m");
+ log_error_errno(r, "Failed to write to terminal: %m");
goto cleanup;
}
r = read_one_char(stream, &read_character_buffer, USEC_INFINITY, NULL);
if (r < 0 && r != -EINTR)
- ret = log_error_errno(r, "Failed to read character: %m");
+ log_error_errno(r, "Failed to read character: %m");
+
+ r = 0;
cleanup:
- if (ioctl(fd, VT_ACTIVATE, original_vt) < 0)
- return log_error_errno(errno, "Failed to switch back to original VT: %m");
+ if (original_vt > 0 && ioctl(fd, VT_ACTIVATE, original_vt) < 0)
+ log_warning_errno(errno, "Failed to switch back to original VT /dev/tty%i: %m", original_vt);
- return ret;
+ return r;
}
static int parse_argv(int argc, char * argv[]) {
enum {
ARG_VERSION = 0x100,
+ ARG_TTY,
};
static const struct option options[] = {
- { "help", no_argument, NULL, 'h' },
- { "version", no_argument, NULL, ARG_VERSION },
- { "continuous", no_argument, NULL, 'c' },
+ { "help", no_argument, NULL, 'h' },
+ { "version", no_argument, NULL, ARG_VERSION },
+ { "continuous", no_argument, NULL, 'c' },
+ { "tty", required_argument, NULL, ARG_TTY },
{}
};
- int c;
+ int c, r;
assert(argc >= 0);
assert(argv);
@@ -257,6 +274,13 @@ static int parse_argv(int argc, char * argv[]) {
arg_continuous = true;
break;
+ case ARG_TTY:
+ r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_tty);
+ if (r < 0)
+ return r;
+
+ break;
+
case '?':
return -EINVAL;
@@ -281,8 +305,7 @@ static int run(int argc, char *argv[]) {
_cleanup_free_ char *message = NULL;
int r;
- log_open();
- log_parse_environment();
+ log_setup();
sigbus_install();
@@ -292,7 +315,7 @@ static int run(int argc, char *argv[]) {
r = acquire_first_emergency_log_message(&message);
if (r < 0)
- return log_error_errno(r, "Failed to acquire first emergency log message: %m");
+ return r;
if (!message) {
log_debug("No emergency-level entries");
@@ -301,11 +324,7 @@ static int run(int argc, char *argv[]) {
assert_se(sigaction_many(&nop_sigaction, SIGTERM, SIGINT) >= 0);
- r = display_emergency_message_fullscreen((const char*) message);
- if (r < 0)
- return log_error_errno(r, "Failed to display emergency message on terminal: %m");
-
- return 0;
+ return display_emergency_message_fullscreen(message);
}
DEFINE_MAIN_FUNCTION(run);