@@ -476,13 +476,20 @@ int hyper_watch_exec_pty(struct hyper_exec *exec, struct hyper_pod *pod)
476476}
477477
478478static int hyper_enter_container (struct hyper_pod * pod ,
479- struct hyper_container * c )
479+ struct hyper_exec * exec )
480480{
481481 int ipcns , utsns , mntns , ret ;
482+ struct hyper_container * c ;
482483 char path [512 ];
483484
484485 ret = ipcns = utsns = mntns = -1 ;
485486
487+ c = hyper_find_container (pod , exec -> id );
488+ if (c == NULL ) {
489+ fprintf (stderr , "can not find container %s\n" , exec -> id );
490+ return -1 ;
491+ }
492+
486493 sprintf (path , "/proc/%d/ns/uts" , pod -> init_pid );
487494 utsns = open (path , O_RDONLY | O_CLOEXEC );
488495 if (utsns < 0 ) {
@@ -519,6 +526,12 @@ static int hyper_enter_container(struct hyper_pod *pod,
519526 /* TODO: wait for container finishing setup root */
520527 chdir ("/" );
521528
529+ /* already in pidns & mntns of container, mount proc filesystem */
530+ if (exec -> init && mount ("proc" , "/proc" , "proc" , MS_NOSUID | MS_NODEV | MS_NOEXEC , NULL ) < 0 ) {
531+ perror ("fail to mount proc filesystem for container" );
532+ goto out ;
533+ }
534+
522535 ret = 0 ;
523536out :
524537 close (ipcns );
@@ -527,34 +540,12 @@ static int hyper_enter_container(struct hyper_pod *pod,
527540 return ret ;
528541}
529542
530- static int hyper_container_final_init (struct hyper_container * c )
531- {
532- if (c -> finalinit )
533- return 0 ;
534-
535- /* already in pidns & mntns of container, mount proc filesystem */
536- if (mount ("proc" , "/proc" , "proc" , MS_NOSUID | MS_NODEV | MS_NOEXEC , NULL ) < 0 ) {
537- perror ("fail to mount proc filesystem for container" );
538- return -1 ;
539- }
540-
541- c -> finalinit = 1 ;
542- return 0 ;
543- }
544-
545543static int hyper_do_exec_cmd (struct hyper_exec * exec , struct hyper_pod * pod , int pipe )
546544{
547545 int pid = -1 , ret = -1 ;
548546 char path [512 ];
549- struct hyper_container * c ;
550547 int pidns ;
551548
552- c = hyper_find_container (pod , exec -> id );
553- if (c == NULL ) {
554- fprintf (stderr , "can not find container %s\n" , exec -> id );
555- return -1 ;
556- }
557-
558549 sprintf (path , "/proc/%d/ns/pid" , pod -> init_pid );
559550 pidns = open (path , O_RDONLY | O_CLOEXEC );
560551 if (pidns < 0 ) {
@@ -580,16 +571,11 @@ static int hyper_do_exec_cmd(struct hyper_exec *exec, struct hyper_pod *pod, int
580571 goto out ;
581572 }
582573
583- if (hyper_enter_container (pod , c ) < 0 ) {
574+ if (hyper_enter_container (pod , exec ) < 0 ) {
584575 fprintf (stderr , "enter container ns failed\n" );
585576 goto exit ;
586577 }
587578
588- if (hyper_container_final_init (c )) {
589- fprintf (stderr , "final container intialization failed\n" );
590- goto exit ;
591- }
592-
593579 // set early env. the container env config can overwrite it
594580 setenv ("HOME" , "/root" , 1 );
595581 setenv ("HOSTNAME" , pod -> hostname , 1 );
0 commit comments