• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2#
3# slay 2.0 - kill all processes belonging to the specified user(s).
4# originally by Chris Ausbrooks <fish@bucket.ualr.edu>
5# based on kall (a script of unknown origin)
6# Heavily rewritten by Pawel Wiecek <coven@debian.org> for Debian
7# This is a free software distributed according to terms of GNU GPL
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
18USER=`whoami`
19SIGNAL=`echo $1 | grep '^\-.*'`
20ME=`basename $0`
21COOL='0'
22
23# this piece of nested ifs is added for Debian package only
24if [ -f /etc/slay_mode ]
25then
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
45else
46  MODE='mean'
47  if [ -z $SLAY_BUTTHEAD ]
48  then
49    SLAY_BUTTHEAD='off'
50  fi
51fi
52
53# Command line handling.
54if [ "$SIGNAL" != "" ]
55then
56  shift
57else
58  SIGNAL="-KILL"
59fi
60
61if [ "$SIGNAL" != "-clean" ]
62then
63  SIGSHOW="$SIGNAL"
64else
65  SIGSHOW="-TERM + -KILL"
66fi
67
68# Help for loosers.
69if [ "$1" = "" -o "$1" = "--help" ]
70then
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
80  exit -1
81fi
82
83# Misuse trap.
84if [ "$USER" != "$1" ]
85then
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
101fi
102
103# Main body.
104while [ "$1" != "" ]
105do
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
134done
135
136# Error message.
137if [ $COOL = "0" ]
138then
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
146fi
147
148# Non-error message.
149if [ $COOL = "1" ]
150then
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
158fi
159