use parson to parse kill command

Signed-off-by: Lai Jiangshan <jiangshanlai@gmail.com>
This commit is contained in:
Lai Jiangshan
2016-06-30 12:09:15 +08:00
parent b15d771a56
commit d919414192
4 changed files with 7 additions and 73 deletions
-5
View File
@@ -79,11 +79,6 @@ struct hyper_win_size {
uint64_t seq;
};
struct hyper_killer {
char *id;
int signal;
};
struct hyper_reader {
char *id;
char *file;
+7 -5
View File
@@ -675,24 +675,26 @@ static int hyper_new_container(char *json, int length)
static int hyper_kill_container(char *json, int length)
{
struct hyper_killer killer;
struct hyper_container *c;
struct hyper_pod *pod = &global_pod;
int ret = -1;
if (hyper_parse_kill_container(&killer, json, length) < 0) {
JSON_Value *value = hyper_json_parse(json, length);
if (value == NULL) {
goto out;
}
c = hyper_find_container(pod, killer.id);
const char *id = json_object_get_string(json_object(value), "container");
c = hyper_find_container(pod, id);
if (c == NULL) {
fprintf(stderr, "can not find container whose id is %s\n", killer.id);
fprintf(stderr, "can not find container whose id is %s\n", id);
goto out;
}
kill(c->exec.pid, killer.signal);
kill(c->exec.pid, (int)json_object_get_number(json_object(value), "signal"));
ret = 0;
out:
json_value_free(value);
return ret;
}
-62
View File
@@ -1257,68 +1257,6 @@ fail:
return NULL;
}
int hyper_parse_kill_container(struct hyper_killer *killer, char *json, int length)
{
int i, n, ret = -1;
jsmn_parser p;
int toks_num = 10;
jsmntok_t *toks = NULL;
memset(killer, 0, sizeof(*killer));
realloc:
toks = realloc(toks, toks_num * sizeof(jsmntok_t));
if (toks == NULL) {
fprintf(stderr, "allocate tokens for kill container failed\n");
goto out;
}
jsmn_init(&p);
n = jsmn_parse(&p, json, length, toks, toks_num);
if (n < 0) {
fprintf(stdout, "jsmn parse failed, n is %d\n", n);
if (n == JSMN_ERROR_NOMEM) {
toks_num *= 2;
goto realloc;
}
goto out;
}
for (i = 0; i < n; i++) {
jsmntok_t *t = &toks[i];
if (t->type != JSMN_STRING)
continue;
if (i++ == n)
goto fail;
if (json_token_streq(json, t, "container")) {
if (toks[i].type != JSMN_STRING)
goto fail;
killer->id = (json_token_str(json, &toks[i]));
} else if (json_token_streq(json, t, "signal")) {
if (toks[i].type != JSMN_PRIMITIVE)
goto fail;
killer->signal = json_token_int(json, &toks[i]);
} else {
fprintf(stderr, "get unknown section %s in kill container\n",
json_token_str(json, t));
goto fail;
}
}
ret = 0;
out:
free(toks);
return ret;
fail:
free(killer->id);
killer->id = NULL;
goto out;
}
int hyper_parse_winsize(struct hyper_win_size *ws, char *json, int length)
{
int i, n, ret = -1;
-1
View File
@@ -10,7 +10,6 @@ struct hyper_exec *hyper_parse_execcmd(char *json, int length);
char *json_token_str(char *js, jsmntok_t *t);
int json_token_streq(char *js, jsmntok_t *t, char *s);
int hyper_parse_winsize(struct hyper_win_size *ws, char *json, int length);
int hyper_parse_kill_container(struct hyper_killer *killer, char *json, int length);
int hyper_parse_write_file(struct hyper_writter *writter, char *json, int length);
int hyper_parse_read_file(struct hyper_reader *reader, char *json, int length);
struct hyper_container *hyper_parse_new_container(struct hyper_pod *pod, char *json, int length);