github.com/jfrog/jfrog-cli-core/v2@v2.51.0/utils/osutils/utils_windows.go (about)

     1  package osutils
     2  
     3  import (
     4  	"syscall"
     5  
     6  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
     7  )
     8  
     9  // This file will be compiled on Windows.
    10  // Checks if the process can be reached.
    11  // If an error occurs, check if the error is part of the invalid parameter. This means the process is not running.
    12  // Else find the exit code. If the exit code 259 means the process is running.
    13  func IsProcessRunning(pid int) (bool, error) {
    14  	process, err := syscall.OpenProcess(syscall.PROCESS_QUERY_INFORMATION, true, uint32(pid))
    15  	if err != nil {
    16  		// Check if err is of type of syscall.Errno, which is a Windows error number.
    17  		if errnoErr, ok := err.(syscall.Errno); ok {
    18  			// 87 - error invalid parameter. For example during the tests when we provide a non existing PID
    19  			if uintptr(errnoErr) == 87 {
    20  				return false, nil
    21  			}
    22  		}
    23  	}
    24  
    25  	var exitCode uint32
    26  	err = syscall.GetExitCodeProcess(process, &exitCode)
    27  	if err != nil {
    28  		return false, errorutils.CheckError(err)
    29  	}
    30  
    31  	// 259 - process still alive
    32  	return exitCode == 259, nil
    33  }