@@ -491,107 +491,40 @@ static int hyper_watch_exec_pty(struct hyper_exec *exec, struct hyper_pod *pod)
491491 return 0 ;
492492}
493493
494- static int hyper_enter_container (struct hyper_pod * pod ,
495- struct hyper_exec * exec )
494+ static int hyper_do_exec_cmd (struct hyper_exec * exec , struct hyper_pod * pod , int pipe )
496495{
497- int ipcns , utsns , mntns , ret ;
498496 struct hyper_container * c ;
499- char path [512 ];
500497
501- ret = ipcns = utsns = mntns = -1 ;
498+ if (hyper_enter_sandbox (pod , pipe ) < 0 ) {
499+ perror ("enter pidns of pod init failed" );
500+ hyper_send_type (pipe , -1 );
501+ goto out ;
502+ }
502503
503504 c = hyper_find_container (pod , exec -> id );
504505 if (c == NULL ) {
505506 fprintf (stderr , "can not find container %s\n" , exec -> id );
506- return -1 ;
507- }
508-
509- sprintf (path , "/proc/%d/ns/uts" , pod -> init_pid );
510- utsns = open (path , O_RDONLY | O_CLOEXEC );
511- if (utsns < 0 ) {
512- perror ("fail to open utsns of pod init" );
513- goto out ;
514- }
515-
516- sprintf (path , "/proc/%d/ns/ipc" , pod -> init_pid );
517- ipcns = open (path , O_RDONLY | O_CLOEXEC );
518- if (ipcns < 0 ) {
519- perror ("fail to open ipcns of pod init" );
520- goto out ;
521- }
522-
523- mntns = c -> ns ;
524- if (mntns < 0 ) {
525- perror ("fail to open mntns of pod init" );
526507 goto out ;
527508 }
528509
529- if (setns (utsns , CLONE_NEWUTS ) < 0 ||
530- setns (ipcns , CLONE_NEWIPC ) < 0 ||
531- setns (mntns , CLONE_NEWNS ) < 0 ) {
510+ if (setns (c -> ns , CLONE_NEWNS ) < 0 ) {
532511 perror ("fail to enter container ns" );
533512 goto out ;
534513 }
514+ chdir ("/" );
535515
536516 /* TODO: merge container env to exec env in hyperd */
537517 if (hyper_setup_env (c -> exec .envs , c -> exec .envs_num ) < 0 ) {
538518 fprintf (stderr , "setup container envs for exec failed\n" );
539519 goto out ;
540520 }
541521
542- /* TODO: wait for container finishing setup root */
543- chdir ("/" );
544-
545522 /* already in pidns & mntns of container, mount proc filesystem */
546523 if (exec -> init && mount ("proc" , "/proc" , "proc" , MS_NOSUID | MS_NODEV | MS_NOEXEC , NULL ) < 0 ) {
547524 perror ("fail to mount proc filesystem for container" );
548525 goto out ;
549526 }
550527
551- ret = 0 ;
552- out :
553- close (ipcns );
554- close (utsns );
555-
556- return ret ;
557- }
558-
559- static int hyper_do_exec_cmd (struct hyper_exec * exec , struct hyper_pod * pod , int pipe )
560- {
561- int pid = -1 , ret = -1 ;
562- char path [512 ];
563- int pidns ;
564-
565- sprintf (path , "/proc/%d/ns/pid" , pod -> init_pid );
566- pidns = open (path , O_RDONLY | O_CLOEXEC );
567- if (pidns < 0 ) {
568- perror ("fail to open pidns of pod init" );
569- goto out ;
570- }
571-
572- /* enter pidns of pod init, so the children of this process will run in
573- * pidns of pod init, see man 2 setns */
574- if (setns (pidns , CLONE_NEWPID ) < 0 ) {
575- perror ("enter pidns of pod init failed" );
576- goto out ;
577- }
578- close (pidns );
579-
580- pid = fork ();
581- if (pid < 0 ) {
582- perror ("fail to fork" );
583- goto out ;
584- } else if (pid > 0 ) {
585- fprintf (stdout , "create exec cmd %s pid %d,ref %d\n" , exec -> argv [0 ], pid , exec -> ref );
586- ret = 0 ;
587- goto out ;
588- }
589-
590- if (hyper_enter_container (pod , exec ) < 0 ) {
591- fprintf (stderr , "enter container ns failed\n" );
592- goto exit ;
593- }
594-
595528 // set early env. the container env config can overwrite it
596529 setenv ("HOME" , "/root" , 1 );
597530 setenv ("HOSTNAME" , pod -> hostname , 1 );
@@ -602,11 +535,8 @@ static int hyper_do_exec_cmd(struct hyper_exec *exec, struct hyper_pod *pod, int
602535
603536 hyper_exec_process (exec );
604537
605- exit :
606- _exit (125 );
607538out :
608- hyper_send_type (pipe , pid );
609- _exit (ret );
539+ _exit (125 );
610540}
611541
612542// do the exec, no return
@@ -627,7 +557,7 @@ static void hyper_exec_process(struct hyper_exec *exec)
627557 goto exit ;
628558 }
629559
630- // set the container env
560+ // set the process env
631561 if (hyper_setup_env (exec -> envs , exec -> envs_num ) < 0 ) {
632562 fprintf (stderr , "setup env failed\n" );
633563 goto exit ;
0 commit comments