fix incorrect logic of hyper_send_type_block

It means set fd block first and send out the type.
And fix the access to exec consurrently, hyper init
should be blocked until do_exec_cmd processes the
exec struct.

Signed-off-by: Gao feng <omarapazanadi@gmail.com>
This commit is contained in:
Gao feng
2015-09-24 10:34:12 +08:00
parent 749387bbfa
commit 08764d5bbc
2 changed files with 39 additions and 10 deletions
+7 -6
View File
@@ -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]);
+32 -4
View File
@@ -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;
}