diff --git a/src/util/files.c b/src/util/files.c index 14a3091..750aa0b 100644 --- a/src/util/files.c +++ b/src/util/files.c @@ -123,8 +123,13 @@ char *infer_mime_type_from_contents(const char *file_path) { close(pipefd[0]); close(pipefd[1]); int devnull = open("/dev/null", O_RDONLY); - dup2(devnull, STDIN_FILENO); - close(devnull); + if (devnull >= 0) { + dup2(devnull, STDIN_FILENO); + close(devnull); + } else { + /* If we cannot open /dev/null, just close stdin */ + close(STDIN_FILENO); + } execlp("xdg-mime", "xdg-mime", "query", "filetype", file_path, NULL); exit(1); } diff --git a/src/wl-copy.c b/src/wl-copy.c index 5f1766a..e70ff4f 100644 --- a/src/wl-copy.c +++ b/src/wl-copy.c @@ -30,6 +30,7 @@ #include #include #include +#include // open #include #include @@ -53,7 +54,20 @@ static void did_set_selection_callback(struct copy_action *copy_action) { * We fork our process and leave the * child running in the background, * while exiting in the parent. + * Also replace stdin/stdout with + * /dev/null so the stdout file + * descriptor isn't kept alive. */ + int devnull = open("/dev/null", O_RDWR); + if (devnull >= 0) { + dup2(devnull, STDOUT_FILENO); + dup2(devnull, STDIN_FILENO); + close(devnull); + } else { + /* If we cannot open /dev/null, just close stdin/stdout */ + close(STDIN_FILENO); + close(STDOUT_FILENO); + } pid_t pid = fork(); if (pid < 0) { perror("fork");