slave: report exec() failure back to main process

This commit is contained in:
Daniel Eklöf 2019-07-17 09:39:12 +02:00
parent 519f721577
commit aa87be43ba
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
3 changed files with 44 additions and 15 deletions

32
main.c
View file

@ -540,22 +540,50 @@ main(int argc, char *const *argv)
wl_display_dispatch_pending(term.wl.display); wl_display_dispatch_pending(term.wl.display);
{
int fork_pipe[2];
if (pipe2(fork_pipe, O_CLOEXEC) < 0) {
LOG_ERRNO("failed to create pipe");
goto out;
}
term.slave = fork(); term.slave = fork();
switch (term.slave) { switch (term.slave) {
case -1: case -1:
LOG_ERRNO("failed to fork"); LOG_ERRNO("failed to fork");
close(fork_pipe[0]);
close(fork_pipe[1]);
goto out; goto out;
case 0: case 0:
/* Child */ /* Child */
slave_spawn(term.ptmx, conf.shell); close(fork_pipe[0]); /* Close read end */
slave_spawn(term.ptmx, conf.shell, fork_pipe[1]);
assert(false); assert(false);
break; break;
default: default: {
close(fork_pipe[1]); /* Close write end */
LOG_DBG("slave has PID %d", term.slave); LOG_DBG("slave has PID %d", term.slave);
int _errno;
static_assert(sizeof(errno) == sizeof(_errno), "errno size mismatch");
ssize_t ret = read(fork_pipe[0], &_errno, sizeof(_errno));
close(fork_pipe[0]);
if (ret < 0) {
LOG_ERRNO("failed to read from pipe");
goto out;
} else if (ret == sizeof(_errno)) {
LOG_ERRNO("%s: failed to execute", conf.shell);
goto out;
} else
LOG_DBG("%s: successfully started", conf.shell);
break; break;
} }
}
}
/* Read logic requires non-blocking mode */ /* Read logic requires non-blocking mode */
{ {

View file

@ -92,7 +92,7 @@ err:
} }
void void
slave_spawn(int ptmx, char *cmd) slave_spawn(int ptmx, char *cmd, int err_fd)
{ {
int pts = -1; int pts = -1;
const char *pts_name = ptsname(ptmx); const char *pts_name = ptsname(ptmx);
@ -138,6 +138,7 @@ slave_spawn(int ptmx, char *cmd)
execvp(argv[0], argv); execvp(argv[0], argv);
err: err:
(void)!write(err_fd, &errno, sizeof(errno));
if (argv) if (argv)
free(argv); free(argv);
if (pts != -1) if (pts != -1)

View file

@ -1,4 +1,4 @@
#pragma once #pragma once
#include <stdbool.h> #include <stdbool.h>
void slave_spawn(int ptmx, char *cmd); void slave_spawn(int ptmx, char *cmd, int err_fd);