From b6eff45cc4611857a0aea9e729cb1ffa437172ad Mon Sep 17 00:00:00 2001 From: Gao feng Date: Mon, 17 Oct 2016 19:53:37 +0800 Subject: [PATCH] do not call any functions before check errno perror changes the errno, result in the errno checking of execvp incorrect. Signed-off-by: Gao feng --- src/exec.c | 10 ++++++---- src/init.c | 4 ++-- src/net.c | 2 -- src/util.c | 3 +-- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/exec.c b/src/exec.c index ab7a68b7..a0cf8f28 100644 --- a/src/exec.c +++ b/src/exec.c @@ -110,7 +110,6 @@ static int pts_loop(struct hyper_event *de, uint64_t seq, int efd, struct hyper_ do { size = read(de->fd, buf->data + buf->get + 12, buf->size - buf->get - 12); - fprintf(stdout, "%s: read %d data\n", __func__, size); if (size < 0) { if (errno == EINTR) continue; @@ -122,6 +121,8 @@ static int pts_loop(struct hyper_event *de, uint64_t seq, int efd, struct hyper_ break; } + fprintf(stdout, "%s: read %d data\n", __func__, size); + if (size == 0) { // eof pts_hup(de, efd, exec); return 0; @@ -544,13 +545,14 @@ static void hyper_exec_process(struct hyper_exec *exec, struct stdio_config *io) } if (execvp(exec->argv[0], exec->argv) < 0) { + // perror possibly changes the errno. + int err = errno; perror("exec failed"); - /* the exit codes follow the `chroot` standard, see docker/docs/reference/run.md#exit-status */ - if (errno == ENOENT) + if (err == ENOENT) _exit(127); - else if (errno == EACCES) + else if (err == EACCES) _exit(126); } diff --git a/src/init.c b/src/init.c index f366d3a6..ab84d80e 100644 --- a/src/init.c +++ b/src/init.c @@ -1244,14 +1244,14 @@ static int hyper_loop(void) while (1) { n = epoll_pwait(ctl.efd, events, MAXEVENTS, -1, &omask); - fprintf(stdout, "%s epoll_wait %d\n", __func__, n); - if (n < 0) { if (errno == EINTR) continue; perror("hyper wait event failed"); return -1; } + fprintf(stdout, "%s epoll_wait %d\n", __func__, n); + for (i = 0; i < n; i++) { if (hyper_handle_event(ctl.efd, &events[i]) < 0) return -1; diff --git a/src/net.c b/src/net.c index bcbe1336..04073a2e 100644 --- a/src/net.c +++ b/src/net.c @@ -48,7 +48,6 @@ int hyper_send_data(int fd, uint8_t *data, uint32_t len) while (length < len) { size = write(fd, data + length, len - length); - if (size <= 0) { if (errno == EINTR) continue; @@ -99,7 +98,6 @@ int hyper_get_type(int fd, uint32_t *type) while (len < 8) { size = read(fd, buf + len, 8 - len); - if (size <= 0) { if (errno == EINTR) continue; diff --git a/src/util.c b/src/util.c index d2af2b8e..36fcd0f7 100644 --- a/src/util.c +++ b/src/util.c @@ -415,13 +415,12 @@ int hyper_open_channel(char *channel, int mode) { struct termios term; int fd = open(channel, O_RDWR | O_CLOEXEC | mode); - fprintf(stdout, "open %s get %d\n", channel, fd); - if (fd < 0) { perror("fail to open channel device"); return -1; } + fprintf(stdout, "open %s get %d\n", channel, fd); bzero(&term, sizeof(term)); cfmakeraw(&term);