handle eof request of stdin

Signed-off-by: Lai Jiangshan <jiangshanlai@gmail.com>
This commit is contained in:
Lai Jiangshan
2016-03-19 10:28:19 +08:00
parent 5279ef3f96
commit a0e1ecb2ba
5 changed files with 30 additions and 6 deletions
+2 -2
View File
@@ -188,7 +188,7 @@ int hyper_event_read(struct hyper_event *de, int efd)
return 0;
}
int hyper_event_write(struct hyper_event *de)
int hyper_event_write(struct hyper_event *de, int efd)
{
struct hyper_buf *buf = &de->wbuf;
uint32_t len = 0;
@@ -246,7 +246,7 @@ int hyper_handle_event(int efd, struct epoll_event *event)
if (event->events & EPOLLOUT) {
fprintf(stdout, "%s event EPOLLOUT, de %p, fd %d, %p\n",
__func__, de, de->fd, de->ops);
if (de->ops->write && de->ops->write(de) < 0)
if (de->ops->write && de->ops->write(de, efd) < 0)
return -1;
}
+2 -2
View File
@@ -8,7 +8,7 @@ struct hyper_event;
struct hyper_event_ops {
int (*read)(struct hyper_event *e, int efd);
int (*write)(struct hyper_event *e);
int (*write)(struct hyper_event *e, int efd);
int (*handle)(struct hyper_event *e, uint32_t len);
void (*hup)(struct hyper_event *e, int efd);
int rbuf_size;
@@ -40,5 +40,5 @@ int hyper_handle_event(int efd, struct epoll_event *event);
void hyper_reset_event(struct hyper_event *de);
void hyper_event_hup(struct hyper_event *de, int efd);
int hyper_event_read(struct hyper_event *dei, int efd);
int hyper_event_write(struct hyper_event *de);
int hyper_event_write(struct hyper_event *de, int efd);
#endif
+14 -1
View File
@@ -136,9 +136,22 @@ static int pts_loop(struct hyper_event *de, uint64_t seq, int efd, struct hyper_
return 0;
}
static int write_to_stdin(struct hyper_event *de, int efd)
{
struct hyper_exec *exec = container_of(de, struct hyper_exec, stdinev);
fprintf(stdout, "%s, seq %" PRIu64"\n", __func__, exec->seq);
int ret = hyper_event_write(de, efd);
if (ret >= 0 && de->wbuf.get == 0 && exec->close_stdin_request)
pts_hup(de, efd, exec);
return ret;
}
struct hyper_event_ops in_ops = {
.hup = stdin_hup,
.write = hyper_event_write,
.write = write_to_stdin,
.wbuf_size = 512,
};
+1
View File
@@ -22,6 +22,7 @@ struct hyper_exec {
int stdinfd;
int stdoutfd;
int stderrfd;
uint8_t close_stdin_request;
uint8_t code;
uint8_t exit;
uint8_t ref;
+11 -1
View File
@@ -1054,7 +1054,7 @@ static int hyper_ttyfd_handle(struct hyper_event *de, uint32_t len)
dprintf(stdout, "find exec %s pid %d, seq is %" PRIu64 "\n",
exec->id ? exec->id : "pod", exec->pid, exec->seq);
// if exec is exited, the event fd of exec is invalid. don't accept any input.
if (exec->exit) {
if (exec->exit || exec->close_stdin_request) {
fprintf(stdout, "exec seq %" PRIu64 " exited, don't accept any input\n", exec->seq);
return 0;
}
@@ -1070,6 +1070,16 @@ static int hyper_ttyfd_handle(struct hyper_event *de, uint32_t len)
if (size > (len - 12))
size = (len - 12);
/* size == 0 means we had received eof */
if (size == 0 && !exec->tty) {
exec->close_stdin_request = 1;
/* we can't hup the stdinev here, force hup on next write */
if (hyper_modify_event(ctl.efd, &exec->stdinev, EPOLLOUT) < 0) {
fprintf(stderr, "modify exec pts event to in & out failed\n");
return -1;
}
}
if (size > 0) {
memcpy(wbuf->data + wbuf->get, rbuf->data + 12, size);
wbuf->get += size;