mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-11 06:03:57 +08:00
7a0302f0d7
runc init is special. For one thing, it needs to do a few things before main(), so we have func init() that checks if we're init and does that. What happens next is main() is called, which does some options parsing, figures out it needs to call initCommand.Action and so it does. Now, main() is entirely unnecessary -- we can do everything right from init(). Hopefully the change makes things slightly less complicated. From a user's perspective, the only change is runc help no longer lists 'runc init` (which I think it also good). Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
52 lines
1.5 KiB
Go
52 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"runtime"
|
|
"strconv"
|
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
|
"github.com/opencontainers/runc/libcontainer/logs"
|
|
_ "github.com/opencontainers/runc/libcontainer/nsenter"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func init() {
|
|
if len(os.Args) > 1 && os.Args[1] == "init" {
|
|
// This is the golang entry point for runc init, executed
|
|
// before main() but after libcontainer/nsenter's nsexec().
|
|
runtime.GOMAXPROCS(1)
|
|
runtime.LockOSThread()
|
|
|
|
level := os.Getenv("_LIBCONTAINER_LOGLEVEL")
|
|
logLevel, err := logrus.ParseLevel(level)
|
|
if err != nil {
|
|
panic(fmt.Sprintf("libcontainer: failed to parse log level: %q: %v", level, err))
|
|
}
|
|
|
|
logPipeFdStr := os.Getenv("_LIBCONTAINER_LOGPIPE")
|
|
logPipeFd, err := strconv.Atoi(logPipeFdStr)
|
|
if err != nil {
|
|
panic(fmt.Sprintf("libcontainer: failed to convert environment variable _LIBCONTAINER_LOGPIPE=%s to int: %s", logPipeFdStr, err))
|
|
}
|
|
err = logs.ConfigureLogging(logs.Config{
|
|
LogPipeFd: logPipeFd,
|
|
LogFormat: "json",
|
|
LogLevel: logLevel,
|
|
})
|
|
if err != nil {
|
|
panic(fmt.Sprintf("libcontainer: failed to configure logging: %v", err))
|
|
}
|
|
logrus.Debug("child process in init()")
|
|
|
|
factory, _ := libcontainer.New("")
|
|
if err := factory.StartInitialization(); err != nil {
|
|
// as the error is sent back to the parent there is no need to log
|
|
// or write it to stderr because the parent process will handle this
|
|
os.Exit(1)
|
|
}
|
|
panic("libcontainer: container init failed to exec")
|
|
}
|
|
}
|