diff --git a/src/preview.c b/src/preview.c index 1213dce..9fda907 100644 --- a/src/preview.c +++ b/src/preview.c @@ -1,4 +1,5 @@ #include +#include #include #include "utils.h" @@ -115,7 +116,7 @@ static void check_init_previews(void) } } -static int run(Preview *p, int *exitcode) +static int run(Preview *p, int *exitcode, int *signal) { int pipe_fds[2]; ERRCHK_RET(pipe(pipe_fds) == -1, FUNCFAILED("pipe"), ERRNOS); @@ -124,7 +125,7 @@ static int run(Preview *p, int *exitcode) char *script = prepend_helpers(p->script, p->script_len); char *args[] = SHELL_ARGS(script); - int ret = spawn(args, NULL, exitcode, spawn_redirect, sp_arg); + int ret = spawn(args, NULL, exitcode, signal, spawn_redirect, sp_arg); free(script); close(pipe_fds[1]); @@ -179,7 +180,7 @@ int preview_run(const char *ext, const char *mimetype, PreviewArgs *pa) Preview *p; size_t i = 0; - int exitcode; + int exitcode, signal; char mimetype_c[MIMETYPE_MAX], *t, *s; strncpy(mimetype_c, mimetype, LEN(mimetype_c) - 1); @@ -192,7 +193,8 @@ run: return ERR; } - ERRCHK_RET_OK(run(p, &exitcode)); + ERRCHK_RET_OK(run(p, &exitcode, &signal)); + switch (exitcode) { case FAILED_PREVIEW_EC: i++; @@ -202,6 +204,9 @@ run: break; } + if (signal == SIGPIPE) + exitcode = 0; + return exitcode == 0 ? OK : ERR; } diff --git a/src/server.c b/src/server.c index d42eaca..e9b6fc7 100644 --- a/src/server.c +++ b/src/server.c @@ -26,7 +26,7 @@ static void kill_ueberzug(void) PRINTINTERR(FUNCFAILED("kill"), ERRNOS); } - spawn_wait(ueberzug_pid, NULL); + spawn_wait(ueberzug_pid, NULL, NULL); } static void sig_handler_exit(int s) @@ -67,7 +67,7 @@ static int listen(char *fifo) char *args[] = { "ueberzug", "layer", NULL }; int sp_arg[] = { pipe_fds[1], pipe_fds[0], STDIN_FILENO }; - if (spawn(args, &ueberzug_pid, NULL, spawn_redirect, sp_arg) != OK) + if (spawn(args, &ueberzug_pid, NULL, NULL, spawn_redirect, sp_arg) != OK) ret = ERR; close(pipe_fds[0]); @@ -126,7 +126,7 @@ exit: static int check_ueberzug(int *exitcode) { char *args[] = SHELL_ARGS("command -v ueberzug > /dev/null"); - return spawn(args, NULL, exitcode, NULL, NULL); + return spawn(args, NULL, exitcode, NULL, NULL, NULL); } static void get_fifo_name(char *buf, size_t len, const char *id_s) @@ -176,7 +176,7 @@ static int run_script(char *script, size_t script_len, char *arg) char *s = prepend_helpers(script, script_len); char *args[] = SHELL_ARGS(s, arg); int exitcode; - ERRCHK_GOTO_OK(spawn(args, NULL, &exitcode, NULL, NULL), ret, cleanup); + ERRCHK_GOTO_OK(spawn(args, NULL, &exitcode, NULL, NULL, NULL), ret, cleanup); cleanup: free(s); diff --git a/src/utils.c b/src/utils.c index 426b649..39135bd 100644 --- a/src/utils.c +++ b/src/utils.c @@ -20,13 +20,24 @@ int spawn_redirect(const void *arg) return OK; } -int spawn_wait(pid_t pid, int *exitcode) +int spawn_wait(pid_t pid, int *exitcode, int *signal) { int stat; ERRCHK_RET(waitpid(pid, &stat, 0) == -1, FUNCFAILED("waitpid"), ERRNOS); - if (exitcode && WIFEXITED(stat)) - *exitcode = WEXITSTATUS(stat); + if (exitcode) + *exitcode = -1; + + if (signal) + *signal = -1; + + if (WIFEXITED(stat)) { + if (exitcode) + *exitcode = WEXITSTATUS(stat); + } else if (WIFSIGNALED(stat)) { + if (signal) + *signal = WTERMSIG(stat); + } return OK; } @@ -39,7 +50,7 @@ int spawn_wait(pid_t pid, int *exitcode) * * cfunc is a function to call when child process is created */ -int spawn(char *args[], pid_t *cpid, int *exitcode, int (*cfunc)(const void *), +int spawn(char *args[], pid_t *cpid, int *exitcode, int *signal, int (*cfunc)(const void *), const void *carg) { if (exitcode) @@ -64,8 +75,8 @@ int spawn(char *args[], pid_t *cpid, int *exitcode, int (*cfunc)(const void *), if (cpid) *cpid = pid; - if (exitcode) - ERRCHK_RET_OK(spawn_wait(pid, exitcode)); + if (exitcode || signal) + ERRCHK_RET_OK(spawn_wait(pid, exitcode, signal)); return OK; } diff --git a/src/utils.h b/src/utils.h index 97c7d9c..6125ee1 100644 --- a/src/utils.h +++ b/src/utils.h @@ -24,9 +24,9 @@ extern char *program; int spawn_redirect(const void *arg); -int spawn_wait(pid_t pid, int *exitcode); -int spawn(char *args[], pid_t *cpid, int *exitcode, int (*cfunc)(const void *), - const void *carg); +int spawn_wait(pid_t pid, int *exitcode, int *signal); +int spawn(char *args[], pid_t *cpid, int *exitcode, int *signal, + int (*cfunc)(const void *), const void *carg); int strcmpnull(const char *s1, const char *s2); int strlennull(const char *s);