github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/utils/lock/utils_unix.go (about)

     1  // +build linux darwin freebsd
     2  
     3  package lock
     4  
     5  import (
     6  	"github.com/jfrog/jfrog-client-go/utils/log"
     7  	"os"
     8  	"syscall"
     9  )
    10  
    11  // This file will be compiled only on unix systems.
    12  // Checks if the process is running.
    13  // If error occurs, check if the error is part of the OS permission errors. This means the process is running.
    14  // Else means the process is not running.
    15  func isProcessRunning(pid int) (bool, error) {
    16  	process, err := os.FindProcess(pid)
    17  	if err != nil {
    18  		return false, err
    19  	}
    20  	err = process.Signal(syscall.Signal(0))
    21  	// If err is not nil, then the other process might still be running.
    22  	if err != nil {
    23  		// If this is a permission error, then the other process is running
    24  		if os.IsPermission(err) {
    25  			log.Debug("Other process still alive: ", err.Error())
    26  			return true, nil
    27  		}
    28  		// The other process died without unlocking. Let's unlock.
    29  		return false, nil
    30  	}
    31  	// This means there were no errors, same process.
    32  	return true, nil
    33  }