robbiew | b1ca092 | 2004-02-16 15:44:27 +0000 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # slay 2.0 - kill all processes belonging to the specified user(s). |
Chris Dearman | 37550cf | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 4 | # originally by Chris Ausbrooks <fish@bucket.ualr.edu> |
robbiew | b1ca092 | 2004-02-16 15:44:27 +0000 | [diff] [blame] | 5 | # based on kall (a script of unknown origin) |
| 6 | # Heavily rewritten by Pawel Wiecek <coven@debian.org> for Debian |
Chris Dearman | 37550cf | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 7 | # This is a free software distributed according to terms of GNU GPL |
robbiew | b1ca092 | 2004-02-16 15:44:27 +0000 | [diff] [blame] | 8 | # (see /usr/share/common-licenses/GPL) |
| 9 | |
| 10 | # Revision history: |
| 11 | # 0.99 First attempt. |
| 12 | # 1.0 Added Butthead. |
| 13 | # 1.1 Added retribution. |
| 14 | # 1.2 Added slayee notification. |
| 15 | # 2.0 Completely rewritten |
| 16 | # 2.1 Fix an *ugly* bug that caused slayer to be slain... |
| 17 | |
| 18 | USER=`whoami` |
| 19 | SIGNAL=`echo $1 | grep '^\-.*'` |
| 20 | ME=`basename $0` |
| 21 | COOL='0' |
| 22 | |
| 23 | # this piece of nested ifs is added for Debian package only |
| 24 | if [ -f /etc/slay_mode ] |
| 25 | then |
| 26 | if grep -q mean /etc/slay_mode |
| 27 | then |
| 28 | MODE='mean' |
| 29 | fi |
| 30 | if grep -q nice /etc/slay_mode |
| 31 | then |
| 32 | MODE='nice' |
| 33 | fi |
| 34 | if [ -z $SLAY_BUTTHEAD ] |
| 35 | then |
| 36 | if grep -q butthead /etc/slay_mode |
| 37 | then |
| 38 | SLAY_BUTTHEAD='on' |
| 39 | fi |
| 40 | if grep -q normal /etc/slay_mode |
| 41 | then |
| 42 | SLAY_BUTTHEAD='off' |
| 43 | fi |
| 44 | fi |
| 45 | else |
| 46 | MODE='mean' |
| 47 | if [ -z $SLAY_BUTTHEAD ] |
| 48 | then |
| 49 | SLAY_BUTTHEAD='off' |
| 50 | fi |
| 51 | fi |
| 52 | |
| 53 | # Command line handling. |
| 54 | if [ "$SIGNAL" != "" ] |
| 55 | then |
| 56 | shift |
| 57 | else |
| 58 | SIGNAL="-KILL" |
| 59 | fi |
| 60 | |
| 61 | if [ "$SIGNAL" != "-clean" ] |
| 62 | then |
| 63 | SIGSHOW="$SIGNAL" |
| 64 | else |
| 65 | SIGSHOW="-TERM + -KILL" |
| 66 | fi |
| 67 | |
| 68 | # Help for loosers. |
| 69 | if [ "$1" = "" -o "$1" = "--help" ] |
| 70 | then |
| 71 | echo "usage: $ME [-signal] name [name...]" |
| 72 | if [ "$SLAY_BUTTHEAD" = "on" ] |
| 73 | then |
| 74 | echo " Like, kills people and stuff." |
| 75 | echo " With -clean kicks ass forst and then does real pain." |
| 76 | else |
| 77 | echo " Kills all processes belonging to any of the given names." |
| 78 | echo " Use -clean as a signal name to kill with TERM first and then with KILL." |
| 79 | fi |
Chris Dearman | 37550cf | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 80 | exit -1 |
robbiew | b1ca092 | 2004-02-16 15:44:27 +0000 | [diff] [blame] | 81 | fi |
| 82 | |
| 83 | # Misuse trap. |
| 84 | if [ "$USER" != "$1" ] |
| 85 | then |
| 86 | if [ "$USER" != "root" ] |
| 87 | then |
| 88 | if [ "$MODE" = "mean" ] |
| 89 | then |
| 90 | $0 -KILL $USER |
| 91 | else |
| 92 | if [ "$SLAY_BUTTHEAD" = "on" ] |
| 93 | then |
| 94 | echo "${ME}: Cut it out." |
| 95 | else |
| 96 | echo "${ME}: Only root gets to do that." |
| 97 | fi |
| 98 | fi |
| 99 | exit 2 |
| 100 | fi |
| 101 | fi |
| 102 | |
| 103 | # Main body. |
| 104 | while [ "$1" != "" ] |
| 105 | do |
| 106 | if [ "$1" = "$USER" ] |
| 107 | then |
| 108 | if [ "$SLAY_BUTTHEAD" = "on" ] |
| 109 | then |
| 110 | echo "${ME}: Beavis, don't make me have to smack you." |
| 111 | else |
| 112 | echo "${ME}: Illegal operation." |
| 113 | fi |
| 114 | fi |
| 115 | COOL="1" |
| 116 | if [ "$SLAY_BUTTHEAD" = "on" ] |
| 117 | then |
| 118 | echo "${ME}: $SIGSHOW is kicking $1's butt!" |
| 119 | echo -e "\\n\\n\\nI'm kicking your butt.\\n\\n\\n" | write $1 2>/dev/null |
| 120 | else |
| 121 | echo "${ME}: Sending $SIGSHOW signal to $1's process(es)..." |
| 122 | echo -e "\\n\\n\\nYour current session has been terminated.\\n\\n\\n" | \ |
| 123 | write $1 2>/dev/null |
| 124 | fi |
| 125 | if [ "$SIGNAL" = "-clean" ] |
| 126 | then |
| 127 | su -m $1 -c "kill -TERM -1 2>/dev/null" |
| 128 | sleep 10 |
| 129 | su -m $1 -c "kill -KILL -1 2>/dev/null" |
| 130 | else |
| 131 | su -m $1 -c "kill $SIGNAL -1 2>/dev/null" |
| 132 | fi |
| 133 | shift |
| 134 | done |
| 135 | |
| 136 | # Error message. |
| 137 | if [ $COOL = "0" ] |
| 138 | then |
| 139 | if [ "$SLAY_BUTTHEAD" = "on" ] |
| 140 | then |
| 141 | echo "${ME}: How old are you, Beavis?" |
| 142 | else |
| 143 | echo "${ME}: Nothing done." |
| 144 | fi |
| 145 | exit 1 |
| 146 | fi |
| 147 | |
| 148 | # Non-error message. |
| 149 | if [ $COOL = "1" ] |
| 150 | then |
| 151 | if [ "$SLAY_BUTTHEAD" = "on" ] |
| 152 | then |
| 153 | echo "${ME}: Whoa, I have the power supreme." |
| 154 | else |
| 155 | echo "${ME}: Done." |
| 156 | fi |
| 157 | exit 0 |
| 158 | fi |