use list to store containers

Signed-off-by: Gao feng <omarapazanadi@gmail.com>
This commit is contained in:
Gao feng
2015-11-13 16:32:12 +08:00
parent 6d3265a7fb
commit 0c997dbe85
7 changed files with 62 additions and 70 deletions
+9 -24
View File
@@ -572,29 +572,16 @@ fail:
struct hyper_container *hyper_find_container(struct hyper_pod *pod, char *id)
{
int i;
struct hyper_container *container;
struct hyper_container *c;
for (i = 0; i < pod->c_num; i++) {
container = &pod->c[i];
if (strlen(container->id) != strlen(id))
list_for_each_entry(c, &pod->containers, list) {
if (strlen(c->id) != strlen(id))
continue;
if (strncmp(container->id, id, strlen(id)))
if (strncmp(c->id, id, strlen(id)))
continue;
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 c;
}
return NULL;
@@ -614,12 +601,10 @@ void hyper_cleanup_container(struct hyper_container *c)
void hyper_cleanup_containers(struct hyper_pod *pod)
{
int i;
struct hyper_container *c, *n;
for (i = 0; i < pod->c_num; i++)
hyper_cleanup_container(&pod->c[i]);
list_for_each_entry_safe(c, n, &pod->containers, list)
hyper_cleanup_container(c);
free(pod->c);
pod->c = NULL;
pod->c_num = 0;
pod->remains = 0;
}
+1 -1
View File
@@ -42,7 +42,7 @@ struct hyper_container {
int sys_num;
int ns;
uint32_t code;
struct list_head dyn;
struct list_head list;
struct hyper_exec exec;
};
-2
View File
@@ -549,8 +549,6 @@ int hyper_release_exec(struct hyper_exec *exec,
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;
}
+1 -2
View File
@@ -41,12 +41,11 @@ struct hyper_pod {
struct hyper_interface *iface;
struct hyper_route *rt;
char **dns;
struct list_head dyn_containers;
struct list_head containers;
struct list_head exec_head;
char *hostname;
char *share_tag;
int init_pid;
uint32_t c_num;
uint32_t i_num;
uint32_t r_num;
uint32_t e_num;
+10 -11
View File
@@ -29,7 +29,7 @@
#include "container.h"
struct hyper_pod global_pod = {
.dyn_containers = LIST_HEAD_INIT(global_pod.dyn_containers),
.containers = LIST_HEAD_INIT(global_pod.containers),
.exec_head = LIST_HEAD_INIT(global_pod.exec_head),
};
struct hyper_exec *global_exec;
@@ -144,6 +144,7 @@ static void hyper_term_all(struct hyper_pod *pod)
DIR *dp;
struct dirent *de;
pid_t *pids = NULL;
struct hyper_container *c;
dp = opendir("/proc");
if (dp == NULL)
@@ -175,9 +176,8 @@ static void hyper_term_all(struct hyper_pod *pod)
free(pids);
closedir(dp);
for (index = 0; index < pod->c_num; index++) {
hyper_kill_process(pod->c[index].exec.pid);
}
list_for_each_entry(c, &pod->containers, list)
hyper_kill_process(c->exec.pid);
}
static int hyper_handle_exit(struct hyper_pod *pod)
@@ -466,13 +466,13 @@ out:
int hyper_start_containers(struct hyper_pod *pod)
{
int i, ret = 0;
struct hyper_container *c;
for (i = 0; i < pod->c_num; i++) {
ret = hyper_start_container_stage0(&pod->c[i], pod);
if (ret)
return ret;
list_for_each_entry(c, &pod->containers, list) {
if (hyper_start_container_stage0(c, pod) < 0)
return -1;
}
return 0;
}
@@ -709,11 +709,10 @@ static int hyper_new_container(char *json, int length)
if (ret < 0) {
//TODO full grace cleanup
hyper_cleanup_container(c);
free(c);
return ret;
}
list_add_tail(&c->dyn, &pod->dyn_containers);
list_add_tail(&c->list, &pod->containers);
return 0;
}
+26 -25
View File
@@ -335,12 +335,16 @@ void hyper_free_container(struct hyper_container *c)
container_free_sysctl(c);
container_free_fsmap(c);
container_free_cmd(c);
list_del_init(&c->list);
free(c);
}
static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container *c,
static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container **container,
char *json, jsmntok_t *toks)
{
int i = 0, j, next, next_container;
struct hyper_container *c = NULL;
jsmntok_t *t;
if (toks[i].type != JSMN_OBJECT) {
@@ -348,6 +352,12 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container *
return -1;
}
c = calloc(1, sizeof(*c));
if (c == NULL) {
fprintf(stdout, "alloc memory for container failed\n");
return -1;
}
c->exec.init = 1;
c->exec.code = -1;
c->exec.e.fd = -1;
@@ -355,6 +365,7 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container *
c->exec.ptyfd = -1;
c->exec.errfd = -1;
c->ns = -1;
INIT_LIST_HEAD(&c->list);
next_container = toks[i].size;
fprintf(stdout, "next container %d\n", next_container);
@@ -426,47 +437,44 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container *
}
}
*container = c;
return i;
fail:
hyper_free_container(c);
*container = NULL;
return -1;
}
static int hyper_parse_containers(struct hyper_pod *pod, char *json, jsmntok_t *toks)
{
int i = 0, j = 0, next;
int i = 0, j = 0, next, c_num;
struct hyper_container *c, *n;
if (toks[i].type != JSMN_ARRAY) {
fprintf(stdout, "format incorrect\n");
return -1;
}
pod->c = calloc(toks[i].size, sizeof(*pod->c));
if (pod->c == NULL) {
fprintf(stdout, "alloc memory for container failed\n");
goto fail;
}
pod->remains = pod->c_num = toks[i].size;
fprintf(stdout, "container count %d\n", pod->c_num);
c_num = toks[i].size;
fprintf(stdout, "container count %d\n", c_num);
i++;
for (j = 0; j < pod->c_num; j++) {
next = hyper_parse_container(pod, &pod->c[j], json, toks + i);
for (j = 0; j < c_num; j++) {
next = hyper_parse_container(pod, &c, json, toks + i);
if (next < 0)
goto fail;
/* Pod created containers, Add to list immediately */
list_add_tail(&c->list, &pod->containers);
i += next;
}
pod->remains = c_num;
return i;
fail:
for (; j > 0; j--)
hyper_free_container(&pod->c[j]);
list_for_each_entry_safe(c, n, &pod->containers, list)
hyper_free_container(c);
free(pod->c);
pod->c = NULL;
return -1;
}
@@ -722,13 +730,7 @@ realloc:
goto fail;
}
c = calloc(1, sizeof(*c));
if (c == NULL) {
fprintf(stdout, "alloc memory for container failed\n");
goto fail;
}
if (hyper_parse_container(pod, c, json, toks) < 0)
if (hyper_parse_container(pod, &c, json, toks) < 0)
goto fail;
c->exec.init = 2; // dynamic container type
@@ -737,7 +739,6 @@ realloc:
fail:
free(toks);
free(c);
return NULL;
}
+15 -5
View File
@@ -386,13 +386,23 @@ void hyper_unmount_all(void)
int hyper_send_finish(struct hyper_pod *pod)
{
int i, ret;
uint8_t *data = calloc(pod->c_num, 4);
int ret = -1;
struct hyper_container *c;
uint8_t *data = NULL, *new;
int c_num = 0;
for (i = 0; i < pod->c_num; i++)
hyper_set_be32(data + (i * 4), pod->c[i].exec.code);
list_for_each_entry(c, &pod->containers, list) {
c_num++;
new = realloc(data, c_num * 4);
if (new == NULL)
goto out;
ret = hyper_send_msg(ctl.chan.fd, FINISH, pod->c_num * 4, data);
hyper_set_be32(new + ((c_num - 1) * 4), c->exec.code);
data = new;
}
ret = hyper_send_msg(ctl.chan.fd, FINISH, c_num * 4, data);
out:
free(data);
return ret;
}