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);
533534out:
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
586589exit:
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);
588592exit_nosend:
589593 fflush(NULL);
590594 _exit(125);
@@ -629,9 +633,9 @@ int hyper_exec_cmd(struct hyper_pod *pod, char *json, int length)
629633
630634int 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;
695703close_tty:
696704 hyper_reset_event(&exec->stdinev);
0 commit comments