move newcontainers to dyn_containers list and fix realloc bug

Signed-off-by: Lai Jiangshan <jiangshanlai@gmail.com>
This commit is contained in:
Lai Jiangshan
2015-11-12 14:14:25 +08:00
parent 7ef8099916
commit 4361f071e8
7 changed files with 38 additions and 15 deletions
+10
View File
@@ -586,6 +586,16 @@ struct hyper_container *hyper_find_container(struct hyper_pod *pod, char *id)
return container;
}
list_for_each_entry(container, &pod->dyn_containers, dyn) {
if (strlen(container->id) != strlen(id))
continue;
if (strncmp(container->id, id, strlen(id)))
continue;
return container;
}
return NULL;
}
+1
View File
@@ -42,6 +42,7 @@ struct hyper_container {
int sys_num;
int ns;
uint32_t code;
struct list_head dyn;
struct hyper_exec exec;
};
+9
View File
@@ -548,6 +548,15 @@ int hyper_release_exec(struct hyper_exec *exec,
if (exec->code)
pod->code = exec->code;
if (exec->init == 2) { // dynamic container
struct hyper_container *c = container_of(exec, struct hyper_container, exec);
// TODO send finish of this container and full cleanup
hyper_cleanup_container(c);
list_del(&c->dyn);
free(c);
return 0;
}
if (--pod->remains > 0)
return 0;
+1
View File
@@ -41,6 +41,7 @@ struct hyper_pod {
struct hyper_interface *iface;
struct hyper_route *rt;
char **dns;
struct list_head dyn_containers;
struct list_head exec_head;
//struct list_head ce_head;
char *hostname;
+9 -5
View File
@@ -29,6 +29,7 @@
#include "container.h"
struct hyper_pod global_pod = {
.dyn_containers = LIST_HEAD_INIT(global_pod.dyn_containers),
.exec_head = LIST_HEAD_INIT(global_pod.exec_head),
};
struct hyper_exec *global_exec;
@@ -690,6 +691,7 @@ static int hyper_start_pod(char *json, int length)
static int hyper_new_container(char *json, int length)
{
int ret;
struct hyper_container *c;
struct hyper_pod *pod = &global_pod;
fprintf(stdout, "call hyper_new_container, json %s, len %d\n", json, length);
@@ -697,19 +699,21 @@ static int hyper_new_container(char *json, int length)
if (!pod->init_pid)
fprintf(stdout, "the pod is not created yet\n");
if (hyper_parse_new_container(pod, json, length) < 0) {
c = hyper_parse_new_container(pod, json, length);
if (c == NULL) {
fprintf(stderr, "parse container json failed\n");
return -1;
}
ret = hyper_start_container_stage0(&pod->c[pod->c_num - 1], pod);
ret = hyper_start_container_stage0(c, pod);
if (ret < 0) {
//TODO full grace cleanup
pod->remains -= 1;
pod->c_num -= 1;
hyper_cleanup_container(c);
free(c);
return ret;
}
list_add_tail(&c->dyn, &pod->dyn_containers);
return 0;
}
@@ -977,7 +981,7 @@ static void hyper_cleanup_shared(struct hyper_pod *pod)
void hyper_cleanup_pod(struct hyper_pod *pod)
{
hyper_cleanup_container(pod);
hyper_cleanup_containers(pod);
hyper_cleanup_network(pod);
hyper_cleanup_shared(pod);
hyper_cleanup_dns(pod);
+7 -9
View File
@@ -530,7 +530,7 @@ out:
return next;
}
int hyper_parse_new_container(struct hyper_pod *pod, char *json, int length)
struct hyper_container *hyper_parse_new_container(struct hyper_pod *pod, char *json, int length)
{
int n;
jsmn_parser p;
@@ -553,26 +553,24 @@ realloc:
goto fail;
}
c = realloc(pod->c, (pod->c_num + 1) * sizeof(*pod->c));
c = calloc(1, sizeof(*c));
if (c == NULL) {
fprintf(stdout, "alloc memory for container failed\n");
goto fail;
}
pod->c = c;
memset(&pod->c[pod->c_num], 0, sizeof(pod->c[pod->c_num]));
// trick: toks-1, TODO: change all "i = 1" to "i = 0"
if (hyper_parse_container(pod, &pod->c[pod->c_num], json, toks-1) < 0)
if (hyper_parse_container(pod, c, json, toks-1) < 0)
goto fail;
pod->remains += 1;
pod->c_num += 1;
c->exec.init = 2; // dynamic container type
free(toks);
return 0;
return c;
fail:
free(toks);
return -1;
free(c);
return NULL;
}
int hyper_parse_winsize(struct hyper_win_size *ws, char *json, int length)
+1 -1
View File
@@ -11,6 +11,6 @@ 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_write_file(struct hyper_writter *writter, char *json, int length);
int hyper_parse_read_file(struct hyper_reader *reader, char *json, int length);
int hyper_parse_new_container(struct hyper_pod *pod, char *json, int length);
struct hyper_container *hyper_parse_new_container(struct hyper_pod *pod, char *json, int length);
#endif