Skip to content

Commit

Permalink
Merge pull request #12953 from tomponline/stable-5.0
Browse files Browse the repository at this point in the history
Backport go tip fixes (stable-5.0)
  • Loading branch information
tomponline authored Feb 23, 2024
2 parents 6a77ab2 + d5a0c78 commit 993a753
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 16 deletions.
6 changes: 5 additions & 1 deletion lxd/instance/drivers/driver_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -785,12 +785,16 @@ func (d *qemu) ovmfPath() string {
func (d *qemu) killQemuProcess(pid int) error {
proc, err := os.FindProcess(pid)
if err != nil {
if err == os.ErrProcessDone {
return nil
}

return err
}

err = proc.Kill()
if err != nil {
if strings.Contains(err.Error(), "process already finished") {
if err == os.ErrProcessDone {
return nil
}

Expand Down
68 changes: 53 additions & 15 deletions shared/subprocess/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"os"
"os/exec"
"strings"
"syscall"

"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -59,13 +58,25 @@ func (p *Process) hasApparmor() bool {

// GetPid returns the pid for the given process object.
func (p *Process) GetPid() (int64, error) {
pr, _ := os.FindProcess(int(p.PID))
err := pr.Signal(syscall.Signal(0))
if err == nil {
return p.PID, nil
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 0, ErrNotRunning
return p.PID, nil
}

// SetApparmor allows setting the AppArmor profile.
Expand All @@ -81,10 +92,21 @@ func (p *Process) SetCreds(uid uint32, gid uint32) {

// Stop will stop the given process object.
func (p *Process) Stop() error {
pr, _ := os.FindProcess(int(p.PID))
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))
err = pr.Signal(syscall.Signal(0))
if err == nil {
err = pr.Kill()
if err == nil {
Expand All @@ -97,7 +119,7 @@ func (p *Process) Stop() error {
}

// Check if either the existence check or the kill resulted in an already finished error.
if strings.Contains(err.Error(), "process already finished") {
if err == os.ErrProcessDone {
if p.hasMonitor {
<-p.chExit
}
Expand Down Expand Up @@ -212,16 +234,24 @@ func (p *Process) Restart(ctx context.Context) error {

// Reload sends the SIGHUP signal to the given process object.
func (p *Process) Reload() error {
pr, _ := os.FindProcess(int(p.PID))
err := pr.Signal(syscall.Signal(0))
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 strings.Contains(err.Error(), "process already finished") {
} else if err == os.ErrProcessDone {
return ErrNotRunning
}

Expand All @@ -245,16 +275,24 @@ func (p *Process) Save(path string) error {

// Signal will send a signal to the given process object given a signal value.
func (p *Process) Signal(signal int64) error {
pr, _ := os.FindProcess(int(p.PID))
err := pr.Signal(syscall.Signal(0))
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 strings.Contains(err.Error(), "process already finished") {
} else if err == os.ErrProcessDone {
return ErrNotRunning
}

Expand Down

0 comments on commit 993a753

Please sign in to comment.