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

Commit d346564

Browse files
committed
kill process in destroy pod
hyper send destroy pod to shutdown vm, we should give process a chance to handle TERM signal. Signed-off-by: Gao feng <omarapazanadi@gmail.com>
1 parent 2b1edec commit d346564

File tree

4 files changed

+45
-39
lines changed

4 files changed

+45
-39
lines changed

src/exec.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,29 @@ int hyper_exec_cmd(char *json, int length)
524524
goto out;
525525
}
526526

527+
static int hyper_send_pod_finished(struct hyper_pod *pod)
528+
{
529+
int ret = -1;
530+
struct hyper_container *c;
531+
uint8_t *data = NULL, *new;
532+
int c_num = 0;
533+
534+
list_for_each_entry(c, &pod->containers, list) {
535+
c_num++;
536+
new = realloc(data, c_num * 4);
537+
if (new == NULL)
538+
goto out;
539+
540+
hyper_set_be32(new + ((c_num - 1) * 4), c->exec.code);
541+
data = new;
542+
}
543+
544+
ret = hyper_send_msg_block(ctl.chan.fd, PODFINISHED, c_num * 4, data);
545+
out:
546+
free(data);
547+
return ret;
548+
}
549+
527550
int hyper_release_exec(struct hyper_exec *exec,
528551
struct hyper_pod *pod)
529552
{
@@ -550,8 +573,11 @@ int hyper_release_exec(struct hyper_exec *exec,
550573
return 0;
551574

552575
if (pod->type == STOPPOD) {
553-
/* stop pod manually */
576+
/* stop pod manually, hyper doesn't care the pod finished codes */
554577
hyper_send_msg_block(ctl.chan.fd, ACK, 0, NULL);
578+
} else if (pod->type == DESTROYPOD) {
579+
/* shutdown vm manually, hyper doesn't care the pod finished codes */
580+
hyper_shutdown();
555581
} else {
556582
/* send out pod finish message, hyper will decide if restart pod or not */
557583
hyper_send_pod_finished(pod);
@@ -562,7 +588,6 @@ int hyper_release_exec(struct hyper_exec *exec,
562588
}
563589

564590
hyper_free_exec(exec);
565-
566591
return 0;
567592
}
568593

src/init.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,18 @@ static void hyper_print_uptime(void)
669669
close(fd);
670670
}
671671

672+
static int hyper_destroy_pod(struct hyper_pod *pod)
673+
{
674+
if (pod->init_pid == 0) {
675+
/* Pod stopped, just shutdown */
676+
hyper_shutdown();
677+
} else {
678+
/* Kill pod */
679+
hyper_term_all(pod);
680+
}
681+
return 0;
682+
}
683+
672684
static int hyper_start_pod(char *json, int length)
673685
{
674686
struct hyper_pod *pod = &global_pod;
@@ -684,7 +696,7 @@ static int hyper_start_pod(char *json, int length)
684696
}
685697

686698
if (hyper_setup_pod(pod) < 0) {
687-
hyper_shutdown(pod);
699+
hyper_destroy_pod(pod);
688700
return -1;
689701
}
690702

@@ -1151,7 +1163,7 @@ static int hyper_channel_handle(struct hyper_event *de, uint32_t len)
11511163
//break;
11521164
case DESTROYPOD:
11531165
fprintf(stdout, "get DESTROYPOD message\n");
1154-
hyper_shutdown(pod);
1166+
hyper_destroy_pod(pod);
11551167
return 0;
11561168
case EXECCMD:
11571169
ret = hyper_exec_cmd((char *)buf->data + 8, len - 8);

src/util.c

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ int hyper_socketpair(int domain, int type, int protocol, int sv[2])
339339
return 0;
340340
}
341341

342-
void hyper_unmount_all(void)
342+
static void hyper_unmount_all(void)
343343
{
344344
FILE *mtab;
345345
struct mntent *mnt;
@@ -384,38 +384,9 @@ void hyper_unmount_all(void)
384384
sync();
385385
}
386386

387-
int hyper_send_pod_finished(struct hyper_pod *pod)
387+
void hyper_shutdown()
388388
{
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-
}
403-
404-
ret = hyper_send_msg_block(ctl.chan.fd, PODFINISHED, c_num * 4, data);
405-
out:
406-
free(data);
407-
return ret;
408-
}
409-
410-
void hyper_shutdown(struct hyper_pod *pod)
411-
{
412-
hyper_send_pod_finished(pod);
413-
/* vm will shutdown immediately after we call reboot,
414-
* no chance to send out eof message in release exec.
415-
* send it out by ourself */
416-
hyper_cleanup_exec(pod);
417-
389+
hyper_send_msg_block(ctl.chan.fd, ACK, 0, NULL);
418390
hyper_unmount_all();
419-
420391
reboot(LINUX_REBOOT_CMD_POWER_OFF);
421392
}

src/util.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ int hyper_setfd_cloexec(int fd);
2424
int hyper_setfd_block(int fd);
2525
int hyper_setfd_nonblock(int fd);
2626
int hyper_socketpair(int domain, int type, int protocol, int sv[2]);
27-
void hyper_shutdown(struct hyper_pod *pod);
28-
int hyper_send_pod_finished(struct hyper_pod *pod);
29-
void hyper_unmount_all(void);
27+
void hyper_shutdown(void);
3028
int hyper_insmod(char *module);
3129
#endif

0 commit comments

Comments
 (0)