2014-02-26 07:19:13 +08:00
|
|
|
// +build linux
|
|
|
|
|
2014-06-05 06:47:57 +08:00
|
|
|
package namespaces
|
2014-02-20 11:53:25 +08:00
|
|
|
|
|
|
|
import (
|
2014-11-04 10:18:55 +08:00
|
|
|
"encoding/json"
|
2014-08-13 02:43:12 +08:00
|
|
|
"fmt"
|
2014-12-23 06:06:22 +08:00
|
|
|
"io/ioutil"
|
2014-08-05 06:05:50 +08:00
|
|
|
"os"
|
2014-08-07 09:44:41 +08:00
|
|
|
"os/exec"
|
2014-08-05 06:05:50 +08:00
|
|
|
|
2014-10-14 13:53:44 +08:00
|
|
|
"github.com/docker/libcontainer/apparmor"
|
2014-08-13 14:18:55 +08:00
|
|
|
"github.com/docker/libcontainer/cgroups"
|
2014-12-17 17:12:23 +08:00
|
|
|
"github.com/docker/libcontainer/configs"
|
2014-07-17 07:52:15 +08:00
|
|
|
"github.com/docker/libcontainer/label"
|
|
|
|
"github.com/docker/libcontainer/system"
|
2014-02-20 11:53:25 +08:00
|
|
|
)
|
|
|
|
|
2014-12-23 06:06:22 +08:00
|
|
|
// ExecIn reexec's cmd with _LIBCONTAINER_INITPID=PID so that it is able to run the
|
2014-08-09 02:11:23 +08:00
|
|
|
// setns code in a single threaded environment joining the existing containers' namespaces.
|
2014-12-23 06:06:22 +08:00
|
|
|
func ExecIn(args []string, env []string, cmd *exec.Cmd, container *configs.Config, state *configs.State) (int, error) {
|
|
|
|
var err error
|
2014-08-07 09:44:41 +08:00
|
|
|
|
2014-11-04 10:18:55 +08:00
|
|
|
parent, child, err := newInitPipe()
|
2014-07-03 06:11:15 +08:00
|
|
|
if err != nil {
|
2014-08-07 09:44:41 +08:00
|
|
|
return -1, err
|
2014-07-03 06:11:15 +08:00
|
|
|
}
|
2014-11-04 10:18:55 +08:00
|
|
|
defer parent.Close()
|
2014-07-03 06:11:15 +08:00
|
|
|
|
2014-11-04 10:18:55 +08:00
|
|
|
cmd.ExtraFiles = []*os.File{child}
|
2014-12-23 06:06:22 +08:00
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("_LIBCONTAINER_INITPID=%d", state.InitPid))
|
2014-08-07 09:44:41 +08:00
|
|
|
|
|
|
|
if err := cmd.Start(); err != nil {
|
2014-11-04 10:18:55 +08:00
|
|
|
child.Close()
|
2014-08-07 09:44:41 +08:00
|
|
|
return -1, err
|
2014-07-17 07:52:15 +08:00
|
|
|
}
|
2014-11-04 10:18:55 +08:00
|
|
|
child.Close()
|
|
|
|
|
|
|
|
terminate := func(terr error) (int, error) {
|
|
|
|
// TODO: log the errors for kill and wait
|
|
|
|
cmd.Process.Kill()
|
|
|
|
cmd.Wait()
|
|
|
|
return -1, terr
|
|
|
|
}
|
2014-07-17 07:52:15 +08:00
|
|
|
|
2014-12-23 06:06:22 +08:00
|
|
|
encoder := json.NewEncoder(parent)
|
|
|
|
|
|
|
|
if err := encoder.Encode(container); err != nil {
|
|
|
|
return terminate(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
process := processArgs{
|
|
|
|
Env: append(env[0:], container.Env...),
|
|
|
|
Args: args,
|
|
|
|
}
|
|
|
|
if err := encoder.Encode(process); err != nil {
|
|
|
|
return terminate(err)
|
|
|
|
}
|
|
|
|
|
2014-08-06 06:34:59 +08:00
|
|
|
// Enter cgroups.
|
2014-08-13 14:18:55 +08:00
|
|
|
if err := EnterCgroups(state, cmd.Process.Pid); err != nil {
|
2014-11-04 10:18:55 +08:00
|
|
|
return terminate(err)
|
2014-08-06 06:34:59 +08:00
|
|
|
}
|
|
|
|
|
2014-11-04 10:18:55 +08:00
|
|
|
if err := json.NewEncoder(parent).Encode(container); err != nil {
|
|
|
|
return terminate(err)
|
2014-07-17 07:52:15 +08:00
|
|
|
}
|
|
|
|
|
2014-12-23 06:06:22 +08:00
|
|
|
return cmd.Process.Pid, nil
|
|
|
|
}
|
2014-07-17 07:52:15 +08:00
|
|
|
|
2014-12-23 06:06:22 +08:00
|
|
|
// Finalize entering into a container and execute a specified command
|
|
|
|
func InitIn(pipe *os.File) (err error) {
|
|
|
|
defer func() {
|
|
|
|
// if we have an error during the initialization of the container's init then send it back to the
|
|
|
|
// parent process in the form of an initError.
|
|
|
|
if err != nil {
|
|
|
|
// ensure that any data sent from the parent is consumed so it doesn't
|
|
|
|
// receive ECONNRESET when the child writes to the pipe.
|
|
|
|
ioutil.ReadAll(pipe)
|
|
|
|
if err := json.NewEncoder(pipe).Encode(initError{
|
|
|
|
Message: err.Error(),
|
|
|
|
}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-08-07 09:44:41 +08:00
|
|
|
}
|
2014-12-23 06:06:22 +08:00
|
|
|
// ensure that this pipe is always closed
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := FinalizeSetns(container); err != nil {
|
|
|
|
return err
|
2014-08-07 09:44:41 +08:00
|
|
|
}
|
2014-12-23 06:06:22 +08:00
|
|
|
|
|
|
|
if err := system.Execv(process.Args[0], process.Args[0:], process.Env); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("unreachable")
|
2014-07-03 06:11:15 +08:00
|
|
|
}
|
|
|
|
|
2014-08-07 09:44:41 +08:00
|
|
|
// Finalize expects that the setns calls have been setup and that is has joined an
|
|
|
|
// existing namespace
|
2014-12-23 06:06:22 +08:00
|
|
|
func FinalizeSetns(container *configs.Config) error {
|
2014-08-07 09:44:41 +08:00
|
|
|
// clear the current processes env and replace it with the environment defined on the container
|
2014-06-05 08:54:00 +08:00
|
|
|
if err := LoadContainerEnvironment(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-08-07 09:44:41 +08:00
|
|
|
|
2014-05-01 08:18:07 +08:00
|
|
|
if err := FinalizeNamespace(container); err != nil {
|
2014-05-24 09:06:14 +08:00
|
|
|
return err
|
2014-02-20 11:53:25 +08:00
|
|
|
}
|
2014-06-17 06:28:28 +08:00
|
|
|
|
2014-10-14 13:53:44 +08:00
|
|
|
if err := apparmor.ApplyProfile(container.AppArmorProfile); err != nil {
|
|
|
|
return fmt.Errorf("set apparmor profile %s: %s", container.AppArmorProfile, err)
|
|
|
|
}
|
|
|
|
|
2014-06-25 08:31:03 +08:00
|
|
|
if container.ProcessLabel != "" {
|
|
|
|
if err := label.SetProcessLabel(container.ProcessLabel); err != nil {
|
2014-06-17 06:28:28 +08:00
|
|
|
return err
|
|
|
|
}
|
2014-03-19 04:49:16 +08:00
|
|
|
}
|
2014-06-17 06:28:28 +08:00
|
|
|
|
2014-12-23 06:06:22 +08:00
|
|
|
return nil
|
2014-02-20 11:53:25 +08:00
|
|
|
}
|
2014-08-06 06:12:28 +08:00
|
|
|
|
2014-12-17 17:30:52 +08:00
|
|
|
func EnterCgroups(state *configs.State, pid int) error {
|
2014-08-13 14:18:55 +08:00
|
|
|
return cgroups.EnterPid(state.CgroupPaths, pid)
|
2014-08-06 06:12:28 +08:00
|
|
|
}
|