File: //proc/self/root/proc/self/root/scripts.20110531.215904.25158/ckillall
#!/usr/bin/perl
# cpanel10 - scripts Copyright(c) 1997-2008 cPanel, Inc.
# All rights Reserved.
# copyright@cpanel.net http://cpanel.net
# This code is subject to the cpanel license. Unauthorized copying is prohibited
use bytes;
use strict;
if ( !-e "/proc/1" ) {
print "Critical Error: /proc is not mounted)!\n";
print "Attempting to mount /proc...";
system( "mount", "-t", "procfs", "/proc", "/proc" );
print "Done\n";
}
if ( !-e "/proc/1" ) {
print "Critical Error: /proc is still not mounted!\n";
exit;
}
my $system = $^O;
my $exen = 'exe';
if ( $system =~ /freebsd/i ) { $exen = 'file'; }
my $pid = $$;
my $signal = $ARGV[0];
my $deadcmd = $ARGV[1];
$signal =~ s/-//g;
if ( $signal eq "HUP" ) { $signal = '1'; }
if ( $signal eq "KILL" ) { $signal = '9'; }
if ( $signal eq "TERM" ) { $signal = '15'; }
if ( $signal eq "USR1" ) { $signal = '10'; }
if ( $signal !~ /^\d+/ || $deadcmd eq "" ) {
print "Usage: ckillall -signal command\n";
exit;
}
if ( $deadcmd eq "init" ) {
print "PANIC! Attempted to kill init!\n";
exit;
}
opendir( PROC, "/proc" );
my @PROCS = readdir(PROC);
closedir(PROC);
@PROCS = grep( /^\d+$/, @PROCS );
@PROCS = grep( !/^${pid}$/, @PROCS );
foreach my $proc (@PROCS) {
open( CMDLINE, "/proc/$proc/cmdline" );
my $cmd = <CMDLINE>;
my @CMDS = split( /[\s\0]+/, $cmd );
my $processname = getbasename( $CMDS[0] );
my $processename = getbasename( $CMDS[1] );
my @BIN = split( /\//, getbin($proc) );
my $file = $BIN[$#BIN];
$processname =~ s/:$//;
## case 36728: added 'python' clause for restartsrv_mailman
if ( ( $file ne "" && $deadcmd eq $file )
|| $processname eq $deadcmd
|| lc $processname eq lc $deadcmd
|| ( $processname eq "sh" && $processename eq $deadcmd )
|| ( $processname eq "perl" && $processename eq $deadcmd )
|| ( $processname eq "python" && $processename eq $deadcmd ) ) {
kill( $signal, $proc );
}
close(CMDLINE);
}
sub getbasename {
my ($name) = @_;
$name =~ s/^-//g;
if ( $name =~ /\// ) {
my @BASE = split( /\//, $name );
return ( $BASE[$#BASE] );
}
elsif ( $name =~ /\s/ ) {
$name =~ /^(\S+)/;
if ( $1 eq "" ) {
return (-1);
}
return ($1);
}
else {
return ($name);
}
}
sub getbin {
my ($pid) = @_;
my ($exe);
if ( -e "/proc/${pid}" ) {
$exe = readlink("/proc/${pid}/${exen}");
}
elsif ( -e "/proc/.${pid}" ) {
$exe = readlink("/proc/.${pid}/${exen}");
}
( $exe, undef ) = split( /\s/, $exe );
return ($exe);
}