#!/usr/local/bin/perl
# $Id: restart-aolserver,v 1.1.1.1 2002/07/09 17:34:56 rmello Exp $

## Restarts an AOLserver. Takes as its only argument the name of the
## server to kill.

## This is a perl script because it needs to run setuid root, 
## and perl has fewer security gotchas than most shells.

use strict;

$ENV{'PATH'} = '/sbin:/bin';
my @superusers=('james','jsc','brucek','dbryant');

my $name;
($name) = (getpwuid($<))[0];

my $superuser = 0;
if (grep ($name eq $_,@superusers) ) {
   $superuser = 1;
}

if (scalar(@ARGV) == 0 && !$superuser) {
    die "Don't run this without any arguments!\n";
}

my $server = shift;
# untaint this variable to make suidperl happy
$server =~ /^([\w-]*)$/;
my $servername = $1;

if ($server && !$servername) {
   die "An AOL servername should only have letters, numbers, underscores, or a dash.\nYou told us to restart $server, and we can't do that.

You just want to say something like \"restart-aolserver student000\".
"
} elsif (!$servername && !$superuser) {
   die "We need something besides the empty string to restart.\n"
}

$< = $>; # set realuid to effective uid (root)

## get the PIDs of all jobdirect servers
open(PID, "/usr/bin/ps -ef |") || die $!;
my @pids;
while (<PID>) {
  next unless /^\s*\S+\s+(\d+).*nsd.*\/$servername\./;
  my $pid = $1;
  push(@pids, $pid);
}
close PID;

print "Killing ", join(" ", @pids), "\n";
kill 'KILL', @pids;