diff --git a/src/container.c b/src/container.c index f376e84..decd47c 100644 --- a/src/container.c +++ b/src/container.c @@ -16,6 +16,7 @@ #include "util.h" #include "hyper.h" +#include "parse.h" static int container_setup_volume(struct hyper_container *container) { @@ -599,57 +600,6 @@ struct hyper_container *hyper_find_container(struct hyper_pod *pod, char *id) return NULL; } -void hyper_free_container(struct hyper_container *c) -{ - int i; - struct volume *vol; - struct env *env; - struct fsmap *map; - struct sysctl *sys; - - free(c->id); - free(c->rootfs); - free(c->image); - free(c->workdir); - free(c->fstype); - - for (i = 0; i < c->vols_num; i++) { - vol = &(c->vols[i]); - free(vol->device); - free(vol->mountpoint); - free(vol->fstype); - } - free(c->vols); - - for (i = 0; i < c->envs_num; i++) { - env = &(c->envs[i]); - free(env->env); - free(env->value); - } - free(c->envs); - - for (i = 0; i < c->sys_num; i++) { - sys = &(c->sys[i]); - free(sys->path); - free(sys->value); - } - free(c->sys); - - for (i = 0; i < c->maps_num; i++) { - map = &(c->maps[i]); - free(map->source); - free(map->path); - } - free(c->maps); - - free(c->exec.id); - for (i = 0; i < c->exec.argc; i++) { - //fprintf(stdout, "argv %d %s\n", i, exec->argv[i]); - free(c->exec.argv[i]); - } - free(c->exec.argv); -} - void hyper_cleanup_container(struct hyper_container *c) { char root[512]; diff --git a/src/parse.c b/src/parse.c index 967eaf7..31fb2de 100644 --- a/src/parse.c +++ b/src/parse.c @@ -37,15 +37,14 @@ static int container_parse_cmd(struct hyper_container *c, char *json, jsmntok_t return -1; } - c->exec.argc = toks[i].size; - - c->exec.argv = calloc(c->exec.argc + 1, sizeof(*c->exec.argv)); + c->exec.argv = calloc(toks[i].size + 1, sizeof(*c->exec.argv)); if (c->exec.argv == NULL) { fprintf(stderr, "allocate memory for exec argv failed\n"); return -1; } c->exec.argv[c->exec.argc] = NULL; + c->exec.argc = toks[i].size; i++; for (j = 0; j < c->exec.argc; j++, i++) { @@ -56,6 +55,33 @@ static int container_parse_cmd(struct hyper_container *c, char *json, jsmntok_t return i; } +static void container_free_cmd(struct hyper_container *c) +{ + int i; + + for (i = 0; i < c->exec.argc; i++) { + free(c->exec.argv[i]); + } + + free(c->exec.argv); + c->exec.argv = NULL; + c->exec.argc = 0; +} + +static void container_free_volumes(struct hyper_container *c) +{ + int i; + + for (i = 0; i < c->vols_num; i++) { + free(c->vols[i].device); + free(c->vols[i].mountpoint); + free(c->vols[i].fstype); + } + free(c->vols); + c->vols = NULL; + c->vols_num = 0; +} + static int container_parse_volumes(struct hyper_container *c, char *json, jsmntok_t *toks) { int i = 0, j; @@ -64,15 +90,16 @@ static int container_parse_volumes(struct hyper_container *c, char *json, jsmnto fprintf(stdout, "volume need array\n"); return -1; } - c->vols_num = toks[i].size; - fprintf(stdout, "volumes num %d\n", c->vols_num); - c->vols = calloc(c->vols_num, sizeof(*c->vols)); + c->vols = calloc(toks[i].size, sizeof(*c->vols)); if (c->vols == NULL) { fprintf(stderr, "allocate memory for volume failed\n"); return -1; } + c->vols_num = toks[i].size; + fprintf(stdout, "volumes num %d\n", c->vols_num); + i++; for (j = 0; j < c->vols_num; j++) { int i_volume, next_volume; @@ -111,6 +138,19 @@ static int container_parse_volumes(struct hyper_container *c, char *json, jsmnto return i; } +void container_free_fsmap(struct hyper_container *c) +{ + int i; + + for (i = 0; i < c->maps_num; i++) { + free(c->maps[i].source); + free(c->maps[i].path); + } + free(c->maps); + c->maps = NULL; + c->maps_num = 0; +} + static int container_parse_fsmap(struct hyper_container *c, char *json, jsmntok_t *toks) { int i = 0, j; @@ -120,15 +160,15 @@ static int container_parse_fsmap(struct hyper_container *c, char *json, jsmntok_ return -1; } - c->maps_num = toks[i].size; - fprintf(stdout, "fsmap num %d\n", c->maps_num); - - c->maps = calloc(c->maps_num, sizeof(*c->maps)); + c->maps = calloc(toks[i].size, sizeof(*c->maps)); if (c->maps == NULL) { fprintf(stderr, "allocate memory for fsmap failed\n"); return -1; } + c->maps_num = toks[i].size; + fprintf(stdout, "fsmap num %d\n", c->maps_num); + i++; for (j = 0; j < c->maps_num; j++) { int i_map, next_map; @@ -163,6 +203,20 @@ static int container_parse_fsmap(struct hyper_container *c, char *json, jsmntok_ return i; } +static void container_free_envs(struct hyper_container *c) +{ + int i; + + for (i = 0; i < c->envs_num; i++) { + free(c->envs[i].env); + free(c->envs[i].value); + } + + free(c->envs); + c->envs = NULL; + c->envs_num = 0; +} + static int container_parse_envs(struct hyper_container *c, char *json, jsmntok_t *toks) { int i = 0, j; @@ -172,15 +226,15 @@ static int container_parse_envs(struct hyper_container *c, char *json, jsmntok_t return -1; } - c->envs_num = toks[i].size; - fprintf(stdout, "envs num %d\n", c->envs_num); - - c->envs = calloc(c->envs_num, sizeof(*c->envs)); + c->envs = calloc(toks[i].size, sizeof(*c->envs)); if (c->envs == NULL) { fprintf(stderr, "allocate memory for env failed\n"); return -1; } + c->envs_num = toks[i].size; + fprintf(stdout, "envs num %d\n", c->envs_num); + i++; for (j = 0; j < c->envs_num; j++) { int i_env, next_env; @@ -211,6 +265,20 @@ static int container_parse_envs(struct hyper_container *c, char *json, jsmntok_t return i; } +static void container_free_sysctl(struct hyper_container *c) +{ + int i; + + for (i = 0; i < c->sys_num; i++) { + free(c->sys[i].path); + free(c->sys[i].value); + } + + free(c->sys); + c->sys = NULL; + c->sys_num = 0; +} + static int container_parse_sysctl(struct hyper_container *c, char *json, jsmntok_t *toks) { int i = 0, j; @@ -221,15 +289,15 @@ static int container_parse_sysctl(struct hyper_container *c, char *json, jsmntok return -1; } - c->sys_num = toks[i].size; - fprintf(stdout, "sysctl size %d\n", c->sys_num); - - c->sys = calloc(c->sys_num, sizeof(*c->sys)); + c->sys = calloc(toks[i].size, sizeof(*c->sys)); if (c->sys == NULL) { fprintf(stderr, "allocate memory for sysctl failed\n"); return -1; } + c->sys_num = toks[i].size; + fprintf(stdout, "sysctl size %d\n", c->sys_num); + i++; for (j = 0; j < c->sys_num; j++) { c->sys[j].path = strdup(json_token_str(json, &toks[++i])); @@ -242,9 +310,35 @@ static int container_parse_sysctl(struct hyper_container *c, char *json, jsmntok return i; } +void hyper_free_container(struct hyper_container *c) +{ + free(c->id); + c->id = NULL; + + free(c->rootfs); + c->rootfs = NULL; + + free(c->image); + c->image = NULL; + + free(c->workdir); + c->workdir = NULL; + + free(c->fstype); + c->fstype = NULL; + + free(c->exec.id); + c->exec.id = NULL; + + container_free_volumes(c); + container_free_envs(c); + container_free_sysctl(c); + container_free_fsmap(c); + container_free_cmd(c); +} static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container *c, - char *json, jsmntok_t *toks) + char *json, jsmntok_t *toks) { int i = 0, j, next, next_container; jsmntok_t *t; @@ -276,7 +370,7 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container * } else if (json_token_streq(json, t, "cmd") && t->size == 1) { next = container_parse_cmd(c, json, &toks[++i]); if (next < 0) - return -1; + goto fail; i += next; } else if (json_token_streq(json, t, "rootfs") && t->size == 1) { c->rootfs = strdup(json_token_str(json, &toks[++i])); @@ -305,22 +399,22 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container * } else if (json_token_streq(json, t, "volumes") && t->size == 1) { next = container_parse_volumes(c, json, &toks[++i]); if (next < 0) - return -1; + goto fail; i += next; } else if (json_token_streq(json, t, "fsmap") && t->size == 1) { next = container_parse_fsmap(c, json, &toks[++i]); if (next < 0) - return -1; + goto fail; i += next; } else if (json_token_streq(json, t, "envs") && t->size == 1) { next = container_parse_envs(c, json, &toks[++i]); if (next < 0) - return -1; + goto fail; i += next; } else if (json_token_streq(json, t, "sysctl") && t->size == 1) { next = container_parse_sysctl(c, json, &toks[++i]); if (next < 0) - return -1; + goto fail; i += next; } else if (json_token_streq(json, t, "restartPolicy") && t->size == 1) { fprintf(stdout, "restart policy %s\n", json_token_str(json, &toks[++i])); @@ -328,41 +422,52 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container * } else { fprintf(stdout, "get unknown section %s in container\n", json_token_str(json, t)); - return -1; + goto fail; } } return i; + +fail: + hyper_free_container(c); + return -1; } static int hyper_parse_containers(struct hyper_pod *pod, char *json, jsmntok_t *toks) { - int i = 0, j, next; + int i = 0, j = 0, next; if (toks[i].type != JSMN_ARRAY) { fprintf(stdout, "format incorrect\n"); return -1; } - pod->remains = pod->c_num = toks[i].size; - fprintf(stdout, "container count %d\n", pod->c_num); - - pod->c = calloc(pod->c_num, sizeof(*pod->c)); + pod->c = calloc(toks[i].size, sizeof(*pod->c)); if (pod->c == NULL) { fprintf(stdout, "alloc memory for container failed\n"); - return -1; + goto fail; } + pod->remains = pod->c_num = toks[i].size; + fprintf(stdout, "container count %d\n", pod->c_num); + i++; for (j = 0; j < pod->c_num; j++) { next = hyper_parse_container(pod, &pod->c[j], json, toks + i); if (next < 0) - return -1; + goto fail; i += next; } return i; +fail: + for (; j > 0; j--) + hyper_free_container(&pod->c[j]); + + free(pod->c); + pod->c = NULL; + return -1; } static int hyper_parse_interfaces(struct hyper_pod *pod, char *json, jsmntok_t *toks) diff --git a/src/parse.h b/src/parse.h index 717895a..24c75dd 100644 --- a/src/parse.h +++ b/src/parse.h @@ -12,5 +12,6 @@ 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); struct hyper_container *hyper_parse_new_container(struct hyper_pod *pod, char *json, int length); +void hyper_free_container(struct hyper_container *c); #endif