This repository has been archived on 2021-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
ui/go/os/kill_unix.go

44 lines
1.2 KiB
Go

// +build unix linux android
package os
import (
"git.openprivacy.ca/openprivacy/log"
"os/exec"
"strconv"
"strings"
)
// CheckProcessAndKill first executes a process search on the system by the last known pid, if the name of the
// process matches the expected name, then it is killed.
// On unix systems the command "ps" is practically universal and should suffice for this...
func CheckProcessAndKill(pid uint64, processName string) {
log.Debugf("killing: %v", pid)
bytes, err := exec.Command("ps", "-p", strconv.Itoa(int(pid)), "-o", "command").Output()
if err == nil {
// check for a binary @ "/<process_name>"
lines := strings.Split(string(bytes), "\n")
if len(lines) >= 2 && strings.Contains(lines[1], "/"+processName+" ") {
Kill(pid)
return
}
log.Debugf("pid did not relate to expected process, not killing out of caution")
return
}
// no such process
log.Debugf("no such process %v", pid)
}
// Kill a process based on pid
func Kill(pid uint64) {
log.Debugf("killing: %v", pid)
bytes, err := exec.Command("kill", strconv.Itoa(int(pid))).Output()
if err == nil {
log.Debugf("kill %v successful: %s", pid, bytes)
} else {
// this might now always succeed
log.Debugf("could not kill pid: %v %v", pid, err)
}
return
}