diff --git a/src/container.c b/src/container.c index c174d98..4d4d31b 100644 --- a/src/container.c +++ b/src/container.c @@ -567,8 +567,9 @@ fail: close(arg.pipe[0]); close(arg.pipe[1]); close(container->ns); - hyper_reset_event(&container->exec.e); - hyper_reset_event(&container->exec.errev); + hyper_reset_event(&container->exec.stdinev); + hyper_reset_event(&container->exec.stdoutev); + hyper_reset_event(&container->exec.stderrev); container->ns = -1; fprintf(stdout, "container %s init exit code %d\n", container->id, -1); container->exec.code = -1; diff --git a/src/event.c b/src/event.c index c6dd3e0..92aa03b 100644 --- a/src/event.c +++ b/src/event.c @@ -230,10 +230,10 @@ int hyper_handle_event(int efd, struct epoll_event *event) __func__, event->events, de, de->fd, de->ops); /* do not handle hup event if have in event */ - if (event->events & EPOLLIN) { + if ((event->events & EPOLLIN) && de->ops->read) { fprintf(stdout, "%s event EPOLLIN, de %p, fd %d, %p\n", __func__, de, de->fd, de->ops); - if (de->ops->read(de, efd) < 0) + if (de->ops->read && de->ops->read(de, efd) < 0) return -1; } else if (event->events & EPOLLHUP) { fprintf(stdout, "%s event EPOLLHUP, de %p, fd %d, %p\n", diff --git a/src/exec.c b/src/exec.c index 930b517..3d14d52 100644 --- a/src/exec.c +++ b/src/exec.c @@ -80,16 +80,23 @@ static void pts_hup(struct hyper_event *de, int efd, struct hyper_exec *exec) hyper_release_exec(exec, pod); } +static void stdin_hup(struct hyper_event *de, int efd) +{ + struct hyper_exec *exec = container_of(de, struct hyper_exec, stdinev); + fprintf(stdout, "%s\n", __func__); + return pts_hup(de, efd, exec); +} + static void stdout_hup(struct hyper_event *de, int efd) { - struct hyper_exec *exec = container_of(de, struct hyper_exec, e); + struct hyper_exec *exec = container_of(de, struct hyper_exec, stdoutev); fprintf(stdout, "%s\n", __func__); return pts_hup(de, efd, exec); } static void stderr_hup(struct hyper_event *de, int efd) { - struct hyper_exec *exec = container_of(de, struct hyper_exec, errev); + struct hyper_exec *exec = container_of(de, struct hyper_exec, stderrev); fprintf(stdout, "%s\n", __func__); return pts_hup(de, efd, exec); } @@ -131,28 +138,33 @@ static int pts_loop(struct hyper_event *de, uint64_t seq, int efd, struct hyper_ return 0; } +struct hyper_event_ops in_ops = { + .hup = stdin_hup, + .write = hyper_event_write, + .wbuf_size = 512, +}; + static int stdout_loop(struct hyper_event *de, int efd) { - struct hyper_exec *exec = container_of(de, struct hyper_exec, e); + struct hyper_exec *exec = container_of(de, struct hyper_exec, stdoutev); fprintf(stdout, "%s, seq %" PRIu64"\n", __func__, exec->seq); return pts_loop(de, exec->seq, efd, exec); } -struct hyper_event_ops pts_ops = { +struct hyper_event_ops out_ops = { .read = stdout_loop, .hup = stdout_hup, - .write = hyper_event_write, - .wbuf_size = 512, /* don't need read buff, the pts data will store in tty buffer */ + /* don't need write buff, the stdout data is one way */ }; static int stderr_loop(struct hyper_event *de, int efd) { - struct hyper_exec *exec = container_of(de, struct hyper_exec, errev); + struct hyper_exec *exec = container_of(de, struct hyper_exec, stderrev); fprintf(stdout, "%s, seq %" PRIu64"\n", __func__, exec->errseq); - return pts_loop(de, exec->errseq, efd, exec); + return pts_loop(de, exec->errseq ? exec->errseq : exec->seq, efd, exec); } struct hyper_event_ops err_ops = { @@ -165,6 +177,7 @@ struct hyper_event_ops err_ops = { int hyper_setup_exec_tty(struct hyper_exec *e) { int unlock = 0; + int ptymaster; char ptmx[512], path[512]; if (e->seq == 0) { @@ -179,7 +192,7 @@ int hyper_setup_exec_tty(struct hyper_exec *e) return -1; } hyper_setfd_nonblock(errpipe[0]); - e->errev.fd = errpipe[0]; + e->stderrev.fd = errpipe[0]; e->stderrfd = errpipe[1]; } @@ -190,7 +203,8 @@ int hyper_setup_exec_tty(struct hyper_exec *e) return -1; } hyper_setfd_nonblock(iopair[0]); - e->e.fd = iopair[0]; + e->stdinev.fd = iopair[0]; + e->stdoutev.fd = dup(iopair[0]); e->ptyfd = iopair[1]; goto done; } @@ -212,18 +226,18 @@ int hyper_setup_exec_tty(struct hyper_exec *e) return -1; } - e->e.fd = open(ptmx, O_RDWR | O_NOCTTY | O_NONBLOCK | O_CLOEXEC); - if (e->e.fd < 0) { + ptymaster = open(ptmx, O_RDWR | O_NOCTTY | O_NONBLOCK | O_CLOEXEC); + if (ptymaster < 0) { perror("open ptmx device for execcmd failed"); return -1; } - if (ioctl(e->e.fd, TIOCSPTLCK, &unlock) < 0) { + if (ioctl(ptymaster, TIOCSPTLCK, &unlock) < 0) { perror("ioctl unlock ptmx device failed"); return -1; } - if (ioctl(e->e.fd, TIOCGPTN, &e->ptyno) < 0) { + if (ioctl(ptymaster, TIOCGPTN, &e->ptyno) < 0) { perror("ioctl get execcmd pty device failed"); return -1; } @@ -236,13 +250,18 @@ int hyper_setup_exec_tty(struct hyper_exec *e) e->ptyfd = open(ptmx, O_RDWR | O_NOCTTY | O_CLOEXEC); fprintf(stdout, "get pty device for exec %s\n", ptmx); + e->stdinev.fd = ptymaster; + e->stdoutev.fd = dup(ptymaster); done: + e->stdinfd = e->ptyfd; e->stdoutfd = e->ptyfd; - if (e->errseq == 0) + if (e->errseq == 0) { + e->stderrev.fd = dup(e->stdoutev.fd); e->stderrfd = e->ptyfd; + } fprintf(stdout, "%s pts event %p, fd %d %d\n", - __func__, &e->e, e->e.fd, e->ptyfd); + __func__, &e->stdinev, ptymaster, e->ptyfd); return 0; } @@ -284,23 +303,27 @@ out: int hyper_watch_exec_pty(struct hyper_exec *exec, struct hyper_pod *pod) { fprintf(stdout, "hyper_init_event container pts event %p, ops %p, fd %d\n", - &exec->e, &pts_ops, exec->e.fd); + &exec->stdinev, &in_ops, exec->stdinev.fd); if (exec->seq == 0) return 0; - if (hyper_init_event(&exec->e, &pts_ops, pod) < 0 || - hyper_add_event(ctl.efd, &exec->e, EPOLLIN) < 0) { - fprintf(stderr, "add container pts master event failed\n"); + if (hyper_init_event(&exec->stdinev, &in_ops, pod) < 0 || + hyper_add_event(ctl.efd, &exec->stdinev, EPOLLOUT) < 0) { + fprintf(stderr, "add container stdin event failed\n"); return -1; } exec->ref++; - if (exec->errseq == 0) - return 0; + if (hyper_init_event(&exec->stdoutev, &out_ops, pod) < 0 || + hyper_add_event(ctl.efd, &exec->stdoutev, EPOLLIN) < 0) { + fprintf(stderr, "add container stdout event failed\n"); + return -1; + } + exec->ref++; - if (hyper_init_event(&exec->errev, &err_ops, pod) < 0 || - hyper_add_event(ctl.efd, &exec->errev, EPOLLIN) < 0) { + if (hyper_init_event(&exec->stderrev, &err_ops, pod) < 0 || + hyper_add_event(ctl.efd, &exec->stderrev, EPOLLIN) < 0) { fprintf(stderr, "add container stderr event failed\n"); return -1; } @@ -557,7 +580,9 @@ close_tty: close(exec->stdoutfd); if (exec->stderrfd != exec->ptyfd) close(exec->stderrfd); - close(exec->e.fd); + close(exec->stdinev.fd); + close(exec->stdoutev.fd); + close(exec->stderrev.fd); free_exec: hyper_free_exec(exec); goto out; @@ -597,8 +622,9 @@ int hyper_release_exec(struct hyper_exec *exec, /* exec has no pty or the pty user already exited */ fprintf(stdout, "last user of exec exit, release\n"); - hyper_reset_event(&exec->e); - hyper_reset_event(&exec->errev); + hyper_reset_event(&exec->stdinev); + hyper_reset_event(&exec->stdoutev); + hyper_reset_event(&exec->stderrev); list_del_init(&exec->list); diff --git a/src/exec.h b/src/exec.h index 96aab1d..7f952cb 100644 --- a/src/exec.h +++ b/src/exec.h @@ -6,8 +6,9 @@ struct hyper_exec { struct list_head list; - struct hyper_event e; - struct hyper_event errev; + struct hyper_event stdinev; + struct hyper_event stdoutev; + struct hyper_event stderrev; char *id; char **argv; int argc; diff --git a/src/init.c b/src/init.c index 6ac87ba..cc0e25d 100644 --- a/src/init.c +++ b/src/init.c @@ -1059,7 +1059,7 @@ static int hyper_ttyfd_handle(struct hyper_event *de, uint32_t len) return 0; } - wbuf = &exec->e.wbuf; + wbuf = &exec->stdinev.wbuf; size = wbuf->size - wbuf->get; if (size == 0) @@ -1073,7 +1073,7 @@ static int hyper_ttyfd_handle(struct hyper_event *de, uint32_t len) if (size > 0) { memcpy(wbuf->data + wbuf->get, rbuf->data + 12, size); wbuf->get += size; - if (hyper_modify_event(ctl.efd, &exec->e, EPOLLIN | EPOLLOUT) < 0) { + if (hyper_modify_event(ctl.efd, &exec->stdinev, EPOLLOUT) < 0) { fprintf(stderr, "modify exec pts event to in & out failed\n"); return -1; } diff --git a/src/parse.c b/src/parse.c index 6ef73ee..32fee32 100644 --- a/src/parse.c +++ b/src/parse.c @@ -469,8 +469,9 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container * c->exec.init = 1; c->exec.code = -1; - c->exec.e.fd = -1; - c->exec.errev.fd = -1; + c->exec.stdinev.fd = -1; + c->exec.stdoutev.fd = -1; + c->exec.stderrev.fd = -1; c->exec.ptyfd = -1; c->exec.stdinfd = -1; c->exec.stdoutfd = -1; @@ -1032,8 +1033,9 @@ realloc: exec->stdinfd = -1; exec->stdoutfd = -1; exec->stderrfd = -1; - exec->e.fd = -1; - exec->errev.fd = -1; + exec->stdinev.fd = -1; + exec->stdoutev.fd = -1; + exec->stderrev.fd = -1; INIT_LIST_HEAD(&exec->list); for (i = 0, j = 0; i < n; i++) {