File:  [LON-CAPA] / loncom / init.d / loncontrol
Revision 1.32: download - view: text, annotated - select for diffs
Tue Jun 12 23:45:42 2007 UTC (16 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: version_2_7_X, version_2_7_1, version_2_7_0, version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, version_2_6_2, version_2_6_1, version_2_6_0, version_2_5_X, version_2_5_99_1, version_2_5_99_0, version_2_5_2, version_2_5_1, version_2_5_0, version_2_4_X, version_2_4_99_0, version_2_4_2, version_2_4_1, HEAD
- give things a chance to cleanly exit on their own before killing

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

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