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

Commit 9611ecf

Browse files
committed
Merge pull request #28 from gao-feng/cleanup
Cleanup
2 parents 7bbc240 + 0887e9a commit 9611ecf

File tree

9 files changed

+423
-254
lines changed

9 files changed

+423
-254
lines changed

src/container.c

Lines changed: 11 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "util.h"
1818
#include "hyper.h"
19+
#include "parse.h"
1920

2021
static int container_setup_volume(struct hyper_container *container)
2122
{
@@ -571,99 +572,39 @@ int hyper_start_container(struct hyper_container *container,
571572

572573
struct hyper_container *hyper_find_container(struct hyper_pod *pod, char *id)
573574
{
574-
int i;
575-
struct hyper_container *container;
576-
577-
for (i = 0; i < pod->c_num; i++) {
578-
container = &pod->c[i];
579-
580-
if (strlen(container->id) != strlen(id))
581-
continue;
582-
583-
if (strncmp(container->id, id, strlen(id)))
584-
continue;
575+
struct hyper_container *c;
585576

586-
return container;
587-
}
588-
589-
list_for_each_entry(container, &pod->dyn_containers, dyn) {
590-
if (strlen(container->id) != strlen(id))
577+
list_for_each_entry(c, &pod->containers, list) {
578+
if (strlen(c->id) != strlen(id))
591579
continue;
592580

593-
if (strncmp(container->id, id, strlen(id)))
581+
if (strncmp(c->id, id, strlen(id)))
594582
continue;
595583

596-
return container;
584+
return c;
597585
}
598586

599587
return NULL;
600588
}
601589

602590
void hyper_cleanup_container(struct hyper_container *c)
603591
{
604-
int i;
605-
struct volume *vol;
606-
struct env *env;
607-
struct fsmap *map;
608-
struct sysctl *sys;
609592
char root[512];
610593

611594
sprintf(root, "/tmp/hyper/%s/devpts/", c->id);
612595
if (umount(root) < 0 && umount2(root, MNT_DETACH))
613596
perror("umount devpts failed");
614597

615-
free(c->id);
616-
free(c->rootfs);
617-
free(c->image);
618-
free(c->workdir);
619-
free(c->fstype);
620-
621-
for (i = 0; i < c->vols_num; i++) {
622-
vol = &(c->vols[i]);
623-
free(vol->device);
624-
free(vol->mountpoint);
625-
free(vol->fstype);
626-
}
627-
free(c->vols);
628-
629-
for (i = 0; i < c->envs_num; i++) {
630-
env = &(c->envs[i]);
631-
free(env->env);
632-
free(env->value);
633-
}
634-
free(c->envs);
635-
636-
for (i = 0; i < c->sys_num; i++) {
637-
sys = &(c->sys[i]);
638-
free(sys->path);
639-
free(sys->value);
640-
}
641-
free(c->sys);
642-
643-
for (i = 0; i < c->maps_num; i++) {
644-
map = &(c->maps[i]);
645-
free(map->source);
646-
free(map->path);
647-
}
648-
free(c->maps);
649598
close(c->ns);
650-
651-
free(c->exec.id);
652-
for (i = 0; i < c->exec.argc; i++) {
653-
//fprintf(stdout, "argv %d %s\n", i, exec->argv[i]);
654-
free(c->exec.argv[i]);
655-
}
656-
free(c->exec.argv);
599+
hyper_free_container(c);
657600
}
658601

659602
void hyper_cleanup_containers(struct hyper_pod *pod)
660603
{
661-
int i;
604+
struct hyper_container *c, *n;
662605

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

666-
free(pod->c);
667-
pod->c = NULL;
668-
pod->c_num = 0;
609+
pod->remains = 0;
669610
}

src/container.h

Lines changed: 2 additions & 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

@@ -53,5 +53,6 @@ int hyper_start_container(struct hyper_container *container,
5353
struct hyper_container *hyper_find_container(struct hyper_pod *pod, char *id);
5454
void hyper_cleanup_container(struct hyper_container *container);
5555
void hyper_cleanup_containers(struct hyper_pod *pod);
56+
void hyper_free_container(struct hyper_container *c);
5657

5758
#endif

src/exec.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -545,15 +545,7 @@ int hyper_release_exec(struct hyper_exec *exec,
545545
fprintf(stdout, "%s container init exited, type %d, remains %d, policy %d\n",
546546
__func__, pod->type, pod->remains, pod->policy);
547547

548-
if (exec->init == 2) { // dynamic container
549-
struct hyper_container *c = container_of(exec, struct hyper_container, exec);
550-
// TODO send finish of this container and full cleanup
551-
hyper_cleanup_container(c);
552-
list_del(&c->dyn);
553-
free(c);
554-
return 0;
555-
}
556-
548+
// TODO send finish of this container and full cleanup
557549
if (--pod->remains > 0)
558550
return 0;
559551

@@ -563,7 +555,7 @@ int hyper_release_exec(struct hyper_exec *exec,
563555

564556
} else {
565557
/* send out pod finish message, hyper will decide if restart pod or not */
566-
hyper_send_finish(pod);
558+
hyper_send_pod_finished(pod);
567559
}
568560

569561
hyper_cleanup_pod(pod);

src/hyper.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@ enum {
1717
DESTROYPOD,
1818
RESTARTCONTAINER,
1919
EXECCMD,
20-
FINISHCMD,
20+
CMDFINISHED,
2121
READY,
2222
ACK,
2323
ERROR,
2424
WINSIZE,
2525
PING,
26-
FINISH,
26+
PODFINISHED,
2727
NEXT,
2828
WRITEFILE,
2929
READFILE,
3030
NEWCONTAINER,
31+
KILLCONTAINER,
3132
};
3233

3334
enum {
@@ -41,17 +42,17 @@ struct hyper_pod {
4142
struct hyper_interface *iface;
4243
struct hyper_route *rt;
4344
char **dns;
44-
struct list_head dyn_containers;
45+
struct list_head containers;
4546
struct list_head exec_head;
4647
char *hostname;
4748
char *share_tag;
4849
int init_pid;
49-
uint32_t c_num;
5050
uint32_t i_num;
5151
uint32_t r_num;
5252
uint32_t e_num;
5353
uint32_t d_num;
5454
uint32_t type;
55+
/* how many containers are running */
5556
uint32_t remains;
5657
uint8_t policy;
5758
int efd;
@@ -65,6 +66,11 @@ struct hyper_win_size {
6566
uint64_t seq;
6667
};
6768

69+
struct hyper_killer {
70+
char *id;
71+
int signal;
72+
};
73+
6874
struct hyper_reader {
6975
char *id;
7076
char *file;

src/init.c

Lines changed: 43 additions & 15 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)
@@ -457,6 +457,8 @@ int hyper_start_container_stage0(struct hyper_container *c, struct hyper_pod *po
457457
goto out;
458458
}
459459

460+
/* container process is spawned and ready to execute */
461+
pod->remains++;
460462
ret = 0;
461463
out:
462464
close(arg.ctl_pipe[0]);
@@ -466,13 +468,13 @@ int hyper_start_container_stage0(struct hyper_container *c, struct hyper_pod *po
466468

467469
int hyper_start_containers(struct hyper_pod *pod)
468470
{
469-
int i, ret = 0;
471+
struct hyper_container *c;
470472

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;
473+
list_for_each_entry(c, &pod->containers, list) {
474+
if (hyper_start_container_stage0(c, pod) < 0)
475+
return -1;
475476
}
477+
476478
return 0;
477479
}
478480

@@ -696,25 +698,48 @@ static int hyper_new_container(char *json, int length)
696698

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

699-
if (!pod->init_pid)
701+
if (!pod->init_pid) {
700702
fprintf(stdout, "the pod is not created yet\n");
703+
return -1;
704+
}
701705

702706
c = hyper_parse_new_container(pod, json, length);
703707
if (c == NULL) {
704708
fprintf(stderr, "parse container json failed\n");
705709
return -1;
706710
}
707711

712+
list_add_tail(&c->list, &pod->containers);
708713
ret = hyper_start_container_stage0(c, pod);
709714
if (ret < 0) {
710715
//TODO full grace cleanup
711716
hyper_cleanup_container(c);
712-
free(c);
713-
return ret;
714717
}
715718

716-
list_add_tail(&c->dyn, &pod->dyn_containers);
717-
return 0;
719+
return ret;
720+
}
721+
722+
static int hyper_kill_container(char *json, int length)
723+
{
724+
struct hyper_killer killer;
725+
struct hyper_container *c;
726+
struct hyper_pod *pod = &global_pod;
727+
int ret = -1;
728+
729+
if (hyper_parse_kill_container(&killer, json, length) < 0) {
730+
goto out;
731+
}
732+
733+
c = hyper_find_container(pod, killer.id);
734+
if (c == NULL) {
735+
fprintf(stderr, "can not find container whose id is %s\n", killer.id);
736+
goto out;
737+
}
738+
739+
kill(c->exec.pid, killer.signal);
740+
ret = 0;
741+
out:
742+
return ret;
718743
}
719744

720745
static int hyper_cmd_write_file(char *json, int length)
@@ -1115,7 +1140,7 @@ static int hyper_channel_handle(struct hyper_event *de, uint32_t len)
11151140
hyper_print_uptime();
11161141
break;
11171142
case STOPPOD:
1118-
ret = hyper_stop_pod(pod);
1143+
hyper_stop_pod(pod);
11191144
return 0;
11201145
//break;
11211146
case DESTROYPOD:
@@ -1143,6 +1168,9 @@ static int hyper_channel_handle(struct hyper_event *de, uint32_t len)
11431168
case NEWCONTAINER:
11441169
ret = hyper_new_container((char *)buf->data + 8, len - 8);
11451170
break;
1171+
case KILLCONTAINER:
1172+
ret = hyper_kill_container((char *)buf->data + 8, len - 8);
1173+
break;
11461174
default:
11471175
ret = -1;
11481176
break;

0 commit comments

Comments
 (0)