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

Commit 0c997db

Browse files
committed
use list to store containers
Signed-off-by: Gao feng <omarapazanadi@gmail.com>
1 parent 6d3265a commit 0c997db

File tree

7 files changed

+63
-71
lines changed

7 files changed

+63
-71
lines changed

src/container.c

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -572,29 +572,16 @@ int hyper_start_container(struct hyper_container *container,
572572

573573
struct hyper_container *hyper_find_container(struct hyper_pod *pod, char *id)
574574
{
575-
int i;
576-
struct hyper_container *container;
577-
578-
for (i = 0; i < pod->c_num; i++) {
579-
container = &pod->c[i];
575+
struct hyper_container *c;
580576

581-
if (strlen(container->id) != strlen(id))
577+
list_for_each_entry(c, &pod->containers, list) {
578+
if (strlen(c->id) != strlen(id))
582579
continue;
583580

584-
if (strncmp(container->id, id, strlen(id)))
581+
if (strncmp(c->id, id, strlen(id)))
585582
continue;
586583

587-
return container;
588-
}
589-
590-
list_for_each_entry(container, &pod->dyn_containers, dyn) {
591-
if (strlen(container->id) != strlen(id))
592-
continue;
593-
594-
if (strncmp(container->id, id, strlen(id)))
595-
continue;
596-
597-
return container;
584+
return c;
598585
}
599586

600587
return NULL;
@@ -614,12 +601,10 @@ void hyper_cleanup_container(struct hyper_container *c)
614601

615602
void hyper_cleanup_containers(struct hyper_pod *pod)
616603
{
617-
int i;
604+
struct hyper_container *c, *n;
618605

619-
for (i = 0; i < pod->c_num; i++)
620-
hyper_cleanup_container(&pod->c[i]);
606+
list_for_each_entry_safe(c, n, &pod->containers, list)
607+
hyper_cleanup_container(c);
621608

622-
free(pod->c);
623-
pod->c = NULL;
624-
pod->c_num = 0;
609+
pod->remains = 0;
625610
}

src/container.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct hyper_container {
4242
int sys_num;
4343
int ns;
4444
uint32_t code;
45-
struct list_head dyn;
45+
struct list_head list;
4646
struct hyper_exec exec;
4747
};
4848

src/exec.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,6 @@ int hyper_release_exec(struct hyper_exec *exec,
549549
struct hyper_container *c = container_of(exec, struct hyper_container, exec);
550550
// TODO send finish of this container and full cleanup
551551
hyper_cleanup_container(c);
552-
list_del(&c->dyn);
553-
free(c);
554552
return 0;
555553
}
556554

src/hyper.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,11 @@ struct hyper_pod {
4141
struct hyper_interface *iface;
4242
struct hyper_route *rt;
4343
char **dns;
44-
struct list_head dyn_containers;
44+
struct list_head containers;
4545
struct list_head exec_head;
4646
char *hostname;
4747
char *share_tag;
4848
int init_pid;
49-
uint32_t c_num;
5049
uint32_t i_num;
5150
uint32_t r_num;
5251
uint32_t e_num;

src/init.c

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

3131
struct hyper_pod global_pod = {
32-
.dyn_containers = LIST_HEAD_INIT(global_pod.dyn_containers),
32+
.containers = LIST_HEAD_INIT(global_pod.containers),
3333
.exec_head = LIST_HEAD_INIT(global_pod.exec_head),
3434
};
3535
struct hyper_exec *global_exec;
@@ -144,6 +144,7 @@ static void hyper_term_all(struct hyper_pod *pod)
144144
DIR *dp;
145145
struct dirent *de;
146146
pid_t *pids = NULL;
147+
struct hyper_container *c;
147148

148149
dp = opendir("/proc");
149150
if (dp == NULL)
@@ -175,9 +176,8 @@ static void hyper_term_all(struct hyper_pod *pod)
175176
free(pids);
176177
closedir(dp);
177178

178-
for (index = 0; index < pod->c_num; index++) {
179-
hyper_kill_process(pod->c[index].exec.pid);
180-
}
179+
list_for_each_entry(c, &pod->containers, list)
180+
hyper_kill_process(c->exec.pid);
181181
}
182182

183183
static int hyper_handle_exit(struct hyper_pod *pod)
@@ -466,13 +466,13 @@ int hyper_start_container_stage0(struct hyper_container *c, struct hyper_pod *po
466466

467467
int hyper_start_containers(struct hyper_pod *pod)
468468
{
469-
int i, ret = 0;
469+
struct hyper_container *c;
470470

471-
for (i = 0; i < pod->c_num; i++) {
472-
ret = hyper_start_container_stage0(&pod->c[i], pod);
473-
if (ret)
474-
return ret;
471+
list_for_each_entry(c, &pod->containers, list) {
472+
if (hyper_start_container_stage0(c, pod) < 0)
473+
return -1;
475474
}
475+
476476
return 0;
477477
}
478478

@@ -709,11 +709,10 @@ static int hyper_new_container(char *json, int length)
709709
if (ret < 0) {
710710
//TODO full grace cleanup
711711
hyper_cleanup_container(c);
712-
free(c);
713712
return ret;
714713
}
715714

716-
list_add_tail(&c->dyn, &pod->dyn_containers);
715+
list_add_tail(&c->list, &pod->containers);
717716
return 0;
718717
}
719718

src/parse.c

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -335,26 +335,37 @@ void hyper_free_container(struct hyper_container *c)
335335
container_free_sysctl(c);
336336
container_free_fsmap(c);
337337
container_free_cmd(c);
338+
339+
list_del_init(&c->list);
340+
free(c);
338341
}
339342

340-
static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container *c,
343+
static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container **container,
341344
char *json, jsmntok_t *toks)
342345
{
343346
int i = 0, j, next, next_container;
347+
struct hyper_container *c = NULL;
344348
jsmntok_t *t;
345349

346350
if (toks[i].type != JSMN_OBJECT) {
347351
fprintf(stderr, "format incorrect\n");
348352
return -1;
349353
}
350354

355+
c = calloc(1, sizeof(*c));
356+
if (c == NULL) {
357+
fprintf(stdout, "alloc memory for container failed\n");
358+
return -1;
359+
}
360+
351361
c->exec.init = 1;
352362
c->exec.code = -1;
353363
c->exec.e.fd = -1;
354364
c->exec.errev.fd = -1;
355365
c->exec.ptyfd = -1;
356366
c->exec.errfd = -1;
357367
c->ns = -1;
368+
INIT_LIST_HEAD(&c->list);
358369

359370
next_container = toks[i].size;
360371
fprintf(stdout, "next container %d\n", next_container);
@@ -426,47 +437,44 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container *
426437
}
427438
}
428439

440+
*container = c;
429441
return i;
430-
431442
fail:
432443
hyper_free_container(c);
444+
*container = NULL;
433445
return -1;
434446
}
435447

436448
static int hyper_parse_containers(struct hyper_pod *pod, char *json, jsmntok_t *toks)
437449
{
438-
int i = 0, j = 0, next;
450+
int i = 0, j = 0, next, c_num;
451+
struct hyper_container *c, *n;
439452

440453
if (toks[i].type != JSMN_ARRAY) {
441454
fprintf(stdout, "format incorrect\n");
442455
return -1;
443456
}
444457

445-
pod->c = calloc(toks[i].size, sizeof(*pod->c));
446-
if (pod->c == NULL) {
447-
fprintf(stdout, "alloc memory for container failed\n");
448-
goto fail;
449-
}
450-
451-
pod->remains = pod->c_num = toks[i].size;
452-
fprintf(stdout, "container count %d\n", pod->c_num);
458+
c_num = toks[i].size;
459+
fprintf(stdout, "container count %d\n", c_num);
453460

454461
i++;
455-
for (j = 0; j < pod->c_num; j++) {
456-
next = hyper_parse_container(pod, &pod->c[j], json, toks + i);
462+
for (j = 0; j < c_num; j++) {
463+
next = hyper_parse_container(pod, &c, json, toks + i);
457464
if (next < 0)
458465
goto fail;
459466

467+
/* Pod created containers, Add to list immediately */
468+
list_add_tail(&c->list, &pod->containers);
460469
i += next;
461470
}
462471

472+
pod->remains = c_num;
463473
return i;
464474
fail:
465-
for (; j > 0; j--)
466-
hyper_free_container(&pod->c[j]);
475+
list_for_each_entry_safe(c, n, &pod->containers, list)
476+
hyper_free_container(c);
467477

468-
free(pod->c);
469-
pod->c = NULL;
470478
return -1;
471479
}
472480

@@ -722,13 +730,7 @@ struct hyper_container *hyper_parse_new_container(struct hyper_pod *pod, char *j
722730
goto fail;
723731
}
724732

725-
c = calloc(1, sizeof(*c));
726-
if (c == NULL) {
727-
fprintf(stdout, "alloc memory for container failed\n");
728-
goto fail;
729-
}
730-
731-
if (hyper_parse_container(pod, c, json, toks) < 0)
733+
if (hyper_parse_container(pod, &c, json, toks) < 0)
732734
goto fail;
733735

734736
c->exec.init = 2; // dynamic container type
@@ -737,7 +739,6 @@ struct hyper_container *hyper_parse_new_container(struct hyper_pod *pod, char *j
737739

738740
fail:
739741
free(toks);
740-
free(c);
741742
return NULL;
742743
}
743744

src/util.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,23 @@ void hyper_unmount_all(void)
386386

387387
int hyper_send_finish(struct hyper_pod *pod)
388388
{
389-
int i, ret;
390-
uint8_t *data = calloc(pod->c_num, 4);
391-
392-
for (i = 0; i < pod->c_num; i++)
393-
hyper_set_be32(data + (i * 4), pod->c[i].exec.code);
389+
int ret = -1;
390+
struct hyper_container *c;
391+
uint8_t *data = NULL, *new;
392+
int c_num = 0;
393+
394+
list_for_each_entry(c, &pod->containers, list) {
395+
c_num++;
396+
new = realloc(data, c_num * 4);
397+
if (new == NULL)
398+
goto out;
399+
400+
hyper_set_be32(new + ((c_num - 1) * 4), c->exec.code);
401+
data = new;
402+
}
394403

395-
ret = hyper_send_msg(ctl.chan.fd, FINISH, pod->c_num * 4, data);
404+
ret = hyper_send_msg(ctl.chan.fd, FINISH, c_num * 4, data);
405+
out:
396406
free(data);
397407
return ret;
398408
}

0 commit comments

Comments
 (0)