github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/pkg/process/process_windows.go (about) 1 package process 2 3 import ( 4 "os" 5 6 "golang.org/x/sys/windows" 7 ) 8 9 // Alive returns true if process with a given pid is running. 10 func Alive(pid int) bool { 11 h, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, uint32(pid)) 12 if err != nil { 13 return false 14 } 15 var c uint32 16 err = windows.GetExitCodeProcess(h, &c) 17 _ = windows.CloseHandle(h) 18 if err != nil { 19 // From the GetExitCodeProcess function (processthreadsapi.h) API docs: 20 // https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getexitcodeprocess 21 // 22 // The GetExitCodeProcess function returns a valid error code defined by the 23 // application only after the thread terminates. Therefore, an application should 24 // not use STILL_ACTIVE (259) as an error code (STILL_ACTIVE is a macro for 25 // STATUS_PENDING (minwinbase.h)). If a thread returns STILL_ACTIVE (259) as 26 // an error code, then applications that test for that value could interpret it 27 // to mean that the thread is still running, and continue to test for the 28 // completion of the thread after the thread has terminated, which could put 29 // the application into an infinite loop. 30 return c == uint32(windows.STATUS_PENDING) 31 } 32 return true 33 } 34 35 // Kill force-stops a process. 36 func Kill(pid int) error { 37 p, err := os.FindProcess(pid) 38 if err == nil { 39 err = p.Kill() 40 if err != nil && err != os.ErrProcessDone { 41 return err 42 } 43 } 44 return nil 45 }