This repository has been archived by the owner on Dec 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
reaver
executable file
·291 lines (270 loc) · 7.29 KB
/
reaver
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/perl -w
#
# reaver: Kill any processes that are not owned by users with jobs assigned
# to this node.
# Copyright 2006, 2007, 2011, 2012, 2015 Ohio Supercomputer Center
#
# License: GNU GPL v2; see ../COPYING for details.
# Revision info:
# $HeadURL$
# $Revision$
# $Date$
#
# Originally intended to be run inside a PBS job epilogue:
# all -j$PBS_JOBID -p /usr/local/sbin/reaver
# where jobid = (jobid to clean up)
#
# Why is this called "reaver" and not "reaper"? Because I like
# the TV show "Firefly", that's why. :) --troy
#
use strict;
use POSIX();
use Getopt::Long;
use diagnostics;
# Turn off output buffering so that "sleep" calls don't cause a delay
# on standard output
$|=1;
# uids below this are assumed to be system accounts
my $sysuid_threshold=100;
# regexp of usernames with uids above $sysuid_threshold that are allowed
# to have processes running on a node without a PBS job
my $safelist="arraysvcs|oscmon|decypher|rtkit|nagios|polkitd";
# Should we attempt to kill strays? (0 = no, 1 = yes)
my $kill_mode = 0;
# Clean up after a particular job (i.e. don't whitelist its owner)
my $reap_job;
# Output is either brief, normal, or verbose. In brief mode, we output only
# the PIDs for stray processes (one per line). Normal mode displays "ps"
# listing for stray procs. Verbose mode is same as brief mode with extras.
my $brief_mode = 0;
my $verbose_mode = 0;
my $long_ps_mode = 1;
# Usage/help functions
my $USAGE = "$0 [-bchklv] [-d <spooldir>] [-j <jobid>]";
my $HELP = "Options:
-b, --brief
Display only the stray process IDs
-c, --classic
Display \"classic\" summary of process information
-d <spooldir>, --spool=<spooldir>
Use <spooldir> as the PBS spool directory; the default is \$PBS_HOME
-h, --help
Print detailed help screen
-j <jobid>, --job=<jobid>
Clean up processes associated with <jobid>
-k, --kill
Attempt to kill all stray processes
-l, --long
Display long summary of process information (default)
-v, --verbose
Display extra info
";
# Find $PBS_HOME
my $spool = undef;
if ( defined($ENV{"PBS_HOME"}) )
{
$spool=$ENV{"PBS_HOME"};
}
else
{
my @defaults=("/usr/spool/PBS",
"/var/spool/pbs",
"/var/spool/torque",
"/var/spool/batch/pbs",
"/var/spool/batch/torque",
"/var/spool/batch/pbs-piv",
"/var/spool/batch/pbs-ipf",
".",
);
foreach my $dir ( @defaults )
{
if ( -d $dir )
{
$spool=$dir;
last;
}
}
}
if ( !defined($spool) )
{
die "Unable to find PBS spool directory!\n";
}
sub print_usage {
print "Usage: $USAGE\n";
}
sub print_help {
print "Usage: $USAGE\n";
print "$HELP\n";
}
sub usage_error {
my $MSG = shift || "";
print $MSG;
print_usage();
exit(1);
}
# Process command line options
my ($opt_b, $opt_c, $opt_d, $opt_h, $opt_j, $opt_k, $opt_l, $opt_v);
Getopt::Long::Configure('bundling');
GetOptions(
"b" => \$opt_b, "brief" => \$opt_b,
"c" => \$opt_c, "classic" => \$opt_c,
"d=s"=> \$opt_d, "spool=s" => \$opt_d,
"h" => \$opt_h, "help" => \$opt_h,
"j=s"=> \$opt_j, "job=s" => \$opt_j,
"k" => \$opt_k, "kill" => \$opt_k,
"l" => \$opt_l, "long" => \$opt_l,
"v" => \$opt_v, "verbose" => \$opt_v
) || usage_error();
if ($opt_h) { print_help(); exit(0); }
if ($opt_b) { $brief_mode = 1; }
if ($opt_c) { $long_ps_mode = 0; }
if ($opt_d) { $spool = $opt_d; }
if ($opt_j)
{
$reap_job = $opt_j;
$reap_job =~ s/\..*$//;
}
if ($opt_k) { $kill_mode = 1; }
if ($opt_l) { $long_ps_mode = 1; }
if ($opt_v) { $verbose_mode = 1; $brief_mode = 0; }
# Make sure $spool and subdirs actually exist
die "$spool does not exist!" if ( ! -d "$spool" );
die "$spool/mom_priv does not exist!" if ( ! -d "$spool/mom_priv" );
die "$spool/mom_priv/jobs does not exist!" if ( ! -d "$spool/mom_priv/jobs" );
# Append to $safelist the names of all users who have jobs currently running
# on this host
# The following approach doesn't put any load on the PBS server, but forces
# reaver to be run by root
my $ptn = "$spool/mom_priv/jobs/*.JB";
if ( glob($ptn) )
{
foreach my $jobfile (glob($ptn))
{
if ( ! defined($reap_job) ||
! $jobfile=~/\/$reap_job\..*\.JB$/ ||
! $jobfile=~/\/$reap_job\.JB$/ )
{
my $jobuserlist;
# TORQUE 4(?) and after have XML .JB files; earlier versions
# are binary
my $isxml = `test -e $jobfile && head -1 $jobfile | grep -ic xml`;
chomp($isxml);
if ( defined($isxml) && $isxml ne "" && int($isxml)==1 )
{
$jobuserlist = `test -e $jobfile && grep euser $jobfile | sed 's/ *<euser [A-Za-z0-9=\"]*>//g' | sed 's:</euser>::'`;
}
else
{
$jobuserlist = `test -e $jobfile && cat $jobfile | strings | grep -A 1 euser | grep -v euser | sort | uniq | tr '\n' '|'`;
}
chomp($jobuserlist);
if ( $jobuserlist ne "" )
{
if ( $safelist eq "" )
{
$safelist = $jobuserlist;
}
else
{
$safelist .= "|".$jobuserlist;
}
}
}
}
}
# Append to $safelist the owner of /dev/console
my $consoleowner = `stat -c %U /dev/console`;
chop($consoleowner);
if ( $consoleowner ne "root" )
{
if ( $safelist eq "" )
{
$safelist = $consoleowner;
}
else
{
$safelist .= "|".$consoleowner;
}
}
if ( $safelist ne "" )
{
print "Safe users: $safelist\n" if $verbose_mode;
}
# find all the target pids
my @pids=();
if ( $long_ps_mode )
{
open(PS,"ps -w -A -o pid,%cpu,%mem,state,uid,user:24,lstart,command |");
}
else
{
open(PS,"ps -A -o pid,state,uid,user:24,command |");
}
# snarf the 1st line
<PS>;
print "Stray processes:\n" if $verbose_mode;
while (<PS>)
{
my ($pid,$p_cpu,$p_mem,$state,$uid,$user,$lstart,$command);
chomp;
if ($long_ps_mode )
{
($pid,$p_cpu,$p_mem,$state,$uid,$user,$lstart,$command) = split(' ', $_, 8);
}
else
{
($pid,$state,$uid,$user,$command) = split(' ', $_, 5);
}
if ( ($uid > $sysuid_threshold) && !($user =~ /^($safelist)$/) && ($uid!=$>) )
{
# make sure pid isn't in a torque cgroup
# if it is, don't add it to the pid list
my $in_torque_cgroup = 0;
if ( -r "/proc/$pid/cgroup" )
{
my $x = `grep -c /torque/ /proc/$pid/cgroup`;
chomp($x);
$in_torque_cgroup = int($x);
}
if ( $in_torque_cgroup==0 )
{
push(@pids,$pid);
if ($brief_mode)
{
print "$pid\n";
}
else
{
if ($long_ps_mode )
{
print $pid," ",$p_cpu," ",$p_mem," ",$state," ",$user," ",$lstart," ",$command,"\n";
}
else
{
printf "%8d %-3s %-9.9s %-10.50s\n", $pid, $state, $user, $command;
}
}
}
}
}
if ($verbose_mode)
{
print " (none)\n" unless @pids;
}
close(PS);
die "ps failure" if ( ($?>>8)!=0 );
# pull the trigger
if ( @pids && $kill_mode)
{
# terminate-with-extreme-prejudice version
# the CONT signal is needed to reawaken processes that are
# in a breakpoint in a debugger
print "Sending SIGCONT...\n" if $verbose_mode;
kill(&POSIX::SIGCONT,@pids);
sleep(1);
print "Sending SIGTERM...\n" if $verbose_mode;
kill(&POSIX::SIGTERM,@pids);
sleep(5);
print "Sending SIGKILL...\n" if $verbose_mode;
kill(&POSIX::SIGKILL,@pids);
}