1
by brian
clean slate |
1 |
#!@PERL@
|
2 |
# Copyright (C) 2000-2002, 2004 MySQL AB
|
|
3 |
#
|
|
4 |
# This program is free software; you can redistribute it and/or modify
|
|
5 |
# it under the terms of the GNU General Public License as published by
|
|
6 |
# the Free Software Foundation; version 2 of the License.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
16 |
||
17 |
# This is a utility for MySQL. It is not needed by any standard part
|
|
18 |
# of MySQL.
|
|
19 |
||
20 |
# Usage: mysql_zap [-signal] [-f] [-t] pattern
|
|
21 |
||
22 |
# Configuration parameters.
|
|
23 |
||
24 |
$sig = ""; # Default to try all signals |
|
25 |
$ans = "y"; |
|
26 |
$opt_f= 0; |
|
27 |
$opt_t= 0; |
|
28 |
$opt_a = ""; |
|
29 |
||
30 |
$BSD = -f '/vmunix' || $ENV{"OS"} eq "SunOS4" || $^O eq 'darwin'; |
|
31 |
$LINUX = $^O eq 'linux'; |
|
32 |
$pscmd = $BSD ? "/bin/ps -auxww" : $LINUX ? "/bin/ps axuw" : "/bin/ps -ef"; |
|
33 |
||
34 |
open(TTYIN, "</dev/tty") || die "can't read /dev/tty: $!"; |
|
35 |
open(TTYOUT, ">/dev/tty") || die "can't write /dev/tty: $!"; |
|
36 |
select(TTYOUT); |
|
37 |
$| = 1; |
|
38 |
select(STDOUT); |
|
39 |
$SIG{'INT'} = 'cleanup'; |
|
40 |
||
41 |
while ($#ARGV >= $[ && $ARGV[0] =~ /^-/) { |
|
42 |
if ($ARGV[0] =~ /(ZERO|HUP|INT|QUIT|ILL|TRAP|ABRT|EMT|FPE|KILL|BUS|SEGV|SYS|PIPE|ALRM|TERM|URG|STOP|TSTP|CONT|CLD|TTIN|TTOU|IO|XCPU|XFSZ|VTALRM|PROF|WINCH|LOST|USR1|USR2)/ || $ARGV[0] =~ /-(\d+)$/) { |
|
43 |
$sig = $1; |
|
44 |
} elsif ($ARGV[0] eq "-f") { |
|
45 |
$opt_f=1; |
|
46 |
} elsif ($ARGV[0] eq "-t") { |
|
47 |
$opt_t=1; |
|
48 |
$ans = "n"; |
|
49 |
}
|
|
50 |
elsif ($ARGV[0] eq "-a") |
|
51 |
{
|
|
52 |
$opt_a = 1; |
|
53 |
}
|
|
54 |
elsif ($ARGV[0] eq "-?" || $ARGV[0] eq "-I" || $ARGV[0] eq "--help") |
|
55 |
{
|
|
56 |
&usage; |
|
57 |
}
|
|
58 |
else { |
|
59 |
print STDERR "$0: illegal argument $ARGV[0] ignored\n"; |
|
60 |
}
|
|
61 |
shift;
|
|
62 |
}
|
|
63 |
||
64 |
&usage if $#ARGV < 0; |
|
65 |
||
66 |
if (!$opt_f) |
|
67 |
{
|
|
68 |
if ($BSD) { |
|
69 |
system "stty cbreak </dev/tty >/dev/tty 2>&1"; |
|
70 |
}
|
|
71 |
else { |
|
72 |
system "stty", 'cbreak', |
|
73 |
system "stty", 'eol', '^A'; |
|
74 |
}
|
|
75 |
}
|
|
76 |
||
77 |
open(PS, "$pscmd|") || die "can't run $pscmd: $!"; |
|
78 |
$title = <PS>; |
|
79 |
print TTYOUT $title; |
|
80 |
||
81 |
# Catch any errors with eval. A bad pattern, for instance.
|
|
82 |
eval <<'EOF'; |
|
83 |
process: while ($cand = <PS>)
|
|
84 |
{
|
|
85 |
chop($cand);
|
|
86 |
($user, $pid) = split(' ', $cand);
|
|
87 |
next if $pid == $$;
|
|
88 |
$found = !@ARGV;
|
|
89 |
if ($opt_a) { $found = 1; }
|
|
90 |
foreach $pat (@ARGV)
|
|
91 |
{
|
|
92 |
if ($opt_a)
|
|
93 |
{
|
|
94 |
if (! ($cand =~ $pat))
|
|
95 |
{
|
|
96 |
next process;
|
|
97 |
}
|
|
98 |
}
|
|
99 |
else
|
|
100 |
{
|
|
101 |
$found = 1 if $cand =~ $pat;
|
|
102 |
}
|
|
103 |
}
|
|
104 |
next if (!$found);
|
|
105 |
if (! $opt_f && ! $opt_t)
|
|
106 |
{
|
|
107 |
print TTYOUT "$cand? ";
|
|
108 |
read(TTYIN, $ans, 1);
|
|
109 |
print TTYOUT "\n" if ($ans ne "\n");
|
|
110 |
}
|
|
111 |
else
|
|
112 |
{
|
|
113 |
print TTYOUT "$cand\n";
|
|
114 |
}
|
|
115 |
if ($ans =~ /^y/i) { &killpid($sig, $pid); }
|
|
116 |
if ($ans =~ /^q/i) { last; }
|
|
117 |
}
|
|
118 |
EOF
|
|
119 |
||
120 |
&cleanup; |
|
121 |
||
122 |
||
123 |
sub usage {
|
|
124 |
print <<EOF;
|
|
125 |
Usage: $0 [-signal] [-?Ift] [--help] pattern
|
|
126 |
Options: -I or -? "info" -f "force" -t "test".
|
|
127 |
||
128 |
Version 1.0
|
|
129 |
Kill processes that match the pattern.
|
|
130 |
If -f isn't given, ask user for confirmation for each process to kill.
|
|
131 |
If signal isn't given, try first with signal 15, then with signal 9.
|
|
132 |
If -t is given, the processes are only shown on stdout.
|
|
133 |
EOF
|
|
134 |
exit(1); |
|
135 |
}
|
|
136 |
||
137 |
sub cleanup {
|
|
138 |
if ($BSD) { |
|
139 |
system "stty -cbreak </dev/tty >/dev/tty 2>&1"; |
|
140 |
}
|
|
141 |
else { |
|
142 |
system "stty", 'icanon'; |
|
143 |
system "stty", 'eol', '^@'; |
|
144 |
}
|
|
145 |
print "\n"; |
|
146 |
exit;
|
|
147 |
}
|
|
148 |
||
149 |
sub killpid {
|
|
150 |
local($signal,$pid) = @_; |
|
151 |
if ($signal) |
|
152 |
{
|
|
153 |
kill $signal,$pid; |
|
154 |
}
|
|
155 |
else
|
|
156 |
{
|
|
157 |
print "kill -15\n"; |
|
158 |
kill 15, $pid; |
|
159 |
for (1..5) { |
|
160 |
sleep 2; |
|
161 |
return if kill(0, $pid) == 0; |
|
162 |
}
|
|
163 |
print "kill -9\n"; |
|
164 |
kill 9, $pid; |
|
165 |
for (1..5) { |
|
166 |
sleep 2; |
|
167 |
return if kill(0, $pid) == 0; |
|
168 |
}
|
|
169 |
print "$pid will not die!\n"; |
|
170 |
}
|
|
171 |
}
|