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

1.1       harris41    1: #!/usr/bin/perl
1.2       harris41    2: #
1.34    ! raeburn     3: # $Id: loncontrol,v 1.33 2008/11/04 21:06:31 www Exp $
1.23      matthew     4: #
1.6       harris41    5: # The LearningOnline Network with CAPA
                      6: #
1.21      matthew     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: #
1.2       harris41   29: # Startup script for the LON-CAPA network processes
1.6       harris41   30: #
1.7       harris41   31: 
1.3       harris41   32: # chkconfig: 345 95 5
1.21      matthew    33: # description: LON-CAPA is a "network of knowledge".  It is used to \
1.6       harris41   34: # distribute knowledge resources and instructional management.
1.28      raeburn    35: # processnames: lonc, lond, lonsql, lonmaxima
1.2       harris41   36: # pidfiles: /home/httpd/perl/logs/lon*.pid
1.7       harris41   37: # config: /etc/httpd/conf/loncapa.conf
1.2       harris41   38: # config: /home/httpd/lonTabs/hosts.tab
                     39: # config: /home/httpd/lonTabs/spare.tab
1.31      albertel   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: 
1.34    ! raeburn    50: use strict;
        !            51: use lib '/home/httpd/lib/perl/';
        !            52: use LONCAPA::Configuration;
1.2       harris41   53: 
1.34    ! raeburn    54: my $command=$ARGV[0]; $command=~s/[^a-z]//g;
1.1       harris41   55: 
                     56: $ENV{'PATH'}="/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin";
                     57: $ENV{'BASH_ENV'}="";
                     58: 
1.20      matthew    59: { # Firewall variable scoping
                     60:     # Firewall code is based on the code in FC2 /etc/init.d/ntpd
                     61:     my $fw_chain = 'RH-Firewall-1-INPUT';
                     62:     my $iptables = '/sbin/iptables';
1.27      albertel   63:     if (! -e $iptables) {
                     64: 	$iptables = '/usr/sbin/iptables';
1.34    ! raeburn    65: 	if (!-e $iptables) {
1.27      albertel   66: 	    print("Unable to find iptables command\n");
                     67: 	}
                     68:     }
1.34    ! raeburn    69:     my $suse_config = "/etc/sysconfig/SuSEfirewall2";
        !            70:     if (!-e $suse_config) {
        !            71:         if (!-e '/etc/sysconfig/iptables') {
        !            72:             print("Unable to find iptables file containing static definitions\n");
        !            73:         }
        !            74:     }
        !            75:     my $lond_port = &get_lond_port();
        !            76:     if (!$lond_port) {
        !            77:         print("Unable to determine lond port number from LON-CAPA configuration.\n");
        !            78:     }
1.20      matthew    79: 
                     80: sub firewall_open_port {
1.34    ! raeburn    81:     return 'inactive firewall' if (! &firewall_is_active);
        !            82:     return 'port number unknown' if !$lond_port;
        !            83:     my @opened;
        !            84:     my $suse_config = "/etc/sysconfig/SuSEfirewall2";
        !            85:     if (-e $suse_config) {
        !            86:         if (open(my $fh,"<$suse_config")) {
        !            87:             while(<$fh>) {
        !            88:                 chomp();
        !            89:                 if (/^FW_SERVICES_EXT_TCP="([^"]+)"\s*$/) {
        !            90:                     my $portstr = $1;
        !            91:                     my @suseports = split(/\s+/,$portstr);
        !            92:                     foreach my $port ($lond_port) {
        !            93:                         if (grep/^\Q$port\E$/,@suseports) {
        !            94:                             push(@opened,$port);
        !            95:                         }
        !            96:                     }
        !            97:                 }
        !            98:             }
        !            99:         }
        !           100:     } else {
        !           101:         if (! `$iptables -L -n 2>/dev/null | grep $fw_chain | wc -l`) { 
        !           102:             return 'chain error';
        !           103:         }
        !           104:         # iptables is running with our chain
        !           105:         #
        !           106:         # We could restrict the servers allowed to attempt to communicate
        !           107:         # here, but the logistics of updating the /home/httpd/lonTabs/host.tab
        !           108:         # file are likely to be a problem
        !           109:         foreach my $port ($lond_port) {
        !           110:             print "Opening firewall access on port $port.\n";
        !           111:             my $result;
        !           112:             my $firewall_command = 
        !           113:                 "$iptables -I $fw_chain -p tcp -d 0/0 --dport $port -j ACCEPT";
        !           114:             system($firewall_command);
        !           115:             my $return_status = $?>>8;
        !           116:             if ($return_status == 1) {
        !           117:                 # Error
        !           118:                 print "Error opening port.\n";
        !           119:             } elsif ($return_status == 2) {
        !           120:                 # Bad command
        !           121:                 print "Bad command error opening port.  Command was\n".
        !           122:                       "  ".$firewall_command."\n";
        !           123:             } elsif ($return_status == 0) {
        !           124:                 push(@opened,$port);
        !           125:             }
        !           126:         }
        !           127:     }
1.33      www       128:     foreach my $port ($lond_port) {
1.34    ! raeburn   129:         if (!grep(/^\Q$port\E$/,@opened)) {
        !           130:             return 'Required port not open: '.$port."\n";  
1.22      matthew   131:         }
1.20      matthew   132:     }
1.34    ! raeburn   133:     return 'ok';
1.20      matthew   134: }
                    135: 
                    136: sub firewall_is_port_open {
1.34    ! raeburn   137:     my ($port) = @_;
1.20      matthew   138:     # returns 1 if the firewall port is open, 0 if not.
                    139:     #
                    140:     # check if firewall is active or installed
                    141:     return if (! &firewall_is_active);
                    142:     if (`$iptables -L -n 2>/dev/null | grep "tcp dpt:$port"`) { 
                    143:         return 1;
                    144:     } else {
                    145:         return 0;
                    146:     }
                    147: }
                    148: 
                    149: sub firewall_is_active {
                    150:     if (-e '/proc/net/ip_tables_names') {
                    151:         return 1;
                    152:     } else {
                    153:         return 0;
                    154:     }
                    155: }
                    156: 
                    157: sub firewall_close_port {
1.34    ! raeburn   158:     return 'inactive firewall' if (! &firewall_is_active);
        !           159:     return 'port number unknown' if !$lond_port;
        !           160:     my $suse_config = "/etc/sysconfig/SuSEfirewall2";
        !           161:     return if (-e $suse_config);
1.33      www       162:     foreach my $port ($lond_port) {
1.34    ! raeburn   163:         print "Closing firewall access on port $port\n";
1.22      matthew   164:         my $firewall_command = 
                    165:             "$iptables -D $fw_chain -p tcp -d 0/0 --dport $port -j ACCEPT";
                    166:         system($firewall_command);
                    167:         my $return_status = $?>>8;
                    168:         if ($return_status == 1) {
                    169:             # Error
                    170:             print "Error closing port.\n";
                    171:         } elsif ($return_status == 2) {
                    172:             # Bad command
                    173:             print "Bad command error closing port.  Command was\n".
                    174:                 "  ".$firewall_command."\n";
1.34    ! raeburn   175:         } else {
        !           176:             print "Port closed.\n";
1.22      matthew   177:         }
1.20      matthew   178:     }
1.34    ! raeburn   179:     return;
        !           180: }
        !           181: 
        !           182: sub get_lond_port {
        !           183:     my $perlvarref=&LONCAPA::Configuration::read_conf();
        !           184:     my $lond_port;
        !           185:     if (ref($perlvarref) eq 'HASH') {
        !           186:         if (defined($perlvarref->{'londPort'})) {
        !           187:             $lond_port = $perlvarref->{'londPort'};
        !           188:         }
        !           189:     }
        !           190:     return $lond_port;
1.20      matthew   191: }
                    192: 
                    193: } # End firewall variable scope
                    194: 
1.11      albertel  195: sub stop_daemon {
1.19      albertel  196:     my ($daemon,$killallname)=@_;
1.11      albertel  197:     my $pidfile="/home/httpd/perl/logs/$daemon.pid";
                    198:     
1.24      albertel  199:     printf("%-15s ",$daemon);
1.11      albertel  200:     if (-e $pidfile) {
                    201: 	open(PIDFILE,$pidfile);
                    202: 	my $daemonpid=<PIDFILE>;
                    203: 	chomp($daemonpid);
                    204: 	kill TERM => $daemonpid;
1.32      albertel  205: 	my $count=0;
                    206: 	while ($count++ < 5 && kill(0 => $daemonpid)) {
                    207: 	    sleep 1;
                    208: 	}
1.11      albertel  209: 	if (kill 0 => $daemonpid) {
                    210: 	    kill KILL => $daemonpid;
1.26      albertel  211: 	    sleep 1;
1.11      albertel  212: 	    if (kill 0 => $daemonpid) {
1.19      albertel  213: 		print("failed to kill");
1.11      albertel  214: 	    } else {
1.19      albertel  215: 		print("killed");
1.11      albertel  216: 	    }
                    217: 	} else {
1.19      albertel  218: 	    print("stopped");
1.11      albertel  219: 	}
1.19      albertel  220:     } else {
                    221: 	print("not running");
                    222:     }
                    223:     system("killall -q -0 $killallname");
                    224:     if ($? == 0) {
                    225: 	system("killall -q $killallname");
                    226: 	print(", killed off extraneous processes");
1.11      albertel  227:     }
1.24      albertel  228:     unlink($pidfile);
1.19      albertel  229:     print("\n");
1.11      albertel  230: }
                    231: 
1.30      albertel  232: sub clean_sockets {
                    233:     opendir(SOCKETS,"/home/httpd/sockets/");
1.34    ! raeburn   234:     my $perlvarref=&LONCAPA::Configuration::read_conf();
        !           235:     return if (ref($perlvarref) ne 'HASH');
1.30      albertel  236:     while (my $fname=readdir(SOCKETS)) {
                    237: 	next if (-d $fname
1.34    ! raeburn   238: 		 || $fname=~/(mysqlsock|maximasock|\Q$perlvarref->{'lonSockDir'}\E)/);
1.30      albertel  239: 	unlink("/home/httpd/sockets/$fname");
                    240:     }
                    241: }
1.20      matthew   242: 
1.29      albertel  243: if ($command eq "restart") {
1.12      albertel  244:     print 'Restarting LON-CAPA'."\n";
                    245:     print 'Ending LON-CAPA client and daemon processes'."\n";
1.33      www       246:     foreach my $daemon ('lonsql','lond','lonc','lonmemcached','lonmaxima') {
1.19      albertel  247: 	my $killallname=$daemon;
                    248: 	if ($daemon eq 'lonc') { $killallname='loncnew'; }
                    249: 	&stop_daemon($daemon,$killallname);
1.12      albertel  250:     }
                    251:     print 'Starting LON-CAPA client and daemon processes (please be patient)'.
                    252: 	"\n";
1.18      albertel  253:     system("su www -c '/home/httpd/perl/loncron --justcheckdaemons'");
1.16      albertel  254: } elsif ($command eq "stop") {
1.6       harris41  255:     print 'Stopping LON-CAPA'."\n";
1.33      www       256:     foreach my $daemon ('lonsql','lond','lonc','lonmemcached','lonmaxima') {
1.19      albertel  257: 	my $killallname=$daemon;
                    258: 	if ($daemon eq 'lonc') { $killallname='loncnew'; }
                    259: 	&stop_daemon($daemon,$killallname);
1.11      albertel  260:     }
1.34    ! raeburn   261:     my $firewall_result = &firewall_close_port();
        !           262:     if ($firewall_result) {
        !           263:         print "$firewall_result\n";
        !           264:     }
1.30      albertel  265:     &clean_sockets();
1.16      albertel  266: } elsif ($command eq "start") {
1.34    ! raeburn   267:     my $firewall_result = &firewall_open_port();
        !           268:     if (($firewall_result eq 'ok') || ($firewall_result eq 'inactive firewall')) {
        !           269:         if ($firewall_result eq 'inactive firewall') {
        !           270:             print "WARNING: iptables firewall is currently inactive\n";
        !           271:         }
        !           272:         print 'Starting LON-CAPA'."\n";
        !           273:         print 'Starting LON-CAPA client and daemon processes (please be patient)'.
        !           274: 	      "\n";
        !           275:         system("su www -c '/home/httpd/perl/loncron --justcheckdaemons'");
        !           276:     } else {
        !           277:         print "Not starting LON-CAPA\n";
        !           278:         if ($firewall_result eq 'port number unknown') {
        !           279:             print "Could not check for status of LON-CAPA port in running firewall - port number unknown.  \n";
        !           280:         } elsif ($firewall_result) {
        !           281:             print "$firewall_result\n";
        !           282:         }
        !           283:     }
1.25      albertel  284: } elsif ($command eq "reload") {
                    285:     print 'Reload LON-CAPA config files'."\n";
                    286:     system("su www -c '/home/httpd/perl/loncron --justreload'");
1.16      albertel  287: } elsif ($command eq "status") {
1.34    ! raeburn   288:     my $lond_port = &get_lond_port();
        !           289:     my $response=`/bin/cat /home/httpd/perl/logs/*.pid 2>&1`;
1.1       harris41  290:     if ($response=~/No such file or directory/) {
1.6       harris41  291: 	print 'LON-CAPA is not running.'."\n";
1.18      albertel  292:     } else {
1.6       harris41  293: 	print 'LON-CAPA is running.'."\n";
1.18      albertel  294: 	system("su www -c '/home/httpd/perl/loncron --justcheckconnections'");
1.1       harris41  295:     }
1.20      matthew   296:     if (! &firewall_is_active) {
                    297:         print 'The iptables firewall is not active'."\n";
                    298:     }
1.34    ! raeburn   299:     my $lond_port = &get_lond_port();
        !           300:     if ($lond_port) {
        !           301:         if (&firewall_is_port_open($lond_port)) {
        !           302:             print "The LON-CAPA port ($lond_port) is open in firewall.\n";
        !           303:         } elsif (&firewall_is_active) {
        !           304:             print "The LON-CAPA port ($lond_port) is NOT open in running firewall!\n";
        !           305:         }
        !           306:     } else {
        !           307:         if (&firewall_is_active) {
        !           308:             print "Could not check for status of LON-CAPA port in running firewall - port number unknown.\n";
        !           309:         } else {
        !           310:             print "LON-CAPA port number is unknown, and firewall is not running.\n";
        !           311:         }
1.20      matthew   312:     }
1.16      albertel  313: } else {
1.34    ! raeburn   314:     print "You need to specify one of restart|stop|start|status on the command line.\n";
1.1       harris41  315: }

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