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

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

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