]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/killall/killall.pl
unfinished sblive driver, playback/mixer only for now - not enabled in
[FreeBSD/FreeBSD.git] / usr.bin / killall / killall.pl
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 1995-1996 Wolfram Schneider <wosch@FreeBSD.org>. Berlin.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
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.
14 #
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
25 # SUCH DAMAGE.
26 #
27 # killall - kill processes by name
28 #
29 # $FreeBSD$
30
31
32 $ENV{'PATH'} = '/bin:/usr/bin'; # security
33 $procfs = '/proc';
34 $signal = 'SIGTERM';            # default signal for kill
35 $debug = 0;
36 $match = 0;                     # 0 match exactly program name
37 $show = 0;                      # do nothings
38
39 # see /sys/miscfs/procfs/procfs_status.c
40 $PROC_NAME =  0;
41 $PROC_EUID = 11;
42 $PROC_RUID = 12;
43
44 sub usage {
45     $! = 2;
46     die "killall [-?|-help] [-d] [-l] [-m] [-s] [-SIGNAL] procname ...\n";
47 }
48
49 $id = $<;                       # real uid of this process / your id
50 while ($_ = $ARGV[0], /^-/) {
51     shift @ARGV;
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 }
60 }
61
62 &usage if $#ARGV < 0;           # no arguments
63 die "Maybe $procfs is not mounted\n" unless -e "$procfs/0/status";
64
65 opendir(PROCFS, "$procfs") || die "$procfs $!\n";
66 print "  PID  EUID  RUID COMMAND\n" if $debug > 1;
67
68 undef %pidu;
69 undef %thiskill;
70
71 foreach (sort{$a <=> $b} grep(/^[0-9]/, readdir(PROCFS))) {
72     $status = "$procfs/$_/status";
73     $pid = $_;
74     next if $pid == $$;         # don't kill yourself
75
76     open(STATUS, "$status") || next; # process maybe already terminated
77     while(<STATUS>) {
78         @proc = split;
79
80         printf "%5d %5d %5d %s\n", $pid, $proc[$PROC_EUID],
81                $proc[$PROC_RUID], $proc[$PROC_NAME] if $debug > 1;
82
83         foreach $program (@ARGV) {
84             # quote meta characters
85             ($programMatch = $program) =~ s/(\W)/\\$1/g if $match;
86
87             # match program name
88             if ($proc[$PROC_NAME] eq $program ||
89                 ($match && $proc[$PROC_NAME] =~ /$programMatch/i)) {
90                 
91                 # id test
92                 if ($proc[$PROC_EUID] eq $id || # effective uid
93                     $proc[$PROC_RUID] eq $id || # real uid
94                     !$id)                       # root
95                 {
96                     push(@kill, $pid) if !$pidu{"$pid"};
97                     $pidu{"$pid"} = $pid;
98                     $thiskill{"$program"}++;
99                 } 
100                 
101                 # process exists, but does not belong to you
102                 else {
103                     $notkillable{"$program"}++;
104                 }
105             }
106         }
107     }
108     close STATUS;
109 }
110 closedir PROCFS;
111
112 # nothing found
113 foreach $program (@ARGV) {
114     if (!$thiskill{"$program"}) {
115         print STDERR "No processes matching ``$program''"; 
116         print STDERR " belong to you" if $notkillable{"$program"};
117         print STDERR "\n";
118     }
119 }
120
121
122 # nothing found
123 exit(1) if $#kill < 0;
124
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;
128
129 $cnt = kill ($signal, @kill) unless $show; # kill processes
130 exit(0) if $show || $cnt == $#kill + 1;
131 exit(1);