From aa87be43ba0deb9977a7c159367214afbe7a50b4 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 17 Jul 2019 09:39:12 +0200 Subject: slave: report exec() failure back to main process --- main.c | 54 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 13 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 117b202c..6a8ab8a7 100644 --- a/main.c +++ b/main.c @@ -540,21 +540,49 @@ main(int argc, char *const *argv) wl_display_dispatch_pending(term.wl.display); - term.slave = fork(); - switch (term.slave) { - case -1: - LOG_ERRNO("failed to fork"); - goto out; + { + int fork_pipe[2]; + if (pipe2(fork_pipe, O_CLOEXEC) < 0) { + LOG_ERRNO("failed to create pipe"); + goto out; + } - case 0: - /* Child */ - slave_spawn(term.ptmx, conf.shell); - assert(false); - break; + term.slave = fork(); + switch (term.slave) { + case -1: + LOG_ERRNO("failed to fork"); + close(fork_pipe[0]); + close(fork_pipe[1]); + goto out; + + case 0: + /* Child */ + close(fork_pipe[0]); /* Close read end */ + slave_spawn(term.ptmx, conf.shell, fork_pipe[1]); + assert(false); + break; - default: - LOG_DBG("slave has PID %d", term.slave); - 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 */ -- cgit v1.3