summaryrefslogtreecommitdiff
path: root/libpod/runc.go
blob: efbb51b249089345a90982a3386f363ed13c596a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package libpod

import (
	"os"
	"os/exec"
	"strings"

	"github.com/sirupsen/logrus"
)

type runcGlobalOptions struct {
	log           string
	logFormat     string
	root          string
	criu          string
	systemdCgroup bool
}
type runcExecOptions struct {
	consoleSocket string
	cwd           string
	env           []string
	tty           bool
	user          string
	processPath   string
	detach        bool
	pidFile       string
	processLabel  string
	apparmor      string
	noNewPrivs    bool
	capAdd        []string
}

func parseGlobalOptionsToArgs(opts runcGlobalOptions) []string {
	args := []string{}
	if opts.log != "" {
		args = append(args, "--log", opts.log)
	}
	if opts.logFormat != "" {
		args = append(args, "--log-format", opts.logFormat)
	}
	if opts.root != "" {
		args = append(args, "--root", opts.root)
	}
	if opts.criu != "" {
		args = append(args, "--criu", opts.criu)
	}
	if opts.systemdCgroup {
		args = append(args, "--systemd-cgroup")
	}
	return args
}

// RuncExec executes 'runc --options exec --options cmd'
func (r *OCIRuntime) RuncExec(container *Container, command []string, globalOpts runcGlobalOptions, execOpts runcExecOptions) error {
	args := []string{}
	args = append(args, parseGlobalOptionsToArgs(globalOpts)...)
	// Add subcommand
	args = append(args, "exec")
	// Now add subcommand args

	if execOpts.consoleSocket != "" {
		args = append(args, "--console-socket", execOpts.consoleSocket)
	}
	if execOpts.cwd != "" {
		args = append(args, "--cwd", execOpts.cwd)
	}

	if len(execOpts.env) > 0 {
		for _, envInput := range execOpts.env {
			args = append(args, "--env", envInput)
		}
	}
	if execOpts.tty {
		args = append(args, "--tty")
	}
	if execOpts.user != "" {
		args = append(args, "--user", execOpts.user)

	}
	if execOpts.processPath != "" {
		args = append(args, "--process", execOpts.processPath)
	}
	if execOpts.detach {
		args = append(args, "--detach")
	}
	if execOpts.pidFile != "" {
		args = append(args, "--pid-file", execOpts.pidFile)
	}
	if execOpts.processLabel != "" {
		args = append(args, "--process-label", execOpts.processLabel)
	}
	if execOpts.apparmor != "" {
		args = append(args, "--apparmor", execOpts.apparmor)
	}
	if execOpts.noNewPrivs {
		args = append(args, "--no-new-privs")
	}
	if len(execOpts.capAdd) > 0 {
		for _, capAddValue := range execOpts.capAdd {
			args = append(args, "--cap", capAddValue)
		}
	}

	// Append Cid
	args = append(args, container.ID())
	// Append Cmd
	args = append(args, command...)

	logrus.Debug("Executing runc command: %s %s", r.path, strings.Join(args, " "))
	cmd := exec.Command(r.path, args...)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Stdin = os.Stdin
	cmd.Start()
	err := cmd.Wait()
	return err
}