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

54
main.c
View file

@ -540,21 +540,49 @@ main(int argc, char *const *argv)
wl_display_dispatch_pending(term.wl.display); wl_display_dispatch_pending(term.wl.display);
term.slave = fork(); {
switch (term.slave) { int fork_pipe[2];
case -1: if (pipe2(fork_pipe, O_CLOEXEC) < 0) {
LOG_ERRNO("failed to fork"); LOG_ERRNO("failed to create pipe");
goto out; goto out;
}
case 0: term.slave = fork();
/* Child */ switch (term.slave) {
slave_spawn(term.ptmx, conf.shell); case -1:
assert(false); LOG_ERRNO("failed to fork");
break; close(fork_pipe[0]);
close(fork_pipe[1]);
goto out;
default: case 0:
LOG_DBG("slave has PID %d", term.slave); /* Child */
break; close(fork_pipe[0]); /* Close read end */
slave_spawn(term.ptmx, conf.shell, fork_pipe[1]);
assert(false);
break;
default: {
close(fork_pipe[1]); /* Close write end */
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;
}
}
} }
/* 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);