diff options
| author | Louis Burda <quent.burda@gmail.com> | 2024-10-24 15:21:29 +0200 |
|---|---|---|
| committer | Louis Burda <quent.burda@gmail.com> | 2024-10-24 15:21:29 +0200 |
| commit | 1113d1a57e92f96b6ade240cf6e973ba25c3b4f4 (patch) | |
| tree | c0bdccc757cebabcef5bdd273e179726a739e7f5 | |
| parent | 67b468093963a33c8b6c4893009a27fc7f81e0d1 (diff) | |
| download | tquery-1113d1a57e92f96b6ade240cf6e973ba25c3b4f4.tar.gz tquery-1113d1a57e92f96b6ade240cf6e973ba25c3b4f4.zip | |
Prevent SIGWINCH from interrupting waitpid
| -rw-r--r-- | Makefile | 3 | ||||
| -rw-r--r-- | tquery.c | 51 |
2 files changed, 35 insertions, 19 deletions
@@ -2,7 +2,8 @@ PREFIX ?= /usr/local BINDIR ?= /bin CFLAGS = -std=c99 -D_XOPEN_SOURCE=700 -Og -g -Wunused-function \ - -Wunused-variable -Wconversion -Wsign-compare -Wuninitialized + -Wunused-variable -Wconversion -Wsign-compare -Wuninitialized \ + $(CFLAGS_EXTRA) LDFLAGS = -lcurses all: tquery @@ -1,5 +1,6 @@ #include <curses.h> +#include <fcntl.h> #include <sys/wait.h> #include <sys/poll.h> #include <sys/fcntl.h> @@ -32,6 +33,11 @@ static const struct sigaction sigint_action = { .sa_handler = sigint_handler }; +static struct sigaction sigwinch_action = { + .sa_flags = SA_RESTART, + .sa_handler = SIG_IGN +}; + static int child_fd = -1; static pid_t child_pid = 0; static const char **child_argv = NULL; @@ -137,32 +143,38 @@ static void invoke(const char **argv, pid_t *pid, int *fd, bool in, bool out, bool err) { int out_pipe[2]; - int zfd; - if (fd && *fd >= 0) { - close(*fd); - *fd = -1; + if (fd) { + if (*fd >= 0) { + close(*fd); + *fd = -1; + } + if (pipe(out_pipe)) + die("pipe:"); } - if (pipe(out_pipe)) die("pipe:"); - *pid = fork(); if (*pid < 0) die("fork:"); if (!*pid) { - zfd = open("/dev/null", O_RDWR); - if (zfd < 0) die("open /dev/null"); - if (!in) dup2(zfd, 0); - if (out && fd) dup2(out_pipe[1], 1); - else if (!out) dup2(zfd, 1); - if (!err) dup2(zfd, 2); - if (fd) close(out_pipe[0]); - close(zfd); + if (!in || !out || !err) { + int zfd = open("/dev/null", O_RDWR); + if (zfd < 0) die("open /dev/null"); + if (!in) dup2(zfd, 0); + if (!out) dup2(zfd, 1); + if (!err) dup2(zfd, 2); + close(zfd); + } + if (fd) { + if (out) dup2(out_pipe[1], 1); + close(out_pipe[0]); + close(out_pipe[1]); + } execvp(argv[0], (char *const *) argv); die("execv '%s':"); } else { if (fd) { - close(out_pipe[1]); *fd = out_pipe[0]; + close(out_pipe[1]); } } } @@ -494,7 +506,7 @@ usage(int rc, bool full) fprintf(stderr, " -e, --stderr Dont close child stderr\n"); fprintf(stderr, " -s, --split Split the query into args\n"); fprintf(stderr, " -r, --run Program to run on Enter\n"); - fprintf(stderr, " -r, --run-io Program to run interactively on Enter\n"); + fprintf(stderr, " -R, --run-io Program to run interactively on Enter\n"); fprintf(stderr, " -x, --hook Program to run on Ctrl-X\n"); fprintf(stderr, " -X, --hook-io Program to run interactively on Ctrl-X\n"); fprintf(stderr, "\n"); @@ -552,8 +564,6 @@ main(int argc, char **argv) nav_init(&output_nav); - sigaction(SIGINT, &sigint_action, NULL); - atexit((void (*)()) endwin); if (!newterm(NULL, stderr, stdin)) die("newterm stdin -> stderr"); @@ -561,6 +571,11 @@ main(int argc, char **argv) noecho(); keypad(stdscr, true); + sigaction(SIGINT, &sigint_action, NULL); + sigaction(SIGWINCH, NULL, &sigwinch_action); + sigwinch_action.sa_flags |= SA_RESTART; + sigaction(SIGWINCH, &sigwinch_action, NULL); + timeout(0); spawn(""); |
