From 0dc7fbcb0ce6a13427c3c362d777554ec55ec74a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Tue, 16 Mar 2021 18:47:31 +0100 Subject: core: add options to customize commands on system signals, quit by default on SIGHUP when not running headless (closes #1595) New options to customize behavior on signals received, with the default behavior: - weechat.signal.sighup: quit in normal mode, reload config in headless - weechat.signal.sigquit: quit - weechat.signal.sigterm: quit - weechat.signal.sigusr1: no command executed by default - weechat.signal.sigusr2: no command executed by default The signals SIGUSR1 and SIGUSR2 are introduced by this commit, so it's now possible to run commands when they are received. The SIGHUP signal makes now WeeChat quit, it was the behavior before version 2.9 of WeeChat (see commit de1e61f7cd50cbd1a99777fe6611642a51abf5f6). --- src/gui/curses/gui-curses-main.c | 114 ++------------------------------------- 1 file changed, 5 insertions(+), 109 deletions(-) (limited to 'src/gui') diff --git a/src/gui/curses/gui-curses-main.c b/src/gui/curses/gui-curses-main.c index 15c95b8eb..4beec888b 100644 --- a/src/gui/curses/gui-curses-main.c +++ b/src/gui/curses/gui-curses-main.c @@ -35,9 +35,9 @@ #include "../../core/wee-config.h" #include "../../core/wee-hook.h" #include "../../core/wee-log.h" +#include "../../core/wee-signal.h" #include "../../core/wee-string.h" #include "../../core/wee-utf8.h" -#include "../../core/wee-util.h" #include "../../core/wee-version.h" #include "../../plugins/plugin.h" #include "../gui-main.h" @@ -164,10 +164,9 @@ gui_main_init () struct t_gui_bar_window *ptr_bar_win; char title[256]; -#ifdef WEECHAT_HEADLESS /* allow Ctrl-C to quit WeeChat in headless mode */ - util_catch_signal (SIGINT, &gui_main_signal_sigint); -#endif /* WEECHAT_HEADLESS */ + if (weechat_headless) + signal_catch (SIGINT, &gui_main_signal_sigint); initscr (); @@ -264,30 +263,6 @@ gui_main_init () gui_window_set_bracketed_paste_mode (CONFIG_BOOLEAN(config_look_paste_bracketed)); } -/* - * Returns signal name with a signal number. - * - * Note: result must be freed after use. - */ - -char * -gui_main_get_signal_name (int signal_number) -{ - const char *signal_name; - char str_signal[32]; - - signal_name = util_signal_search_number (signal_number); - if (!signal_name) - return NULL; - - snprintf (str_signal, sizeof (str_signal), - "SIG%s", - signal_name); - string_toupper (str_signal); - - return strdup (str_signal); -} - /* * Callback for system signal SIGWINCH: refreshes screen. */ @@ -298,82 +273,6 @@ gui_main_signal_sigwinch () gui_signal_sigwinch_received = 1; } -/* - * Sends a WeeChat signal on a system signal received. - * - * Returns: - * WEECHAT_RC_OK: the WeeChat handler must be executed - * WEECHAT_RC_OK_EAT: signal eaten, the WeeChat handler must NOT be executed - */ - -int -gui_main_handle_signal (const char *signal_name) -{ - int rc; - char str_signal[32]; - - if (!signal_name) - return WEECHAT_RC_OK; - - snprintf (str_signal, sizeof (str_signal), "signal_%s", signal_name); - string_tolower (str_signal); - - rc = hook_signal_send (str_signal, WEECHAT_HOOK_SIGNAL_STRING, NULL); - - return (rc == WEECHAT_RC_OK_EAT) ? WEECHAT_RC_OK_EAT : WEECHAT_RC_OK; -} - -/* - * Callback for signals received that will make WeeChat reload configuration. - */ - -void -gui_main_handle_reload_signal () -{ - char *signal_name; - - signal_name = gui_main_get_signal_name (weechat_reload_signal); - - if (gui_main_handle_signal (signal_name) != WEECHAT_RC_OK_EAT) - { - log_printf (_("Signal %s received, reloading configuration..."), - signal_name); - command_reload_files (); - } - - if (signal_name) - free (signal_name); - - weechat_reload_signal = 0; -} - -/* - * Callback for signals received that will make WeeChat quit. - */ - -void -gui_main_handle_quit_signals () -{ - char *signal_name; - - signal_name = gui_main_get_signal_name (weechat_quit_signal); - - if (gui_main_handle_signal (signal_name) != WEECHAT_RC_OK_EAT) - { - if (!weechat_quit) - { - log_printf (_("Signal %s received, exiting WeeChat..."), - signal_name); - (void) hook_signal_send ("quit", WEECHAT_HOOK_SIGNAL_STRING, NULL); - weechat_quit = 1; - } - } - - if (signal_name) - free (signal_name); - - weechat_quit_signal = 0; -} /* * Displays infos about ncurses lib. */ @@ -510,7 +409,7 @@ gui_main_loop () /* catch SIGWINCH signal: redraw screen */ if (!weechat_headless) - util_catch_signal (SIGWINCH, &gui_main_signal_sigwinch); + signal_catch (SIGWINCH, &gui_main_signal_sigwinch); /* hook stdin (read keyboard) */ if (weechat_headless) @@ -566,10 +465,7 @@ gui_main_loop () hook_process_exec (); /* handle signals received */ - if (weechat_reload_signal > 0) - gui_main_handle_reload_signal (); - if (weechat_quit_signal > 0) - gui_main_handle_quit_signals (); + signal_handle (); } /* remove keyboard hook */ -- cgit v1.2.3