diff --git a/src/exec.c b/src/exec.c index ce56fbe..a8170dc 100644 --- a/src/exec.c +++ b/src/exec.c @@ -284,7 +284,7 @@ static int hyper_do_exec_cmd(void *data) struct hyper_exec_arg *arg = data; struct hyper_exec *exec = arg->exec; struct hyper_pod *pod = arg->pod; - int pipe[2], pid; + int pipe[2] = {-1, -1}, pid; if (exec->id) { char path[512]; @@ -324,11 +324,6 @@ static int hyper_do_exec_cmd(void *data) goto out; } - if (hyper_send_type_block(arg->pipe[1], READY, 0) < 0) { - fprintf(stderr, "%s send ready message failed\n", __func__); - goto out; - } - fprintf(stdout, "hyper init get ready message\n"); exec->pid = pid; fprintf(stdout, "create exec cmd %s pid %d\n", exec->argv[0], pid); @@ -339,6 +334,11 @@ static int hyper_do_exec_cmd(void *data) fprintf(stderr, "add pts master event failed\n"); goto out; } + + if (hyper_send_type_block(arg->pipe[1], READY, 0) < 0) { + fprintf(stderr, "%s send ready message failed\n", __func__); + goto out; + } out: close(pipe[0]); close(pipe[1]); @@ -419,6 +419,7 @@ int hyper_exec_cmd(char *json, int length) return -1; } + fprintf(stdout, "%s get ready message %"PRIu32 "\n", __func__, type); ret = 0; out: close(arg.pipe[0]); diff --git a/src/net.c b/src/net.c index ff1f3c6..4ece161 100644 --- a/src/net.c +++ b/src/net.c @@ -90,7 +90,7 @@ int hyper_send_type(int fd, uint32_t type) return hyper_send_msg(fd, type, 0, NULL); } -int hyper_get_type_block(int fd, uint32_t *type) +int hyper_get_type(int fd, uint32_t *type) { int len = 0, size; uint8_t buf[8]; @@ -111,6 +111,34 @@ int hyper_get_type_block(int fd, uint32_t *type) return 0; } +int hyper_get_type_block(int fd, uint32_t *type) +{ + int ret = 0, flags; + + flags = fcntl(fd, F_GETFL, 0); + if (flags < 0) { + fprintf(stderr, "%s get fd flag failed\n", __func__); + return -1; + } + + if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) < 0) { + perror("set fd BLOCK failed"); + return -1; + } + + ret = hyper_get_type(fd, type); + if (ret < 0) { + fprintf(stderr, "%s can not get type\n", __func__); + } + + if (fcntl(fd, F_SETFL, flags) < 0) { + perror("restore fd flag failed"); + return -1; + } + + return ret; +} + int hyper_send_type_block(int fd, uint32_t type, int need_ack) { int ret = 0, flags; @@ -134,7 +162,7 @@ int hyper_send_type_block(int fd, uint32_t type, int need_ack) if (need_ack == 0) goto out; - ret = hyper_get_type_block(fd, &t); + ret = hyper_get_type(fd, &t); if (ret < 0) { fprintf(stderr, "can not get type\n"); goto out; @@ -145,8 +173,8 @@ int hyper_send_type_block(int fd, uint32_t type, int need_ack) if (t != ACK) ret = -1; out: - if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) { - perror("set fd BLOCK failed"); + if (fcntl(fd, F_SETFL, flags) < 0) { + perror("restore fd flag failed"); return -1; }