Annotation of loncom/init.d/loncontrol, revision 1.22
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';
1.22 ! matthew 48: my $lond_port = 5663;
! 49: my $lonhttpd_port = 8080;
1.20 matthew 50:
51: sub firewall_open_port {
52: return if (! &firewall_is_active);
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
1.22 ! matthew 59: foreach my $port ($lond_port,$lonhttpd_port) {
! 60: print "Opening firewall access on port $port.\n";
! 61:
! 62: my $firewall_command =
! 63: "$iptables -I $fw_chain -p tcp -d 0/0 --dport $port -j ACCEPT";
! 64: system($firewall_command);
! 65: my $return_status = $?>>8;
! 66: if ($return_status == 1) {
! 67: # Error
! 68: print "Error opening port.\n";
! 69: } elsif ($return_status == 2) {
! 70: # Bad command
! 71: print "Bad command error opening port. Command was\n".
! 72: " ".$firewall_command."\n";
! 73: }
1.20 matthew 74: }
1.22 ! matthew 75:
1.20 matthew 76: }
77:
78: sub firewall_is_port_open {
79: # returns 1 if the firewall port is open, 0 if not.
80: #
81: # check if firewall is active or installed
82: return if (! &firewall_is_active);
83: if (`$iptables -L -n 2>/dev/null | grep "tcp dpt:$port"`) {
84: return 1;
85: } else {
86: return 0;
87: }
88: }
89:
90: sub firewall_is_active {
91: if (-e '/proc/net/ip_tables_names') {
92: return 1;
93: } else {
94: return 0;
95: }
96: }
97:
98: sub firewall_close_port {
99: return if (! &firewall_is_active);
1.22 ! matthew 100: foreach my $port ($lond_port,$lonhttpd_port) {
! 101: print "Closing firewall access on port $port.\n";
! 102: my $firewall_command =
! 103: "$iptables -D $fw_chain -p tcp -d 0/0 --dport $port -j ACCEPT";
! 104: system($firewall_command);
! 105: my $return_status = $?>>8;
! 106: if ($return_status == 1) {
! 107: # Error
! 108: print "Error closing port.\n";
! 109: } elsif ($return_status == 2) {
! 110: # Bad command
! 111: print "Bad command error closing port. Command was\n".
! 112: " ".$firewall_command."\n";
! 113: }
1.20 matthew 114: }
115: }
116:
117: } # End firewall variable scope
118:
1.11 albertel 119: sub stop_daemon {
1.19 albertel 120: my ($daemon,$killallname)=@_;
1.11 albertel 121: my $pidfile="/home/httpd/perl/logs/$daemon.pid";
122:
123: printf("%-10s ",$daemon);
124: if (-e $pidfile) {
125: open(PIDFILE,$pidfile);
126: my $daemonpid=<PIDFILE>;
127: chomp($daemonpid);
128: kill TERM => $daemonpid;
129: sleep 2;
130: if (kill 0 => $daemonpid) {
131: kill KILL => $daemonpid;
132: sleep 2;
133: if (kill 0 => $daemonpid) {
1.19 albertel 134: print("failed to kill");
1.11 albertel 135: } else {
1.19 albertel 136: print("killed");
1.11 albertel 137: }
138: } else {
1.19 albertel 139: print("stopped");
1.11 albertel 140: }
1.19 albertel 141: } else {
142: print("not running");
143: }
144: system("killall -q -0 $killallname");
145: if ($? == 0) {
146: system("killall -q $killallname");
147: print(", killed off extraneous processes");
1.11 albertel 148: }
1.19 albertel 149: print("\n");
1.11 albertel 150: }
151:
1.20 matthew 152:
1.16 albertel 153: if (($command eq "restartold") or ($command eq "reloadold")) {
1.6 harris41 154: print 'Restarting LON-CAPA'."\n";
155: print 'Ending LON-CAPA client and daemon processes'."\n";
1.13 albertel 156: foreach my $daemon ('lonsql','lond','lonc','lonhttpd') {
1.19 albertel 157: &stop_daemon($daemon,$daemon);
1.11 albertel 158: }
1.6 harris41 159: print 'Starting LON-CAPA client and daemon processes (please be patient)'.
160: "\n";
1.18 albertel 161: system("su www -c '/home/httpd/perl/loncron --oldlonc --justcheckdaemons'");
1.16 albertel 162: } elsif (($command eq "restart") or ($command eq "reload")) {
1.12 albertel 163: print 'Restarting LON-CAPA'."\n";
164: print 'Ending LON-CAPA client and daemon processes'."\n";
1.13 albertel 165: foreach my $daemon ('lonsql','lond','lonc','lonhttpd') {
1.19 albertel 166: my $killallname=$daemon;
167: if ($daemon eq 'lonc') { $killallname='loncnew'; }
168: &stop_daemon($daemon,$killallname);
1.12 albertel 169: }
170: print 'Starting LON-CAPA client and daemon processes (please be patient)'.
171: "\n";
1.18 albertel 172: system("su www -c '/home/httpd/perl/loncron --justcheckdaemons'");
1.16 albertel 173: } elsif ($command eq "stop") {
1.6 harris41 174: print 'Stopping LON-CAPA'."\n";
1.13 albertel 175: foreach my $daemon ('lonsql','lond','lonc','lonhttpd') {
1.19 albertel 176: my $killallname=$daemon;
177: if ($daemon eq 'lonc') { $killallname='loncnew'; }
178: &stop_daemon($daemon,$killallname);
1.11 albertel 179: }
1.20 matthew 180: &firewall_close_port();
1.16 albertel 181: } elsif ($command eq "startold") {
1.20 matthew 182: &firewall_open_port();
1.6 harris41 183: print 'Starting LON-CAPA'."\n";
184: print 'Starting LON-CAPA client and daemon processes (please be patient)'.
185: "\n";
1.18 albertel 186: system("su www -c '/home/httpd/perl/loncron --oldlonc --justcheckdaemons'");
1.16 albertel 187: } elsif ($command eq "start") {
1.20 matthew 188: &firewall_open_port();
1.12 albertel 189: print 'Starting LON-CAPA'."\n";
190: print 'Starting LON-CAPA client and daemon processes (please be patient)'.
191: "\n";
1.18 albertel 192: system("su www -c '/home/httpd/perl/loncron --justcheckdaemons'");
1.16 albertel 193: } elsif ($command eq "status") {
1.1 harris41 194: $response=`/bin/cat /home/httpd/perl/logs/*.pid 2>&1`;
195: if ($response=~/No such file or directory/) {
1.6 harris41 196: print 'LON-CAPA is not running.'."\n";
1.18 albertel 197: } else {
1.6 harris41 198: print 'LON-CAPA is running.'."\n";
1.18 albertel 199: system("su www -c '/home/httpd/perl/loncron --justcheckconnections'");
1.1 harris41 200: }
1.20 matthew 201: if (! &firewall_is_active) {
202: print 'The iptables firewall is not active'."\n";
203: }
204: if (&firewall_is_port_open()) {
205: print 'The LON-CAPA port is open in firewall.'."\n";
206: } elsif (&firewall_is_active) {
207: print 'The LON-CAPA port is NOT open in running firewall!'."\n";
208: }
1.16 albertel 209: } else {
1.18 albertel 210: print 'You need to specify one of restart|stop|start|status on the command line.'."\n";
1.1 harris41 211: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>