From ddec3100b936943cdb6ce3f51864d14dba0b46e3 Mon Sep 17 00:00:00 2001
From: x7upLime <x7uplime@gmail.com>
Date: Tue, 7 Mar 2023 09:27:34 +0100
Subject: [PATCH] Ensures sigkilling only minikube started processes

Adds a helper function to check that the executable name
is minikube.
---
 cmd/minikube/cmd/delete.go | 34 ++++++++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 8 deletions(-)

diff --git a/cmd/minikube/cmd/delete.go b/cmd/minikube/cmd/delete.go
index 02055e7e65..02cab5956c 100644
--- a/cmd/minikube/cmd/delete.go
+++ b/cmd/minikube/cmd/delete.go
@@ -651,17 +651,15 @@ func killProcess(path string) error {
 
 // trySigKillProcess takes a PID as argument and tries to SIGKILL it
 func trySigKillProcess(pid int) error {
-	entry, err := ps.FindProcess(pid)
+	itDoes, err := doesPIDBelongToMinikube(pid)
 	if err != nil {
-		return errors.Wrap(err, fmt.Sprintf("ps.FindProcess for %d", pid))
-	}
-	if entry == nil {
-		klog.Infof("Stale pid: %d", pid)
-		return errors.Wrap(err, fmt.Sprintf("removing stale pid: %d", pid))
+		return err
+	}
+
+	if !itDoes {
+		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)
 	if err != nil {
 		return errors.Wrap(err, fmt.Sprintf("os.FindProcess: %d", pid))
@@ -676,6 +674,26 @@ func trySigKillProcess(pid int) error {
 	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
 // one or more space separated pids
 func GetPids(path string) ([]int, error) {