2015-02-07 04:48:57 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package libcontainer
|
|
|
|
|
|
|
|
import (
|
2015-03-06 06:33:13 +08:00
|
|
|
"os"
|
2015-02-07 04:48:57 +08:00
|
|
|
"syscall"
|
|
|
|
|
2015-06-22 10:29:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/apparmor"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/label"
|
2015-06-30 02:12:54 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/seccomp"
|
2015-06-22 10:29:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/system"
|
2015-02-07 04:48:57 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type linuxStandardInit struct {
|
2015-04-03 04:55:55 +08:00
|
|
|
parentPid int
|
|
|
|
config *initConfig
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *linuxStandardInit) Init() error {
|
|
|
|
// join any namespaces via a path to the namespace fd if provided
|
|
|
|
if err := joinExistingNamespaces(l.config.Config.Namespaces); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-26 06:31:39 +08:00
|
|
|
var console *linuxConsole
|
|
|
|
if l.config.Console != "" {
|
|
|
|
console = newConsoleFromPath(l.config.Console)
|
2015-02-10 06:07:18 +08:00
|
|
|
if err := console.dupStdio(); err != nil {
|
2015-02-07 04:48:57 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, err := syscall.Setsid(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-26 06:31:39 +08:00
|
|
|
if console != nil {
|
2015-02-07 04:48:57 +08:00
|
|
|
if err := system.Setctty(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-02-11 03:51:45 +08:00
|
|
|
if err := setupNetwork(l.config); err != nil {
|
2015-02-07 04:48:57 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := setupRoute(l.config.Config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := setupRlimits(l.config.Config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-27 07:37:24 +08:00
|
|
|
if err := setOomScoreAdj(l.config.Config.OomScoreAdj); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-19 23:45:33 +08:00
|
|
|
|
2015-02-07 04:48:57 +08:00
|
|
|
label.Init()
|
|
|
|
// InitializeMountNamespace() can be executed only for a new mount namespace
|
|
|
|
if l.config.Config.Namespaces.Contains(configs.NEWNS) {
|
2015-02-26 06:31:39 +08:00
|
|
|
if err := setupRootfs(l.config.Config, console); err != nil {
|
2015-02-07 04:48:57 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if hostname := l.config.Config.Hostname; hostname != "" {
|
|
|
|
if err := syscall.Sethostname([]byte(hostname)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := apparmor.ApplyProfile(l.config.Config.AppArmorProfile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := label.SetProcessLabel(l.config.Config.ProcessLabel); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-23 10:17:30 +08:00
|
|
|
|
2015-07-07 07:18:08 +08:00
|
|
|
for key, value := range l.config.Config.Sysctl {
|
2015-04-23 10:17:30 +08:00
|
|
|
if err := writeSystemProperty(key, value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-12-19 23:45:33 +08:00
|
|
|
|
2015-02-13 08:23:05 +08:00
|
|
|
for _, path := range l.config.Config.ReadonlyPaths {
|
|
|
|
if err := remountReadonly(path); err != nil {
|
|
|
|
return err
|
2015-02-10 06:42:21 +08:00
|
|
|
}
|
2015-02-13 08:23:05 +08:00
|
|
|
}
|
|
|
|
for _, path := range l.config.Config.MaskPaths {
|
|
|
|
if err := maskFile(path); err != nil {
|
2015-02-07 04:48:57 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pdeath, err := system.GetParentDeathSignal()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-30 02:12:54 +08:00
|
|
|
if l.config.Config.Seccomp != nil {
|
|
|
|
if err := seccomp.InitSeccomp(l.config.Config.Seccomp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-02-10 05:11:57 +08:00
|
|
|
if err := finalizeNamespace(l.config); err != nil {
|
2015-02-07 04:48:57 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// finalizeNamespace can change user/group which clears the parent death
|
|
|
|
// signal, so we restore it here.
|
|
|
|
if err := pdeath.Restore(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-03 04:55:55 +08:00
|
|
|
// compare the parent from the inital start of the init process and make sure that it did not change.
|
|
|
|
// if the parent changes that means it died and we were reparened to something else so we should
|
|
|
|
// just kill ourself and not cause problems for someone else.
|
|
|
|
if syscall.Getppid() != l.parentPid {
|
2015-02-07 04:48:57 +08:00
|
|
|
return syscall.Kill(syscall.Getpid(), syscall.SIGKILL)
|
|
|
|
}
|
2015-03-06 06:33:13 +08:00
|
|
|
return system.Execv(l.config.Args[0], l.config.Args[0:], os.Environ())
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|