After positive feedback from a few folks, activate a switchover to

using killall.c instead of the perl version that depends on procfs.
The C version uses sysctl().  The program is based on a hack that was
originally written about 6 years ago and has evolved somewhat since then.
(which is why it is a superset of killall.pl, rather than being a clone.)

With apologies to: wosch
This commit is contained in:
Peter Wemm 2000-08-28 22:09:38 +00:00
parent 21c3015a24
commit 8750ed5f77
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=65177
4 changed files with 36 additions and 155 deletions

View File

@ -1,10 +1,5 @@
# $FreeBSD$
MAINTAINER= wosch
MAN1= killall.1
beforeinstall:
${INSTALL} -c -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \
${.CURDIR}/killall.pl ${DESTDIR}${BINDIR}/killall
PROG= killall
.include <bsd.prog.mk>

View File

@ -38,8 +38,11 @@
.Op Fl l
.Op Fl m
.Op Fl s
.Op Fl u Ar user
.Op Fl t Ar tty
.Op Fl c Ar procname
.Op Fl SIGNAL
.Ar procname ...
.Op Ar procname ...
.Sh DESCRIPTION
.Nm Killall
kills processes selected by name, as opposed to the selection by pid
@ -61,12 +64,7 @@ Be more verbose about what will be done. For a single
.Fl d
option, a list of the processes that will be sent the signal will be
printed, or a message indicating that no matching processes have been
found. If the option
.Fl d
has been specified at least twice, the effective UID, PID, and name
of all processes found in
.Xr procfs 5
will be listed in addition.
found.
.It Fl h \&| Ns Fl \&?
.It Fl help
Give a help on the command usage and exit.
@ -77,12 +75,9 @@ List the names of the available signals and exit, like in
Match the argument
.Ar procname
as a (case insensitive) regular expression against the names
of processes found in
.Xr procfs 5 .
of processes found.
CAUTION! This is dangerous, a single dot will match any process
running under the effective UID of the caller. The regular expression
syntax in effect is that used by
.Xr perl 1 .
running under the effective UID of the caller.
.It Fl s
Show only what would be done, but do not send any signal.
.It Fl SIGNAL
@ -91,8 +86,23 @@ Send a different signal instead of the default
The signal may be specified either as a name
.Pq with \&or without a leading Dv SIG ,
or numerically.
.It Fl u Ar user
Limit potentially matching processes to those belonging to
the specified
.Ar user .
.It Fl c Ar tty
Limit potentially matching processes to those running on
the specified
.Ar tty .
.It Fl c Ar procname
When used with the
.Fl u
or
.Fl t
flags, limit potentially matching processes to those matching
the specified
.Ar progname .
.El
.Sh ALL PROCESSES
Sending a signal to all processes with uid
.Nm XYZ
@ -102,8 +112,6 @@ So use
.Xr kill 1
for this job (e.g. $ kill -TERM -1 or
as root $ echo kill -TERM -1 | su -m <user>)
.Sh DIAGNOSTICS
The
.Nm
@ -118,7 +126,7 @@ Diagnostic messages will only be printed if requested by
options.
.Sh SEE ALSO
.Xr kill 1 ,
.Xr procfs 5
.Xr sysctl 3
.Sh HISTORY
The
.Nm
@ -128,8 +136,14 @@ It has been modeled after the
.Nm
command as available on other platforms.
.Sh AUTHORS
The program has been contributed by
The
.Nm
program was originally written in Perl and was contributed by
.An Wolfram Schneider ,
this manual page has been written by
.if n Joerg Wunsch.
.if t J\(:org Wunsch.
The current version of
.Nm
was rewritten in C by Peter Wemm using
.Xr sysctl 3 .

View File

@ -27,6 +27,7 @@
* $FreeBSD$
*/
#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/user.h>
@ -46,7 +47,7 @@
static char *prog;
static void
static void __dead2
usage(void)
{
@ -138,6 +139,8 @@ main(int ac, char **av)
printsig(stdout);
exit(0);
}
if (strcmp(*av, "-help") == 0)
usage();
if (**av == '-') {
++*av;
switch (**av) {

View File

@ -1,131 +0,0 @@
#!/usr/bin/perl
#
# Copyright (c) 1995-1996 Wolfram Schneider <wosch@FreeBSD.org>. Berlin.
# All rights reserved.
#
# 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.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
#
# killall - kill processes by name
#
# $FreeBSD$
$ENV{'PATH'} = '/bin:/usr/bin'; # security
$procfs = '/proc';
$signal = 'SIGTERM'; # default signal for kill
$debug = 0;
$match = 0; # 0 match exactly program name
$show = 0; # do nothings
# see /sys/miscfs/procfs/procfs_status.c
$PROC_NAME = 0;
$PROC_EUID = 11;
$PROC_RUID = 12;
sub usage {
$! = 2;
die "killall [-?|-help] [-d] [-l] [-m] [-s] [-SIGNAL] procname ...\n";
}
$id = $<; # real uid of this process / your id
while ($_ = $ARGV[0], /^-/) {
shift @ARGV;
if (/^--$/) { $_ = $ARGV[0]; last }
elsif (/^-(h|help|\?)$/) { &usage }
elsif (/^-[dv]$/) { $debug++ }
elsif (/^-l$/) { exec 'kill', '-l' }
elsif (/^-m$/) { $match = 1 }
elsif (/^-s$/) { $show = 1 }
elsif (/^-([a-z][a-z0-9]+|[0-9]+)$/i) { $signal = $1 }
elsif (/^-/) { &usage }
}
&usage if $#ARGV < 0; # no arguments
die "Maybe $procfs is not mounted\n" unless -e "$procfs/0/status";
opendir(PROCFS, "$procfs") || die "$procfs $!\n";
print " PID EUID RUID COMMAND\n" if $debug > 1;
undef %pidu;
undef %thiskill;
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;
foreach $program (@ARGV) {
# quote meta characters
($programMatch = $program) =~ s/(\W)/\\$1/g if $match;
# match program name
if ($proc[$PROC_NAME] eq $program ||
($match && $proc[$PROC_NAME] =~ /$programMatch/i)) {
# id test
if ($proc[$PROC_EUID] eq $id || # effective uid
$proc[$PROC_RUID] eq $id || # real uid
!$id) # root
{
push(@kill, $pid) if !$pidu{"$pid"};
$pidu{"$pid"} = $pid;
$thiskill{"$program"}++;
}
# process exists, but does not belong to you
else {
$notkillable{"$program"}++;
}
}
}
}
close STATUS;
}
closedir PROCFS;
# nothing found
foreach $program (@ARGV) {
if (!$thiskill{"$program"}) {
print STDERR "No processes matching ``$program''";
print STDERR " belong to you" if $notkillable{"$program"};
print STDERR "\n";
}
}
# nothing found
exit(1) if $#kill < 0;
$signal =~ y/a-z/A-Z/; # signal name in upper case
$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);