This allows us to maintain GoDoc in a single place, and for
"Kill" and "Alive" to have consistent error-handling (Windows
does not support negative process-IDs).
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,29 @@ |
| 0 |
+package process |
|
| 1 |
+ |
|
| 2 |
+import "fmt" |
|
| 3 |
+ |
|
| 4 |
+// Alive returns true if process with a given pid is running. |
|
| 5 |
+// |
|
| 6 |
+// It only considers positive PIDs; 0 (all processes in the current process |
|
| 7 |
+// group), -1 (all processes with a PID larger than 1), and negative (-n, |
|
| 8 |
+// all processes in process group "n") values for pid are never considered |
|
| 9 |
+// to be alive. |
|
| 10 |
+func Alive(pid int) bool {
|
|
| 11 |
+ if pid < 1 {
|
|
| 12 |
+ return false |
|
| 13 |
+ } |
|
| 14 |
+ return alive(pid) |
|
| 15 |
+} |
|
| 16 |
+ |
|
| 17 |
+// Kill force-stops a process. It only allows positive PIDs; 0 (all processes |
|
| 18 |
+// in the current process group), -1 (all processes with a PID larger than 1), |
|
| 19 |
+// and negative (-n, all processes in process group "n") values for pid producs |
|
| 20 |
+// an error. Refer to [KILL(2)] for details. |
|
| 21 |
+// |
|
| 22 |
+// [KILL(2)]: https://man7.org/linux/man-pages/man2/kill.2.html |
|
| 23 |
+func Kill(pid int) error {
|
|
| 24 |
+ if pid < 1 {
|
|
| 25 |
+ return fmt.Errorf("invalid PID (%d): only positive PIDs are allowed", pid)
|
|
| 26 |
+ } |
|
| 27 |
+ return kill(pid) |
|
| 28 |
+} |
| ... | ... |
@@ -14,17 +14,10 @@ import ( |
| 14 | 14 |
"golang.org/x/sys/unix" |
| 15 | 15 |
) |
| 16 | 16 |
|
| 17 |
-// Alive returns true if process with a given pid is running. It only considers |
|
| 18 |
-// positive PIDs; 0 (all processes in the current process group), -1 (all processes |
|
| 19 |
-// with a PID larger than 1), and negative (-n, all processes in process group |
|
| 20 |
-// "n") values for pid are never considered to be alive. |
|
| 21 |
-func Alive(pid int) bool {
|
|
| 22 |
- if pid < 1 {
|
|
| 23 |
- return false |
|
| 24 |
- } |
|
| 17 |
+func alive(pid int) bool {
|
|
| 25 | 18 |
switch runtime.GOOS {
|
| 26 | 19 |
case "darwin": |
| 27 |
- // OS X does not have a proc filesystem. Use kill -0 pid to judge if the |
|
| 20 |
+ // macOS does not have a proc filesystem. Use kill -0 pid to judge if the |
|
| 28 | 21 |
// process exists. From KILL(2): https://www.freebsd.org/cgi/man.cgi?query=kill&sektion=2&manpath=OpenDarwin+7.2.1 |
| 29 | 22 |
// |
| 30 | 23 |
// Sig may be one of the signals specified in sigaction(2) or it may |
| ... | ... |
@@ -41,16 +34,7 @@ func Alive(pid int) bool {
|
| 41 | 41 |
} |
| 42 | 42 |
} |
| 43 | 43 |
|
| 44 |
-// Kill force-stops a process. It only considers positive PIDs; 0 (all processes |
|
| 45 |
-// in the current process group), -1 (all processes with a PID larger than 1), |
|
| 46 |
-// and negative (-n, all processes in process group "n") values for pid are |
|
| 47 |
-// ignored. Refer to [KILL(2)] for details. |
|
| 48 |
-// |
|
| 49 |
-// [KILL(2)]: https://man7.org/linux/man-pages/man2/kill.2.html |
|
| 50 |
-func Kill(pid int) error {
|
|
| 51 |
- if pid < 1 {
|
|
| 52 |
- return fmt.Errorf("invalid PID (%d): only positive PIDs are allowed", pid)
|
|
| 53 |
- } |
|
| 44 |
+func kill(pid int) error {
|
|
| 54 | 45 |
err := unix.Kill(pid, unix.SIGKILL) |
| 55 | 46 |
if err != nil && !errors.Is(err, unix.ESRCH) {
|
| 56 | 47 |
return err |
| ... | ... |
@@ -6,8 +6,7 @@ import ( |
| 6 | 6 |
"golang.org/x/sys/windows" |
| 7 | 7 |
) |
| 8 | 8 |
|
| 9 |
-// Alive returns true if process with a given pid is running. |
|
| 10 |
-func Alive(pid int) bool {
|
|
| 9 |
+func alive(pid int) bool {
|
|
| 11 | 10 |
h, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, uint32(pid)) |
| 12 | 11 |
if err != nil {
|
| 13 | 12 |
return false |
| ... | ... |
@@ -32,8 +31,7 @@ func Alive(pid int) bool {
|
| 32 | 32 |
return true |
| 33 | 33 |
} |
| 34 | 34 |
|
| 35 |
-// Kill force-stops a process. |
|
| 36 |
-func Kill(pid int) error {
|
|
| 35 |
+func kill(pid int) error {
|
|
| 37 | 36 |
p, err := os.FindProcess(pid) |
| 38 | 37 |
if err == nil {
|
| 39 | 38 |
err = p.Kill() |