From a59d63c5d3fd7fad7043b42663f5d83e9fa9bc85 Mon Sep 17 00:00:00 2001 From: Yang Hongyang Date: Wed, 6 Jul 2016 09:58:30 -0400 Subject: [PATCH] Fix and refactor init args 1. According to docs of Cmd.Path and Cmd.Args from package "os/exec": Path is the path of the command to run. Args holds command line arguments, including the command as Args[0]. We have mixed usage of args. In InitPath(), InitArgs only take arguments, in InitArgs(), InitArgs including the command as Args[0]. This is confusing. 2. InitArgs() already have the ability to configure a LinuxFactory with the provided absolute path to the init binary and arguements as InitPath() does. 3. exec.Command() will take care of serching executable path. 4. The default "/proc/self/exe" instead of os.Args[0] is passed to InitArgs in order to allow relative path for the runC binary. Signed-off-by: Yang Hongyang --- libcontainer/container_linux.go | 6 +----- libcontainer/factory_linux.go | 33 ++------------------------------- 2 files changed, 3 insertions(+), 36 deletions(-) diff --git a/libcontainer/container_linux.go b/libcontainer/container_linux.go index a6bc8cd9..0d407b6b 100644 --- a/libcontainer/container_linux.go +++ b/libcontainer/container_linux.go @@ -35,7 +35,6 @@ type linuxContainer struct { root string config *configs.Config cgroupManager cgroups.Manager - initPath string initArgs []string initProcess parentProcess initProcessStartTime string @@ -308,10 +307,7 @@ func (c *linuxContainer) newParentProcess(p *Process, doInit bool) (parentProces } func (c *linuxContainer) commandTemplate(p *Process, childPipe, rootDir *os.File) (*exec.Cmd, error) { - cmd := &exec.Cmd{ - Path: c.initPath, - Args: c.initArgs, - } + cmd := exec.Command(c.initArgs[0], c.initArgs[1:]...) cmd.Stdin = p.Stdin cmd.Stdout = p.Stdout cmd.Stderr = p.Stderr diff --git a/libcontainer/factory_linux.go b/libcontainer/factory_linux.go index 7be538bc..00ea26e5 100644 --- a/libcontainer/factory_linux.go +++ b/libcontainer/factory_linux.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "os" - "os/exec" "path/filepath" "regexp" "runtime/debug" @@ -33,32 +32,9 @@ var ( ) // InitArgs returns an options func to configure a LinuxFactory with the -// provided init arguments. +// provided init binary path and arguments. func InitArgs(args ...string) func(*LinuxFactory) error { return func(l *LinuxFactory) error { - name := args[0] - if filepath.Base(name) == name { - if lp, err := exec.LookPath(name); err == nil { - name = lp - } - } else { - abs, err := filepath.Abs(name) - if err != nil { - return err - } - name = abs - } - l.InitPath = "/proc/self/exe" - l.InitArgs = append([]string{name}, args[1:]...) - return nil - } -} - -// InitPath returns an options func to configure a LinuxFactory with the -// provided absolute path to the init binary and arguements. -func InitPath(path string, args ...string) func(*LinuxFactory) error { - return func(l *LinuxFactory) error { - l.InitPath = path l.InitArgs = args return nil } @@ -113,10 +89,10 @@ func New(root string, options ...func(*LinuxFactory) error) (Factory, error) { } l := &LinuxFactory{ Root: root, + InitArgs: []string{"/proc/self/exe", "init"}, Validator: validate.New(), CriuPath: "criu", } - InitArgs(os.Args[0], "init")(l) Cgroupfs(l) for _, opt := range options { if err := opt(l); err != nil { @@ -131,9 +107,6 @@ type LinuxFactory struct { // Root directory for the factory to store state. Root string - // InitPath is the absolute path to the init binary. - InitPath string - // InitArgs are arguments for calling the init responsibilities for spawning // a container. InitArgs []string @@ -193,7 +166,6 @@ func (l *LinuxFactory) Create(id string, config *configs.Config) (Container, err id: id, root: containerRoot, config: config, - initPath: l.InitPath, initArgs: l.InitArgs, criuPath: l.CriuPath, cgroupManager: l.NewCgroupsManager(config.Cgroups, nil), @@ -221,7 +193,6 @@ func (l *LinuxFactory) Load(id string) (Container, error) { initProcessStartTime: state.InitProcessStartTime, id: id, config: &state.Config, - initPath: l.InitPath, initArgs: l.InitArgs, criuPath: l.CriuPath, cgroupManager: l.NewCgroupsManager(state.Config.Cgroups, state.CgroupPaths),