Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 4361f07

Browse files
committed
move newcontainers to dyn_containers list and fix realloc bug
Signed-off-by: Lai Jiangshan <jiangshanlai@gmail.com>
1 parent 7ef8099 commit 4361f07

File tree

7 files changed

+38
-15
lines changed

7 files changed

+38
-15
lines changed

src/container.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,16 @@ struct hyper_container *hyper_find_container(struct hyper_pod *pod, char *id)
586586
return container;
587587
}
588588

589+
list_for_each_entry(container, &pod->dyn_containers, dyn) {
590+
if (strlen(container->id) != strlen(id))
591+
continue;
592+
593+
if (strncmp(container->id, id, strlen(id)))
594+
continue;
595+
596+
return container;
597+
}
598+
589599
return NULL;
590600
}
591601

src/container.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ struct hyper_container {
4242
int sys_num;
4343
int ns;
4444
uint32_t code;
45+
struct list_head dyn;
4546
struct hyper_exec exec;
4647
};
4748

src/exec.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,15 @@ int hyper_release_exec(struct hyper_exec *exec,
548548
if (exec->code)
549549
pod->code = exec->code;
550550

551+
if (exec->init == 2) { // dynamic container
552+
struct hyper_container *c = container_of(exec, struct hyper_container, exec);
553+
// TODO send finish of this container and full cleanup
554+
hyper_cleanup_container(c);
555+
list_del(&c->dyn);
556+
free(c);
557+
return 0;
558+
}
559+
551560
if (--pod->remains > 0)
552561
return 0;
553562

src/hyper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ struct hyper_pod {
4141
struct hyper_interface *iface;
4242
struct hyper_route *rt;
4343
char **dns;
44+
struct list_head dyn_containers;
4445
struct list_head exec_head;
4546
//struct list_head ce_head;
4647
char *hostname;

src/init.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "container.h"
3030

3131
struct hyper_pod global_pod = {
32+
.dyn_containers = LIST_HEAD_INIT(global_pod.dyn_containers),
3233
.exec_head = LIST_HEAD_INIT(global_pod.exec_head),
3334
};
3435
struct hyper_exec *global_exec;
@@ -690,26 +691,29 @@ static int hyper_start_pod(char *json, int length)
690691
static int hyper_new_container(char *json, int length)
691692
{
692693
int ret;
694+
struct hyper_container *c;
693695
struct hyper_pod *pod = &global_pod;
694696

695697
fprintf(stdout, "call hyper_new_container, json %s, len %d\n", json, length);
696698

697699
if (!pod->init_pid)
698700
fprintf(stdout, "the pod is not created yet\n");
699701

700-
if (hyper_parse_new_container(pod, json, length) < 0) {
702+
c = hyper_parse_new_container(pod, json, length);
703+
if (c == NULL) {
701704
fprintf(stderr, "parse container json failed\n");
702705
return -1;
703706
}
704707

705-
ret = hyper_start_container_stage0(&pod->c[pod->c_num - 1], pod);
708+
ret = hyper_start_container_stage0(c, pod);
706709
if (ret < 0) {
707710
//TODO full grace cleanup
708-
pod->remains -= 1;
709-
pod->c_num -= 1;
711+
hyper_cleanup_container(c);
712+
free(c);
710713
return ret;
711714
}
712715

716+
list_add_tail(&c->dyn, &pod->dyn_containers);
713717
return 0;
714718
}
715719

@@ -977,7 +981,7 @@ static void hyper_cleanup_shared(struct hyper_pod *pod)
977981

978982
void hyper_cleanup_pod(struct hyper_pod *pod)
979983
{
980-
hyper_cleanup_container(pod);
984+
hyper_cleanup_containers(pod);
981985
hyper_cleanup_network(pod);
982986
hyper_cleanup_shared(pod);
983987
hyper_cleanup_dns(pod);

src/parse.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ int hyper_parse_pod(struct hyper_pod *pod, char *json, int length)
530530
return next;
531531
}
532532

533-
int hyper_parse_new_container(struct hyper_pod *pod, char *json, int length)
533+
struct hyper_container *hyper_parse_new_container(struct hyper_pod *pod, char *json, int length)
534534
{
535535
int n;
536536
jsmn_parser p;
@@ -553,26 +553,24 @@ int hyper_parse_new_container(struct hyper_pod *pod, char *json, int length)
553553
goto fail;
554554
}
555555

556-
c = realloc(pod->c, (pod->c_num + 1) * sizeof(*pod->c));
556+
c = calloc(1, sizeof(*c));
557557
if (c == NULL) {
558558
fprintf(stdout, "alloc memory for container failed\n");
559559
goto fail;
560560
}
561-
pod->c = c;
562-
memset(&pod->c[pod->c_num], 0, sizeof(pod->c[pod->c_num]));
563561

564562
// trick: toks-1, TODO: change all "i = 1" to "i = 0"
565-
if (hyper_parse_container(pod, &pod->c[pod->c_num], json, toks-1) < 0)
563+
if (hyper_parse_container(pod, c, json, toks-1) < 0)
566564
goto fail;
567565

568-
pod->remains += 1;
569-
pod->c_num += 1;
566+
c->exec.init = 2; // dynamic container type
570567
free(toks);
571-
return 0;
568+
return c;
572569

573570
fail:
574571
free(toks);
575-
return -1;
572+
free(c);
573+
return NULL;
576574
}
577575

578576
int hyper_parse_winsize(struct hyper_win_size *ws, char *json, int length)

src/parse.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ int json_token_streq(char *js, jsmntok_t *t, char *s);
1111
int hyper_parse_winsize(struct hyper_win_size *ws, char *json, int length);
1212
int hyper_parse_write_file(struct hyper_writter *writter, char *json, int length);
1313
int hyper_parse_read_file(struct hyper_reader *reader, char *json, int length);
14-
int hyper_parse_new_container(struct hyper_pod *pod, char *json, int length);
14+
struct hyper_container *hyper_parse_new_container(struct hyper_pod *pod, char *json, int length);
1515

1616
#endif

0 commit comments

Comments
 (0)