From dcca8653a54b5a03b0234238dbc6388f6b59adc3 Mon Sep 17 00:00:00 2001
From: ed neville <ed@s5h.net>
Date: Fri, 17 Dec 2021 14:29:48 +0000
Subject: [PATCH] script to kill subjects processes from userdel
 
Closes #404
Closes #317
 
Signed-off-by: ed neville <ed@s5h.net>
 
Conflict: NA
Reference: https://github.com/shadow-maint/shadow/commit/dcca8653a54b5a03b0234238dbc6388f6b59adc3
---
 .../userdel-pre.d/01-kill_user_procs.sh       | 28 +++++++++++++++++++
 1 file changed, 28 insertions(+)
 create mode 100755 etc/shadow-maint/userdel-pre.d/01-kill_user_procs.sh
 
diff --git a/etc/shadow-maint/userdel-pre.d/01-kill_user_procs.sh b/etc/shadow-maint/userdel-pre.d/01-kill_user_procs.sh
new file mode 100755
index 00000000..ca481b1b
--- /dev/null
+++ b/etc/shadow-maint/userdel-pre.d/01-kill_user_procs.sh
@@ -0,0 +1,28 @@
+#!/bin/sh
+
+PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
+
+# Check user exists, and if so, send sigkill to processes that the user owns
+
+RUNNING=`ps -eo user | grep -Fx "$SUBJECT" | wc -l`
+
+# if the user does not exist, RUNNING will be 0
+
+if [ "${RUNNING}x" = "0x" ]; then
+  exit 0
+fi
+
+ls -1 /proc | while IFS= read -r PROC; do
+  echo "$PROC" | grep -E '^[0-9]+$' >/dev/null
+  if [ $? -ne 0 ]; then
+    continue
+  fi
+  if [ -d "/proc/${PROC}" ]; then
+    USR=`stat -c "%U" /proc/${PROC}`
+    if [ "${USR}" = "${SUBJECT}" ]; then
+      echo "Killing ${SUBJECT} owned ${PROC}"
+      kill -9 "${PROC}"
+    fi
+  fi
+done
+
-- 
2.27.0

