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

Commit 85712c4

Browse files
authored
Merge pull request #241 from laijs/tiny-cleanup
Tiny cleanup
2 parents 9572c94 + a325b7f commit 85712c4

File tree

4 files changed

+55
-35
lines changed

4 files changed

+55
-35
lines changed

src/exec.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ static void hyper_free_exec(struct hyper_exec *exec)
590590
free(exec);
591591
}
592592

593-
int hyper_exec_cmd(char *json, int length)
593+
int hyper_exec_cmd(struct hyper_pod *pod, char *json, int length)
594594
{
595595
struct hyper_exec *exec;
596596

@@ -602,7 +602,18 @@ int hyper_exec_cmd(char *json, int length)
602602
return -1;
603603
}
604604

605-
exec->pod = &global_pod;
605+
if (hyper_find_container(pod, exec->container_id) == NULL) {
606+
fprintf(stderr, "call hyper_exec_cmd, no such container: %s\n", exec->container_id);
607+
hyper_free_exec(exec);
608+
return -1;
609+
}
610+
if (hyper_find_exec_by_name(pod, exec->id) != NULL) {
611+
fprintf(stderr, "call hyper_exec_cmd, process id conflicts");
612+
hyper_free_exec(exec);
613+
return -1;
614+
}
615+
616+
exec->pod = pod;
606617
int ret = hyper_run_process(exec);
607618
if (ret < 0) {
608619
hyper_free_exec(exec);
@@ -719,6 +730,24 @@ static int hyper_release_exec(struct hyper_exec *exec)
719730
return 0;
720731
}
721732

733+
struct hyper_exec *hyper_find_process(struct hyper_pod *pod, const char *container, const char *process)
734+
{
735+
struct hyper_container *c = hyper_find_container(pod, container);
736+
if (c) {
737+
if (strcmp(c->exec.id, process) == 0) {
738+
return &c->exec;
739+
}
740+
} else {
741+
return NULL;
742+
}
743+
744+
struct hyper_exec *exec = hyper_find_exec_by_name(pod, process);
745+
if (strcmp(exec->container_id, container) == 0) {
746+
return exec;
747+
}
748+
return NULL;
749+
}
750+
722751
struct hyper_exec *hyper_find_exec_by_name(struct hyper_pod *pod, const char *process)
723752
{
724753
struct hyper_exec *exec;

src/exec.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ struct hyper_exec {
4444

4545
struct hyper_pod;
4646

47-
int hyper_exec_cmd(char *json, int length);
47+
int hyper_exec_cmd(struct hyper_pod *pod, char *json, int length);
4848
int hyper_run_process(struct hyper_exec *e);
49+
struct hyper_exec *hyper_find_process(struct hyper_pod *pod, const char *container, const char *process);
4950
struct hyper_exec *hyper_find_exec_by_name(struct hyper_pod *pod, const char *process);
5051
struct hyper_exec *hyper_find_exec_by_pid(struct list_head *head, int pid);
5152
struct hyper_exec *hyper_find_exec_by_seq(struct hyper_pod *pod, uint64_t seq);

src/hyper.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ int hyper_enter_sandbox(struct hyper_pod *pod, int pidpipe);
8787
void hyper_pod_destroyed(int failed);
8888
int hyper_ctl_append_msg(struct hyper_event *he, uint32_t type, uint8_t *data, uint32_t len);
8989

90-
extern struct hyper_pod global_pod;
9190
extern struct hyper_epoll hyper_epoll;
9291
extern sigset_t orig_mask;
9392
#endif

src/init.c

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include "container.h"
3131
#include "syscall.h"
3232

33-
struct hyper_pod global_pod = {
33+
static struct hyper_pod global_pod = {
3434
.containers = LIST_HEAD_INIT(global_pod.containers),
3535
.exec_head = LIST_HEAD_INIT(global_pod.exec_head),
3636
};
@@ -62,20 +62,11 @@ static int hyper_set_win_size(struct hyper_pod *pod, char *json, int length)
6262
goto out;
6363
}
6464

65-
struct hyper_container *c = hyper_find_container(pod, container);
66-
if (!c) {
67-
fprintf(stderr, "call hyper_set_win_size, can not find the container: %s\n", container);
65+
exec = hyper_find_process(pod, container, process);
66+
if (!exec) {
67+
fprintf(stderr, "call hyper_set_win_size, can not find the process: %s\n", process);
6868
goto out;
6969
}
70-
if (strcmp(c->exec.id, process) == 0) {
71-
exec = &c->exec;
72-
} else {
73-
exec = hyper_find_exec_by_name(pod, process);
74-
if (!exec) {
75-
fprintf(stderr, "call hyper_set_win_size, can not find the process: %s\n", process);
76-
goto out;
77-
}
78-
}
7970

8071
size.ws_row = (int)json_object_get_number(json_object(value), "row");
8172
size.ws_col = (int)json_object_get_number(json_object(value), "column");
@@ -564,10 +555,8 @@ static int hyper_destroy_pod(struct hyper_pod *pod, int error)
564555
return 0;
565556
}
566557

567-
static int hyper_start_pod(char *json, int length)
558+
static int hyper_start_pod(struct hyper_pod *pod, char *json, int length)
568559
{
569-
struct hyper_pod *pod = &global_pod;
570-
571560
fprintf(stdout, "call hyper_start_pod, json %s, len %d\n", json, length);
572561

573562
if (pod->init_pid)
@@ -593,11 +582,10 @@ static int hyper_start_pod(char *json, int length)
593582
return 0;
594583
}
595584

596-
static int hyper_new_container(char *json, int length)
585+
static int hyper_new_container(struct hyper_pod *pod, char *json, int length)
597586
{
598587
int ret;
599588
struct hyper_container *c;
600-
struct hyper_pod *pod = &global_pod;
601589

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

@@ -612,6 +600,12 @@ static int hyper_new_container(char *json, int length)
612600
return -1;
613601
}
614602

603+
if (hyper_find_container(pod, c->id) != NULL) {
604+
fprintf(stderr, "container id conflicts");
605+
hyper_cleanup_container(c, pod);
606+
return -1;
607+
}
608+
615609
list_add_tail(&c->list, &pod->containers);
616610
ret = hyper_setup_container(c, pod);
617611
if (ret >= 0)
@@ -627,10 +621,9 @@ static int hyper_new_container(char *json, int length)
627621
return ret;
628622
}
629623

630-
static int hyper_kill_container(char *json, int length)
624+
static int hyper_kill_container(struct hyper_pod *pod, char *json, int length)
631625
{
632626
struct hyper_container *c;
633-
struct hyper_pod *pod = &global_pod;
634627
int ret = -1;
635628

636629
JSON_Value *value = hyper_json_parse(json, length);
@@ -652,10 +645,9 @@ static int hyper_kill_container(char *json, int length)
652645
return ret;
653646
}
654647

655-
static int hyper_remove_container(char *json, int length)
648+
static int hyper_remove_container(struct hyper_pod *pod, char *json, int length)
656649
{
657650
struct hyper_container *c;
658-
struct hyper_pod *pod = &global_pod;
659651
int ret = -1;
660652

661653
JSON_Value *value = hyper_json_parse(json, length);
@@ -719,7 +711,7 @@ static int hyper_open_container_file(void *data)
719711
exit(ret);
720712
}
721713

722-
static int hyper_cmd_rw_file(char *json, int length, uint32_t *rdatalen, uint8_t **rdata, int rw)
714+
static int hyper_cmd_rw_file(struct hyper_pod *pod, char *json, int length, uint32_t *rdatalen, uint8_t **rdata, int rw)
723715
{
724716
struct file_command cmd = {
725717
.id = NULL,
@@ -730,7 +722,6 @@ static int hyper_cmd_rw_file(char *json, int length, uint32_t *rdatalen, uint8_t
730722
.rw = rw,
731723
};
732724
struct hyper_container *c;
733-
struct hyper_pod *pod = &global_pod;
734725
char *data = NULL;
735726
void *stack = NULL;
736727
int stacksize = getpagesize() * 4;
@@ -1103,7 +1094,7 @@ static int hyper_ctlmsg_handle(struct hyper_event *he, uint32_t len)
11031094
hyper_set_be32(data, APIVERSION);
11041095
break;
11051096
case STARTPOD:
1106-
ret = hyper_start_pod((char *)buf->data + 8, len - 8);
1097+
ret = hyper_start_pod(pod, (char *)buf->data + 8, len - 8);
11071098
hyper_print_uptime();
11081099
break;
11091100
case DESTROYPOD:
@@ -1112,13 +1103,13 @@ static int hyper_ctlmsg_handle(struct hyper_event *he, uint32_t len)
11121103
hyper_destroy_pod(pod, 0);
11131104
return 0;
11141105
case EXECCMD:
1115-
ret = hyper_exec_cmd((char *)buf->data + 8, len - 8);
1106+
ret = hyper_exec_cmd(pod, (char *)buf->data + 8, len - 8);
11161107
break;
11171108
case WRITEFILE:
1118-
ret = hyper_cmd_rw_file((char *)buf->data + 8, len - 8, NULL, NULL, WRITEFILE);
1109+
ret = hyper_cmd_rw_file(pod, (char *)buf->data + 8, len - 8, NULL, NULL, WRITEFILE);
11191110
break;
11201111
case READFILE:
1121-
ret = hyper_cmd_rw_file((char *)buf->data + 8, len - 8, &datalen, &data, READFILE);
1112+
ret = hyper_cmd_rw_file(pod, (char *)buf->data + 8, len - 8, &datalen, &data, READFILE);
11221113
break;
11231114
case PING:
11241115
break;
@@ -1129,13 +1120,13 @@ static int hyper_ctlmsg_handle(struct hyper_event *he, uint32_t len)
11291120
ret = hyper_set_win_size(pod, (char *)buf->data + 8, len - 8);
11301121
break;
11311122
case NEWCONTAINER:
1132-
ret = hyper_new_container((char *)buf->data + 8, len - 8);
1123+
ret = hyper_new_container(pod, (char *)buf->data + 8, len - 8);
11331124
break;
11341125
case KILLCONTAINER:
1135-
ret = hyper_kill_container((char *)buf->data + 8, len - 8);
1126+
ret = hyper_kill_container(pod, (char *)buf->data + 8, len - 8);
11361127
break;
11371128
case REMOVECONTAINER:
1138-
ret = hyper_remove_container((char *)buf->data + 8, len - 8);
1129+
ret = hyper_remove_container(pod, (char *)buf->data + 8, len - 8);
11391130
break;
11401131
case ONLINECPUMEM:
11411132
hyper_cmd_online_cpu_mem();

0 commit comments

Comments
 (0)