From df52d638541d8199cd79b4be42647976447fecbc Mon Sep 17 00:00:00 2001 From: Andrey Vagin Date: Wed, 28 Jan 2015 12:11:19 +0300 Subject: [PATCH] namespaces: send config, network state and other arguments in one packet Signed-off-by: Andrey Vagin --- namespaces/exec.go | 37 ++++++++++--------------------------- namespaces/init.go | 27 ++++++++++----------------- 2 files changed, 20 insertions(+), 44 deletions(-) diff --git a/namespaces/exec.go b/namespaces/exec.go index 0c0f6cf6c..1c157e3a2 100644 --- a/namespaces/exec.go +++ b/namespaces/exec.go @@ -68,18 +68,8 @@ func executeSetupCmd(args []string, ppid int, container *configs.Config, process return terr } - encoder := json.NewEncoder(parent) - - if err := encoder.Encode(container); err != nil { - return terminate(err) - } - - if err := encoder.Encode(process); err != nil { - return terminate(err) - } - // send the state to the container's init process then shutdown writes for the parent - if err := encoder.Encode(networkState); err != nil { + if err := json.NewEncoder(parent).Encode(process); err != nil { return terminate(err) } @@ -158,21 +148,6 @@ func Exec(args []string, env []string, console string, command *exec.Cmd, contai return terr } - encoder := json.NewEncoder(parent) - - if err := encoder.Encode(container); err != nil { - return terminate(err) - } - - process := processArgs{ - Env: append(env[0:], container.Env...), - Args: args, - ConsolePath: console, - } - if err := encoder.Encode(process); err != nil { - return terminate(err) - } - started, err := system.GetProcessStartTime(command.Process.Pid) if err != nil { return terminate(err) @@ -195,6 +170,14 @@ func Exec(args []string, env []string, console string, command *exec.Cmd, contai return terminate(err) } + process := processArgs{ + Env: append(env[0:], container.Env...), + Args: args, + ConsolePath: console, + Config: container, + NetworkState: &networkState, + } + // Start the setup process to setup the init process if container.Namespaces.Contains(configs.NEWUSER) { if err = executeSetupCmd(command.Args, command.Process.Pid, container, &process, &networkState); err != nil { @@ -203,7 +186,7 @@ func Exec(args []string, env []string, console string, command *exec.Cmd, contai } // send the state to the container's init process then shutdown writes for the parent - if err := encoder.Encode(networkState); err != nil { + if err := json.NewEncoder(parent).Encode(process); err != nil { return terminate(err) } // shutdown writes for the parent side of the pipe diff --git a/namespaces/init.go b/namespaces/init.go index 01f721141..5af898e18 100644 --- a/namespaces/init.go +++ b/namespaces/init.go @@ -26,9 +26,11 @@ import ( // Process is used for transferring parameters from Exec() to Init() type processArgs struct { - Args []string `json:"args,omitempty"` - Env []string `json:"environment,omitempty"` - ConsolePath string `json:"console_path,omitempty"` + Args []string `json:"args,omitempty"` + Env []string `json:"environment,omitempty"` + ConsolePath string `json:"console_path,omitempty"` + Config *configs.Config `json:"config,omitempty"` + NetworkState *network.NetworkState `json:"network_state,omitempty"` } // TODO(vishh): This is part of the libcontainer API and it does much more than just namespaces related work. @@ -55,29 +57,20 @@ func Init(pipe *os.File, setupUserns bool) (err error) { pipe.Close() }() - decoder := json.NewDecoder(pipe) - - var container *configs.Config - if err := decoder.Decode(&container); err != nil { - return err - } - - var process *processArgs - if err := decoder.Decode(&process); err != nil { - return err - } - uncleanRootfs, err := os.Getwd() if err != nil { return err } + var process *processArgs // We always read this as it is a way to sync with the parent as well - var networkState *network.NetworkState - if err := decoder.Decode(&networkState); err != nil { + if err := json.NewDecoder(pipe).Decode(&process); err != nil { return err } + container := process.Config + networkState := process.NetworkState + if setupUserns { err = SetupContainer(container, networkState, process.ConsolePath) if err == nil {