forked from nightlyone/lockfile
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request nightlyone#12 from stewi1014/master
Added relevant and go-compatable tests to testing file. Fixed dead PID on windows.
- Loading branch information
Showing
4 changed files
with
148 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,30 @@ | ||
package lockfile | ||
|
||
import ( | ||
"os" | ||
"reflect" | ||
"syscall" | ||
) | ||
|
||
func isProcessAlive(p *os.Process) error { | ||
// Extract handle value from the os.Process struct to avoid the need | ||
// of a second, manually opened process handle. | ||
value := reflect.ValueOf(p) | ||
// Dereference *os.Process to os.Process | ||
value = value.Elem() | ||
field := value.FieldByName("handle") | ||
|
||
handle := syscall.Handle(field.Uint()) | ||
//For some reason these consts don't exist in syscall. | ||
const ( | ||
error_invalid_parameter = 87 | ||
code_still_active = 259 | ||
) | ||
|
||
var code uint32 | ||
err := syscall.GetExitCodeProcess(handle, &code) | ||
func isRunning(pid int) (bool, error) { | ||
procHnd, err := syscall.OpenProcess(syscall.PROCESS_QUERY_INFORMATION, true, uint32(pid)) | ||
if err != nil { | ||
return err | ||
if scerr, ok := err.(syscall.Errno); ok { | ||
if uintptr(scerr) == error_invalid_parameter { | ||
return false, nil | ||
} | ||
} | ||
} | ||
|
||
// code will contain the exit code of the process or 259 (STILL_ALIVE) | ||
// if the process has not exited yet. | ||
if code == 259 { | ||
return nil | ||
var code uint32 | ||
err = syscall.GetExitCodeProcess(procHnd, &code) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return ErrDeadOwner | ||
return code == code_still_active, nil | ||
} |