Annotation of loncom/init.d/loncontrol, revision 1.21

1.1       harris41    1: #!/usr/bin/perl
1.2       harris41    2: #
1.6       harris41    3: # The LearningOnline Network with CAPA
                      4: #
1.21    ! matthew     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: #
1.2       harris41   27: # Startup script for the LON-CAPA network processes
1.6       harris41   28: #
1.7       harris41   29: 
1.3       harris41   30: # chkconfig: 345 95 5
1.21    ! matthew    31: # description: LON-CAPA is a "network of knowledge".  It is used to \
1.6       harris41   32: # distribute knowledge resources and instructional management.
1.2       harris41   33: # processnames: lonc, lond, lonsql
                     34: # pidfiles: /home/httpd/perl/logs/lon*.pid
1.7       harris41   35: # config: /etc/httpd/conf/loncapa.conf
1.2       harris41   36: # config: /home/httpd/lonTabs/hosts.tab
                     37: # config: /home/httpd/lonTabs/spare.tab
                     38: 
1.1       harris41   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: 
1.20      matthew    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: 
1.11      albertel  112: sub stop_daemon {
1.19      albertel  113:     my ($daemon,$killallname)=@_;
1.11      albertel  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) {
1.19      albertel  127: 		print("failed to kill");
1.11      albertel  128: 	    } else {
1.19      albertel  129: 		print("killed");
1.11      albertel  130: 	    }
                    131: 	} else {
1.19      albertel  132: 	    print("stopped");
1.11      albertel  133: 	}
1.19      albertel  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");
1.11      albertel  141:     }
1.19      albertel  142:     print("\n");
1.11      albertel  143: }
                    144: 
1.20      matthew   145: 
1.16      albertel  146: if (($command eq "restartold") or ($command eq "reloadold")) {
1.6       harris41  147:     print 'Restarting LON-CAPA'."\n";
                    148:     print 'Ending LON-CAPA client and daemon processes'."\n";
1.13      albertel  149:     foreach my $daemon ('lonsql','lond','lonc','lonhttpd') {
1.19      albertel  150: 	&stop_daemon($daemon,$daemon);
1.11      albertel  151:     }
1.6       harris41  152:     print 'Starting LON-CAPA client and daemon processes (please be patient)'.
                    153: 	"\n";
1.18      albertel  154:     system("su www -c '/home/httpd/perl/loncron --oldlonc --justcheckdaemons'");
1.16      albertel  155: } elsif (($command eq "restart") or ($command eq "reload")) {
1.12      albertel  156:     print 'Restarting LON-CAPA'."\n";
                    157:     print 'Ending LON-CAPA client and daemon processes'."\n";
1.13      albertel  158:     foreach my $daemon ('lonsql','lond','lonc','lonhttpd') {
1.19      albertel  159: 	my $killallname=$daemon;
                    160: 	if ($daemon eq 'lonc') { $killallname='loncnew'; }
                    161: 	&stop_daemon($daemon,$killallname);
1.12      albertel  162:     }
                    163:     print 'Starting LON-CAPA client and daemon processes (please be patient)'.
                    164: 	"\n";
1.18      albertel  165:     system("su www -c '/home/httpd/perl/loncron --justcheckdaemons'");
1.16      albertel  166: } elsif ($command eq "stop") {
1.6       harris41  167:     print 'Stopping LON-CAPA'."\n";
1.13      albertel  168:     foreach my $daemon ('lonsql','lond','lonc','lonhttpd') {
1.19      albertel  169: 	my $killallname=$daemon;
                    170: 	if ($daemon eq 'lonc') { $killallname='loncnew'; }
                    171: 	&stop_daemon($daemon,$killallname);
1.11      albertel  172:     }
1.20      matthew   173:     &firewall_close_port();
1.16      albertel  174: } elsif ($command eq "startold") {
1.20      matthew   175:     &firewall_open_port();
1.6       harris41  176:     print 'Starting LON-CAPA'."\n";
                    177:     print 'Starting LON-CAPA client and daemon processes (please be patient)'.
                    178: 	"\n"; 
1.18      albertel  179:     system("su www -c '/home/httpd/perl/loncron --oldlonc --justcheckdaemons'");
1.16      albertel  180: } elsif ($command eq "start") {
1.20      matthew   181:     &firewall_open_port();
1.12      albertel  182:     print 'Starting LON-CAPA'."\n";
                    183:     print 'Starting LON-CAPA client and daemon processes (please be patient)'.
                    184: 	"\n"; 
1.18      albertel  185:     system("su www -c '/home/httpd/perl/loncron --justcheckdaemons'");
1.16      albertel  186: } elsif ($command eq "status") {
1.1       harris41  187:     $response=`/bin/cat /home/httpd/perl/logs/*.pid 2>&1`;
                    188:     if ($response=~/No such file or directory/) {
1.6       harris41  189: 	print 'LON-CAPA is not running.'."\n";
1.18      albertel  190:     } else {
1.6       harris41  191: 	print 'LON-CAPA is running.'."\n";
1.18      albertel  192: 	system("su www -c '/home/httpd/perl/loncron --justcheckconnections'");
1.1       harris41  193:     }
1.20      matthew   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:     }
1.16      albertel  202: } else {
1.18      albertel  203:     print 'You need to specify one of restart|stop|start|status on the command line.'."\n";
1.1       harris41  204: }

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