diff --git a/rune/libcontainer/container_linux.go b/rune/libcontainer/container_linux.go index 1a6f894c0fffac3525757e1be6960c7bb55f3563..63ba4b1a7599729814f58e78c416376b13f43d4f 100644 --- a/rune/libcontainer/container_linux.go +++ b/rune/libcontainer/container_linux.go @@ -494,7 +494,7 @@ func (c *linuxContainer) newParentProcess(p *Process) (parentProcess, error) { return c.newInitProcess(p, cmd, messageSockPair, logFilePair) } -func (c *linuxContainer) commandTemplate(p *Process, childInitPipe *os.File, childLogPipe *os.File, agentPipe *os.File, detached int) *exec.Cmd { +func (c *linuxContainer) commandTemplate(p *Process, childInitPipe *os.File, childLogPipe *os.File, agentPipe *os.File, detached bool) *exec.Cmd { cmd := exec.Command(c.initPath, c.initArgs[1:]...) cmd.Args[0] = c.initArgs[0] cmd.Stdin = p.Stdin @@ -538,8 +538,13 @@ func (c *linuxContainer) commandTemplate(p *Process, childInitPipe *os.File, chi cmd.Env = append(cmd.Env, "_LIBCONTAINER_PAL_ROOTFS=" + string(c.config.Rootfs)) } - cmd.Env = append(cmd.Env, - fmt.Sprintf("_LIBCONTAINER_DETACHED=%d", detached)) + if detached { + cmd.Env = append(cmd.Env, + fmt.Sprintf("_LIBCONTAINER_DETACHED=%d", 1)) + } else { + cmd.Env = append(cmd.Env, + fmt.Sprintf("_LIBCONTAINER_DETACHED=%d", 0)) + } } // NOTE: when running a container with no PID namespace and the parent process spawning the container is diff --git a/rune/libcontainer/factory_linux.go b/rune/libcontainer/factory_linux.go index ada3c480a5602535588b9ddc1df345306dc8415a..d3909ff1fe1db3ddacbc15ef8253af5197478da9 100644 --- a/rune/libcontainer/factory_linux.go +++ b/rune/libcontainer/factory_linux.go @@ -340,6 +340,7 @@ func (l *LinuxFactory) StartInitialization() (err error) { consoleSocket *os.File logPipe *os.File agentPipe *os.File + detached = false envInitPipe = os.Getenv("_LIBCONTAINER_INITPIPE") envFifoFd = os.Getenv("_LIBCONTAINER_FIFOFD") envConsole = os.Getenv("_LIBCONTAINER_CONSOLE") @@ -396,6 +397,16 @@ func (l *LinuxFactory) StartInitialization() (err error) { defer agentPipe.Close() } + if envDetached != "" { + tmpDetached, err := strconv.Atoi(envDetached) + if err != nil { + return fmt.Errorf("unable to convert _LIBCONTAINER_DETACHED=%s to int: %s", envDetached, err) + } + if tmpDetached != 0 { + detached = true + } + } + // clear the current process's environment to clean any libcontainer // specific env vars. os.Clearenv() @@ -418,7 +429,7 @@ func (l *LinuxFactory) StartInitialization() (err error) { } }() - i, err := newContainerInit(it, pipe, consoleSocket, fifofd, logPipe, envLogLevel, agentPipe, envDetached) + i, err := newContainerInit(it, pipe, consoleSocket, fifofd, logPipe, envLogLevel, agentPipe, detached) if err != nil { return err } diff --git a/rune/libcontainer/init_linux.go b/rune/libcontainer/init_linux.go index 5d1dcb0f471c326ec82b705f8db7c2ac2846ac2d..d607ebea318ca86ce81edf7ef8eb5c9471673ef3 100644 --- a/rune/libcontainer/init_linux.go +++ b/rune/libcontainer/init_linux.go @@ -73,7 +73,7 @@ type initer interface { Init() error } -func newContainerInit(t initType, pipe *os.File, consoleSocket *os.File, fifoFd int, logPipe *os.File, logLevel string, agentPipe *os.File, detached string) (initer, error) { +func newContainerInit(t initType, pipe *os.File, consoleSocket *os.File, fifoFd int, logPipe *os.File, logLevel string, agentPipe *os.File, detached bool) (initer, error) { var config *initConfig if err := json.NewDecoder(pipe).Decode(&config); err != nil { return nil, err diff --git a/rune/libcontainer/process.go b/rune/libcontainer/process.go index 6a0d512fed5b0e563e9c3111071c0ee00423d4c5..f6f208c7d114ab9439197e00ede0a95212bfbfa8 100644 --- a/rune/libcontainer/process.go +++ b/rune/libcontainer/process.go @@ -82,7 +82,7 @@ type Process struct { // Provide agent service hosted by main runelet for child runelet. AgentPipe *os.File - Detached int + Detached bool } // Wait waits for the process to exit. diff --git a/rune/libcontainer/setns_init_linux.go b/rune/libcontainer/setns_init_linux.go index 4c7efb13e84ecb62b021b2f46155fc5f88d07d7a..f898ed8b519aeaefae05c310ad0c6290d36a8576 100644 --- a/rune/libcontainer/setns_init_linux.go +++ b/rune/libcontainer/setns_init_linux.go @@ -28,7 +28,7 @@ type linuxSetnsInit struct { logPipe *os.File logLevel string agentPipe *os.File - detached string + detached bool } func (l *linuxSetnsInit) getSessionRingName() string { diff --git a/rune/libcontainer/standard_init_linux.go b/rune/libcontainer/standard_init_linux.go index 07534b755e595de3fde63afea89438f77b6b79ec..1af231f12b311e215cea21485627b2667753c0f9 100644 --- a/rune/libcontainer/standard_init_linux.go +++ b/rune/libcontainer/standard_init_linux.go @@ -31,7 +31,7 @@ type linuxStandardInit struct { logPipe *os.File logLevel string agentPipe *os.File - detached string + detached bool } func (l *linuxStandardInit) getSessionRingParams() (string, uint32, uint32) { diff --git a/rune/libenclave/bootstrap.go b/rune/libenclave/bootstrap.go index 6629e293eb4cd467f829379484c065a645bdf05d..f5b92a6c4890576734849efa3e90d29d5445b411 100644 --- a/rune/libenclave/bootstrap.go +++ b/rune/libenclave/bootstrap.go @@ -11,7 +11,7 @@ type enclaveRuntimeEnv struct { logLevel string fifoFd int agentPipe *os.File - detached string + detached bool } var enclaveEnv enclaveRuntimeEnv @@ -25,7 +25,7 @@ func GetEnclaveRunetimeEnv() *enclaveRuntimeEnv { // environment variable must be staged and then recovered after re-exec. This // process is so called as libenclave bootstrapping, and the resulting process // is so called as runelet. -func StartBootstrap(initPipe *os.File, logPipe *os.File, logLevel string, fifoFd int, agentPipe *os.File, detached string) (err error) { +func StartBootstrap(initPipe *os.File, logPipe *os.File, logLevel string, fifoFd int, agentPipe *os.File, detached bool) (err error) { logrus.Debug("bootstrapping libenclave ...") enclaveEnv.initPipe = initPipe diff --git a/rune/libenclave/runelet.go b/rune/libenclave/runelet.go index 71f14103a479f62179f780431ac39ba7d565dced..86aa9d6ed656b21be1b3f7ce4ed53527eaa749a8 100644 --- a/rune/libenclave/runelet.go +++ b/rune/libenclave/runelet.go @@ -13,7 +13,6 @@ import ( "io/ioutil" "os" "os/signal" - "strconv" "strings" "syscall" ) @@ -71,8 +70,7 @@ func StartInitialization() (exitCode int32, err error) { } // If runelet run as detach mode, close logrus before initpipe closed. - detach, err := strconv.Atoi(env.detached) - if detach != 0 { + if env.detached { logrus.SetOutput(ioutil.Discard) } diff --git a/rune/utils_linux.go b/rune/utils_linux.go index 95987297fc0543d09521dc4a8bd93a0e7e1c5a06..54021889b21b58e11672c9bc21b9237ae6503621 100644 --- a/rune/utils_linux.go +++ b/rune/utils_linux.go @@ -307,7 +307,7 @@ func (r *runner) run(config *specs.Process) (int, error) { ) if detach { - process.Detached = 1 + process.Detached = true } // Setting up IO is a two stage process. We need to modify process to deal