Ensures sigkilling only minikube started processes

Adds a helper function to check that the executable name
is minikube.
pull/15782/head
x7upLime 2023-03-07 09:27:34 +01:00
parent b5e59b5db4
commit ddec3100b9
1 changed files with 26 additions and 8 deletions

View File

@ -651,17 +651,15 @@ func killProcess(path string) error {
// trySigKillProcess takes a PID as argument and tries to SIGKILL it // trySigKillProcess takes a PID as argument and tries to SIGKILL it
func trySigKillProcess(pid int) error { func trySigKillProcess(pid int) error {
entry, err := ps.FindProcess(pid) itDoes, err := doesPIDBelongToMinikube(pid)
if err != nil { if err != nil {
return errors.Wrap(err, fmt.Sprintf("ps.FindProcess for %d", pid)) return err
} }
if entry == nil {
klog.Infof("Stale pid: %d", pid) if !itDoes {
return errors.Wrap(err, fmt.Sprintf("removing stale pid: %d", pid)) return fmt.Errorf("Stale pid: %d", pid)
} }
// We found a process, but it still may not be ours.
klog.Infof("Found process %d: %s", pid, entry.Executable())
proc, err := os.FindProcess(pid) proc, err := os.FindProcess(pid)
if err != nil { if err != nil {
return errors.Wrap(err, fmt.Sprintf("os.FindProcess: %d", pid)) return errors.Wrap(err, fmt.Sprintf("os.FindProcess: %d", pid))
@ -676,6 +674,26 @@ func trySigKillProcess(pid int) error {
return nil return nil
} }
// doesPIDBelongToMinikube tries to find the process with that PID
// and checks if the executable name is "minikube"
func doesPIDBelongToMinikube(pid int) (bool, error) {
entry, err := ps.FindProcess(pid)
if err != nil {
return false, errors.Wrap(err, fmt.Sprintf("ps.FindProcess for %d", pid))
}
if entry == nil {
klog.Infof("Process not found. pid %d", pid)
return false, nil
}
klog.Infof("Found process %d", pid)
if entry.Executable() != "minikube" {
return false, nil
}
return true, nil
}
// GetPids opens the file at PATH and tries to read // GetPids opens the file at PATH and tries to read
// one or more space separated pids // one or more space separated pids
func GetPids(path string) ([]int, error) { func GetPids(path string) ([]int, error) {