From 069db8db4751c2245fa1ea0cd5ed289d86820abf Mon Sep 17 00:00:00 2001 From: Wolfram Schneider Date: Thu, 1 Jan 1998 17:24:43 +0000 Subject: [PATCH] Re-order the for loop for multiple procnames. This decrease the system load and makes a killall ppp rlogin ftp ssh ping traceroute telnet a lot faster. Remove duplicated pid's before killing (killall lynx lynx). --- usr.bin/killall/killall.pl | 56 ++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/usr.bin/killall/killall.pl b/usr.bin/killall/killall.pl index 2e8dc66c473..098f08b73a0 100755 --- a/usr.bin/killall/killall.pl +++ b/usr.bin/killall/killall.pl @@ -26,7 +26,7 @@ # # killall - kill processes by name # -# $Id$ +# $Id: killall.pl,v 1.8 1997/02/22 19:55:24 peter Exp $ $ENV{'PATH'} = '/bin:/usr/bin'; # security @@ -62,48 +62,52 @@ while ($_ = $ARGV[0], /^-/) { &usage if $#ARGV < 0; # no arguments die "Maybe $procfs is not mounted\n" unless -e "$procfs/0/status"; -while ($program = $ARGV[0]) { - shift @ARGV; - $thiskill = 0; +opendir(PROCFS, "$procfs") || die "$procfs $!\n"; +print " PID EUID RUID COMMAND\n" if $debug > 1; - opendir(PROCFS, "$procfs") || die "$procfs $!\n"; - print " PID EUID RUID COMMAND\n" if $debug > 1; +undef %pidu; +undef %thiskill; - # 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 - 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() { + @proc = split; - open(STATUS, "$status") || next; # process maybe already terminated - while() { - @proc = split; + printf "%5d %5d %5d %s\n", $pid, $proc[$PROC_EUID], + $proc[$PROC_RUID], $proc[$PROC_NAME] if $debug > 1; - printf "%5d %5d %5d %s\n", $pid, $proc[$PROC_EUID], - $proc[$PROC_RUID], $proc[$PROC_NAME] if $debug > 1; + foreach $program (@ARGV) { + # quote meta characters + ($programMatch = $program) =~ s/(\W)/\\$1/g if $match; if ( # match program name ($proc[$PROC_NAME] eq $program || - ($match && $proc[$PROC_NAME] =~ /$programMatch/oi) + ($match && $proc[$PROC_NAME] =~ /$programMatch/i) ) && # id test ($proc[$PROC_EUID] eq $id || # effective uid $proc[$PROC_RUID] eq $id || # real uid !$id)) # root { - push(@kill, $pid); - $thiskill++; + push(@kill, $pid) if !$pidu{"$pid"}; + $pidu{"$pid"} = $pid; + $thiskill{"$program"}++; } - } - close STATUS; + } } - closedir PROCFS; - - # nothing found - warn "No matching processes ``$program''\n" unless $thiskill; + close STATUS; } +closedir PROCFS; + +# nothing found +foreach $program (@ARGV) { + warn "No matching processes ``$program''\n" unless $thiskill{"$program"}; +} + # nothing found exit(1) if $#kill < 0;