_ >   1   #!/usr/local/bin/perl
  2   # $Id$
  3  
  4   ## Restarts an AOLserver. Takes as its only argument the name of the
  5   ## server to kill.
  6  
    1 #!/usr/bin/perl
    2 ## Restarts an AOLserver.
    3 ## Takes as its only argument the name of the server to kill.
    4 ## bquinn 6/16/2000 with help from {ryanlee, doug}@arsdigita
7 5 ## This is a perl script because it needs to run setuid root,
  8 6 ## and perl has fewer security gotchas than most shells.
< >     7 ##
    8 ## Make sure that $PIDFILE points to the right location.
9 9
< >   10   use strict;
11 10
< >     11 use strict;
    12 undef %ENV;
12 13 $ENV{'PATH'} = '/sbin:/bin';
< >   13   my @superusers=('james','jsc','brucek','dbryant');
14 14
< >   15   my $name;
  16   ($name) = (getpwuid($<))[0];
  17  
  18   my $superuser = 0;
  19   if (grep ($name eq $_,@superusers) ) {
  20      $superuser = 1;
    15 if (scalar(@ARGV) == 0) {
    16      die "Don't run this without any arguments!";
21 17 }
  22 18
< >   23   if (scalar(@ARGV) == 0 && !$superuser) {
  24       die "Don't run this without any arguments!\n";
  25   }
  26  
27 19 my $server = shift;
< >   28   # untaint this variable to make suidperl happy
29 20 $server =~ /^([\w-]*)$/;
< >   30   my $servername = $1;
    21 my $service_name = $1;
    22 my $PIDFILE = "/usr/local/aolserver/log/nspid.$service_name";
    23 my $pid;
31 24
< >   32   if ($server && !$servername) {
  33      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.
    25 $< = $>; # set realuid to effective uid (root)
34 26
< >   35   You just want to say something like \"restart-aolserver student000\".
  36   "
  37   } elsif (!$servername && !$superuser) {
  38      die "We need something besides the empty string to restart.\n"
    27 # Get the PID of the process to kill.
    28
    29 open(IN,"$PIDFILE") || die "No such server\n";
    30 while(<IN>) {
    31     chomp($_);
    32     $pid=$_;
39 33 }
< >     34 close(IN) || die "Problem closing PID file\n";
40 35
< >   41   $< = $>; # set realuid to effective uid (root)
    36 # Remove the PID file.  We have to delete the file to make sure that a subsequent call
    37 # to this script will kill some other process.  We delete the file before the process dies
    38 # because if the service is set to respawn then we may delete the new pid file.
42 39
< >   43   ## get the PIDs of all jobdirect servers
  44   open(PID, "/usr/bin/ps -ef |") || die $!;
  45   my @pids;
  46   while (<PID>) {
  47     next unless /^\s*\S+\s+(\d+).*nsd.*\/$servername\./;
  48     my $pid = $1;
  49     push(@pids, $pid);
  50   }
  51   close PID;
    40 my $cmd ="rm -f $PIDFILE";
    41 $cmd =~ /^(.*)$/;
    42 my $untaint_cmd = $1;
    43 `$untaint_cmd`;
52 44
< >   53   print "Killing ", join(" ", @pids), "\n";
  54   kill 'KILL', @pids;
    45 # Issue the kill
    46 $pid =~ /^(.*)$/;
    47 my $untaint_pid = $1;
    48 print "Killing $untaint_pid\n";
    49 kill 9, $untaint_pid;
55 50
< _     51