File:  [LON-CAPA] / loncom / init.d / loncontrol
Revision 1.22: download - view: text, annotated - select for diffs
Thu Dec 2 21:16:56 2004 UTC (19 years, 7 months ago) by matthew
Branches: MAIN
CVS tags: version_1_2_99_1, HEAD
Added support for multiple port opening and added port 8080.

    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 $lond_port = 5663;
   49:     my $lonhttpd_port = 8080;
   50: 
   51: sub firewall_open_port {
   52:     return if (! &firewall_is_active);
   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:     foreach my $port ($lond_port,$lonhttpd_port) {
   60:         print "Opening firewall access on port $port.\n";
   61: 
   62:         my $firewall_command = 
   63:             "$iptables -I $fw_chain -p tcp -d 0/0 --dport $port -j ACCEPT";
   64:         system($firewall_command);
   65:         my $return_status = $?>>8;
   66:         if ($return_status == 1) {
   67:             # Error
   68:             print "Error opening port.\n";
   69:         } elsif ($return_status == 2) {
   70:             # Bad command
   71:             print "Bad command error opening port.  Command was\n".
   72:                 "  ".$firewall_command."\n";
   73:         }
   74:     }
   75:     
   76: }
   77: 
   78: sub firewall_is_port_open {
   79:     # returns 1 if the firewall port is open, 0 if not.
   80:     #
   81:     # check if firewall is active or installed
   82:     return if (! &firewall_is_active);
   83:     if (`$iptables -L -n 2>/dev/null | grep "tcp dpt:$port"`) { 
   84:         return 1;
   85:     } else {
   86:         return 0;
   87:     }
   88: }
   89: 
   90: sub firewall_is_active {
   91:     if (-e '/proc/net/ip_tables_names') {
   92:         return 1;
   93:     } else {
   94:         return 0;
   95:     }
   96: }
   97: 
   98: sub firewall_close_port {
   99:     return if (! &firewall_is_active);
  100:     foreach my $port ($lond_port,$lonhttpd_port) {
  101:         print "Closing firewall access on port $port.\n";
  102:         my $firewall_command = 
  103:             "$iptables -D $fw_chain -p tcp -d 0/0 --dport $port -j ACCEPT";
  104:         system($firewall_command);
  105:         my $return_status = $?>>8;
  106:         if ($return_status == 1) {
  107:             # Error
  108:             print "Error closing port.\n";
  109:         } elsif ($return_status == 2) {
  110:             # Bad command
  111:             print "Bad command error closing port.  Command was\n".
  112:                 "  ".$firewall_command."\n";
  113:         }
  114:     }
  115: }
  116: 
  117: } # End firewall variable scope
  118: 
  119: sub stop_daemon {
  120:     my ($daemon,$killallname)=@_;
  121:     my $pidfile="/home/httpd/perl/logs/$daemon.pid";
  122:     
  123:     printf("%-10s ",$daemon);
  124:     if (-e $pidfile) {
  125: 	open(PIDFILE,$pidfile);
  126: 	my $daemonpid=<PIDFILE>;
  127: 	chomp($daemonpid);
  128: 	kill TERM => $daemonpid;
  129: 	sleep 2;
  130: 	if (kill 0 => $daemonpid) {
  131: 	    kill KILL => $daemonpid;
  132: 	    sleep 2;
  133: 	    if (kill 0 => $daemonpid) {
  134: 		print("failed to kill");
  135: 	    } else {
  136: 		print("killed");
  137: 	    }
  138: 	} else {
  139: 	    print("stopped");
  140: 	}
  141:     } else {
  142: 	print("not running");
  143:     }
  144:     system("killall -q -0 $killallname");
  145:     if ($? == 0) {
  146: 	system("killall -q $killallname");
  147: 	print(", killed off extraneous processes");
  148:     }
  149:     print("\n");
  150: }
  151: 
  152: 
  153: if (($command eq "restartold") or ($command eq "reloadold")) {
  154:     print 'Restarting LON-CAPA'."\n";
  155:     print 'Ending LON-CAPA client and daemon processes'."\n";
  156:     foreach my $daemon ('lonsql','lond','lonc','lonhttpd') {
  157: 	&stop_daemon($daemon,$daemon);
  158:     }
  159:     print 'Starting LON-CAPA client and daemon processes (please be patient)'.
  160: 	"\n";
  161:     system("su www -c '/home/httpd/perl/loncron --oldlonc --justcheckdaemons'");
  162: } elsif (($command eq "restart") or ($command eq "reload")) {
  163:     print 'Restarting LON-CAPA'."\n";
  164:     print 'Ending LON-CAPA client and daemon processes'."\n";
  165:     foreach my $daemon ('lonsql','lond','lonc','lonhttpd') {
  166: 	my $killallname=$daemon;
  167: 	if ($daemon eq 'lonc') { $killallname='loncnew'; }
  168: 	&stop_daemon($daemon,$killallname);
  169:     }
  170:     print 'Starting LON-CAPA client and daemon processes (please be patient)'.
  171: 	"\n";
  172:     system("su www -c '/home/httpd/perl/loncron --justcheckdaemons'");
  173: } elsif ($command eq "stop") {
  174:     print 'Stopping LON-CAPA'."\n";
  175:     foreach my $daemon ('lonsql','lond','lonc','lonhttpd') {
  176: 	my $killallname=$daemon;
  177: 	if ($daemon eq 'lonc') { $killallname='loncnew'; }
  178: 	&stop_daemon($daemon,$killallname);
  179:     }
  180:     &firewall_close_port();
  181: } elsif ($command eq "startold") {
  182:     &firewall_open_port();
  183:     print 'Starting LON-CAPA'."\n";
  184:     print 'Starting LON-CAPA client and daemon processes (please be patient)'.
  185: 	"\n"; 
  186:     system("su www -c '/home/httpd/perl/loncron --oldlonc --justcheckdaemons'");
  187: } elsif ($command eq "start") {
  188:     &firewall_open_port();
  189:     print 'Starting LON-CAPA'."\n";
  190:     print 'Starting LON-CAPA client and daemon processes (please be patient)'.
  191: 	"\n"; 
  192:     system("su www -c '/home/httpd/perl/loncron --justcheckdaemons'");
  193: } elsif ($command eq "status") {
  194:     $response=`/bin/cat /home/httpd/perl/logs/*.pid 2>&1`;
  195:     if ($response=~/No such file or directory/) {
  196: 	print 'LON-CAPA is not running.'."\n";
  197:     } else {
  198: 	print 'LON-CAPA is running.'."\n";
  199: 	system("su www -c '/home/httpd/perl/loncron --justcheckconnections'");
  200:     }
  201:     if (! &firewall_is_active) {
  202:         print 'The iptables firewall is not active'."\n";
  203:     }
  204:     if (&firewall_is_port_open()) {
  205:         print 'The LON-CAPA port is open in firewall.'."\n";
  206:     } elsif (&firewall_is_active) {
  207:         print 'The LON-CAPA port is NOT open in running firewall!'."\n";
  208:     }
  209: } else {
  210:     print 'You need to specify one of restart|stop|start|status on the command line.'."\n";
  211: }

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