1995-06-25 18:08:27 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
#
|
1996-02-03 22:52:27 +00:00
|
|
|
# Copyright (c) 1995 Wolfram Schneider, Berlin
|
|
|
|
# All rights reserved. Alle Rechte vorbehalten.
|
1995-06-25 18:08:27 +00:00
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions
|
|
|
|
# are met:
|
|
|
|
# 1. Redistributions of source code must retain the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer.
|
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# 3. All advertising materials mentioning features or use of this software
|
|
|
|
# must display the following acknowledgement:
|
|
|
|
# This product includes software developed by Wolfram Schneider
|
|
|
|
# 4. The name of the author may not be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written permission
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
#
|
1996-02-03 22:52:27 +00:00
|
|
|
# killall - kill processes by name
|
1995-06-25 18:08:27 +00:00
|
|
|
#
|
1996-05-30 22:04:09 +00:00
|
|
|
# $Id: $
|
1995-06-25 18:08:27 +00:00
|
|
|
#
|
|
|
|
|
1996-02-03 22:52:27 +00:00
|
|
|
$ENV{'PATH'} = '/bin:/usr/bin'; # security
|
1995-06-25 18:08:27 +00:00
|
|
|
$procfs = '/proc';
|
|
|
|
$signal = 'SIGTERM'; # default signal for kill
|
|
|
|
$debug = 0;
|
|
|
|
$match = 0; # 0 match exactly program name
|
1996-02-03 22:52:27 +00:00
|
|
|
$show = 0; # do nothings
|
1995-06-25 18:08:27 +00:00
|
|
|
|
1996-02-03 22:52:27 +00:00
|
|
|
# see /sys/miscfs/procfs/procfs_status.c
|
|
|
|
$PROC_NAME = 0;
|
|
|
|
$PROC_EUID = 11;
|
|
|
|
$PROC_RUID = 12;
|
1995-06-25 18:08:27 +00:00
|
|
|
|
|
|
|
sub usage {
|
|
|
|
$! = 2;
|
1996-05-30 22:04:09 +00:00
|
|
|
die "killall [-?|-help] [-d] [-l] [-m] [-s] [-SIGNAL] procname ...\n";
|
1995-06-25 18:08:27 +00:00
|
|
|
}
|
|
|
|
|
1996-02-03 22:52:27 +00:00
|
|
|
$id = $<; # real uid of this process / your id
|
1995-06-25 18:08:27 +00:00
|
|
|
while ($_ = $ARGV[0], /^-/) {
|
|
|
|
shift @ARGV;
|
1996-02-03 22:52:27 +00:00
|
|
|
if (/^--$/) { $_ = $ARGV[0]; last }
|
|
|
|
elsif (/^-(h|help|\?)$/) { &usage }
|
|
|
|
elsif (/^-[dv]$/) { $debug++ }
|
|
|
|
elsif (/^-l$/) { exec 'kill', '-l' }
|
1995-06-25 18:08:27 +00:00
|
|
|
elsif (/^-m$/) { $match = 1 }
|
1996-02-03 22:52:27 +00:00
|
|
|
elsif (/^-s$/) { $show = 1 }
|
1996-01-22 08:06:13 +00:00
|
|
|
elsif (/^-([a-z][a-z0-9]+|[0-9]+)$/i) { $signal = $1 }
|
1996-02-03 22:52:27 +00:00
|
|
|
elsif (/^-/) { &usage }
|
1995-06-25 18:08:27 +00:00
|
|
|
}
|
|
|
|
|
1996-05-30 22:04:09 +00:00
|
|
|
&usage if $#ARGV < 0; # no arguments
|
1995-06-25 18:08:27 +00:00
|
|
|
die "Maybe $procfs is not mounted\n" unless -e "$procfs/0/status";
|
1996-05-30 22:04:09 +00:00
|
|
|
|
|
|
|
while ($program = $ARGV[0]) {
|
|
|
|
shift @ARGV;
|
|
|
|
$thiskill = 0;
|
|
|
|
|
|
|
|
opendir(PROCFS, "$procfs") || die "$procfs $!\n";
|
|
|
|
print " PID EUID RUID COMMAND\n" if $debug > 1;
|
|
|
|
|
|
|
|
# quote meta characters
|
|
|
|
($programMatch = $program) =~ s/(\W)/\\$1/g;
|
|
|
|
|
|
|
|
foreach (sort{$a <=> $b} grep(/^[0-9]/, readdir(PROCFS))) {
|
|
|
|
$status = "$procfs/$_/status";
|
|
|
|
$pid = $_;
|
|
|
|
next if $pid == $$; # don't kill yourself
|
|
|
|
|
|
|
|
open(STATUS, "$status") || next; # process maybe already terminated
|
|
|
|
while(<STATUS>) {
|
|
|
|
@proc = split;
|
|
|
|
|
|
|
|
printf "%5d %5d %5d %s\n", $pid, $proc[$PROC_EUID],
|
|
|
|
$proc[$PROC_RUID], $proc[$PROC_NAME] if $debug > 1;
|
|
|
|
|
|
|
|
if ( # match program name
|
|
|
|
($proc[$PROC_NAME] eq $program ||
|
|
|
|
($match && $proc[$PROC_NAME] =~ /$programMatch/oi)
|
|
|
|
) &&
|
|
|
|
# id test
|
|
|
|
($proc[$PROC_EUID] eq $id || # effective uid
|
|
|
|
$proc[$PROC_RUID] eq $id || # real uid
|
|
|
|
!$id)) # root
|
|
|
|
{
|
|
|
|
push(@kill, $pid);
|
|
|
|
$thiskill++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close STATUS;
|
1995-06-25 18:08:27 +00:00
|
|
|
}
|
1996-05-30 22:04:09 +00:00
|
|
|
closedir PROCFS;
|
1995-06-25 18:08:27 +00:00
|
|
|
|
1996-05-30 22:04:09 +00:00
|
|
|
# nothing found
|
|
|
|
warn "No matching processes ``$program''\n" unless $thiskill;
|
1995-06-25 18:08:27 +00:00
|
|
|
}
|
1996-02-03 22:52:27 +00:00
|
|
|
|
1996-05-30 22:04:09 +00:00
|
|
|
# nothing found
|
|
|
|
exit(1) if $#kill < 0;
|
|
|
|
|
1996-02-03 22:52:27 +00:00
|
|
|
$signal =~ y/a-z/A-Z/; # signal name in upper case
|
1995-06-25 18:08:27 +00:00
|
|
|
$signal =~ s/^SIG//; # strip a leading SIG if present
|
|
|
|
print "kill -$signal @kill\n" if $debug || $show;
|
|
|
|
|
|
|
|
$cnt = kill ($signal, @kill) unless $show; # kill processes
|
|
|
|
exit(0) if $show || $cnt == $#kill + 1;
|
|
|
|
exit(1);
|