File: //proc/self/root/scripts.20110531.215904.25158/restartsrv
#!/usr/bin/perl
# cpanel - restartsrv Copyright(c) 2010 cPanel, Inc.
# All rights Reserved.
# copyright@cpanel.net http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
BEGIN { unshift @INC, '/usr/local/cpanel'; }
use strict;
use Socket ();
use Cpanel::Config::LoadCpConf ();
use Cpanel::OSSys ();
$ENV{'RESTARTSRV'} = 1;
$SIG{'CHLD'} = \&reaper;
$| = 1;
my %CPCONF = Cpanel::Config::LoadCpConf::loadcpconf();
my ( $st, $chkp, $command, $chkcmd );
my ( $system, $nodename, $release, $version, $machine ) = Cpanel::OSSys::uname();
my $service = servicefixup( $ARGV[0] );
cleanupenv();
if ( $service eq 'httpd' ) {
$chkp = '80,GET / HTTP/1.0,HTTP/1..';
$st = '/scripts/restartsrv_httpd --status';
$command = '/scripts/restartsrv_httpd --restart';
}
elsif ( $service eq 'clamd' ) {
$chkp = '';
$chkcmd = '/scripts/restartsrv_clamd --check';
$st = '/scripts/restartsrv_clamd --status';
$command = '/scripts/restartsrv_clamd --restart';
}
elsif ( $service eq 'ftpserver' || $service eq 'ftp' ) {
$chkp = '21,,220';
$st = '/scripts/restartsrv_ftpserver --status';
$command = '/scripts/restartsrv_ftpserver --restart';
}
elsif ( $service eq 'syslogd' ) {
$chkp = '';
$st = '/scripts/restartsrv_syslogd --status';
$chkcmd = '/scripts/restartsrv_syslogd --check';
$command = "/scripts/restartsrv_syslogd --restart";
}
elsif ( $service eq 'rsyslogd' ) {
$chkp = '';
$st = '/scripts/restartsrv_rsyslogd --status';
$chkcmd = '/scripts/restartsrv_rsyslogd --check';
$command = '/scripts/restartsrv_rsyslogd --restart';
}
elsif ( $service eq 'exim' ) {
$chkp = '25,,220';
$st = '/scripts/restartsrv_exim --status';
$command = '/scripts/restartsrv_exim --restart';
}
elsif ( $service eq 'sshd' ) {
require Cpanel::SSH::Port;
my $ssh_port = Cpanel::SSH::Port::getport() || 22;
$chkp = $ssh_port . ',,SSH';
$st = '/scripts/restartsrv_sshd --status';
$command = '/scripts/restartsrv_sshd --restart';
}
elsif ( $service eq 'inetd' ) {
$chkp = '143,,..OK';
$st = '/scripts/restartsrv_inetd --status';
$command = '/scripts/restartsrv_inetd --restart';
}
elsif ( $service eq 'ipaliases' ) {
$chkp = '';
$st = '/scripts/restartsrv_ipaliases --status';
$command = '/scripts/restartsrv_ipaliases --restart';
}
elsif ( $service eq 'cppop' ) {
$chkp = '110,,.OK';
$st = '/scripts/restartsrv_cppop --status';
$command = '/scripts/restartsrv_cppop --restart';
}
elsif ( $service eq 'named' ) {
$chkp = '';
$st = '/scripts/restartsrv_named --status';
$chkcmd = '/scripts/restartsrv_named --check';
$command = '/scripts/restartsrv_named --restart';
}
elsif ( $service eq 'nsd' ) {
$chkp = '';
$st = '/scripts/restartsrv_nsd --status';
$chkcmd = '/scripts/restartsrv_nsd --check';
$command = '/scripts/restartsrv_nsd --restart';
}
elsif ( $service eq 'mysql' ) {
$chkp = '';
$st = '/scripts/restartsrv_mysql --status';
$chkcmd = '/scripts/restartsrv_mysql --check';
$command = '/scripts/restartsrv_mysql --restart';
}
elsif ( $service eq 'postgresql' ) {
$chkp = '';
$st = '/scripts/restartsrv_postgresql --status';
$chkcmd = '/scripts/restartsrv_postgresql --check';
$command = '/scripts/restartsrv_postgresql --restart';
}
elsif ( $service eq 'interchange' ) {
$chkp = '';
$st = '/scripts/restartsrv_interchange --status';
$chkcmd = '/scripts/restartsrv_interchange --check';
$command = '/scripts/restartsrv_interchange --restart';
}
elsif ( $service eq 'tomcat' ) {
$chkp = '';
$st = '/scripts/restartsrv_tomcat --status';
$chkcmd = '/scripts/restartsrv_tomcat --check';
$command = '/scripts/restartsrv_tomcat --restart';
}
elsif ( $service eq 'entropychat' ) {
$chkp = '';
$st = '/scripts/restartsrv_entropychat --status';
$chkcmd = '/scripts/restartsrv_entropychat --check';
$command = '/scripts/restartsrv_entropychat --restart';
}
elsif ( $service eq 'melange' ) {
$chkp = '';
$st = '/scripts/restartsrv_melange --status';
$chkcmd = '/scripts/restartsrv_melange --check';
$command = '/scripts/restartsrv_melange --restart';
}
elsif ( $service eq 'spamd' ) {
$chkp = '';
$st = '/scripts/restartsrv_spamd --status';
$chkcmd = '/scripts/restartsrv_spamd --check';
$command = '/scripts/restartsrv_spamd --restart';
}
elsif ( $service eq 'chkservd' ) {
$chkp = '';
$st = '/scripts/restartsrv_chkservd --status';
$chkcmd = '/scripts/restartsrv_chkservd --check';
$command = '/scripts/restartsrv_chkservd --restart';
}
elsif ( $service eq 'imap' ) {
$chkp = '';
$st = '/scripts/restartsrv_imap --status';
$chkcmd = '/scripts/restartsrv_imap --check';
$command = '/scripts/restartsrv_imap --restart';
}
elsif ( $service eq 'dovecot' ) {
$chkp = '';
$st = '/scripts/restartsrv_dovecot --status';
$chkcmd = '/scripts/restartsrv_dovecot --check';
$command = '/scripts/restartsrv_dovecot --restart';
}
elsif ( $service eq 'mailman' ) {
$chkp = '';
$st = '/scripts/restartsrv_mailman --status';
$chkcmd = '/scripts/restartsrv_mailman --check';
$command = '/scripts/restartsrv_mailman --restart';
}
else {
die "ERROR: Attempt to restart unknown service \"$service\"\n";
}
chdir('/');
if ( !fork() ) {
chdir('/');
for ( my $n = 0; $n < 1024; $n++ ) { Cpanel::OSSys::close($n); }
Cpanel::OSSys::setsid();
open( STDIN, "</dev/null" );
open( STDOUT, ">/dev/null" );
open( STDERR, ">/dev/null" );
system($command);
exit;
}
open( TAIL, '/var/log/messages' );
seek( TAIL, 0, &SEEK_END );
$SIG{'ALRM'} = sub {
close(TAIL);
exit;
};
print "Waiting for $service to restart...";
my $status;
my $timeout = 60;
if ( -e '/var/run/restartsrv_' . $service . '.lock' ) {
my $count = 1;
while (( $count <= ( $timeout * 2 ) )
&& ( -e '/var/run/restartsrv_' . $service . '.lock' ) ) {
print ".";
sleep(1);
$count++;
}
}
else {
my $got_status;
for ( my $i = 1; $i <= $timeout && !$got_status; $i++ ) {
print ".";
sleep(1);
if ( 0 == $i % 12 ) {
$status = `$st`;
if ( !defined($status) ) { $status = ''; }
$got_status = $status !~ m/^\s*$/;
}
}
}
print "finished.\n\n";
print "$status\n";
if ( $status =~ /disabled/ ) {
print "Service: [$service] has been disabled by the sys admin\n";
exit;
}
if ( $chkcmd ne '' ) {
my @res = `$chkcmd`;
chomp @res;
if ( !@res || ( @res == 1 && $res[0] eq '' ) ) {
print "$service started ok\n";
exit if ( $service eq 'named' || $service eq 'nsd' );
alarm(2);
while (<TAIL>) {
if (/ $service[\[\]\d]*:/) {
print;
}
}
alarm(0);
exit;
}
else {
my $res = join( ', ', @res );
print "$service has failed, please contact the sysadmin (result was \"$res\").\n";
alarm(2);
while (<TAIL>) {
if (/ $service[\[\]\d]*:/) {
print;
}
}
alarm(0);
exit;
}
}
if ( $chkp ne '' ) {
my ( $port, $send, $res ) = split( /\,/, $chkp );
my ($sok) = 0;
eval {
$SIG{'ALRM'} = sub {
die "Can't connect";
};
alarm(10);
my $proto = getprotobyname('tcp');
socket( SC, &Socket::AF_INET, &Socket::SOCK_STREAM, $proto );
my $sin = Socket::sockaddr_in( $port, Socket::inet_aton("127.0.0.1") )
|| die 'Can\'t Connect';
connect( SC, $sin ) || die 'Can\'t Connect';
alarm(0);
$SIG{'ALRM'} = sub {
die 'The Server Returned an Invalid Response!';
};
alarm(30);
if ( $send ne '' ) {
send( SC, "$send\n\n", 0 );
}
my $retr;
recv( SC, $retr, ( length($res) ), 0 );
if ( $retr =~ /^$res/ ) {
$sok = 1;
}
else {
$res =~ s/\n//g;
$retr =~ s/\n//g;
print "$service: [$retr != $res]\n";
}
alarm(0);
};
alarm(0);
if ($sok) {
print "$service started ok\n";
}
else {
alarm(2);
while (<TAIL>) {
if (/ $service[\[\]\d]*:/) {
print;
}
}
alarm(0);
print "$service has failed, please contact the sysadmin.\n";
exit;
}
}
exit(0);
sub reaper {
my $thedead;
while ( ( $thedead = waitpid( -1, 1 ) ) > 0 ) {
##!! the dead shall do what ?
}
$SIG{'CHLD'} = \&reaper;
}
sub cleanupenv {
my %KEEPENVS = (
"CPANEL" => 1,
"WHM50" => 1,
"WHMLITE" => 1,
"MYSQLCCHK" => 1,
"USER" => 1,
"USERNAME" => 1,
"HOME" => 1,
"PATH" => 1,
"LANGUAGE" => 1,
"LC_MESSAGES" => 1
);
foreach my $env ( keys %ENV ) {
if ( !$KEEPENVS{$env} ) {
delete $ENV{$env};
}
}
}
sub servicefixup {
my $service = shift;
if ( $service eq 'proftpd' ) { $service = 'ftpserver'; }
elsif ( $service eq 'ftpd' ) { $service = 'ftpserver'; }
elsif ( $service eq 'pure-ftpd' ) { $service = 'ftpserver'; }
elsif ( $service eq 'pureftpd' ) { $service = 'ftpserver'; }
elsif ( $service eq 'apache' || $service =~ m/http/i ) {
$service = 'httpd';
}
elsif ( $service eq 'bind' ) { $service = 'named'; }
elsif ( $service eq 'postgres' ) { $service = 'postgresql'; }
elsif ( $service =~ m/rsyslog/i ) { $service = 'rsyslogd'; }
elsif ( $service =~ m/syslog/i ) { $service = 'syslogd'; }
elsif ( $service =~ m/clam/i ) { $service = 'clamd'; }
elsif ( $service =~ m/ssh/i ) { $service = 'sshd'; }
elsif ( $service =~ m/imap/i || $service =~ m/courier/i ) {
if ( $CPCONF{'maildir'} eq '1' ) { $service = 'imap'; }
else { $service = 'inetd'; }
}
elsif ( $service =~ m/inet/i ) { $service = 'inetd'; }
elsif ( $service =~ m/pop/i ) { $service = 'cppop'; }
if ( $service eq 'named' && -e '/var/cpanel/usensd' ) {
$service = 'nsd';
}
elsif ( $service eq 'nsd' && !-e '/var/cpanel/usensd' ) {
$service = 'named';
}
return $service;
}
sub loadcpconfig {
%CPCONF = Cpanel::Config::LoadCpConf::loadcpconf();
}
sub SEEK_END {
return (2);
}