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

Commit 6376e77

Browse files
authored
Merge pull request #289 from YaoZengzeng/eventfd-type
hyper_get/send_type by eventfd
2 parents 2819bb5 + 976ba1f commit 6376e77

File tree

8 files changed

+130
-106
lines changed

8 files changed

+130
-106
lines changed

src/container.c

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <errno.h>
1515
#include <fcntl.h>
1616
#include <dirent.h>
17+
#include <sys/eventfd.h>
1718

1819
#include "util.h"
1920
#include "hyper.h"
@@ -525,8 +526,8 @@ static int hyper_rescan_scsi(void)
525526
struct hyper_container_arg {
526527
struct hyper_container *c;
527528
struct hyper_pod *pod;
528-
int pipe[2];
529-
int pipens[2];
529+
int mntns_referenced_efd;
530+
int container_inited_efd;
530531
};
531532

532533
static int hyper_setup_container_rootfs(void *data)
@@ -535,10 +536,9 @@ static int hyper_setup_container_rootfs(void *data)
535536
struct hyper_container *container = arg->c;
536537
char root[512], rootfs[512];
537538
int setup_dns;
538-
uint32_t type;
539539

540540
/* wait for ns-opened ready message */
541-
if (hyper_get_type(arg->pipens[0], &type) < 0 || type != READY) {
541+
if (hyper_eventfd_recv(arg->mntns_referenced_efd) < 0) {
542542
fprintf(stderr, "wait for /proc/self/ns/mnt opened failed\n");
543543
goto fail;
544544
}
@@ -677,12 +677,14 @@ static int hyper_setup_container_rootfs(void *data)
677677
goto fail;
678678
}
679679

680-
hyper_send_type(arg->pipe[1], READY);
680+
fprintf(stdout, "hyper send container inited event: normal\n");
681+
hyper_eventfd_send(arg->container_inited_efd, HYPER_EVENTFD_NORMAL);
681682
fflush(NULL);
682683
_exit(0);
683684

684685
fail:
685-
hyper_send_type(arg->pipe[1], ERROR);
686+
fprintf(stderr, "hyper send container inited event: error\n");
687+
hyper_eventfd_send(arg->container_inited_efd, HYPER_EVENTFD_ERROR);
686688
_exit(125);
687689
}
688690

@@ -712,19 +714,20 @@ int hyper_setup_container(struct hyper_container *container, struct hyper_pod *p
712714
struct hyper_container_arg arg = {
713715
.c = container,
714716
.pod = pod,
715-
.pipe = {-1, -1},
716-
.pipens = {-1, -1},
717+
.mntns_referenced_efd = -1,
718+
.container_inited_efd = -1,
717719
};
718720
int flags = CLONE_NEWNS | SIGCHLD;
719721
char path[128];
720722
void *stack;
721-
uint32_t type;
722723
int pid;
723724

724725
container->exec.pod = pod;
725726

726-
if (pipe2(arg.pipe, O_CLOEXEC) < 0 || pipe2(arg.pipens, O_CLOEXEC) < 0) {
727-
perror("create pipe between pod init execcmd failed");
727+
arg.mntns_referenced_efd = eventfd(0, EFD_CLOEXEC);
728+
arg.container_inited_efd = eventfd(0, EFD_CLOEXEC);
729+
if (arg.mntns_referenced_efd < 0 || arg.container_inited_efd < 0) {
730+
perror("create eventfd between pod init execcmd failed");
728731
goto fail;
729732
}
730733

@@ -757,26 +760,23 @@ int hyper_setup_container(struct hyper_container *container, struct hyper_pod *p
757760
perror("open container mount ns failed");
758761
goto fail;
759762
}
760-
hyper_send_type(arg.pipens[1], READY);
763+
fprintf(stdout, "hyper send mntns referenced event: normal\n");
764+
hyper_eventfd_send(arg.mntns_referenced_efd, HYPER_EVENTFD_NORMAL);
761765

762766
/* wait for ready message */
763-
if (hyper_get_type(arg.pipe[0], &type) < 0 || type != READY) {
767+
if (hyper_eventfd_recv(arg.container_inited_efd) < 0) {
764768
fprintf(stderr, "wait for setup container rootfs failed\n");
765769
goto fail;
766770
}
767771

768-
close(arg.pipe[0]);
769-
close(arg.pipe[1]);
770-
close(arg.pipens[0]);
771-
close(arg.pipens[1]);
772+
close(arg.mntns_referenced_efd);
773+
close(arg.container_inited_efd);
772774
return 0;
773775
fail:
774776
close(container->ns);
775777
container->ns = -1;
776-
close(arg.pipe[0]);
777-
close(arg.pipe[1]);
778-
close(arg.pipens[0]);
779-
close(arg.pipens[1]);
778+
close(arg.mntns_referenced_efd);
779+
close(arg.container_inited_efd);
780780
return -1;
781781
}
782782

@@ -799,10 +799,11 @@ struct hyper_container *hyper_find_container(struct hyper_pod *pod, const char *
799799

800800
static void hyper_cleanup_container_mounts(struct hyper_container *container, struct hyper_pod *pod)
801801
{
802-
int pid, pipe[2] = {-1, -1};
802+
int pid, efd = -1;
803803

804-
if (pipe2(pipe, O_CLOEXEC) < 0) {
805-
perror("create pipe for unmount failed");
804+
efd = eventfd(0, EFD_CLOEXEC);
805+
if (efd < 0) {
806+
perror("create eventfd for unmount failed");
806807
return;
807808
}
808809

@@ -812,23 +813,25 @@ static void hyper_cleanup_container_mounts(struct hyper_container *container, st
812813
goto out;
813814
} else if (pid == 0) {
814815
if (hyper_enter_sandbox(pod, -1) < 0) {
815-
hyper_send_type(pipe[1], -1);
816+
fprintf(stderr, "hyper send enter sandbox event: error\n");
817+
hyper_eventfd_send(efd, HYPER_EVENTFD_ERROR);
816818
_exit(-1);
817819
}
818820
if (setns(container->ns, CLONE_NEWNS) < 0) {
819821
perror("fail to enter container ns");
820-
hyper_send_type(pipe[1], -1);
822+
fprintf(stderr, "hyper send enter container ns event: error\n");
823+
hyper_eventfd_send(efd, HYPER_EVENTFD_ERROR);
821824
_exit(-1);
822825
}
823826
hyper_unmount_all();
824-
hyper_send_type(pipe[1], 0);
827+
fprintf(stdout, "hyper send cleanup container mounts event: normal\n");
828+
hyper_eventfd_send(efd, HYPER_EVENTFD_NORMAL);
825829
_exit(0);
826830
}
827-
hyper_get_type(pipe[0], (uint32_t *)&pid);
831+
hyper_eventfd_recv(efd);
828832

829833
out:
830-
close(pipe[0]);
831-
close(pipe[1]);
834+
close(efd);
832835
}
833836

834837
void hyper_cleanup_container(struct hyper_container *c, struct hyper_pod *pod)

src/exec.c

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <sys/ioctl.h>
99
#include <sys/wait.h>
1010
#include <sys/socket.h>
11+
#include <sys/eventfd.h>
1112
#include <dirent.h>
1213
#include <sched.h>
1314
#include <errno.h>
@@ -494,11 +495,11 @@ static int hyper_setup_stdio_events(struct hyper_exec *exec, struct stdio_config
494495
return 0;
495496
}
496497

497-
static int hyper_do_exec_cmd(struct hyper_exec *exec, int pipe, struct stdio_config *io)
498+
static int hyper_do_exec_cmd(struct hyper_exec *exec, int pid_efd, int process_inited_efd, struct stdio_config *io)
498499
{
499500
struct hyper_container *c;
500501

501-
if (hyper_enter_sandbox(exec->pod, pipe) < 0) {
502+
if (hyper_enter_sandbox(exec->pod, pid_efd) < 0) {
502503
perror("enter pidns of pod init failed");
503504
goto out;
504505
}
@@ -529,14 +530,15 @@ static int hyper_do_exec_cmd(struct hyper_exec *exec, int pipe, struct stdio_con
529530
else
530531
unsetenv("TERM");
531532

532-
hyper_exec_process(exec, pipe, io);
533+
hyper_exec_process(exec, process_inited_efd, io);
533534
out:
534-
hyper_send_type(pipe, -1);
535+
fprintf(stderr, "hyper send process inited event: error\n");
536+
hyper_eventfd_send(process_inited_efd, HYPER_EVENTFD_ERROR);
535537
_exit(125);
536538
}
537539

538540
// do the exec, no return
539-
static void hyper_exec_process(struct hyper_exec *exec, int pipe, struct stdio_config *io)
541+
static void hyper_exec_process(struct hyper_exec *exec, int process_inited_fd, struct stdio_config *io)
540542
{
541543
bool defaultPath = false;
542544
if (sigprocmask(SIG_SETMASK, &orig_mask, NULL) < 0) {
@@ -564,7 +566,8 @@ static void hyper_exec_process(struct hyper_exec *exec, int pipe, struct stdio_c
564566
setsid();
565567

566568
//send success notification before stdio was installed, otherwise the debug log in hyper_send_type() will be sent to user tty.
567-
hyper_send_type(pipe, 0);
569+
fprintf(stdout, "hyper send process inited event: normal\n");
570+
hyper_eventfd_send(process_inited_fd, HYPER_EVENTFD_NORMAL);
568571

569572
if (hyper_install_process_stdio(exec, io) < 0) {
570573
fprintf(stderr, "dup pts to exec stdio failed\n");
@@ -584,7 +587,8 @@ static void hyper_exec_process(struct hyper_exec *exec, int pipe, struct stdio_c
584587
}
585588

586589
exit:
587-
hyper_send_type(pipe, -1);
590+
fprintf(stderr, "hyper send process inited event: error\n");
591+
hyper_eventfd_send(process_inited_fd, HYPER_EVENTFD_ERROR);
588592
exit_nosend:
589593
fflush(NULL);
590594
_exit(125);
@@ -629,9 +633,9 @@ int hyper_exec_cmd(struct hyper_pod *pod, char *json, int length)
629633

630634
int hyper_run_process(struct hyper_exec *exec)
631635
{
632-
int pipe[2] = {-1, -1};
636+
int pid_efd = -1, process_inited_efd = -1;
633637
int pid, ret = -1;
634-
uint32_t cpid, type;
638+
int64_t cpid;
635639
struct stdio_config io = {-1, -1,-1, -1,-1, -1};
636640

637641
if (exec->argv == NULL || exec->seq == 0 || exec->container_id == NULL || strlen(exec->container_id) == 0) {
@@ -645,8 +649,11 @@ int hyper_run_process(struct hyper_exec *exec)
645649
goto out;
646650
}
647651

648-
if (pipe2(pipe, O_CLOEXEC) < 0) {
649-
perror("create pipe between pod init execcmd failed");
652+
653+
pid_efd = eventfd(0, EFD_CLOEXEC);
654+
process_inited_efd = eventfd(0, EFD_CLOEXEC);
655+
if (pid_efd < 0 || process_inited_efd < 0) {
656+
perror("create eventfd between pod init execcmd failed");
650657
goto close_tty;
651658
}
652659

@@ -656,15 +663,17 @@ int hyper_run_process(struct hyper_exec *exec)
656663
goto close_tty;
657664
} else if (pid == 0) {
658665
if (strcmp(exec->container_id, HYPERSTART_EXEC_CONTAINER) == 0) {
659-
hyper_send_type(pipe[1], getpid());
660-
hyper_exec_process(exec, pipe[1], &io);
666+
fprintf(stdout, "hyper send process pid: normal\n");
667+
hyper_eventfd_send(pid_efd, getpid());
668+
hyper_exec_process(exec, process_inited_efd, &io);
661669
_exit(125);
662670
}
663-
hyper_do_exec_cmd(exec, pipe[1], &io);
671+
hyper_do_exec_cmd(exec, pid_efd, process_inited_efd, &io);
664672
}
665673
fprintf(stdout, "prerequisite process pid %d\n", pid);
666674

667-
if (hyper_get_type(pipe[0], &cpid) < 0 || (int)cpid < 0) {
675+
cpid = hyper_eventfd_recv(pid_efd);
676+
if (cpid < 0) {
668677
fprintf(stderr, "run process failed\n");
669678
goto close_tty;
670679
}
@@ -674,13 +683,12 @@ int hyper_run_process(struct hyper_exec *exec)
674683
goto close_tty;
675684
}
676685

677-
if (hyper_get_type(pipe[0], &type) < 0 || (int)type < 0) {
678-
fprintf(stderr, "container process %u exec failed\n", cpid);
686+
if (hyper_eventfd_recv(process_inited_efd) < 0) {
687+
fprintf(stderr, "container process %u exec failed\n", (unsigned int)cpid);
679688
goto close_tty;
680689
}
681690

682-
//two message may come back in reversed sequence
683-
exec->pid = (type == 0) ? cpid : type;
691+
exec->pid = cpid;
684692
list_add_tail(&exec->list, &exec->pod->exec_head);
685693
exec->ref++;
686694
fprintf(stdout, "%s process pid %d\n", __func__, exec->pid);
@@ -689,8 +697,8 @@ int hyper_run_process(struct hyper_exec *exec)
689697
close(io.stdinfd);
690698
close(io.stdoutfd);
691699
close(io.stderrfd);
692-
close(pipe[0]);
693-
close(pipe[1]);
700+
close(pid_efd);
701+
close(process_inited_efd);
694702
return ret;
695703
close_tty:
696704
hyper_reset_event(&exec->stdinev);

src/hyper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static inline int hyper_create(char *hyper_path)
8787
return 0;
8888
}
8989

90-
int hyper_enter_sandbox(struct hyper_pod *pod, int pidpipe);
90+
int hyper_enter_sandbox(struct hyper_pod *pod, int pefd);
9191
void hyper_pod_destroyed(struct hyper_pod *pod, int failed);
9292
int hyper_ctl_append_msg(struct hyper_event *he, uint32_t type, uint8_t *data, uint32_t len);
9393

0 commit comments

Comments
 (0)