Skip to content

Commit

Permalink
Restore signal handlers in wl-copy before spawning children
Browse files Browse the repository at this point in the history
We ignore SIGHUP and SIGPIPE in wl-copy itself, yet we want our child
processes, notably cat, to still get terminated if it receives SIGPIPE.
Signal handlers set to SIG_IGN unfortunately get inherited over exec, so
reset them before execing.

Fixes #190
  • Loading branch information
bugaevc committed Aug 20, 2023
1 parent 8dbdefb commit 6256adf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/types/copy-action.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <signal.h>

static void do_set_selection(struct copy_action *self, uint32_t serial) {
/* Set the selection and make sure it reaches
Expand Down Expand Up @@ -86,6 +87,8 @@ static void do_send(struct source *source, const char *mime_type, int fd) {
close(self->fd_to_copy_from);
dup2(fd, STDOUT_FILENO);
close(fd);
signal(SIGHUP, SIG_DFL);
signal(SIGPIPE, SIG_DFL);
execlp("cat", "cat", NULL);
perror("exec cat");
exit(1);
Expand Down
5 changes: 5 additions & 0 deletions src/util/files.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <stdlib.h> // exit
#include <libgen.h> // basename
#include <sys/wait.h>
#include <signal.h>
#include <syslog.h>

#ifdef HAVE_MEMFD
Expand Down Expand Up @@ -167,6 +168,8 @@ char *infer_mime_type_from_contents(const char *file_path) {
/* If we cannot open /dev/null, just close stdin */
close(STDIN_FILENO);
}
signal(SIGHUP, SIG_DFL);
signal(SIGPIPE, SIG_DFL);
execlp("xdg-mime", "xdg-mime", "query", "filetype", file_path, NULL);
exit(1);
}
Expand Down Expand Up @@ -274,6 +277,8 @@ char *dump_stdin_into_a_temp_file() {
}
dup2(fd, STDOUT_FILENO);
close(fd);
signal(SIGHUP, SIG_DFL);
signal(SIGPIPE, SIG_DFL);
execlp("cat", "cat", NULL);
perror("exec cat");
exit(1);
Expand Down

0 comments on commit 6256adf

Please sign in to comment.