Merge branch 'url-launcher-redirect-stdout-stderr'

This commit is contained in:
Daniel Eklöf 2021-02-26 09:15:16 +01:00
commit ebf8070244
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
2 changed files with 37 additions and 13 deletions

35
spawn.c
View file

@ -35,28 +35,39 @@ spawn(struct reaper *reaper, const char *cwd, char *const argv[],
/* Child */
close(pipe_fds[0]);
if (setsid() < 0)
goto child_err;
/* Clear signal mask */
sigset_t mask;
sigemptyset(&mask);
if (sigprocmask(SIG_SETMASK, &mask, NULL) < 0) {
const int errno_copy = errno;
LOG_ERRNO("failed to restore signals");
(void)!write(pipe_fds[1], &errno_copy, sizeof(errno_copy));
_exit(errno_copy);
}
if (sigprocmask(SIG_SETMASK, &mask, NULL) < 0)
goto child_err;
if ((stdin_fd >= 0 && (dup2(stdin_fd, STDIN_FILENO) < 0 || close(stdin_fd) < 0)) ||
(stdout_fd >= 0 && (dup2(stdout_fd, STDOUT_FILENO) < 0 || close(stdout_fd) < 0)) ||
(stderr_fd >= 0 && (dup2(stderr_fd, STDERR_FILENO) < 0 || close(stderr_fd) < 0)) ||
bool close_stderr = stderr_fd >= 0;
bool close_stdout = stdout_fd >= 0 && stdout_fd != stderr_fd;
bool close_stdin = stdin_fd >= 0 && stdin_fd != stdout_fd && stdin_fd != stderr_fd;
if ((stdin_fd >= 0 && (dup2(stdin_fd, STDIN_FILENO) < 0
|| (close_stdin && close(stdin_fd) < 0))) ||
(stdout_fd >= 0 && (dup2(stdout_fd, STDOUT_FILENO) < 0
|| (close_stdout && close(stdout_fd) < 0))) ||
(stderr_fd >= 0 && (dup2(stderr_fd, STDERR_FILENO) < 0
|| (close_stderr && close(stderr_fd) < 0))) ||
(cwd != NULL && chdir(cwd) < 0) ||
execvp(argv[0], argv) < 0)
{
const int errno_copy = errno;
(void)!write(pipe_fds[1], &errno_copy, sizeof(errno_copy));
_exit(errno_copy);
goto child_err;
}
xassert(false);
_exit(errno);
child_err:
;
const int errno_copy = errno;
(void)!write(pipe_fds[1], &errno_copy, sizeof(errno_copy));
_exit(errno_copy);
}
/* Parent */

View file

@ -3,6 +3,10 @@
#include <stdlib.h>
#include <string.h>
#include <wctype.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#define LOG_MODULE "url-mode"
#define LOG_ENABLE_DBG 0
@ -81,18 +85,27 @@ activate_url(struct seat *seat, struct terminal *term, const struct url *url)
size_t argc;
char **argv;
int dev_null = open("/dev/null", O_RDWR);
if (dev_null < 0) {
LOG_ERRNO("failed to open /dev/null");
break;
}
if (spawn_expand_template(
&term->conf->url_launch, 1,
(const char *[]){"url"},
(const char *[]){url_string},
&argc, &argv))
{
spawn(term->reaper, term->cwd, argv, -1, -1, -1);
spawn(term->reaper, term->cwd, argv, dev_null, dev_null, dev_null);
for (size_t i = 0; i < argc; i++)
free(argv[i]);
free(argv);
}
close(dev_null);
break;
}
}