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

1.1       harris41    1: #!/usr/bin/perl
1.2       harris41    2: #
1.45    ! raeburn     3: # $Id: loncontrol,v 1.44 2015/09/20 18:31:33 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.35      raeburn    35: # processnames: lonc, lond, lonsql, lonmaxima, lonr
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
1.40      raeburn    43: # Required-Start: SuSEfirewall2_setup mysql apache2 $network $remote_fs
                     44: # Should-Start:   $ALL
1.31      albertel   45: # Required-Stop:
1.42      raeburn    46: # Default-Start:  3 5
1.31      albertel   47: # Default-Stop:
                     48: # Description:    Starts the LON-CAPA services
                     49: ### END INIT INFO
                     50: 
1.34      raeburn    51: use strict;
                     52: use lib '/home/httpd/lib/perl/';
                     53: use LONCAPA::Configuration;
1.37      raeburn    54: use LONCAPA::Firewall;
1.36      raeburn    55: use Apache::lonnet;
1.2       harris41   56: 
1.34      raeburn    57: my $command=$ARGV[0]; $command=~s/[^a-z]//g;
1.1       harris41   58: 
                     59: $ENV{'PATH'}="/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin";
                     60: $ENV{'BASH_ENV'}="";
                     61: 
1.11      albertel   62: sub stop_daemon {
1.19      albertel   63:     my ($daemon,$killallname)=@_;
1.11      albertel   64:     my $pidfile="/home/httpd/perl/logs/$daemon.pid";
                     65:     
1.24      albertel   66:     printf("%-15s ",$daemon);
1.11      albertel   67:     if (-e $pidfile) {
                     68: 	open(PIDFILE,$pidfile);
                     69: 	my $daemonpid=<PIDFILE>;
                     70: 	chomp($daemonpid);
                     71: 	kill TERM => $daemonpid;
1.32      albertel   72: 	my $count=0;
                     73: 	while ($count++ < 5 && kill(0 => $daemonpid)) {
                     74: 	    sleep 1;
                     75: 	}
1.11      albertel   76: 	if (kill 0 => $daemonpid) {
                     77: 	    kill KILL => $daemonpid;
1.26      albertel   78: 	    sleep 1;
1.11      albertel   79: 	    if (kill 0 => $daemonpid) {
1.19      albertel   80: 		print("failed to kill");
1.11      albertel   81: 	    } else {
1.19      albertel   82: 		print("killed");
1.11      albertel   83: 	    }
                     84: 	} else {
1.19      albertel   85: 	    print("stopped");
1.11      albertel   86: 	}
1.19      albertel   87:     } else {
                     88: 	print("not running");
                     89:     }
                     90:     system("killall -q -0 $killallname");
                     91:     if ($? == 0) {
                     92: 	system("killall -q $killallname");
                     93: 	print(", killed off extraneous processes");
1.11      albertel   94:     }
1.24      albertel   95:     unlink($pidfile);
1.45    ! raeburn    96:     if ($killallname eq 'loncnew') {
        !            97:         &clean_nossl_loncpids();
        !            98:     }
1.19      albertel   99:     print("\n");
1.11      albertel  100: }
                    101: 
1.30      albertel  102: sub clean_sockets {
                    103:     opendir(SOCKETS,"/home/httpd/sockets/");
1.34      raeburn   104:     my $perlvarref=&LONCAPA::Configuration::read_conf();
                    105:     return if (ref($perlvarref) ne 'HASH');
1.30      albertel  106:     while (my $fname=readdir(SOCKETS)) {
                    107: 	next if (-d $fname
1.34      raeburn   108: 		 || $fname=~/(mysqlsock|maximasock|\Q$perlvarref->{'lonSockDir'}\E)/);
1.30      albertel  109: 	unlink("/home/httpd/sockets/$fname");
                    110:     }
                    111: }
1.20      matthew   112: 
1.45    ! raeburn   113: sub clean_nossl_loncpids {
        !           114:     my $childpiddir = '/home/httpd/perl/logs/loncpids';
        !           115:     my $nossldir = '/home/httpd/sockets/nosslverify';
        !           116:     foreach my $dir ($childpiddir,$nossldir) {
        !           117:         if (-d $dir) {
        !           118:             if (opendir(my $dh,$dir)) {
        !           119:                 while (my $fname=readdir($dh)) {
        !           120:                     next if ($fname =~ /^\./);
        !           121:                     unlink("$dir/$fname");
        !           122:                 }
        !           123:                 closedir($dh);
        !           124:             }
        !           125:         }
        !           126:     }
        !           127: }
        !           128: 
1.43      raeburn   129: if (($command eq "restart") || ($command eq 'stop') || 
                    130:     ($command eq 'start') || ($command eq "status")) {
1.37      raeburn   131:     my $iptables = &LONCAPA::Firewall::get_pathto_iptables();
1.39      raeburn   132:     my @fw_chains = &LONCAPA::Firewall::get_fw_chains($iptables);
1.37      raeburn   133:     my $lond_port = &LONCAPA::Firewall::get_lond_port();
1.41      raeburn   134:     my %iphost;
1.43      raeburn   135:     if ($command eq 'restart') {
                    136:         print 'Restarting LON-CAPA'."\n";
                    137:         print 'Ending LON-CAPA client and daemon processes'."\n";
                    138:         foreach my $daemon ('lonsql','lond','lonc','lonmemcached','lonmaxima','lonr') {
                    139:             my $killallname=$daemon;
                    140:             if ($daemon eq 'lonc') { $killallname='loncnew'; }
                    141:             &stop_daemon($daemon,$killallname);
                    142:         }
1.44      raeburn   143:         %iphost = &Apache::lonnet::get_iphost('',1);
1.43      raeburn   144:         my $firewall_result =
                    145:             &LONCAPA::Firewall::firewall_close_port($iptables,\@fw_chains,
                    146:                                              $lond_port,\%iphost,[$lond_port]);
                    147:         if ($firewall_result) {
                    148:             print "$firewall_result\n";
                    149:         }
                    150:         $firewall_result =
                    151:             &LONCAPA::Firewall::firewall_open_port($iptables,\@fw_chains,
                    152:                                             $lond_port, \%iphost,[$lond_port]);
                    153:         if (($firewall_result eq 'ok') || ($firewall_result eq 'inactive firewall')) {
                    154:             if ($firewall_result eq 'inactive firewall') {
                    155:                 print "WARNING: iptables firewall is currently inactive\n";
                    156:             }
                    157:         } elsif ($firewall_result) {
                    158:             print "$firewall_result\n";
                    159:         }
                    160:         print 'Starting LON-CAPA client and daemon processes (please be patient)'.
                    161:               "\n";
                    162:         system("su www -c '/home/httpd/perl/loncron --justcheckdaemons'");
                    163:     } elsif ($command eq 'stop') {
1.37      raeburn   164:         print 'Stopping LON-CAPA'."\n";
                    165:         foreach my $daemon ('lonsql','lond','lonc','lonmemcached','lonmaxima','lonr') {
                    166: 	    my $killallname=$daemon;
                    167: 	    if ($daemon eq 'lonc') { $killallname='loncnew'; }
                    168: 	    &stop_daemon($daemon,$killallname);
                    169:         }
                    170:         my $firewall_result = 
1.41      raeburn   171:             &LONCAPA::Firewall::firewall_close_port($iptables,\@fw_chains,$lond_port,\%iphost,[$lond_port]);
1.37      raeburn   172:         if ($firewall_result) {
                    173:             print "$firewall_result\n";
                    174:         }
                    175:         &clean_sockets();
                    176:     } elsif ($command eq "start") {
1.44      raeburn   177:         %iphost = &Apache::lonnet::get_iphost('',1);
1.37      raeburn   178:         my $firewall_result = 
1.39      raeburn   179:             &LONCAPA::Firewall::firewall_open_port($iptables,\@fw_chains,$lond_port,\%iphost,[$lond_port]);
1.37      raeburn   180:         if (($firewall_result eq 'ok') || ($firewall_result eq 'inactive firewall')) {
                    181:             if ($firewall_result eq 'inactive firewall') {
                    182:                 print "WARNING: iptables firewall is currently inactive\n";
                    183:             }
                    184:             print 'Starting LON-CAPA'."\n";
                    185:             print 'Starting LON-CAPA client and daemon processes (please be patient)'.
                    186: 	          "\n";
                    187:             system("su www -c '/home/httpd/perl/loncron --justcheckdaemons'");
                    188:         } else {
                    189:             print "Not starting LON-CAPA\n";
                    190:             if ($firewall_result eq 'port number unknown') {
                    191:                 print "Could not check for status of LON-CAPA port in running firewall - port number unknown.  \n";
                    192:             } elsif ($firewall_result) {
                    193:                 print "$firewall_result\n";
                    194:             }
                    195:         }
                    196:     } elsif ($command eq "status") {
1.41      raeburn   197:         %iphost = &Apache::lonnet::get_iphost();
1.37      raeburn   198:         my $response=`/bin/cat /home/httpd/perl/logs/*.pid 2>&1`;
                    199:         if ($response=~/No such file or directory/) {
                    200: 	    print 'LON-CAPA is not running.'."\n";
                    201:         } else {
                    202: 	    print 'LON-CAPA is running.'."\n";
                    203: 	    system("su www -c '/home/httpd/perl/loncron --justcheckconnections'");
                    204:         }
                    205:         if (! &LONCAPA::Firewall::firewall_is_active()) {
                    206:             print 'The iptables firewall is not active'."\n";
1.34      raeburn   207:         }
1.37      raeburn   208:         if ($lond_port) {
1.39      raeburn   209:             my $londopen;
                    210:             foreach my $fw_chain (@fw_chains) { 
                    211:                 my $numopen = &LONCAPA::Firewall::firewall_is_port_open($iptables,$fw_chain,$lond_port,$lond_port,\%iphost);
                    212:                 $londopen += $numopen; 
                    213:             }
                    214:             if ($londopen) {
1.37      raeburn   215:                 print "The LON-CAPA port ($lond_port) is open in firewall.\n";
                    216:             } elsif (&LONCAPA::Firewall::firewall_is_active) {
                    217:                 print "The LON-CAPA port ($lond_port) is NOT open in running firewall!\n";
                    218:             }
                    219:         } else {
                    220:             if (&LONCAPA::Firewall::firewall_is_active()) {
                    221:                 print "Could not check for status of LON-CAPA port in running firewall - port number unknown.\n";
                    222:             } else {
                    223:                 print "LON-CAPA port number is unknown, and firewall is not running.\n";
                    224:             }
1.34      raeburn   225:         }
                    226:     }
1.25      albertel  227: } elsif ($command eq "reload") {
                    228:     print 'Reload LON-CAPA config files'."\n";
                    229:     system("su www -c '/home/httpd/perl/loncron --justreload'");
1.16      albertel  230: } else {
1.37      raeburn   231:     print "You need to specify one of reload|restart|stop|start|status on the command line.\n";
1.1       harris41  232: }

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