forked from arm/remoteproc-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.go
More file actions
103 lines (86 loc) · 2.31 KB
/
Copy pathproxy.go
File metadata and controls
103 lines (86 loc) · 2.31 KB
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
package proxy
import (
"fmt"
"os"
"os/exec"
"syscall"
"github.com/opencontainers/runtime-spec/specs-go"
"golang.org/x/sys/unix"
)
var namespaceFlags = map[specs.LinuxNamespaceType]uintptr{
specs.CgroupNamespace: unix.CLONE_NEWCGROUP,
specs.IPCNamespace: unix.CLONE_NEWIPC,
specs.MountNamespace: unix.CLONE_NEWNS,
specs.NetworkNamespace: unix.CLONE_NEWNET,
specs.PIDNamespace: unix.CLONE_NEWPID,
specs.TimeNamespace: unix.CLONE_NEWTIME,
specs.UserNamespace: unix.CLONE_NEWUSER,
specs.UTSNamespace: unix.CLONE_NEWUTS,
}
var namespaceCloneFlagsFn = namespaceCloneFlags
func namespaceCloneFlags(spec *specs.Spec) (uintptr, error) {
if spec == nil {
return 0, nil
}
var flags uintptr
for _, ns := range spec.Linux.Namespaces {
if ns.Path != "" {
continue
}
flag, ok := namespaceFlags[ns.Type]
if !ok {
return 0, fmt.Errorf("unknown namespace type %q", ns.Type)
}
flags |= flag
}
return flags, nil
}
func effectiveNamespaceFlags(isRoot bool, spec *specs.Spec) (uintptr, error) {
flags, err := namespaceCloneFlagsFn(spec)
if err != nil {
return 0, err
}
if !isRoot {
if flags != 0 {
fmt.Fprintln(os.Stderr, "[WARN] running non-root; namespace isolation disabled")
}
return 0, nil
}
return flags, nil
}
func NewProcess(spec *specs.Spec, devicePath string) (int, error) {
execPath, err := os.Executable()
if err != nil {
return -1, fmt.Errorf("failed to get executable path: %w", err)
}
isRoot := os.Geteuid() == 0
namespaceFlags, err := effectiveNamespaceFlags(isRoot, spec)
if err != nil {
return -1, err
}
cmd := exec.Command(execPath, "proxy", "--device-path", devicePath)
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
Cloneflags: namespaceFlags,
}
if err := cmd.Start(); err != nil {
return -1, fmt.Errorf("failed to start proxy process: %w", err)
}
return cmd.Process.Pid, nil
}
func StopFirmware(pid int) error {
return SendSignal(pid, syscall.SIGTERM)
}
func StartFirmware(pid int) error {
return SendSignal(pid, syscall.SIGUSR1)
}
func SendSignal(pid int, signal syscall.Signal) error {
process, err := os.FindProcess(pid)
if err != nil {
return fmt.Errorf("failed to find process %d: %w", pid, err)
}
if err := process.Signal(signal); err != nil {
return fmt.Errorf("failed to send %s: %w", signal, err)
}
return nil
}