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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
//go:build !windows
package subprocess
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"syscall"
"gopkg.in/yaml.v2"
"github.com/lxc/incus/v6/shared/util"
)
// Process struct. Has ability to set runtime arguments.
type Process struct {
exitCode int64 `yaml:"-"`
exitErr error `yaml:"-"`
chExit chan struct{} `yaml:"-"`
hasMonitor bool `yaml:"-"`
closeFds bool `yaml:"-"`
Name string `yaml:"name"`
Args []string `yaml:"args,flow"`
Apparmor string `yaml:"apparmor"`
Cwd string `yaml:"cwd"`
PID int64 `yaml:"pid"`
Stdin io.ReadCloser `yaml:"-"`
Stdout io.WriteCloser `yaml:"-"`
Stderr io.WriteCloser `yaml:"-"`
UID uint32 `yaml:"uid"`
GID uint32 `yaml:"gid"`
SetGroups bool `yaml:"set_groups"`
SysProcAttr *syscall.SysProcAttr
}
func (p *Process) hasApparmor() bool {
if util.IsFalse(os.Getenv("INCUS_SECURITY_APPARMOR")) {
return false
}
_, err := exec.LookPath("aa-exec")
if err != nil {
return false
}
if !util.PathExists("/sys/kernel/security/apparmor") {
return false
}
return true
}
// GetPid returns the pid for the given process object.
func (p *Process) GetPid() (int64, error) {
pr, err := os.FindProcess(int(p.PID))
if err != nil {
if err == os.ErrProcessDone {
return 0, ErrNotRunning
}
return 0, err
}
err = pr.Signal(syscall.Signal(0))
if err != nil {
if err == os.ErrProcessDone {
return 0, ErrNotRunning
}
return 0, err
}
return p.PID, nil
}
// SetApparmor allows setting the AppArmor profile.
func (p *Process) SetApparmor(profile string) {
p.Apparmor = profile
}
// SetCreds allows setting process credentials.
func (p *Process) SetCreds(uid uint32, gid uint32) {
p.UID = uid
p.GID = gid
}
// Stop will stop the given process object.
func (p *Process) Stop() error {
pr, err := os.FindProcess(int(p.PID))
if err != nil {
if err == os.ErrProcessDone {
if p.hasMonitor {
<-p.chExit
}
return ErrNotRunning
}
return err
}
// Check if process exists.
err = pr.Signal(syscall.Signal(0))
if err == nil {
err = pr.Kill()
if err == nil {
if p.hasMonitor {
<-p.chExit
}
return nil // Killed successfully.
}
}
// Check if either the existence check or the kill resulted in an already finished error.
if err == os.ErrProcessDone {
if p.hasMonitor {
<-p.chExit
}
return ErrNotRunning
}
return fmt.Errorf("Could not kill process: %w", err)
}
// Start will start the given process object.
func (p *Process) Start(ctx context.Context) error {
return p.start(ctx, nil)
}
// StartWithFiles will start the given process object with extra file descriptors.
func (p *Process) StartWithFiles(ctx context.Context, fds []*os.File) error {
return p.start(ctx, fds)
}
func (p *Process) start(ctx context.Context, fds []*os.File) error {
var cmd *exec.Cmd
if p.Apparmor != "" && p.hasApparmor() {
cmd = exec.CommandContext(ctx, "aa-exec", append([]string{"-p", p.Apparmor, p.Name}, p.Args...)...)
} else {
cmd = exec.CommandContext(ctx, p.Name, p.Args...)
}
cmd.Stdout = p.Stdout
cmd.Stderr = p.Stderr
cmd.Stdin = p.Stdin
cmd.SysProcAttr = p.SysProcAttr
if p.Cwd != "" {
cmd.Dir = p.Cwd
}
if cmd.SysProcAttr == nil {
cmd.SysProcAttr = &syscall.SysProcAttr{}
}
cmd.SysProcAttr.Setsid = true
if p.UID != 0 || p.GID != 0 {
cmd.SysProcAttr.Credential = &syscall.Credential{}
cmd.SysProcAttr.Credential.Uid = p.UID
cmd.SysProcAttr.Credential.Gid = p.GID
}
if fds != nil {
cmd.ExtraFiles = fds
}
if p.Stdout != nil && p.closeFds {
defer func() { _ = p.Stdout.Close() }()
}
if p.Stderr != nil && p.Stderr != p.Stdout && p.closeFds {
defer func() { _ = p.Stderr.Close() }()
}
// Start the process.
err := cmd.Start()
if err != nil {
return fmt.Errorf("Unable to start process: %w", err)
}
p.PID = int64(cmd.Process.Pid)
// Reset exitCode/exitErr
p.exitCode = 0
p.exitErr = nil
// Spawn a goroutine waiting for it to exit.
p.chExit = make(chan struct{})
p.hasMonitor = true
go func() {
defer close(p.chExit)
err := cmd.Wait()
if cmd.ProcessState != nil {
p.exitCode = int64(cmd.ProcessState.ExitCode())
} else {
p.exitCode = -1
}
if err != nil {
p.exitErr = err
return
}
if p.exitCode != 0 {
p.exitErr = fmt.Errorf("Process exited with non-zero value %d", p.exitCode)
}
}()
return nil
}
// Restart stop and starts the given process object.
func (p *Process) Restart(ctx context.Context) error {
err := p.Stop()
if err != nil {
return fmt.Errorf("Unable to stop process: %w", err)
}
err = p.Start(ctx)
if err != nil {
return fmt.Errorf("Unable to start process: %w", err)
}
return nil
}
// Reload sends the SIGHUP signal to the given process object.
func (p *Process) Reload() error {
pr, err := os.FindProcess(int(p.PID))
if err != nil {
if err == os.ErrProcessDone {
return ErrNotRunning
}
return fmt.Errorf("Could not reload process: %w", err)
}
err = pr.Signal(syscall.Signal(0))
if err == nil {
err = pr.Signal(syscall.SIGHUP)
if err != nil {
return fmt.Errorf("Could not reload process: %w", err)
}
return nil
} else if err == os.ErrProcessDone {
return ErrNotRunning
}
return fmt.Errorf("Could not reload process: %w", err)
}
// Save will save the given process object to a YAML file. Can be imported at a later point.
func (p *Process) Save(path string) error {
dat, err := yaml.Marshal(p)
if err != nil {
return fmt.Errorf("Unable to serialize process struct to YAML: %w", err)
}
err = os.WriteFile(path, dat, 0o644)
if err != nil {
return fmt.Errorf("Unable to write to file '%s': %w", path, err)
}
return nil
}
// Signal will send a signal to the given process object given a signal value.
func (p *Process) Signal(signal int64) error {
pr, err := os.FindProcess(int(p.PID))
if err != nil {
if err == os.ErrProcessDone {
return ErrNotRunning
}
return err
}
err = pr.Signal(syscall.Signal(0))
if err == nil {
err = pr.Signal(syscall.Signal(signal))
if err != nil {
return fmt.Errorf("Could not signal process: %w", err)
}
return nil
} else if err == os.ErrProcessDone {
return ErrNotRunning
}
return fmt.Errorf("Could not signal process: %w", err)
}
// Wait will wait for the given process object exit code.
func (p *Process) Wait(ctx context.Context) (int64, error) {
if !p.hasMonitor {
return -1, fmt.Errorf("Unable to wait on process we didn't spawn")
}
select {
case <-p.chExit:
return p.exitCode, p.exitErr
case <-ctx.Done():
return -1, ctx.Err()
}
}
|