From 23e24f7f89d03bacae8427139af8e4e46ad99084 Mon Sep 17 00:00:00 2001 From: Vladimir Stoiakin Date: Mon, 12 May 2025 17:47:29 +0300 Subject: [PATCH] misc: do not hang if graphics backend is not available --- CHANGELOG.md | 1 + buffyboard/main.c | 4 ++++ unl0kr/main.c | 42 ++++++++++++++++++++++++------------ unl0kr/terminal.c | 54 +++++------------------------------------------ 4 files changed, 38 insertions(+), 63 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f09d7b..ebff8d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ If a change only affects particular applications, they are listed in parentheses - misc: Use standard C library instead of builtin functions (!49, thanks @vstoiakin) - feat: Add support for split packaging of buffyboard and unl0kr (!54, thanks @vstoiakin) +- fix: Do not hang if graphics backend is not available (!57, thanks @vstoiakin) ## 3.3.0 (2025-04-15) diff --git a/buffyboard/main.c b/buffyboard/main.c index 65d7629..c8f4c67 100644 --- a/buffyboard/main.c +++ b/buffyboard/main.c @@ -221,6 +221,10 @@ int main(int argc, char *argv[]) { /* Initialise display */ lv_display_t *disp = lv_linux_fbdev_create(); + if (access("/dev/fb0", F_OK) != 0) { + bbx_log(BBX_LOG_LEVEL_ERROR, "/dev/fb0 is not available"); + sigaction_handler(SIGTERM); + } lv_linux_fbdev_set_file(disp, "/dev/fb0"); if (conf_opts.quirks.fbdev_force_refresh) { lv_linux_fbdev_set_force_refresh(disp, true); diff --git a/unl0kr/main.c b/unl0kr/main.c index 85599d1..b600747 100644 --- a/unl0kr/main.c +++ b/unl0kr/main.c @@ -25,7 +25,6 @@ #include #include -#include #include @@ -193,6 +192,11 @@ static void shutdown(void); */ static void sigaction_handler(int signum); +/** + * Restore the terminal and exit from the program with EXIT_FAILURE. + */ +static void exit_failure(); + /** * Static functions @@ -372,6 +376,11 @@ static void sigaction_handler(int signum) { exit(0); } +static void exit_failure() { + ul_terminal_reset_current_terminal(); + exit(EXIT_FAILURE); +} + /** * Main @@ -423,6 +432,10 @@ int main(int argc, char *argv[]) { case UL_BACKENDS_BACKEND_FBDEV: bbx_log(BBX_LOG_LEVEL_VERBOSE, "Using framebuffer backend"); disp = lv_linux_fbdev_create(); + if (access("/dev/fb0", F_OK) != 0) { + bbx_log(BBX_LOG_LEVEL_ERROR, "/dev/fb0 is not available"); + exit_failure(); + } lv_linux_fbdev_set_file(disp, "/dev/fb0"); if (conf_opts.quirks.fbdev_force_refresh) { lv_linux_fbdev_set_force_refresh(disp, true); @@ -434,27 +447,28 @@ int main(int argc, char *argv[]) { bbx_log(BBX_LOG_LEVEL_VERBOSE, "Using DRM backend"); disp = lv_linux_drm_create(); - char *format_string = "/dev/dri/card%d"; - char drm_path[16] = {0}; - struct stat buffer; + char drm_path[16]; + bool found = false; + for (int i = 0; i < 9; i++) { + sprintf(drm_path, "/dev/dri/card%d", i); - for (size_t i = 0; i < 9; i++) { - sprintf(drm_path, format_string, i); - - if (stat(drm_path, &buffer) != 0) { - continue; + if (access(drm_path, F_OK) == 0) { + found = true; + break; } - - lv_linux_drm_set_file(disp, drm_path, -1); - - break; } + if (!found) { + bbx_log(BBX_LOG_LEVEL_ERROR, "/dev/dri/card* are not available"); + exit_failure(); + } + + lv_linux_drm_set_file(disp, drm_path, -1); break; #endif /* LV_USE_LINUX_DRM */ default: bbx_log(BBX_LOG_LEVEL_ERROR, "Unable to find suitable backend"); - exit(EXIT_FAILURE); + exit_failure(); } /* Override display properties with command line options if necessary */ diff --git a/unl0kr/terminal.c b/unl0kr/terminal.c index 4cff459..1501216 100644 --- a/unl0kr/terminal.c +++ b/unl0kr/terminal.c @@ -26,50 +26,6 @@ static int current_fd = -1; static int original_mode = -1; static int original_kb_mode = -1; - -/** - * Static prototypes - */ - -/** - * Close the current file descriptor and reopen /dev/tty0. - * - * @return true if opening was successful, false otherwise - */ -static bool reopen_current_terminal(void); - -/** - * Close the current file descriptor. - */ -static void close_current_terminal(void); - - -/** - * Static functions - */ - -static bool reopen_current_terminal(void) { - close_current_terminal(); - - current_fd = open("/dev/tty0", O_RDWR); - if (current_fd < 0) { - bbx_log(BBX_LOG_LEVEL_WARNING, "Could not open /dev/tty0"); - return false; - } - - return true; -} - -static void close_current_terminal(void) { - if (current_fd < 0) { - return; - } - - close(current_fd); - current_fd = -1; -} - - /** * Public functions */ @@ -80,11 +36,10 @@ void ul_terminal_prepare_current_terminal(bool enable_graphics_mode, bool disabl return; } - /* Reopen the current terminal */ - reopen_current_terminal(); - + /* Open the current terminal */ + current_fd = open("/dev/tty0", O_RDWR); if (current_fd < 0) { - bbx_log(BBX_LOG_LEVEL_WARNING, "Could not prepare current terminal"); + bbx_log(BBX_LOG_LEVEL_WARNING, "Could not open /dev/tty0"); return; } @@ -128,5 +83,6 @@ void ul_terminal_reset_current_terminal(void) { bbx_log(BBX_LOG_LEVEL_WARNING, "Could not reset terminal keyboard mode"); } - close_current_terminal(); + close(current_fd); + current_fd = -1; }