File: slay

package info (click to toggle)
slay 2.4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 76 kB
  • ctags: 1
  • sloc: sh: 167; makefile: 38
file content (161 lines) | stat: -rwxr-xr-x 3,228 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/sh
#
# slay 2.0 - kill all processes belonging to the specified user(s).
# originally by Chris Ausbrooks <fish@bucket.ualr.edu> 
# based on kall (a script of unknown origin)
# Heavily rewritten by Pawel Wiecek <coven@debian.org> for Debian

# Revision history:
# 0.99	First attempt.
# 1.0	Added Butthead.
# 1.1	Added retribution.
# 1.2	Added slayee notification.
# 2.0   Completely rewritten
# 2.1	Fix an *ugly* bug that caused slayer to be slain...

USER=`whoami`
SIGNAL='-KILL'
SLAYEE=''
ME=`basename $0`
COOL='0'

# this piece of nested ifs is added for Debian package only
if [ -f /etc/slay_mode ]
then
  if grep -q mean /etc/slay_mode
  then
    MODE='mean'
  fi
  if grep -q nice /etc/slay_mode
  then
    MODE='nice'
  fi
  if [ -z $SLAY_BUTTHEAD ]
  then
    if grep -q butthead /etc/slay_mode
    then
      SLAY_BUTTHEAD='on'
    fi
    if grep -q normal /etc/slay_mode
    then
      SLAY_BUTTHEAD='off'
    fi
  fi
else
  MODE='mean'
  if [ -z $SLAY_BUTTHEAD ]
  then
    SLAY_BUTTHEAD='off'
  fi
fi

# Command line handling.
while [ $# -gt 0 ]
do
 case $1 in
  -*)
   SIGNAL=$1
   ;;
  *)
   SLAYEE="$SLAYEE $1"
 esac
 shift
done

if [ "$SIGNAL" != "-clean" ]
then
  SIGSHOW="$SIGNAL"
else
  SIGSHOW="-TERM + -KILL"
fi

# Help for loosers.
if [ "$SLAYEE" = "" -o "$SIGNAL" = "--help" ]
then
  echo "usage: $ME [-signal] name [name...]"
  if [ "$SLAY_BUTTHEAD" = "on" ]
  then
    echo "       Like, kills people and stuff."
    echo "       With -clean kicks ass forst and then does real pain."
  else
    echo "       Kills all processes belonging to any of the given names."
    echo "       Use -clean as a signal name to kill with TERM first and then with KILL."
  fi
  exit -1 
fi

# Misuse trap.
if [ "$USER" != "$1" ]
then
  if [ "$USER" != "root" ]
  then
    if [ "$MODE" = "mean" ]
    then
      $0 -KILL $USER
    else
      if [ "$SLAY_BUTTHEAD" = "on" ]
      then
        echo "${ME}: Cut it out."
      else
        echo "${ME}: Only root gets to do that."
      fi
    fi
    exit 2
  fi
fi

# Main body.
for slayee in $SLAYEE
do
  if [ "$slayee" = "$USER" ]
  then
    if [ "$SLAY_BUTTHEAD" = "on" ]
    then
      echo "${ME}: Beavis, don't make me have to smack you."
    else
      echo "${ME}: Illegal operation."
    fi
  fi
  COOL="1"
  if [ "$SLAY_BUTTHEAD" = "on" ]
  then
    echo "${ME}: $SIGSHOW is kicking $slayee's butt!"
    echo -e "\\n\\n\\nI'm kicking your butt.\\n\\n\\n" | write $slayee 2>/dev/null
  else
    echo "${ME}: Sending $SIGSHOW signal to $slayee's process(es)..."
    echo -e "\\n\\n\\nYour current session has been terminated.\\n\\n\\n" | \
    	write $slayee 2>/dev/null
  fi
  if [ "$SIGNAL" = "-clean" ]
  then
    su -m $slayee -c "kill -TERM -1 2>/dev/null"
    sleep 10
    su -m $slayee -c "kill -KILL -1 2>/dev/null"
  else
    su -m $slayee -c "kill $SIGNAL -1 2>/dev/null"
  fi
done

# Error message.
if [ $COOL = "0" ]
then
  if [ "$SLAY_BUTTHEAD" = "on" ]
  then
    echo "${ME}: How old are you, Beavis?"
  else
    echo "${ME}: Nothing done."
  fi
  exit 1
fi

# Non-error message.
if [ $COOL = "1" ]
then
  if [ "$SLAY_BUTTHEAD" = "on" ]
  then
    echo "${ME}: Whoa, I have the power supreme."
  else
    echo "${ME}: Done."
  fi
  exit 0
fi