mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-11 14:13:58 +08:00
9a599f62fb
Add support for children processes logging (including nsexec). A pipe is used to send logs from children to parent in JSON. The JSON format used is the same used by logrus JSON formatted, i.e. children process can use standard logrus APIs. Signed-off-by: Marco Vedovati <mvedovati@suse.com>
47 lines
1.2 KiB
Go
47 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"runtime"
|
|
"strconv"
|
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
|
_ "github.com/opencontainers/runc/libcontainer/nsenter"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/urfave/cli"
|
|
)
|
|
|
|
func init() {
|
|
if len(os.Args) > 1 && os.Args[1] == "init" {
|
|
runtime.GOMAXPROCS(1)
|
|
runtime.LockOSThread()
|
|
|
|
// in child process, we need to retrieve the log pipe
|
|
envLogPipe := os.Getenv("_LIBCONTAINER_LOGPIPE")
|
|
logPipeFd, err := strconv.Atoi(envLogPipe)
|
|
|
|
if err != nil {
|
|
return
|
|
}
|
|
logPipe := os.NewFile(uintptr(logPipeFd), "logpipe")
|
|
logrus.SetOutput(logPipe)
|
|
logrus.SetFormatter(&logrus.JSONFormatter{})
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
logrus.Debug("child process in init()")
|
|
}
|
|
}
|
|
|
|
var initCommand = cli.Command{
|
|
Name: "init",
|
|
Usage: `initialize the namespaces and launch the process (do not call it outside of runc)`,
|
|
Action: func(context *cli.Context) error {
|
|
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")
|
|
},
|
|
}
|