From 0887e9a6ded7ed0fac9b023df50f9dfcda840b87 Mon Sep 17 00:00:00 2001 From: Gao feng Date: Wed, 18 Nov 2015 18:50:07 +0800 Subject: [PATCH] support kill container Signed-off-by: Gao feng --- src/hyper.h | 6 ++++++ src/init.c | 26 ++++++++++++++++++++++ src/parse.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/parse.h | 1 + 4 files changed, 95 insertions(+) diff --git a/src/hyper.h b/src/hyper.h index 1a82276..7845342 100644 --- a/src/hyper.h +++ b/src/hyper.h @@ -28,6 +28,7 @@ enum { WRITEFILE, READFILE, NEWCONTAINER, + KILLCONTAINER, }; enum { @@ -65,6 +66,11 @@ struct hyper_win_size { uint64_t seq; }; +struct hyper_killer { + char *id; + int signal; +}; + struct hyper_reader { char *id; char *file; diff --git a/src/init.c b/src/init.c index fe431b3..49eddd6 100644 --- a/src/init.c +++ b/src/init.c @@ -719,6 +719,29 @@ static int hyper_new_container(char *json, int length) return ret; } +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) { + goto out; + } + + c = hyper_find_container(pod, killer.id); + if (c == NULL) { + fprintf(stderr, "can not find container whose id is %s\n", killer.id); + goto out; + } + + kill(c->exec.pid, killer.signal); + ret = 0; +out: + return ret; +} + static int hyper_cmd_write_file(char *json, int length) { struct hyper_writter writter; @@ -1145,6 +1168,9 @@ static int hyper_channel_handle(struct hyper_event *de, uint32_t len) case NEWCONTAINER: ret = hyper_new_container((char *)buf->data + 8, len - 8); break; + case KILLCONTAINER: + ret = hyper_kill_container((char *)buf->data + 8, len - 8); + break; default: ret = -1; break; diff --git a/src/parse.c b/src/parse.c index bdddbda..c728326 100644 --- a/src/parse.c +++ b/src/parse.c @@ -740,6 +740,68 @@ 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 = strdup(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; diff --git a/src/parse.h b/src/parse.h index 24c75dd..52f91f9 100644 --- a/src/parse.h +++ b/src/parse.h @@ -9,6 +9,7 @@ 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);