2021-08-23 09:14:53 -04:00
|
|
|
//go:build linux || freebsd || darwin
|
2017-11-01 19:37:53 -04:00
|
|
|
// +build linux freebsd darwin
|
2016-03-18 14:50:19 -04:00
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package system // import "github.com/docker/docker/pkg/system"
|
2016-03-18 14:50:19 -04:00
|
|
|
|
|
|
|
import (
|
2020-03-26 23:20:41 -04:00
|
|
|
"fmt"
|
2021-08-24 06:10:50 -04:00
|
|
|
"os"
|
2020-03-26 23:20:41 -04:00
|
|
|
"strings"
|
2016-03-18 14:50:19 -04:00
|
|
|
"syscall"
|
2017-05-23 10:22:32 -04:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2016-03-18 14:50:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// IsProcessAlive returns true if process with a given pid is running.
|
|
|
|
func IsProcessAlive(pid int) bool {
|
2017-05-23 10:22:32 -04:00
|
|
|
err := unix.Kill(pid, syscall.Signal(0))
|
|
|
|
if err == nil || err == unix.EPERM {
|
2016-03-18 14:50:19 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// KillProcess force-stops a process.
|
|
|
|
func KillProcess(pid int) {
|
2017-05-23 10:22:32 -04:00
|
|
|
unix.Kill(pid, unix.SIGKILL)
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
2020-03-26 23:20:41 -04:00
|
|
|
|
|
|
|
// IsProcessZombie return true if process has a state with "Z"
|
|
|
|
// http://man7.org/linux/man-pages/man5/proc.5.html
|
|
|
|
func IsProcessZombie(pid int) (bool, error) {
|
|
|
|
statPath := fmt.Sprintf("/proc/%d/stat", pid)
|
2021-08-24 06:10:50 -04:00
|
|
|
dataBytes, err := os.ReadFile(statPath)
|
2020-03-26 23:20:41 -04:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
data := string(dataBytes)
|
|
|
|
sdata := strings.SplitN(data, " ", 4)
|
|
|
|
if len(sdata) >= 3 && sdata[2] == "Z" {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|