]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
script to kill subjects processes from userdel 460/head
authored neville <ed@s5h.net>
Fri, 17 Dec 2021 14:29:48 +0000 (14:29 +0000)
committered neville <ed@s5h.net>
Wed, 29 Dec 2021 20:43:38 +0000 (20:43 +0000)
Closes #404
Closes #317

Signed-off-by: ed neville <ed@s5h.net>
etc/shadow-maint/userdel-pre.d/01-kill_user_procs.sh [new file with mode: 0755]

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 (executable)
index 0000000..ca481b1
--- /dev/null
@@ -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
+