File:  [LON-CAPA] / loncom / init.d / loncontrol
Revision 1.46: download - view: text, annotated - select for diffs
Sun Jun 9 02:35:41 2019 UTC (4 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- Dynamic management of LON-CAPA port 5663 compatible with firewalld
  - &get_default_zone(() provides default zone
  - Revert changes in rev. 1.17 to &uses_firewalld() -- now returns
    1 if firewalld in use, but not the default zone.
  - Additional arg, value = 1 if firewalld in use  is passed to
    &firewall_open_port(), &firewall_close_port(),
    &firewall_is_port_open(), &firewall_close_anywhere()

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

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