File:  [LON-CAPA] / loncom / init.d / loncontrol
Revision 1.21: download - view: text, annotated - select for diffs
Thu Dec 2 20:49:50 2004 UTC (19 years, 7 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Added GPL header.

    1: #!/usr/bin/perl
    2: #
    3: # The LearningOnline Network with CAPA
    4: #
    5: # Copyright Michigan State University Board of Trustees
    6: #
    7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    8: #
    9: # LON-CAPA is free software; you can redistribute it and/or modify
   10: # it under the terms of the GNU General Public License as published by
   11: # the Free Software Foundation; either version 2 of the License, or
   12: # (at your option) any later version.
   13: #
   14: # LON-CAPA is distributed in the hope that it will be useful,
   15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17: # GNU General Public License for more details.
   18: #
   19: # You should have received a copy of the GNU General Public License
   20: # along with LON-CAPA; if not, write to the Free Software
   21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   22: #
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: # Startup script for the LON-CAPA network processes
   28: #
   29: 
   30: # chkconfig: 345 95 5
   31: # description: LON-CAPA is a "network of knowledge".  It is used to \
   32: # distribute knowledge resources and instructional management.
   33: # processnames: lonc, lond, lonsql
   34: # pidfiles: /home/httpd/perl/logs/lon*.pid
   35: # config: /etc/httpd/conf/loncapa.conf
   36: # config: /home/httpd/lonTabs/hosts.tab
   37: # config: /home/httpd/lonTabs/spare.tab
   38: 
   39: $command=$ARGV[0]; $command=~s/[^a-z]//g;
   40: 
   41: $ENV{'PATH'}="/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin";
   42: $ENV{'BASH_ENV'}="";
   43: 
   44: { # Firewall variable scoping
   45:     # Firewall code is based on the code in FC2 /etc/init.d/ntpd
   46:     my $fw_chain = 'RH-Firewall-1-INPUT';
   47:     my $iptables = '/sbin/iptables';
   48:     my $port = 5663;
   49: 
   50: sub firewall_open_port {
   51:     return if (! &firewall_is_active);
   52:     print "Opening firewall access on port $port\n";
   53:     if (! `$iptables -L -n 2>/dev/null | grep $fw_chain | wc -l`) { return; }
   54:     # iptables is running with our chain
   55:     #
   56:     # We could restrict the servers allowed to attempt to communicate
   57:     # here, but the logistics of updating the /home/httpd/lonTabs/host.tab
   58:     # file are likely to be a problem
   59:     my $firewall_command = 
   60:         "$iptables -I $fw_chain -p tcp -d 0/0 --dport $port -j ACCEPT";
   61:     system($firewall_command);
   62:     my $return_status = $?>>8;
   63:     if ($return_status == 1) {
   64:         # Error
   65:         print "Error opening port.\n";
   66:     } elsif ($return_status == 2) {
   67:         # Bad command
   68:         print "Bad command error opening port.  Command was\n".
   69:             "  ".$firewall_command."\n";
   70:     }
   71: }
   72: 
   73: sub firewall_is_port_open {
   74:     # returns 1 if the firewall port is open, 0 if not.
   75:     #
   76:     # check if firewall is active or installed
   77:     return if (! &firewall_is_active);
   78:     if (`$iptables -L -n 2>/dev/null | grep "tcp dpt:$port"`) { 
   79:         return 1;
   80:     } else {
   81:         return 0;
   82:     }
   83: }
   84: 
   85: sub firewall_is_active {
   86:     if (-e '/proc/net/ip_tables_names') {
   87:         return 1;
   88:     } else {
   89:         return 0;
   90:     }
   91: }
   92: 
   93: sub firewall_close_port {
   94:     return if (! &firewall_is_active);
   95:     print "Closing firewall access on port $port\n";
   96:     my $firewall_command = 
   97:         "$iptables -D $fw_chain -p tcp -d 0/0 --dport $port -j ACCEPT";
   98:     system($firewall_command);
   99:     my $return_status = $?>>8;
  100:     if ($return_status == 1) {
  101:         # Error
  102:         print "Error closing port.\n";
  103:     } elsif ($return_status == 2) {
  104:         # Bad command
  105:         print "Bad command error closing port.  Command was\n".
  106:             "  ".$firewall_command."\n";
  107:     }
  108: }
  109: 
  110: } # End firewall variable scope
  111: 
  112: sub stop_daemon {
  113:     my ($daemon,$killallname)=@_;
  114:     my $pidfile="/home/httpd/perl/logs/$daemon.pid";
  115:     
  116:     printf("%-10s ",$daemon);
  117:     if (-e $pidfile) {
  118: 	open(PIDFILE,$pidfile);
  119: 	my $daemonpid=<PIDFILE>;
  120: 	chomp($daemonpid);
  121: 	kill TERM => $daemonpid;
  122: 	sleep 2;
  123: 	if (kill 0 => $daemonpid) {
  124: 	    kill KILL => $daemonpid;
  125: 	    sleep 2;
  126: 	    if (kill 0 => $daemonpid) {
  127: 		print("failed to kill");
  128: 	    } else {
  129: 		print("killed");
  130: 	    }
  131: 	} else {
  132: 	    print("stopped");
  133: 	}
  134:     } else {
  135: 	print("not running");
  136:     }
  137:     system("killall -q -0 $killallname");
  138:     if ($? == 0) {
  139: 	system("killall -q $killallname");
  140: 	print(", killed off extraneous processes");
  141:     }
  142:     print("\n");
  143: }
  144: 
  145: 
  146: if (($command eq "restartold") or ($command eq "reloadold")) {
  147:     print 'Restarting LON-CAPA'."\n";
  148:     print 'Ending LON-CAPA client and daemon processes'."\n";
  149:     foreach my $daemon ('lonsql','lond','lonc','lonhttpd') {
  150: 	&stop_daemon($daemon,$daemon);
  151:     }
  152:     print 'Starting LON-CAPA client and daemon processes (please be patient)'.
  153: 	"\n";
  154:     system("su www -c '/home/httpd/perl/loncron --oldlonc --justcheckdaemons'");
  155: } elsif (($command eq "restart") or ($command eq "reload")) {
  156:     print 'Restarting LON-CAPA'."\n";
  157:     print 'Ending LON-CAPA client and daemon processes'."\n";
  158:     foreach my $daemon ('lonsql','lond','lonc','lonhttpd') {
  159: 	my $killallname=$daemon;
  160: 	if ($daemon eq 'lonc') { $killallname='loncnew'; }
  161: 	&stop_daemon($daemon,$killallname);
  162:     }
  163:     print 'Starting LON-CAPA client and daemon processes (please be patient)'.
  164: 	"\n";
  165:     system("su www -c '/home/httpd/perl/loncron --justcheckdaemons'");
  166: } elsif ($command eq "stop") {
  167:     print 'Stopping LON-CAPA'."\n";
  168:     foreach my $daemon ('lonsql','lond','lonc','lonhttpd') {
  169: 	my $killallname=$daemon;
  170: 	if ($daemon eq 'lonc') { $killallname='loncnew'; }
  171: 	&stop_daemon($daemon,$killallname);
  172:     }
  173:     &firewall_close_port();
  174: } elsif ($command eq "startold") {
  175:     &firewall_open_port();
  176:     print 'Starting LON-CAPA'."\n";
  177:     print 'Starting LON-CAPA client and daemon processes (please be patient)'.
  178: 	"\n"; 
  179:     system("su www -c '/home/httpd/perl/loncron --oldlonc --justcheckdaemons'");
  180: } elsif ($command eq "start") {
  181:     &firewall_open_port();
  182:     print 'Starting LON-CAPA'."\n";
  183:     print 'Starting LON-CAPA client and daemon processes (please be patient)'.
  184: 	"\n"; 
  185:     system("su www -c '/home/httpd/perl/loncron --justcheckdaemons'");
  186: } elsif ($command eq "status") {
  187:     $response=`/bin/cat /home/httpd/perl/logs/*.pid 2>&1`;
  188:     if ($response=~/No such file or directory/) {
  189: 	print 'LON-CAPA is not running.'."\n";
  190:     } else {
  191: 	print 'LON-CAPA is running.'."\n";
  192: 	system("su www -c '/home/httpd/perl/loncron --justcheckconnections'");
  193:     }
  194:     if (! &firewall_is_active) {
  195:         print 'The iptables firewall is not active'."\n";
  196:     }
  197:     if (&firewall_is_port_open()) {
  198:         print 'The LON-CAPA port is open in firewall.'."\n";
  199:     } elsif (&firewall_is_active) {
  200:         print 'The LON-CAPA port is NOT open in running firewall!'."\n";
  201:     }
  202: } else {
  203:     print 'You need to specify one of restart|stop|start|status on the command line.'."\n";
  204: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>