2015-02-07 04:48:57 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package libcontainer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-12-17 17:16:34 +08:00
|
|
|
"io"
|
2015-06-27 02:38:23 +08:00
|
|
|
"net"
|
2015-02-07 04:48:57 +08:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
2017-01-26 21:10:16 +08:00
|
|
|
"unsafe"
|
2015-02-07 04:48:57 +08:00
|
|
|
|
2015-05-06 21:14:04 +08:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-06-22 10:29:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/system"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/user"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/utils"
|
2015-06-27 02:38:23 +08:00
|
|
|
"github.com/vishvananda/netlink"
|
2015-02-07 04:48:57 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type initType string
|
|
|
|
|
|
|
|
const (
|
2015-02-18 13:50:43 +08:00
|
|
|
initSetns initType = "setns"
|
|
|
|
initStandard initType = "standard"
|
2015-02-07 04:48:57 +08:00
|
|
|
)
|
|
|
|
|
2015-02-10 07:16:27 +08:00
|
|
|
type pid struct {
|
|
|
|
Pid int `json:"pid"`
|
|
|
|
}
|
|
|
|
|
2015-02-11 03:51:45 +08:00
|
|
|
// network is an internal struct used to setup container networks.
|
|
|
|
type network struct {
|
|
|
|
configs.Network
|
|
|
|
|
2015-07-24 10:19:25 +08:00
|
|
|
// TempVethPeerName is a unique temporary veth peer name that was placed into
|
2015-02-11 03:51:45 +08:00
|
|
|
// the container's namespace.
|
|
|
|
TempVethPeerName string `json:"temp_veth_peer_name"`
|
|
|
|
}
|
|
|
|
|
2015-02-14 08:06:17 +08:00
|
|
|
// initConfig is used for transferring parameters from Exec() to Init()
|
2015-02-07 04:48:57 +08:00
|
|
|
type initConfig struct {
|
2017-03-15 00:36:38 +08:00
|
|
|
Args []string `json:"args"`
|
|
|
|
Env []string `json:"env"`
|
|
|
|
Cwd string `json:"cwd"`
|
|
|
|
Capabilities *configs.Capabilities `json:"capabilities"`
|
|
|
|
ProcessLabel string `json:"process_label"`
|
|
|
|
AppArmorProfile string `json:"apparmor_profile"`
|
|
|
|
NoNewPrivileges bool `json:"no_new_privileges"`
|
|
|
|
User string `json:"user"`
|
|
|
|
AdditionalGroups []string `json:"additional_groups"`
|
|
|
|
Config *configs.Config `json:"config"`
|
|
|
|
Networks []*network `json:"network"`
|
|
|
|
PassedFilesCount int `json:"passed_files_count"`
|
|
|
|
ContainerId string `json:"containerid"`
|
|
|
|
Rlimits []configs.Rlimit `json:"rlimits"`
|
|
|
|
CreateConsole bool `json:"create_console"`
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type initer interface {
|
2016-06-07 04:15:18 +08:00
|
|
|
Init() error
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
|
|
|
|
2017-03-03 04:53:06 +08:00
|
|
|
func newContainerInit(t initType, pipe *os.File, consoleSocket *os.File, stateDirFD int) (initer, error) {
|
2015-02-07 04:48:57 +08:00
|
|
|
var config *initConfig
|
|
|
|
if err := json.NewDecoder(pipe).Decode(&config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-07 11:16:11 +08:00
|
|
|
if err := populateProcessEnvironment(config.Env); err != nil {
|
2015-02-07 04:48:57 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch t {
|
|
|
|
case initSetns:
|
|
|
|
return &linuxSetnsInit{
|
2017-03-03 04:53:06 +08:00
|
|
|
pipe: pipe,
|
|
|
|
consoleSocket: consoleSocket,
|
|
|
|
config: config,
|
2015-02-07 04:48:57 +08:00
|
|
|
}, nil
|
|
|
|
case initStandard:
|
|
|
|
return &linuxStandardInit{
|
2017-03-03 04:53:06 +08:00
|
|
|
pipe: pipe,
|
|
|
|
consoleSocket: consoleSocket,
|
|
|
|
parentPid: syscall.Getppid(),
|
|
|
|
config: config,
|
|
|
|
stateDirFD: stateDirFD,
|
2015-02-07 04:48:57 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("unknown init type %q", t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// populateProcessEnvironment loads the provided environment variables into the
|
|
|
|
// current processes's environment.
|
|
|
|
func populateProcessEnvironment(env []string) error {
|
|
|
|
for _, pair := range env {
|
|
|
|
p := strings.SplitN(pair, "=", 2)
|
|
|
|
if len(p) < 2 {
|
|
|
|
return fmt.Errorf("invalid environment '%v'", pair)
|
|
|
|
}
|
|
|
|
if err := os.Setenv(p[0], p[1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// finalizeNamespace drops the caps, sets the correct user
|
2015-02-14 08:06:17 +08:00
|
|
|
// and working dir, and closes any leaked file descriptors
|
2015-03-25 14:58:22 +08:00
|
|
|
// before executing the command inside the namespace
|
2015-02-10 05:11:57 +08:00
|
|
|
func finalizeNamespace(config *initConfig) error {
|
2015-04-01 05:40:05 +08:00
|
|
|
// Ensure that all unwanted fds we may have accidentally
|
2015-02-07 04:48:57 +08:00
|
|
|
// inherited are marked close-on-exec so they stay out of the
|
|
|
|
// container
|
2015-04-01 05:40:05 +08:00
|
|
|
if err := utils.CloseExecFrom(config.PassedFilesCount + 3); err != nil {
|
2015-02-07 04:48:57 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-03-26 03:40:32 +08:00
|
|
|
|
2017-03-21 08:45:26 +08:00
|
|
|
capabilities := &configs.Capabilities{}
|
2015-03-26 03:40:32 +08:00
|
|
|
if config.Capabilities != nil {
|
|
|
|
capabilities = config.Capabilities
|
2017-03-21 08:45:26 +08:00
|
|
|
} else if config.Config.Capabilities != nil {
|
|
|
|
capabilities = config.Config.Capabilities
|
2015-03-26 03:40:32 +08:00
|
|
|
}
|
2017-03-15 00:36:38 +08:00
|
|
|
w, err := newContainerCapList(capabilities)
|
2015-02-10 07:38:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-07 04:48:57 +08:00
|
|
|
// drop capabilities in bounding set before changing user
|
2017-03-15 00:36:38 +08:00
|
|
|
if err := w.ApplyBoundingSet(); err != nil {
|
2015-02-07 04:48:57 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// preserve existing capabilities while we change users
|
|
|
|
if err := system.SetKeepCaps(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := setupUser(config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := system.ClearKeepCaps(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-15 00:36:38 +08:00
|
|
|
if err := w.ApplyCaps(); err != nil {
|
2015-02-07 04:48:57 +08:00
|
|
|
return err
|
|
|
|
}
|
2016-01-22 03:08:32 +08:00
|
|
|
if config.Cwd != "" {
|
|
|
|
if err := syscall.Chdir(config.Cwd); err != nil {
|
2016-08-09 00:26:59 +08:00
|
|
|
return fmt.Errorf("chdir to cwd (%q) set in config.json failed: %v", config.Cwd, err)
|
2016-01-22 03:08:32 +08:00
|
|
|
}
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-03 23:29:34 +08:00
|
|
|
// setupConsole sets up the console from inside the container, and sends the
|
|
|
|
// master pty fd to the config.Pipe (using cmsg). This is done to ensure that
|
|
|
|
// consoles are scoped to a container properly (see runc#814 and the many
|
|
|
|
// issues related to that). This has to be run *after* we've pivoted to the new
|
|
|
|
// rootfs (and the users' configuration is entirely set up).
|
2017-03-03 04:53:06 +08:00
|
|
|
func setupConsole(socket *os.File, config *initConfig, mount bool) error {
|
|
|
|
defer socket.Close()
|
2016-09-14 17:02:53 +08:00
|
|
|
// At this point, /dev/ptmx points to something that we would expect. We
|
|
|
|
// used to change the owner of the slave path, but since the /dev/pts mount
|
|
|
|
// can have gid=X set (at the users' option). So touching the owner of the
|
|
|
|
// slave PTY is not necessary, as the kernel will handle that for us. Note
|
|
|
|
// however, that setupUser (specifically fixStdioPermissions) *will* change
|
|
|
|
// the UID owner of the console to be the user the process will run as (so
|
|
|
|
// they can actually control their console).
|
|
|
|
console, err := newConsole()
|
2016-06-03 23:29:34 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// After we return from here, we don't need the console anymore.
|
|
|
|
defer console.Close()
|
|
|
|
|
|
|
|
linuxConsole, ok := console.(*linuxConsole)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("failed to cast console to *linuxConsole")
|
|
|
|
}
|
|
|
|
// Mount the console inside our rootfs.
|
|
|
|
if mount {
|
2016-09-14 06:09:09 +08:00
|
|
|
if err := linuxConsole.mount(); err != nil {
|
2016-06-03 23:29:34 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// While we can access console.master, using the API is a good idea.
|
2017-03-03 04:53:06 +08:00
|
|
|
if err := utils.SendFd(socket, linuxConsole.File()); err != nil {
|
2016-06-03 23:29:34 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Now, dup over all the things.
|
|
|
|
return linuxConsole.dupStdio()
|
|
|
|
}
|
|
|
|
|
2015-12-17 17:16:34 +08:00
|
|
|
// syncParentReady sends to the given pipe a JSON payload which indicates that
|
|
|
|
// the init is ready to Exec the child process. It then waits for the parent to
|
|
|
|
// indicate that it is cleared to Exec.
|
|
|
|
func syncParentReady(pipe io.ReadWriter) error {
|
|
|
|
// Tell parent.
|
2016-06-06 18:26:35 +08:00
|
|
|
if err := writeSync(pipe, procReady); err != nil {
|
2015-12-17 17:16:34 +08:00
|
|
|
return err
|
|
|
|
}
|
2016-06-06 18:26:35 +08:00
|
|
|
|
2015-12-17 17:16:34 +08:00
|
|
|
// Wait for parent to give the all-clear.
|
2016-06-06 18:26:35 +08:00
|
|
|
if err := readSync(pipe, procRun); err != nil {
|
|
|
|
return err
|
2015-12-17 17:16:34 +08:00
|
|
|
}
|
2016-06-06 18:26:35 +08:00
|
|
|
|
2015-12-17 17:16:34 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-17 18:20:06 +08:00
|
|
|
// syncParentHooks sends to the given pipe a JSON payload which indicates that
|
|
|
|
// the parent should execute pre-start hooks. It then waits for the parent to
|
|
|
|
// indicate that it is cleared to resume.
|
|
|
|
func syncParentHooks(pipe io.ReadWriter) error {
|
|
|
|
// Tell parent.
|
2016-06-06 18:26:35 +08:00
|
|
|
if err := writeSync(pipe, procHooks); err != nil {
|
2016-02-17 18:20:06 +08:00
|
|
|
return err
|
|
|
|
}
|
2016-06-06 18:26:35 +08:00
|
|
|
|
2016-02-17 18:20:06 +08:00
|
|
|
// Wait for parent to give the all-clear.
|
2016-06-06 18:26:35 +08:00
|
|
|
if err := readSync(pipe, procResume); err != nil {
|
|
|
|
return err
|
2016-02-17 18:20:06 +08:00
|
|
|
}
|
2016-06-06 18:26:35 +08:00
|
|
|
|
2016-02-17 18:20:06 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-07 04:48:57 +08:00
|
|
|
// setupUser changes the groups, gid, and uid for the user inside the container
|
2015-02-10 05:11:57 +08:00
|
|
|
func setupUser(config *initConfig) error {
|
2015-02-07 04:48:57 +08:00
|
|
|
// Set up defaults.
|
|
|
|
defaultExecUser := user.ExecUser{
|
|
|
|
Uid: syscall.Getuid(),
|
|
|
|
Gid: syscall.Getgid(),
|
|
|
|
Home: "/",
|
|
|
|
}
|
|
|
|
passwdPath, err := user.GetPasswdPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
groupPath, err := user.GetGroupPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
execUser, err := user.GetExecUserPath(config.User, &defaultExecUser, passwdPath, groupPath)
|
|
|
|
if err != nil {
|
2015-02-10 05:11:57 +08:00
|
|
|
return err
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
2015-05-01 07:02:31 +08:00
|
|
|
|
2015-06-13 00:40:42 +08:00
|
|
|
var addGroups []int
|
2016-06-10 18:35:13 +08:00
|
|
|
if len(config.AdditionalGroups) > 0 {
|
|
|
|
addGroups, err = user.GetAdditionalGroupsPath(config.AdditionalGroups, groupPath)
|
2015-06-13 00:40:42 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-01 07:02:31 +08:00
|
|
|
}
|
2015-09-23 02:12:55 +08:00
|
|
|
// before we change to the container's user make sure that the processes STDIO
|
|
|
|
// is correctly owned by the user that we are switching to.
|
|
|
|
if err := fixStdioPermissions(execUser); err != nil {
|
|
|
|
return err
|
2015-09-19 04:55:49 +08:00
|
|
|
}
|
2015-05-01 07:02:31 +08:00
|
|
|
suppGroups := append(execUser.Sgids, addGroups...)
|
2015-02-07 04:48:57 +08:00
|
|
|
if err := syscall.Setgroups(suppGroups); err != nil {
|
2015-02-10 05:11:57 +08:00
|
|
|
return err
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
2015-05-01 07:02:31 +08:00
|
|
|
|
2015-02-07 04:48:57 +08:00
|
|
|
if err := system.Setgid(execUser.Gid); err != nil {
|
2015-02-10 05:11:57 +08:00
|
|
|
return err
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
|
|
|
if err := system.Setuid(execUser.Uid); err != nil {
|
2015-02-10 05:11:57 +08:00
|
|
|
return err
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
|
|
|
// if we didn't get HOME already, set it based on the user's HOME
|
|
|
|
if envHome := os.Getenv("HOME"); envHome == "" {
|
|
|
|
if err := os.Setenv("HOME", execUser.Home); err != nil {
|
2015-02-10 05:11:57 +08:00
|
|
|
return err
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-23 02:12:55 +08:00
|
|
|
// fixStdioPermissions fixes the permissions of PID 1's STDIO within the container to the specified user.
|
|
|
|
// The ownership needs to match because it is created outside of the container and needs to be
|
|
|
|
// localized.
|
|
|
|
func fixStdioPermissions(u *user.ExecUser) error {
|
|
|
|
var null syscall.Stat_t
|
|
|
|
if err := syscall.Stat("/dev/null", &null); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, fd := range []uintptr{
|
|
|
|
os.Stdin.Fd(),
|
|
|
|
os.Stderr.Fd(),
|
|
|
|
os.Stdout.Fd(),
|
|
|
|
} {
|
|
|
|
var s syscall.Stat_t
|
|
|
|
if err := syscall.Fstat(int(fd), &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-14 17:02:53 +08:00
|
|
|
// Skip chown of /dev/null if it was used as one of the STDIO fds.
|
2015-09-23 02:12:55 +08:00
|
|
|
if s.Rdev == null.Rdev {
|
|
|
|
continue
|
|
|
|
}
|
2016-09-14 17:02:53 +08:00
|
|
|
// We only change the uid owner (as it is possible for the mount to
|
|
|
|
// prefer a different gid, and there's no reason for us to change it).
|
|
|
|
// The reason why we don't just leave the default uid=X mount setup is
|
|
|
|
// that users expect to be able to actually use their console. Without
|
|
|
|
// this code, you couldn't effectively run as a non-root user inside a
|
|
|
|
// container and also have a console set up.
|
|
|
|
if err := syscall.Fchown(int(fd), u.Uid, int(s.Gid)); err != nil {
|
2015-09-23 02:12:55 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-11 03:51:45 +08:00
|
|
|
// setupNetwork sets up and initializes any network interface inside the container.
|
|
|
|
func setupNetwork(config *initConfig) error {
|
2015-02-07 04:48:57 +08:00
|
|
|
for _, config := range config.Networks {
|
2015-02-10 07:16:27 +08:00
|
|
|
strategy, err := getStrategy(config.Type)
|
2015-02-07 04:48:57 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-11 03:51:45 +08:00
|
|
|
if err := strategy.initialize(config); err != nil {
|
|
|
|
return err
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupRoute(config *configs.Config) error {
|
|
|
|
for _, config := range config.Routes {
|
2015-06-27 02:38:23 +08:00
|
|
|
_, dst, err := net.ParseCIDR(config.Destination)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
src := net.ParseIP(config.Source)
|
|
|
|
if src == nil {
|
|
|
|
return fmt.Errorf("Invalid source for route: %s", config.Source)
|
|
|
|
}
|
|
|
|
gw := net.ParseIP(config.Gateway)
|
|
|
|
if gw == nil {
|
|
|
|
return fmt.Errorf("Invalid gateway for route: %s", config.Gateway)
|
|
|
|
}
|
|
|
|
l, err := netlink.LinkByName(config.InterfaceName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
route := &netlink.Route{
|
|
|
|
Scope: netlink.SCOPE_UNIVERSE,
|
|
|
|
Dst: dst,
|
|
|
|
Src: src,
|
|
|
|
Gw: gw,
|
|
|
|
LinkIndex: l.Attrs().Index,
|
|
|
|
}
|
|
|
|
if err := netlink.RouteAdd(route); err != nil {
|
2015-02-07 04:48:57 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-25 23:03:30 +08:00
|
|
|
func setupRlimits(limits []configs.Rlimit, pid int) error {
|
2016-03-11 06:35:16 +08:00
|
|
|
for _, rlimit := range limits {
|
2016-03-25 23:03:30 +08:00
|
|
|
if err := system.Prlimit(pid, rlimit.Type, syscall.Rlimit{Max: rlimit.Hard, Cur: rlimit.Soft}); err != nil {
|
2015-02-07 04:48:57 +08:00
|
|
|
return fmt.Errorf("error setting rlimit type %v: %v", rlimit.Type, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-02-12 08:45:23 +08:00
|
|
|
|
2017-01-26 21:10:16 +08:00
|
|
|
const _P_PID = 1
|
|
|
|
|
|
|
|
type siginfo struct {
|
|
|
|
si_signo int32
|
|
|
|
si_errno int32
|
|
|
|
si_code int32
|
|
|
|
// below here is a union; si_pid is the only field we use
|
|
|
|
si_pid int32
|
|
|
|
// Pad to 128 bytes as detailed in blockUntilWaitable
|
|
|
|
pad [96]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// isWaitable returns true if the process has exited false otherwise.
|
|
|
|
// Its based off blockUntilWaitable in src/os/wait_waitid.go
|
|
|
|
func isWaitable(pid int) (bool, error) {
|
|
|
|
si := &siginfo{}
|
|
|
|
_, _, e := syscall.Syscall6(syscall.SYS_WAITID, _P_PID, uintptr(pid), uintptr(unsafe.Pointer(si)), syscall.WEXITED|syscall.WNOWAIT|syscall.WNOHANG, 0, 0)
|
|
|
|
if e != 0 {
|
|
|
|
return false, os.NewSyscallError("waitid", e)
|
|
|
|
}
|
|
|
|
|
|
|
|
return si.si_pid != 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isNoChildren returns true if err represents a syscall.ECHILD false otherwise
|
|
|
|
func isNoChildren(err error) bool {
|
|
|
|
switch err := err.(type) {
|
|
|
|
case syscall.Errno:
|
|
|
|
if err == syscall.ECHILD {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
case *os.SyscallError:
|
|
|
|
if err.Err == syscall.ECHILD {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-11-08 07:22:27 +08:00
|
|
|
// signalAllProcesses freezes then iterates over all the processes inside the
|
2017-01-22 02:02:01 +08:00
|
|
|
// manager's cgroups sending the signal s to them.
|
2017-01-26 21:10:16 +08:00
|
|
|
// If s is SIGKILL then it will wait for each process to exit.
|
|
|
|
// For all other signals it will check if the process is ready to report its
|
|
|
|
// exit status and only if it is will a wait be performed.
|
2016-11-08 07:22:27 +08:00
|
|
|
func signalAllProcesses(m cgroups.Manager, s os.Signal) error {
|
2015-02-12 08:45:23 +08:00
|
|
|
var procs []*os.Process
|
|
|
|
if err := m.Freeze(configs.Frozen); err != nil {
|
2015-05-06 21:14:04 +08:00
|
|
|
logrus.Warn(err)
|
2015-02-12 08:45:23 +08:00
|
|
|
}
|
2016-01-09 03:37:18 +08:00
|
|
|
pids, err := m.GetAllPids()
|
2015-02-12 08:45:23 +08:00
|
|
|
if err != nil {
|
|
|
|
m.Freeze(configs.Thawed)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, pid := range pids {
|
2016-03-15 06:46:08 +08:00
|
|
|
p, err := os.FindProcess(pid)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warn(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
procs = append(procs, p)
|
2016-11-08 07:22:27 +08:00
|
|
|
if err := p.Signal(s); err != nil {
|
2016-03-15 06:46:08 +08:00
|
|
|
logrus.Warn(err)
|
2015-02-12 08:45:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := m.Freeze(configs.Thawed); err != nil {
|
2015-05-06 21:14:04 +08:00
|
|
|
logrus.Warn(err)
|
2015-02-12 08:45:23 +08:00
|
|
|
}
|
2017-01-22 02:02:01 +08:00
|
|
|
|
2015-02-12 08:45:23 +08:00
|
|
|
for _, p := range procs {
|
2017-01-26 21:10:16 +08:00
|
|
|
if s != syscall.SIGKILL {
|
|
|
|
if ok, err := isWaitable(p.Pid); err != nil {
|
|
|
|
if !isNoChildren(err) {
|
|
|
|
logrus.Warn("signalAllProcesses: ", p.Pid, err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
} else if !ok {
|
|
|
|
// Not ready to report so don't wait
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 08:45:23 +08:00
|
|
|
if _, err := p.Wait(); err != nil {
|
2017-01-26 21:10:16 +08:00
|
|
|
if !isNoChildren(err) {
|
|
|
|
logrus.Warn("wait: ", err)
|
|
|
|
}
|
2015-02-12 08:45:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|