|
@@ -17,7 +17,7 @@ import (
|
|
|
|
|
|
// TODO(vishh): This is part of the libcontainer API and it does much more than just namespaces related work.
|
|
// TODO(vishh): This is part of the libcontainer API and it does much more than just namespaces related work.
|
|
// Move this to libcontainer package.
|
|
// Move this to libcontainer package.
|
|
-// Exec performes setup outside of a namespace so that a container can be
|
|
|
|
|
|
+// Exec performs setup outside of a namespace so that a container can be
|
|
// executed. Exec is a high level function for working with container namespaces.
|
|
// executed. Exec is a high level function for working with container namespaces.
|
|
func Exec(container *libcontainer.Config, term Terminal, rootfs, dataPath string, args []string, createCommand CreateCommand, startCallback func()) (int, error) {
|
|
func Exec(container *libcontainer.Config, term Terminal, rootfs, dataPath string, args []string, createCommand CreateCommand, startCallback func()) (int, error) {
|
|
var (
|
|
var (
|
|
@@ -32,6 +32,7 @@ func Exec(container *libcontainer.Config, term Terminal, rootfs, dataPath string
|
|
if err != nil {
|
|
if err != nil {
|
|
return -1, err
|
|
return -1, err
|
|
}
|
|
}
|
|
|
|
+ defer syncPipe.Close()
|
|
|
|
|
|
if container.Tty {
|
|
if container.Tty {
|
|
master, console, err = system.CreateMasterAndConsole()
|
|
master, console, err = system.CreateMasterAndConsole()
|
|
@@ -52,16 +53,13 @@ func Exec(container *libcontainer.Config, term Terminal, rootfs, dataPath string
|
|
return -1, err
|
|
return -1, err
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // Now we passed the pipe to the child, close our side
|
|
|
|
+ syncPipe.CloseChild()
|
|
|
|
+
|
|
started, err := system.GetProcessStartTime(command.Process.Pid)
|
|
started, err := system.GetProcessStartTime(command.Process.Pid)
|
|
if err != nil {
|
|
if err != nil {
|
|
return -1, err
|
|
return -1, err
|
|
}
|
|
}
|
|
- if err := WritePid(dataPath, command.Process.Pid, started); err != nil {
|
|
|
|
- command.Process.Kill()
|
|
|
|
- command.Wait()
|
|
|
|
- return -1, err
|
|
|
|
- }
|
|
|
|
- defer DeletePid(dataPath)
|
|
|
|
|
|
|
|
// Do this before syncing with child so that no children
|
|
// Do this before syncing with child so that no children
|
|
// can escape the cgroup
|
|
// can escape the cgroup
|
|
@@ -75,14 +73,32 @@ func Exec(container *libcontainer.Config, term Terminal, rootfs, dataPath string
|
|
defer cleaner.Cleanup()
|
|
defer cleaner.Cleanup()
|
|
}
|
|
}
|
|
|
|
|
|
- if err := InitializeNetworking(container, command.Process.Pid, syncPipe); err != nil {
|
|
|
|
|
|
+ var networkState network.NetworkState
|
|
|
|
+ if err := InitializeNetworking(container, command.Process.Pid, syncPipe, &networkState); err != nil {
|
|
|
|
+ command.Process.Kill()
|
|
|
|
+ command.Wait()
|
|
|
|
+ return -1, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ state := &libcontainer.State{
|
|
|
|
+ InitPid: command.Process.Pid,
|
|
|
|
+ InitStartTime: started,
|
|
|
|
+ NetworkState: networkState,
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if err := libcontainer.SaveState(dataPath, state); err != nil {
|
|
command.Process.Kill()
|
|
command.Process.Kill()
|
|
command.Wait()
|
|
command.Wait()
|
|
return -1, err
|
|
return -1, err
|
|
}
|
|
}
|
|
|
|
+ defer libcontainer.DeleteState(dataPath)
|
|
|
|
|
|
// Sync with child
|
|
// Sync with child
|
|
- syncPipe.Close()
|
|
|
|
|
|
+ if err := syncPipe.ReadFromChild(); err != nil {
|
|
|
|
+ command.Process.Kill()
|
|
|
|
+ command.Wait()
|
|
|
|
+ return -1, err
|
|
|
|
+ }
|
|
|
|
|
|
if startCallback != nil {
|
|
if startCallback != nil {
|
|
startCallback()
|
|
startCallback()
|
|
@@ -101,10 +117,10 @@ func Exec(container *libcontainer.Config, term Terminal, rootfs, dataPath string
|
|
// args provided
|
|
// args provided
|
|
//
|
|
//
|
|
// console: the /dev/console to setup inside the container
|
|
// console: the /dev/console to setup inside the container
|
|
-// init: the progam executed inside the namespaces
|
|
|
|
|
|
+// init: the program executed inside the namespaces
|
|
// root: the path to the container json file and information
|
|
// root: the path to the container json file and information
|
|
-// pipe: sync pipe to syncronize the parent and child processes
|
|
|
|
-// args: the arguemnts to pass to the container to run as the user's program
|
|
|
|
|
|
+// pipe: sync pipe to synchronize the parent and child processes
|
|
|
|
+// args: the arguments to pass to the container to run as the user's program
|
|
func DefaultCreateCommand(container *libcontainer.Config, console, rootfs, dataPath, init string, pipe *os.File, args []string) *exec.Cmd {
|
|
func DefaultCreateCommand(container *libcontainer.Config, console, rootfs, dataPath, init string, pipe *os.File, args []string) *exec.Cmd {
|
|
// get our binary name from arg0 so we can always reexec ourself
|
|
// get our binary name from arg0 so we can always reexec ourself
|
|
env := []string{
|
|
env := []string{
|
|
@@ -135,7 +151,7 @@ func DefaultCreateCommand(container *libcontainer.Config, console, rootfs, dataP
|
|
return command
|
|
return command
|
|
}
|
|
}
|
|
|
|
|
|
-// SetupCgroups applies the cgroup restrictions to the process running in the contaienr based
|
|
|
|
|
|
+// SetupCgroups applies the cgroup restrictions to the process running in the container based
|
|
// on the container's configuration
|
|
// on the container's configuration
|
|
func SetupCgroups(container *libcontainer.Config, nspid int) (cgroups.ActiveCgroup, error) {
|
|
func SetupCgroups(container *libcontainer.Config, nspid int) (cgroups.ActiveCgroup, error) {
|
|
if container.Cgroups != nil {
|
|
if container.Cgroups != nil {
|
|
@@ -150,18 +166,17 @@ func SetupCgroups(container *libcontainer.Config, nspid int) (cgroups.ActiveCgro
|
|
|
|
|
|
// InitializeNetworking creates the container's network stack outside of the namespace and moves
|
|
// InitializeNetworking creates the container's network stack outside of the namespace and moves
|
|
// interfaces into the container's net namespaces if necessary
|
|
// interfaces into the container's net namespaces if necessary
|
|
-func InitializeNetworking(container *libcontainer.Config, nspid int, pipe *SyncPipe) error {
|
|
|
|
- context := map[string]string{}
|
|
|
|
|
|
+func InitializeNetworking(container *libcontainer.Config, nspid int, pipe *SyncPipe, networkState *network.NetworkState) error {
|
|
for _, config := range container.Networks {
|
|
for _, config := range container.Networks {
|
|
strategy, err := network.GetStrategy(config.Type)
|
|
strategy, err := network.GetStrategy(config.Type)
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
- if err := strategy.Create((*network.Network)(config), nspid, context); err != nil {
|
|
|
|
|
|
+ if err := strategy.Create((*network.Network)(config), nspid, networkState); err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- return pipe.SendToChild(context)
|
|
|
|
|
|
+ return pipe.SendToChild(networkState)
|
|
}
|
|
}
|
|
|
|
|
|
// GetNamespaceFlags parses the container's Namespaces options to set the correct
|
|
// GetNamespaceFlags parses the container's Namespaces options to set the correct
|