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
56 lines (45 loc) · 1.22 KB
/
Copy pathproxy.go
File metadata and controls
56 lines (45 loc) · 1.22 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
package proxy
import (
"fmt"
"log/slog"
"os"
"os/exec"
"syscall"
"github.com/opencontainers/runtime-spec/specs-go"
)
func NewProcess(logger *slog.Logger, namespaces []specs.LinuxNamespace, 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 := LinuxCloneFlags(logger, isRoot, namespaces)
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
}