8
8
#include <sys/ioctl.h>
9
9
#include <sys/wait.h>
10
10
#include <sys/socket.h>
11
+ #include <sys/eventfd.h>
11
12
#include <dirent.h>
12
13
#include <sched.h>
13
14
#include <errno.h>
@@ -494,11 +495,11 @@ static int hyper_setup_stdio_events(struct hyper_exec *exec, struct stdio_config
494
495
return 0 ;
495
496
}
496
497
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 )
498
499
{
499
500
struct hyper_container * c ;
500
501
501
- if (hyper_enter_sandbox (exec -> pod , pipe ) < 0 ) {
502
+ if (hyper_enter_sandbox (exec -> pod , pid_efd ) < 0 ) {
502
503
perror ("enter pidns of pod init failed" );
503
504
goto out ;
504
505
}
@@ -529,14 +530,15 @@ static int hyper_do_exec_cmd(struct hyper_exec *exec, int pipe, struct stdio_con
529
530
else
530
531
unsetenv ("TERM" );
531
532
532
- hyper_exec_process (exec , pipe , io );
533
+ hyper_exec_process (exec , process_inited_efd , io );
533
534
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 );
535
537
_exit (125 );
536
538
}
537
539
538
540
// 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 )
540
542
{
541
543
bool defaultPath = false;
542
544
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
564
566
setsid ();
565
567
566
568
//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 );
568
571
569
572
if (hyper_install_process_stdio (exec , io ) < 0 ) {
570
573
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
584
587
}
585
588
586
589
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 );
588
592
exit_nosend :
589
593
fflush (NULL );
590
594
_exit (125 );
@@ -629,9 +633,9 @@ int hyper_exec_cmd(struct hyper_pod *pod, char *json, int length)
629
633
630
634
int hyper_run_process (struct hyper_exec * exec )
631
635
{
632
- int pipe [ 2 ] = { -1 , -1 } ;
636
+ int pid_efd = -1 , process_inited_efd = -1 ;
633
637
int pid , ret = -1 ;
634
- uint32_t cpid , type ;
638
+ int64_t cpid ;
635
639
struct stdio_config io = {-1 , -1 ,-1 , -1 ,-1 , -1 };
636
640
637
641
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)
645
649
goto out ;
646
650
}
647
651
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" );
650
657
goto close_tty ;
651
658
}
652
659
@@ -656,15 +663,17 @@ int hyper_run_process(struct hyper_exec *exec)
656
663
goto close_tty ;
657
664
} else if (pid == 0 ) {
658
665
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 );
661
669
_exit (125 );
662
670
}
663
- hyper_do_exec_cmd (exec , pipe [ 1 ] , & io );
671
+ hyper_do_exec_cmd (exec , pid_efd , process_inited_efd , & io );
664
672
}
665
673
fprintf (stdout , "prerequisite process pid %d\n" , pid );
666
674
667
- if (hyper_get_type (pipe [0 ], & cpid ) < 0 || (int )cpid < 0 ) {
675
+ cpid = hyper_eventfd_recv (pid_efd );
676
+ if (cpid < 0 ) {
668
677
fprintf (stderr , "run process failed\n" );
669
678
goto close_tty ;
670
679
}
@@ -674,13 +683,12 @@ int hyper_run_process(struct hyper_exec *exec)
674
683
goto close_tty ;
675
684
}
676
685
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 );
679
688
goto close_tty ;
680
689
}
681
690
682
- //two message may come back in reversed sequence
683
- exec -> pid = (type == 0 ) ? cpid : type ;
691
+ exec -> pid = cpid ;
684
692
list_add_tail (& exec -> list , & exec -> pod -> exec_head );
685
693
exec -> ref ++ ;
686
694
fprintf (stdout , "%s process pid %d\n" , __func__ , exec -> pid );
@@ -689,8 +697,8 @@ int hyper_run_process(struct hyper_exec *exec)
689
697
close (io .stdinfd );
690
698
close (io .stdoutfd );
691
699
close (io .stderrfd );
692
- close (pipe [ 0 ] );
693
- close (pipe [ 1 ] );
700
+ close (pid_efd );
701
+ close (process_inited_efd );
694
702
return ret ;
695
703
close_tty :
696
704
hyper_reset_event (& exec -> stdinev );
0 commit comments