bootstrap.go 1.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
package libenclave // import "github.com/opencontainers/runc/libenclave"

import (
	"github.com/sirupsen/logrus"
	"os"
)

// `rune init` needs to execute self (/proc/self/exe) in container environment
// as `runc init` executes entrypoint. Thus, some internal states in form of
// 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.
13
func StartBootstrap(initPipe *os.File, logPipe *os.File, logLevel string, fifoFd int, agentPipe *os.File, detached string) (err error) {
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
	logrus.Debug("bootstrapping libenclave ...")

	if err = stageFd("_LIBENCLAVE_INITPIPE", initPipe); err != nil {
		return err
	}
	defer func() {
		if err != nil {
			unstageFd("_LIBENCLAVE_INITPIPE")
		}
	}()

	if fifoFd != -1 {
		if err = stageFd("_LIBENCLAVE_FIFOFD", fifoFd); err != nil {
			return err
		}
		defer func() {
			if err != nil {
				unstageFd("_LIBENCLAVE_FIFOFD")
			}
		}()
	}

	if err = stageFd("_LIBENCLAVE_LOGPIPE", logPipe); err != nil {
		return err
	}
	defer func() {
		if err != nil {
			unstageFd("_LIBENCLAVE_LOGPIPE")
		}
	}()

	if err = os.Setenv("_LIBENCLAVE_LOGLEVEL", logLevel); err != nil {
		return err
	}
	defer func() {
		if err != nil {
			os.Unsetenv("_LIBENCLAVE_LOGLEVEL")
		}
	}()

	if err = stageFd("_LIBENCLAVE_AGENTPIPE", agentPipe); err != nil {
		return err
	}
	defer func() {
		if err != nil {
			unstageFd("_LIBENCLAVE_AGENTPIPE")
		}
	}()

63 64
	os.Setenv("_LIBENCLAVE_DETACHED", detached)
	
65 66
	return nil
}