2015-08-15 08:28:28 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-01-30 09:22:50 +08:00
|
|
|
"encoding/json"
|
2015-08-15 08:28:28 +08:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2016-01-16 02:54:02 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-08-15 08:28:28 +08:00
|
|
|
|
2016-04-06 23:57:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/utils"
|
2016-04-13 04:35:51 +08:00
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
2016-06-07 02:45:46 +08:00
|
|
|
"github.com/urfave/cli"
|
2015-08-15 08:28:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var execCommand = cli.Command{
|
|
|
|
Name: "exec",
|
|
|
|
Usage: "execute new process inside the container",
|
2016-02-11 01:30:06 +08:00
|
|
|
ArgsUsage: `<container-id> <container command>
|
|
|
|
|
|
|
|
Where "<container-id>" is the name for the instance of the container and
|
|
|
|
"<container command>" is the command to be executed in the container.
|
|
|
|
|
2016-05-24 16:59:24 +08:00
|
|
|
EXAMPLE:
|
2016-02-11 01:30:06 +08:00
|
|
|
For example, if the container is configured to run the linux ps command the
|
|
|
|
following will output a list of processes running in the container:
|
|
|
|
|
|
|
|
# runc exec <container-id> ps`,
|
2016-01-08 06:34:11 +08:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "console",
|
|
|
|
Usage: "specify the pty slave path for use with the container",
|
|
|
|
},
|
2016-01-16 02:54:02 +08:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "cwd",
|
|
|
|
Usage: "current working directory in the container",
|
|
|
|
},
|
|
|
|
cli.StringSliceFlag{
|
|
|
|
Name: "env, e",
|
|
|
|
Usage: "set environment variables",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "tty, t",
|
|
|
|
Usage: "allocate a pseudo-TTY",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "user, u",
|
|
|
|
Usage: "UID (format: <uid>[:<gid>])",
|
|
|
|
},
|
2016-01-30 09:22:50 +08:00
|
|
|
cli.StringFlag{
|
2016-03-04 02:44:33 +08:00
|
|
|
Name: "process, p",
|
2016-01-30 09:22:50 +08:00
|
|
|
Usage: "path to the process.json",
|
|
|
|
},
|
2016-01-30 09:37:52 +08:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "detach,d",
|
|
|
|
Usage: "detach from the container's process",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "pid-file",
|
|
|
|
Value: "",
|
|
|
|
Usage: "specify the file to write the process id to",
|
|
|
|
},
|
2016-03-04 02:44:33 +08:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "process-label",
|
|
|
|
Usage: "set the asm process label for the process commonly used with selinux",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "apparmor",
|
|
|
|
Usage: "set the apparmor profile for the process",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "no-new-privs",
|
|
|
|
Usage: "set the no new privileges value for the process",
|
|
|
|
},
|
|
|
|
cli.StringSliceFlag{
|
|
|
|
Name: "cap, c",
|
|
|
|
Value: &cli.StringSlice{},
|
|
|
|
Usage: "add a capability to the bounding set for the process",
|
|
|
|
},
|
2016-03-15 06:46:08 +08:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "no-subreaper",
|
|
|
|
Usage: "disable the use of the subreaper used to reap reparented processes",
|
|
|
|
},
|
2016-01-08 06:34:11 +08:00
|
|
|
},
|
2016-05-10 13:58:09 +08:00
|
|
|
Action: func(context *cli.Context) error {
|
2015-08-15 08:28:28 +08:00
|
|
|
if os.Geteuid() != 0 {
|
2016-05-10 13:58:09 +08:00
|
|
|
return fmt.Errorf("runc should be run as root")
|
2015-08-15 08:28:28 +08:00
|
|
|
}
|
2016-01-30 09:22:50 +08:00
|
|
|
status, err := execProcess(context)
|
2016-05-10 13:58:09 +08:00
|
|
|
if err == nil {
|
|
|
|
os.Exit(status)
|
2015-08-15 08:28:28 +08:00
|
|
|
}
|
2016-05-10 13:58:09 +08:00
|
|
|
return fmt.Errorf("exec failed: %v", err)
|
2015-08-15 08:28:28 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-01-30 09:22:50 +08:00
|
|
|
func execProcess(context *cli.Context) (int, error) {
|
2015-08-15 08:28:28 +08:00
|
|
|
container, err := getContainer(context)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2016-04-10 01:42:54 +08:00
|
|
|
path := context.String("process")
|
|
|
|
if path == "" && len(context.Args()) == 1 {
|
|
|
|
return -1, fmt.Errorf("process args cannot be empty")
|
|
|
|
}
|
2016-02-25 03:19:30 +08:00
|
|
|
detach := context.Bool("detach")
|
|
|
|
state, err := container.State()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2016-04-06 23:57:59 +08:00
|
|
|
bundle := utils.SearchLabels(state.Config.Labels, "bundle")
|
2016-02-25 03:19:30 +08:00
|
|
|
p, err := getProcess(context, bundle)
|
2016-01-30 09:22:50 +08:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2016-03-15 06:46:08 +08:00
|
|
|
r := &runner{
|
|
|
|
enableSubreaper: !context.Bool("no-subreaper"),
|
|
|
|
shouldDestroy: false,
|
|
|
|
container: container,
|
|
|
|
console: context.String("console"),
|
|
|
|
detach: detach,
|
|
|
|
pidFile: context.String("pid-file"),
|
|
|
|
}
|
|
|
|
return r.run(p)
|
2016-01-30 09:22:50 +08:00
|
|
|
}
|
2016-01-16 02:54:02 +08:00
|
|
|
|
2016-01-30 09:22:50 +08:00
|
|
|
func getProcess(context *cli.Context, bundle string) (*specs.Process, error) {
|
|
|
|
if path := context.String("process"); path != "" {
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
var p specs.Process
|
|
|
|
if err := json.NewDecoder(f).Decode(&p); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-08 01:36:14 +08:00
|
|
|
return &p, validateProcessSpec(&p)
|
2016-01-30 09:22:50 +08:00
|
|
|
}
|
|
|
|
// process via cli flags
|
|
|
|
if err := os.Chdir(bundle); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-06 02:46:12 +08:00
|
|
|
spec, err := loadSpec(specConfig)
|
2016-01-30 09:22:50 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-16 02:54:02 +08:00
|
|
|
p := spec.Process
|
2016-02-09 06:25:03 +08:00
|
|
|
p.Args = context.Args()[1:]
|
2016-01-16 02:54:02 +08:00
|
|
|
// override the cwd, if passed
|
|
|
|
if context.String("cwd") != "" {
|
|
|
|
p.Cwd = context.String("cwd")
|
|
|
|
}
|
2016-03-04 02:44:33 +08:00
|
|
|
if ap := context.String("apparmor"); ap != "" {
|
|
|
|
p.ApparmorProfile = ap
|
|
|
|
}
|
|
|
|
if l := context.String("process-label"); l != "" {
|
|
|
|
p.SelinuxLabel = l
|
|
|
|
}
|
|
|
|
if caps := context.StringSlice("cap"); len(caps) > 0 {
|
|
|
|
p.Capabilities = caps
|
|
|
|
}
|
2016-01-16 02:54:02 +08:00
|
|
|
// append the passed env variables
|
|
|
|
for _, e := range context.StringSlice("env") {
|
|
|
|
p.Env = append(p.Env, e)
|
|
|
|
}
|
|
|
|
// set the tty
|
|
|
|
if context.IsSet("tty") {
|
|
|
|
p.Terminal = context.Bool("tty")
|
|
|
|
}
|
2016-03-04 02:44:33 +08:00
|
|
|
if context.IsSet("no-new-privs") {
|
|
|
|
p.NoNewPrivileges = context.Bool("no-new-privs")
|
|
|
|
}
|
2016-01-16 02:54:02 +08:00
|
|
|
// override the user, if passed
|
|
|
|
if context.String("user") != "" {
|
|
|
|
u := strings.SplitN(context.String("user"), ":", 2)
|
|
|
|
if len(u) > 1 {
|
|
|
|
gid, err := strconv.Atoi(u[1])
|
|
|
|
if err != nil {
|
2016-01-30 09:22:50 +08:00
|
|
|
return nil, fmt.Errorf("parsing %s as int for gid failed: %v", u[1], err)
|
2016-01-16 02:54:02 +08:00
|
|
|
}
|
|
|
|
p.User.GID = uint32(gid)
|
|
|
|
}
|
|
|
|
uid, err := strconv.Atoi(u[0])
|
|
|
|
if err != nil {
|
2016-01-30 09:22:50 +08:00
|
|
|
return nil, fmt.Errorf("parsing %s as int for uid failed: %v", u[0], err)
|
2016-01-16 02:54:02 +08:00
|
|
|
}
|
|
|
|
p.User.UID = uint32(uid)
|
|
|
|
}
|
2016-01-30 09:22:50 +08:00
|
|
|
return &p, nil
|
2015-08-15 08:28:28 +08:00
|
|
|
}
|