aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2016-11-27 16:47:21 +0100
committerMarc André Tanner <mat@brain-dump.org>2016-11-27 16:47:21 +0100
commitce66ec2833143cc14eb6cffc6f16b736df9d1f31 (patch)
treef48ef5c733fbe0dfbdc10f41267e22990dce858f
parent81e20069163d7c0780d759d904149e247f97db86 (diff)
downloadvis-ce66ec2833143cc14eb6cffc6f16b736df9d1f31.tar.gz
vis-ce66ec2833143cc14eb6cffc6f16b736df9d1f31.tar.xz
vis: cleanup signal handling code
Move all signal handling code out of "library" code into user application.
-rw-r--r--main.c24
-rw-r--r--ui-curses.c35
-rw-r--r--vis-core.h2
-rw-r--r--vis.c16
4 files changed, 38 insertions, 39 deletions
diff --git a/main.c b/main.c
index 7bab3cd..78a44c1 100644
--- a/main.c
+++ b/main.c
@@ -2237,16 +2237,32 @@ int main(int argc, char *argv[]) {
/* install signal handlers etc. */
struct sigaction sa;
memset(&sa, 0, sizeof sa);
+ sigfillset(&sa.sa_mask);
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = signal_handler;
- if (sigaction(SIGBUS, &sa, NULL) || sigaction(SIGINT, &sa, NULL))
- vis_die(vis, "sigaction: %s", strerror(errno));
+ if (sigaction(SIGBUS, &sa, NULL) == -1 ||
+ sigaction(SIGINT, &sa, NULL) == -1 ||
+ sigaction(SIGCONT, &sa, NULL) == -1 ||
+ sigaction(SIGWINCH, &sa, NULL) == -1 ||
+ sigaction(SIGTERM, &sa, NULL) == -1 ||
+ sigaction(SIGHUP, &sa, NULL) == -1) {
+ vis_die(vis, "Failed to set signal handler: %s\n", strerror(errno));
+ }
+
+ sa.sa_handler = SIG_IGN;
+ if (sigaction(SIGPIPE, &sa, NULL) == -1)
+ vis_die(vis, "Failed to ignore SIGPIPE\n");
sigset_t blockset;
sigemptyset(&blockset);
+ sigaddset(&blockset, SIGBUS);
+ sigaddset(&blockset, SIGINT);
+ sigaddset(&blockset, SIGCONT);
sigaddset(&blockset, SIGWINCH);
- sigprocmask(SIG_BLOCK, &blockset, NULL);
- signal(SIGPIPE, SIG_IGN);
+ sigaddset(&blockset, SIGTERM);
+ sigaddset(&blockset, SIGHUP);
+ if (sigprocmask(SIG_BLOCK, &blockset, NULL) == -1)
+ vis_die(vis, "Failed to block signals\n");
for (int i = 1; i < argc; i++) {
if (argv[i][0] != '-') {
diff --git a/ui-curses.c b/ui-curses.c
index 1b1a158..45e4a28 100644
--- a/ui-curses.c
+++ b/ui-curses.c
@@ -7,7 +7,6 @@
#include <strings.h>
#include <limits.h>
#include <ctype.h>
-#include <signal.h>
#include <locale.h>
#include <poll.h>
#include <sys/ioctl.h>
@@ -102,17 +101,6 @@ struct UiCursesWin {
CellStyle styles[UI_STYLE_MAX];
};
-static volatile sig_atomic_t need_resize; /* SIGWINCH received */
-static volatile sig_atomic_t terminate; /* SIGTERM received */
-
-static void sigwinch_handler(int sig) {
- need_resize = true;
-}
-
-static void sigterm_handler(int sig) {
- terminate = true;
-}
-
__attribute__((noreturn)) static void ui_die(Ui *ui, const char *msg, va_list ap) {
UiCurses *uic = (UiCurses*)ui;
endwin();
@@ -843,16 +831,6 @@ static void ui_resize(Ui *ui) {
static void ui_update(Ui *ui) {
UiCurses *uic = (UiCurses*)ui;
- if (need_resize) {
- need_resize = false;
- ui_resize(ui);
- vis_update(uic->vis);
- return;
- }
-
- if (terminate)
- ui_die_msg(ui, "Killed by SIGTERM\n");
-
for (UiCursesWin *win = uic->windows; win; win = win->next)
ui_window_update(win);
debug("ui-doupdate\n");
@@ -1156,20 +1134,7 @@ static bool ui_init(Ui *ui, Vis *vis) {
meta(stdscr, TRUE);
curs_set(0);
- struct sigaction sa;
- sa.sa_flags = 0;
- sigemptyset(&sa.sa_mask);
- sa.sa_handler = sigwinch_handler;
- if (sigaction(SIGWINCH, &sa, NULL) == -1)
- goto err;
- if (sigaction(SIGCONT, &sa, NULL) == -1)
- goto err;
- sa.sa_handler = sigterm_handler;
- if (sigaction(SIGTERM, &sa, NULL) == -1)
- goto err;
-
ui_resize(ui);
-
return true;
err:
ui_die_msg(ui, "Failed to start curses interface: %s\n", errno != 0 ? strerror(errno) : "");
diff --git a/vis-core.h b/vis-core.h
index 56c51bf..79270f6 100644
--- a/vis-core.h
+++ b/vis-core.h
@@ -178,6 +178,8 @@ struct Vis {
int exit_status; /* exit status when terminating main loop */
volatile sig_atomic_t cancel_filter; /* abort external command/filter (SIGINT occured) */
volatile sig_atomic_t sigbus; /* one of the memory mapped region became unavailable (SIGBUS) */
+ volatile sig_atomic_t need_resize; /* need to resize UI (SIGWINCH occured) */
+ volatile sig_atomic_t terminate; /* need to terminate we were being killed by SIGTERM */
sigjmp_buf sigbus_jmpbuf; /* used to jump back to a known good state in the mainloop after (SIGBUS) */
Map *actions; /* registered editor actions / special keys commands */
lua_State *lua; /* lua context used for syntax highligthing */
diff --git a/vis.c b/vis.c
index b7619fe..c8a102b 100644
--- a/vis.c
+++ b/vis.c
@@ -994,6 +994,14 @@ bool vis_signal_handler(Vis *vis, int signum, const siginfo_t *siginfo, const vo
case SIGINT:
vis->cancel_filter = true;
return true;
+ case SIGCONT:
+ case SIGWINCH:
+ vis->need_resize = true;
+ return true;
+ case SIGTERM:
+ case SIGHUP:
+ vis->terminate = true;
+ return true;
}
return false;
}
@@ -1037,6 +1045,14 @@ int vis_run(Vis *vis, int argc, char *argv[]) {
free(name);
}
+ if (vis->terminate)
+ vis_die(vis, "Killed by SIGTERM\n");
+
+ if (vis->need_resize) {
+ vis->ui->resize(vis->ui);
+ vis->need_resize = false;
+ }
+
vis_update(vis);
idle.tv_sec = vis->mode->idle_timeout;
int r = pselect(1, &fds, NULL, NULL, timeout, &emptyset);