github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/pkg/system/process_unix.go (about)

     1  // +build linux freebsd darwin
     2  
     3  package system // import "github.com/docker/docker/pkg/system"
     4  
     5  import (
     6  	"fmt"
     7  	"io/ioutil"
     8  	"strings"
     9  	"syscall"
    10  
    11  	"golang.org/x/sys/unix"
    12  )
    13  
    14  // IsProcessAlive returns true if process with a given pid is running.
    15  func IsProcessAlive(pid int) bool {
    16  	err := unix.Kill(pid, syscall.Signal(0))
    17  	if err == nil || err == unix.EPERM {
    18  		return true
    19  	}
    20  
    21  	return false
    22  }
    23  
    24  // KillProcess force-stops a process.
    25  func KillProcess(pid int) {
    26  	unix.Kill(pid, unix.SIGKILL)
    27  }
    28  
    29  // IsProcessZombie return true if process has a state with "Z"
    30  // http://man7.org/linux/man-pages/man5/proc.5.html
    31  func IsProcessZombie(pid int) (bool, error) {
    32  	statPath := fmt.Sprintf("/proc/%d/stat", pid)
    33  	dataBytes, err := ioutil.ReadFile(statPath)
    34  	if err != nil {
    35  		return false, err
    36  	}
    37  	data := string(dataBytes)
    38  	sdata := strings.SplitN(data, " ", 4)
    39  	if len(sdata) >= 3 && sdata[2] == "Z" {
    40  		return true, nil
    41  	}
    42  
    43  	return false, nil
    44  }