3 # Copyright (c) 1995-1996 Wolfram Schneider <wosch@FreeBSD.org>. Berlin.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 # killall - kill processes by name
32 $ENV{'PATH'} = '/bin:/usr/bin'; # security
34 $signal = 'SIGTERM'; # default signal for kill
36 $match = 0; # 0 match exactly program name
37 $show = 0; # do nothings
39 # see /sys/miscfs/procfs/procfs_status.c
46 die "killall [-?|-help] [-d] [-l] [-m] [-s] [-SIGNAL] procname ...\n";
49 $id = $<; # real uid of this process / your id
50 while ($_ = $ARGV[0], /^-/) {
52 if (/^--$/) { $_ = $ARGV[0]; last }
53 elsif (/^-(h|help|\?)$/) { &usage }
54 elsif (/^-[dv]$/) { $debug++ }
55 elsif (/^-l$/) { exec 'kill', '-l' }
56 elsif (/^-m$/) { $match = 1 }
57 elsif (/^-s$/) { $show = 1 }
58 elsif (/^-([a-z][a-z0-9]+|[0-9]+)$/i) { $signal = $1 }
59 elsif (/^-/) { &usage }
62 &usage if $#ARGV < 0; # no arguments
63 die "Maybe $procfs is not mounted\n" unless -e "$procfs/0/status";
65 opendir(PROCFS, "$procfs") || die "$procfs $!\n";
66 print " PID EUID RUID COMMAND\n" if $debug > 1;
71 foreach (sort{$a <=> $b} grep(/^[0-9]/, readdir(PROCFS))) {
72 $status = "$procfs/$_/status";
74 next if $pid == $$; # don't kill yourself
76 open(STATUS, "$status") || next; # process maybe already terminated
80 printf "%5d %5d %5d %s\n", $pid, $proc[$PROC_EUID],
81 $proc[$PROC_RUID], $proc[$PROC_NAME] if $debug > 1;
83 foreach $program (@ARGV) {
84 # quote meta characters
85 ($programMatch = $program) =~ s/(\W)/\\$1/g if $match;
88 if ($proc[$PROC_NAME] eq $program ||
89 ($match && $proc[$PROC_NAME] =~ /$programMatch/i)) {
92 if ($proc[$PROC_EUID] eq $id || # effective uid
93 $proc[$PROC_RUID] eq $id || # real uid
96 push(@kill, $pid) if !$pidu{"$pid"};
98 $thiskill{"$program"}++;
101 # process exists, but does not belong to you
103 $notkillable{"$program"}++;
113 foreach $program (@ARGV) {
114 if (!$thiskill{"$program"}) {
115 print STDERR "No processes matching ``$program''";
116 print STDERR " belong to you" if $notkillable{"$program"};
123 exit(1) if $#kill < 0;
125 $signal =~ y/a-z/A-Z/; # signal name in upper case
126 $signal =~ s/^SIG//; # strip a leading SIG if present
127 print "kill -$signal @kill\n" if $debug || $show;
129 $cnt = kill ($signal, @kill) unless $show; # kill processes
130 exit(0) if $show || $cnt == $#kill + 1;