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

Commit fe5e0fc

Browse files
authored
Merge pull request #235 from laijs/update-hyperstart-api
Update hyperstart api
2 parents 5b33d7a + ecfb7e9 commit fe5e0fc

File tree

6 files changed

+73
-53
lines changed

6 files changed

+73
-53
lines changed

src/api.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
#ifndef _HYPERSTART_API_H_
22
#define _HYPERSTART_API_H_
33

4-
#define APIVERSION 4242
4+
// when APIVERSION < 1000000, the version MUST be exactly matched on both sides
5+
#define APIVERSION 4243
56

67
// control command id
78
enum {
8-
GETVERSION,
9+
GETVERSION, // 0
910
STARTPOD,
10-
GETPOD,
11+
GETPOD_DEPRECATED,
1112
STOPPOD_DEPRECATED,
1213
DESTROYPOD,
13-
RESTARTCONTAINER,
14+
RESTARTCONTAINER_DEPRECATED, // 5
1415
EXECCMD,
15-
CMDFINISHED,
16+
CMDFINISHED_DEPRECATED,
1617
READY,
1718
ACK,
18-
ERROR,
19+
ERROR, // 10
1920
WINSIZE,
2021
PING,
21-
PODFINISHED,
22+
PODFINISHED_DEPRECATED,
2223
NEXT,
23-
WRITEFILE,
24+
WRITEFILE, // 15
2425
READFILE,
2526
NEWCONTAINER,
2627
KILLCONTAINER,
2728
ONLINECPUMEM,
28-
SETUPINTERFACE,
29+
SETUPINTERFACE, // 20
2930
SETUPROUTE,
3031
REMOVECONTAINER,
32+
PROCESSASYNCEVENT,
3133
};
3234

3335
/*

src/exec.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ static int hyper_send_exec_eof(struct hyper_exec *exec) {
5656
}
5757

5858
static int hyper_send_exec_code(struct hyper_exec *exec) {
59-
return send_exec_finishing(exec->seq, 13, exec->code);
59+
char *pae; // ProcessAsyncEvent
60+
#define PAE "{\"container\":\"%s\",\"process\":\"%s\",\"event\":\"finished\",\"status\":%d}"
61+
if (asprintf(&pae, PAE, exec->container_id, exec->id, exec->code) < 0) {
62+
return -1;
63+
}
64+
#undef PAE
65+
return hyper_ctl_append_msg(&hyper_epoll.ctl, PROCESSASYNCEVENT, (uint8_t *)pae, strlen(pae));
6066
}
6167

6268
static void pts_hup(struct hyper_event *de, int efd, struct hyper_exec *exec)
@@ -703,9 +709,6 @@ static int hyper_release_exec(struct hyper_exec *exec)
703709
if (exec->pod->req_destroy) {
704710
/* shutdown vm manually, hyper doesn't care the pod finished codes */
705711
hyper_pod_destroyed(0);
706-
} else {
707-
/* send out pod finish message, hyper will decide if restart pod or not */
708-
hyper_send_pod_finished(exec->pod);
709712
}
710713

711714
hyper_cleanup_pod(exec->pod);
@@ -716,6 +719,19 @@ static int hyper_release_exec(struct hyper_exec *exec)
716719
return 0;
717720
}
718721

722+
struct hyper_exec *hyper_find_exec_by_name(struct hyper_pod *pod, const char *process)
723+
{
724+
struct hyper_exec *exec;
725+
726+
list_for_each_entry(exec, &pod->exec_head, list) {
727+
if (strcmp(exec->id, process) == 0) {
728+
return exec;
729+
}
730+
}
731+
732+
return NULL;
733+
}
734+
719735
struct hyper_exec *hyper_find_exec_by_pid(struct list_head *head, int pid)
720736
{
721737
struct hyper_exec *exec;

src/exec.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ struct hyper_exec {
2727

2828
// configs
2929
char *container_id;
30+
char *id;
3031
char *user;
3132
char *group;
3233
char **additional_groups;
@@ -45,6 +46,7 @@ struct hyper_pod;
4546

4647
int hyper_exec_cmd(char *json, int length);
4748
int hyper_run_process(struct hyper_exec *e);
49+
struct hyper_exec *hyper_find_exec_by_name(struct hyper_pod *pod, const char *process);
4850
struct hyper_exec *hyper_find_exec_by_pid(struct list_head *head, int pid);
4951
struct hyper_exec *hyper_find_exec_by_seq(struct hyper_pod *pod, uint64_t seq);
5052
int hyper_handle_exec_exit(struct hyper_pod *pod, int pid, uint8_t code);

src/hyper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ int hyper_open_serial(char *tty);
8585
void hyper_cleanup_pod(struct hyper_pod *pod);
8686
int hyper_enter_sandbox(struct hyper_pod *pod, int pidpipe);
8787
void hyper_pod_destroyed(int failed);
88-
int hyper_send_pod_finished(struct hyper_pod *pod);
88+
int hyper_ctl_append_msg(struct hyper_event *he, uint32_t type, uint8_t *data, uint32_t len);
8989

9090
extern struct hyper_pod global_pod;
9191
extern struct hyper_epoll hyper_epoll;

src/init.c

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -42,50 +42,40 @@ struct hyper_epoll hyper_epoll;
4242
sigset_t orig_mask;
4343

4444
static int hyper_handle_exit(struct hyper_pod *pod);
45-
static int hyper_ctl_append_msg(struct hyper_event *he, uint32_t type, uint8_t *data, uint32_t len);
4645

47-
int hyper_send_pod_finished(struct hyper_pod *pod)
48-
{
49-
struct hyper_container *c;
50-
uint8_t *data = NULL, *new;
51-
int c_num = 0;
52-
53-
list_for_each_entry(c, &pod->containers, list) {
54-
c_num++;
55-
new = realloc(data, c_num * 4);
56-
if (new == NULL) {
57-
free(data);
58-
return -1;
59-
}
60-
61-
hyper_set_be32(new + ((c_num - 1) * 4), c->exec.code);
62-
data = new;
63-
}
64-
65-
return hyper_ctl_append_msg(&hyper_epoll.ctl, PODFINISHED, data, c_num * 4);
66-
}
67-
68-
static int hyper_set_win_size(char *json, int length)
46+
static int hyper_set_win_size(struct hyper_pod *pod, char *json, int length)
6947
{
7048
struct winsize size;
7149
struct hyper_exec *exec;
72-
int ret;
50+
int ret = -1;
7351

74-
fprintf(stdout, "call hyper_win_size, json %s, len %d\n", json, length);
52+
fprintf(stdout, "call hyper_set_win_size, json %s, len %d\n", json, length);
7553
JSON_Value *value = hyper_json_parse(json, length);
7654
if (value == NULL) {
7755
fprintf(stderr, "set term size failed\n");
78-
ret = -1;
7956
goto out;
8057
}
81-
const uint64_t seq = (uint64_t)json_object_get_number(json_object(value), "seq");
58+
const char *container = json_object_get_string(json_object(value), "container");
59+
const char *process = json_object_get_string(json_object(value), "process");
60+
if (!container || !process) {
61+
fprintf(stderr, "call hyper_set_win_size, invalid config");
62+
goto out;
63+
}
8264

83-
exec = hyper_find_exec_by_seq(&global_pod, seq);
84-
if (exec == NULL) {
85-
fprintf(stdout, "can not find exec whose seq is %" PRIu64"\n", seq);
86-
ret = 0;
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);
8768
goto out;
8869
}
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+
}
8979

9080
size.ws_row = (int)json_object_get_number(json_object(value), "row");
9181
size.ws_col = (int)json_object_get_number(json_object(value), "column");
@@ -1069,7 +1059,7 @@ static int hyper_ttyfd_read(struct hyper_event *he, int efd, int events)
10691059
return ret == 0 ? 0 : -1;
10701060
}
10711061

1072-
static int hyper_ctl_append_msg(struct hyper_event *he, uint32_t type, uint8_t *data, uint32_t len)
1062+
int hyper_ctl_append_msg(struct hyper_event *he, uint32_t type, uint8_t *data, uint32_t len)
10731063
{
10741064
int ret = -1;
10751065
fprintf(stdout, "hyper ctl append type %d, len %d\n", type, len);
@@ -1118,10 +1108,6 @@ static int hyper_ctlmsg_handle(struct hyper_event *he, uint32_t len)
11181108
ret = hyper_start_pod((char *)buf->data + 8, len - 8);
11191109
hyper_print_uptime();
11201110
break;
1121-
case STOPPOD_DEPRECATED:
1122-
fprintf(stderr, "get abandoned STOPPOD message\n");
1123-
ret = -1;
1124-
break;
11251111
case DESTROYPOD:
11261112
pod->req_destroy = 1;
11271113
fprintf(stdout, "get DESTROYPOD message\n");
@@ -1137,13 +1123,12 @@ static int hyper_ctlmsg_handle(struct hyper_event *he, uint32_t len)
11371123
ret = hyper_cmd_rw_file((char *)buf->data + 8, len - 8, &datalen, &data, READFILE);
11381124
break;
11391125
case PING:
1140-
case GETPOD:
11411126
break;
11421127
case READY:
11431128
ret = hyper_rescan();
11441129
break;
11451130
case WINSIZE:
1146-
ret = hyper_set_win_size((char *)buf->data + 8, len - 8);
1131+
ret = hyper_set_win_size(pod, (char *)buf->data + 8, len - 8);
11471132
break;
11481133
case NEWCONTAINER:
11491134
ret = hyper_new_container((char *)buf->data + 8, len - 8);
@@ -1163,6 +1148,14 @@ static int hyper_ctlmsg_handle(struct hyper_event *he, uint32_t len)
11631148
case SETUPROUTE:
11641149
ret = hyper_cmd_setup_route((char *)buf->data + 8, len - 8);
11651150
break;
1151+
case GETPOD_DEPRECATED:
1152+
case STOPPOD_DEPRECATED:
1153+
case RESTARTCONTAINER_DEPRECATED:
1154+
case CMDFINISHED_DEPRECATED:
1155+
case PODFINISHED_DEPRECATED:
1156+
fprintf(stderr, "get abandoned command\n");
1157+
ret = -1;
1158+
break;
11661159
default:
11671160
ret = -1;
11681161
break;

src/parse.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ void hyper_cleanup_exec(struct hyper_exec *exec)
188188
free(exec->container_id);
189189
exec->container_id = NULL;
190190

191+
free(exec->id);
192+
exec->id = NULL;
193+
191194
free(exec->user);
192195
exec->user = NULL;
193196
free(exec->group);
@@ -474,7 +477,11 @@ static int hyper_parse_process(struct hyper_exec *exec, char *json, jsmntok_t *t
474477
for (j = 0; j < toks_size; j++) {
475478
t = &toks[i];
476479
dprintf(stdout, "%d name %s\n", i, json_token_str(json, t));
477-
if (json_token_streq(json, t, "user") && t->size == 1) {
480+
if (json_token_streq(json, t, "id") && t->size == 1) {
481+
exec->id = (json_token_str(json, &toks[++i]));
482+
dprintf(stdout, "container process id %s\n", exec->id);
483+
i++;
484+
} else if (json_token_streq(json, t, "user") && t->size == 1) {
478485
exec->user = (json_token_str(json, &toks[++i]));
479486
dprintf(stdout, "container process user %s\n", exec->user);
480487
i++;

0 commit comments

Comments
 (0)