support kill container

Signed-off-by: Gao feng <omarapazanadi@gmail.com>
This commit is contained in:
Gao feng
2015-11-18 18:50:07 +08:00
parent a2086298f6
commit 0887e9a6de
4 changed files with 95 additions and 0 deletions
+6
View File
@@ -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;
+26
View File
@@ -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;
+62
View File
@@ -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;
+1
View File
@@ -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);