1: #!/usr/bin/perl
2: # The LearningOnline Network with CAPA
3: # lonc maintains the connections to remote computers
4: #
5: # $Id: loncnew,v 1.14 2003/07/03 02:10:18 foxr Exp $
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: #
30: # new lonc handles n requestors spread out bver m connections to londs.
31: # This module is based on the Event class.
32: # Development iterations:
33: # - Setup basic event loop. (done)
34: # - Add timer dispatch. (done)
35: # - Add ability to accept lonc UNIX domain sockets. (done)
36: # - Add ability to create/negotiate lond connections (done).
37: # - Add general logic for dispatching requests and timeouts. (done).
38: # - Add support for the lonc/lond requests. (done).
39: # - Add logging/status monitoring.
40: # - Add Signal handling - HUP restarts. USR1 status report.
41: # - Add Configuration file I/O (done).
42: # - Add management/status request interface.
43: # - Add deferred request capability. (done)
44: # - Detect transmission timeouts.
45: #
46:
47: # Change log:
48: # $Log: loncnew,v $
49: # Revision 1.14 2003/07/03 02:10:18 foxr
50: # Get all of the signals to work correctly.
51: #
52: # Revision 1.13 2003/07/02 01:31:55 foxr
53: # Added kill -HUP logic (restart).
54: #
55: # Revision 1.11 2003/06/25 01:54:44 foxr
56: # Fix more problems with transaction failure.
57: #
58: # Revision 1.10 2003/06/24 02:46:04 foxr
59: # Put a limit on the number of times we'll retry a connection.
60: # Start getting the signal stuff put in as well...note that need to get signals
61: # going or else 6the client will permanently give up on dead servers.
62: #
63: # Revision 1.9 2003/06/13 02:38:43 foxr
64: # Add logging in 'expected format'
65: #
66: # Revision 1.8 2003/06/11 02:04:35 foxr
67: # Support delayed transactions... this is done uniformly by encapsulating
68: # transactions in an object ... a LondTransaction that is implemented by
69: # LondTransaction.pm
70: #
71: # Revision 1.7 2003/06/03 01:59:39 foxr
72: # complete coding to support deferred transactions.
73: #
74: #
75:
76: use lib "/home/httpd/lib/perl/";
77: use lib "/home/foxr/newloncapa/types";
78: use Event qw(:DEFAULT );
79: use POSIX qw(:signal_h);
80: use POSIX;
81: use IO::Socket;
82: use IO::Socket::INET;
83: use IO::Socket::UNIX;
84: use IO::File;
85: use IO::Handle;
86: use Socket;
87: use Crypt::IDEA;
88: use LONCAPA::Queue;
89: use LONCAPA::Stack;
90: use LONCAPA::LondConnection;
91: use LONCAPA::LondTransaction;
92: use LONCAPA::Configuration;
93: use LONCAPA::HashIterator;
94:
95:
96: #
97: # Disable all signals we might receive from outside for now.
98: #
99: #$SIG{QUIT} = IGNORE;
100: #$SIG{HUP} = IGNORE;
101: #$SIG{USR1} = IGNORE;
102: #$SIG{INT} = IGNORE;
103: #$SIG{CHLD} = IGNORE;
104: #$SIG{__DIE__} = IGNORE;
105:
106:
107: # Read the httpd configuration file to get perl variables
108: # normally set in apache modules:
109:
110: my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
111: my %perlvar = %{$perlvarref};
112:
113: #
114: # parent and shared variables.
115:
116: my %ChildHash; # by pid -> host.
117:
118:
119: my $MaxConnectionCount = 10; # Will get from config later.
120: my $ClientConnection = 0; # Uniquifier for client events.
121:
122: my $DebugLevel = 0;
123: my $IdleTimeout= 3600; # Wait an hour before pruning connections.
124:
125: #
126: # The variables below are only used by the child processes.
127: #
128: my $RemoteHost; # Name of host child is talking to.
129: my $UnixSocketDir= "/home/httpd/sockets";
130: my $IdleConnections = Stack->new(); # Set of idle connections
131: my %ActiveConnections; # Connections to the remote lond.
132: my %ActiveTransactions; # LondTransactions in flight.
133: my %ActiveClients; # Serial numbers of active clients by socket.
134: my $WorkQueue = Queue->new(); # Queue of pending transactions.
135: my $ConnectionCount = 0;
136: my $IdleSeconds = 0; # Number of seconds idle.
137: my $Status = ""; # Current status string.
138: my $RecentLogEntry = "";
139: my $ConnectionRetries=5; # Number of connection retries allowed.
140: my $ConnectionRetriesLeft=5; # Number of connection retries remaining.
141:
142: #
143: # The hash below gives the HTML format for log messages
144: # given a severity.
145: #
146: my %LogFormats;
147:
148: $LogFormats{"CRITICAL"} = "<font color=red>CRITICAL: %s</font>";
149: $LogFormats{"SUCCESS"} = "<font color=green>SUCCESS: %s</font>";
150: $LogFormats{"INFO"} = "<font color=yellow>INFO: %s</font>";
151: $LogFormats{"WARNING"} = "<font color=blue>WARNING: %s</font>";
152: $LogFormats{"DEFAULT"} = " %s ";
153:
154:
155:
156: =pod
157:
158: =head2 LogPerm
159:
160: Makes an entry into the permanent log file.
161:
162: =cut
163: sub LogPerm {
164: my $message=shift;
165: my $execdir=$perlvar{'lonDaemons'};
166: my $now=time;
167: my $local=localtime($now);
168: my $fh=IO::File->new(">>$execdir/logs/lonnet.perm.log");
169: print $fh "$now:$message:$local\n";
170: }
171:
172: =pod
173:
174: =head2 Log
175:
176: Logs a message to the log file.
177: Parameters:
178:
179: =item severity
180:
181: One of CRITICAL, WARNING, INFO, SUCCESS used to select the
182: format string used to format the message. if the severity is
183: not a defined severity the Default format string is used.
184:
185: =item message
186:
187: The base message. In addtion to the format string, the message
188: will be appended to a string containing the name of our remote
189: host and the time will be formatted into the message.
190:
191: =cut
192:
193: sub Log {
194: my $severity = shift;
195: my $message = shift;
196:
197: if(!$LogFormats{$severity}) {
198: $severity = "DEFAULT";
199: }
200:
201: my $format = $LogFormats{$severity};
202:
203: # Put the window dressing in in front of the message format:
204:
205: my $now = time;
206: my $local = localtime($now);
207: my $finalformat = "$local ($$) [$RemoteHost] [$Status] ";
208: my $finalformat = $finalformat.$format."\n";
209:
210: # open the file and put the result.
211:
212: my $execdir = $perlvar{'lonDaemons'};
213: my $fh = IO::File->new(">>$execdir/logs/lonc.log");
214: my $msg = sprintf($finalformat, $message);
215: $RecentLogEntry = $msg;
216: print $fh $msg;
217:
218:
219: }
220:
221:
222: =pod
223:
224: =head2 GetPeerName
225:
226: Returns the name of the host that a socket object is connected to.
227:
228: =cut
229:
230: sub GetPeername {
231: my $connection = shift;
232: my $AdrFamily = shift;
233: my $peer = $connection->peername();
234: my $peerport;
235: my $peerip;
236: if($AdrFamily == AF_INET) {
237: ($peerport, $peerip) = sockaddr_in($peer);
238: my $peername = gethostbyaddr($iaddr, $AdrFamily);
239: return $peername;
240: } elsif ($AdrFamily == AF_UNIX) {
241: my $peerfile;
242: ($peerfile) = sockaddr_un($peer);
243: return $peerfile;
244: }
245: }
246: #----------------------------- Timer management ------------------------
247: =pod
248:
249: =head2 Debug
250:
251: Invoked to issue a debug message.
252:
253: =cut
254:
255: sub Debug {
256: my $level = shift;
257: my $message = shift;
258: if ($level <= $DebugLevel) {
259: print $message." host = ".$RemoteHost."\n";
260: }
261: }
262:
263: sub SocketDump {
264: my $level = shift;
265: my $socket= shift;
266: if($level <= $DebugLevel) {
267: $socket->Dump();
268: }
269: }
270:
271: =pod
272:
273: =head2 ShowStatus
274:
275: Place some text as our pid status.
276: and as what we return in a SIGUSR1
277:
278: =cut
279: sub ShowStatus {
280: my $state = shift;
281: my $now = time;
282: my $local = localtime($now);
283: $Status = $local.": ".$state;
284: $0='lonc: '.$state.' '.$local;
285: }
286:
287: =pod
288:
289: =head2 Tick
290:
291: Invoked each timer tick.
292:
293: =cut
294:
295:
296: sub Tick {
297: my $client;
298: ShowStatus(GetServerHost()." Connection count: ".$ConnectionCount);
299:
300: # Is it time to prune connection count:
301:
302:
303: if($IdleConnections->Count() &&
304: ($WorkQueue->Count() == 0)) { # Idle connections and nothing to do?
305: $IdleSeconds++;
306: if($IdleSeconds > $IdleTimeout) { # Prune a connection...
307: $Socket = $IdleConnections->pop();
308: KillSocket($Socket);
309: }
310: } else {
311: $IdleSeconds = 0; # Reset idle count if not idle.
312: }
313:
314: # Do we have work in the queue, but no connections to service them?
315: # If so, try to make some new connections to get things going again.
316: #
317:
318: my $Requests = $WorkQueue->Count();
319: if (($ConnectionCount == 0) && ($Requests > 0)) {
320: if ($ConnectionRetriesLeft > 0) {
321: my $Connections = ($Requests <= $MaxConnectionCount) ?
322: $Requests : $MaxConnectionCount;
323: Debug(1,"Work but no connections, start ".$Connections." of them");
324: for ($i =0; $i < $Connections; $i++) {
325: MakeLondConnection();
326: }
327: } else {
328: Debug(1,"Work in queue, but gave up on connections..flushing\n");
329: EmptyQueue(); # Connections can't be established.
330: }
331:
332: }
333: }
334:
335: =pod
336:
337: =head2 SetupTimer
338:
339: Sets up a 1 per sec recurring timer event. The event handler is used to:
340:
341: =item
342:
343: Trigger timeouts on communications along active sockets.
344:
345: =item
346:
347: Trigger disconnections of idle sockets.
348:
349: =cut
350:
351: sub SetupTimer {
352: Debug(6, "SetupTimer");
353: Event->timer(interval => 1, debug => 1, cb => \&Tick );
354: }
355:
356: =pod
357:
358: =head2 ServerToIdle
359:
360: This function is called when a connection to the server is
361: ready for more work.
362:
363: If there is work in the Work queue the top element is dequeued
364: and the connection will start to work on it. If the work queue is
365: empty, the connection is pushed on the idle connection stack where
366: it will either get another work unit, or alternatively, if it sits there
367: long enough, it will be shut down and released.
368:
369: =cut
370:
371: sub ServerToIdle {
372: my $Socket = shift; # Get the socket.
373: delete($ActiveTransactions{$Socket}); # Server has no transaction
374:
375: &Debug(6, "Server to idle");
376:
377: # If there's work to do, start the transaction:
378:
379: $reqdata = $WorkQueue->dequeue(); # This is a LondTransaction
380: unless($reqdata eq undef) {
381: Debug(9, "Queue gave request data: ".$reqdata->getRequest());
382: &StartRequest($Socket, $reqdata);
383:
384: } else {
385:
386: # There's no work waiting, so push the server to idle list.
387: &Debug(8, "No new work requests, server connection going idle");
388: $IdleConnections->push($Socket);
389: }
390: }
391:
392: =pod
393:
394: =head2 ClientWritable
395:
396: Event callback for when a client socket is writable.
397:
398: This callback is established when a transaction reponse is
399: avaiable from lond. The response is forwarded to the unix socket
400: as it becomes writable in this sub.
401:
402: Parameters:
403:
404: =item Event
405:
406: The event that has been triggered. Event->w->data is
407: the data and Event->w->fd is the socket to write.
408:
409: =cut
410:
411: sub ClientWritable {
412: my $Event = shift;
413: my $Watcher = $Event->w;
414: my $Data = $Watcher->data;
415: my $Socket = $Watcher->fd;
416:
417: # Try to send the data:
418:
419: &Debug(6, "ClientWritable writing".$Data);
420: &Debug(9, "Socket is: ".$Socket);
421:
422: if($Socket->connected) {
423: my $result = $Socket->send($Data, 0);
424:
425: # $result undefined: the write failed.
426: # otherwise $result is the number of bytes written.
427: # Remove that preceding string from the data.
428: # If the resulting data is empty, destroy the watcher
429: # and set up a read event handler to accept the next
430: # request.
431:
432: &Debug(9,"Send result is ".$result." Defined: ".defined($result));
433: if(defined($result)) {
434: &Debug(9, "send result was defined");
435: if($result == length($Data)) { # Entire string sent.
436: &Debug(9, "ClientWritable data all written");
437: $Watcher->cancel();
438: #
439: # Set up to read next request from socket:
440:
441: my $descr = sprintf("Connection to lonc client %d",
442: $ActiveClients{$Socket});
443: Event->io(cb => \&ClientRequest,
444: poll => 'r',
445: desc => $descr,
446: data => "",
447: fd => $Socket);
448:
449: } else { # Partial string sent.
450: $Watcher->data(substr($Data, $result));
451: }
452:
453: } else { # Error of some sort...
454:
455: # Some errnos are possible:
456: my $errno = $!;
457: if($errno == POSIX::EWOULDBLOCK ||
458: $errno == POSIX::EAGAIN ||
459: $errno == POSIX::EINTR) {
460: # No action taken?
461: } else { # Unanticipated errno.
462: &Debug(5,"ClientWritable error or peer shutdown".$RemoteHost);
463: $Watcher->cancel; # Stop the watcher.
464: $Socket->shutdown(2); # Kill connection
465: $Socket->close(); # Close the socket.
466: }
467:
468: }
469: } else {
470: $Watcher->cancel(); # A delayed request...just cancel.
471: }
472: }
473:
474: =pod
475:
476: =head2 CompleteTransaction
477:
478: Called when the reply data has been received for a lond
479: transaction. The reply data must now be sent to the
480: ultimate client on the other end of the Unix socket. This is
481: done by setting up a writable event for the socket with the
482: data the reply data.
483:
484: Parameters:
485:
486: =item Socket
487:
488: Socket on which the lond transaction occured. This is a
489: LondConnection. The data received is in the TransactionReply member.
490:
491: =item Transaction
492:
493: The transaction that is being completed.
494:
495: =cut
496:
497: sub CompleteTransaction {
498: &Debug(6,"Complete transaction");
499: my $Socket = shift;
500: my $Transaction = shift;
501:
502: if (!$Transaction->isDeferred()) { # Normal transaction
503: my $data = $Socket->GetReply(); # Data to send.
504: StartClientReply($Transaction, $data);
505: } else { # Delete deferred transaction file.
506: Log("SUCCESS", "A delayed transaction was completed");
507: LogPerm("S:$Client:".$Transaction->getRequest());
508: unlink $Transaction->getFile();
509: }
510: }
511: =pod
512: =head1 StartClientReply
513:
514: Initiates a reply to a client where the reply data is a parameter.
515:
516: =head2 parameters:
517:
518: =item Transaction
519:
520: The transaction for which we are responding to the client.
521:
522: =item data
523:
524: The data to send to apached client.
525:
526: =cut
527: sub StartClientReply {
528: my $Transaction = shift;
529: my $data = shift;
530:
531:
532: my $Client = $Transaction->getClient();
533:
534: &Debug(8," Reply was: ".$data);
535: my $Serial = $ActiveClients{$Client};
536: my $desc = sprintf("Connection to lonc client %d",
537:
538: $Serial);
539: Event->io(fd => $Client,
540: poll => "w",
541: desc => $desc,
542: cb => \&ClientWritable,
543: data => $data);
544: }
545: =pod
546: =head2 FailTransaction
547:
548: Finishes a transaction with failure because the associated lond socket
549: disconnected. There are two possibilities:
550: - The transaction is deferred: in which case we just quietly
551: delete the transaction since there is no client connection.
552: - The transaction is 'live' in which case we initiate the sending
553: of "con_lost" to the client.
554:
555: Deleting the transaction means killing it from the
556: %ActiveTransactions hash.
557:
558: Parameters:
559:
560: =item client
561:
562: The LondTransaction we are failing.
563:
564: =cut
565:
566: sub FailTransaction {
567: my $transaction = shift;
568: Debug(1, "Failing transaction: ".$transaction->getRequest());
569: if (!$transaction->isDeferred()) { # If the transaction is deferred we'll get to it.
570: my $client = $transaction->getClient();
571: Debug(1," Replying con_lost to ".$transaction->getRequest());
572: StartClientReply($transaction, "con_lost\n");
573: }
574:
575: }
576:
577: =pod
578: =head1 EmptyQueue
579:
580: Fails all items in the work queue with con_lost.
581: Note that each item in the work queue is a transaction.
582:
583: =cut
584: sub EmptyQueue {
585: while($WorkQueue->Count()) {
586: my $request = $WorkQueue->dequeue(); # This is a transaction
587: FailTransaction($request);
588: }
589: }
590:
591: =pod
592:
593: =head2 CloseAllLondConnections
594:
595: Close all connections open on lond prior to exit e.g.
596:
597: =cut
598: sub CloseAllLondConnections {
599: foreach $Socket (keys %ActiveConnections) {
600: KillSocket($Socket);
601: }
602: }
603: =cut
604:
605: =pod
606:
607: =head2 KillSocket
608:
609: Destroys a socket. This function can be called either when a socket
610: has died of 'natural' causes or because a socket needs to be pruned due to
611: idleness. If the socket has died naturally, if there are no longer any
612: live connections a new connection is created (in case there are transactions
613: in the queue). If the socket has been pruned, it is never re-created.
614:
615: Parameters:
616:
617: =item Socket
618:
619: The socket to kill off.
620:
621: =item Restart
622:
623: nonzero if we are allowed to create a new connection.
624:
625:
626: =cut
627: sub KillSocket {
628: my $Socket = shift;
629:
630: $Socket->Shutdown();
631:
632: # If the socket came from the active connection set,
633: # delete its transaction... note that FailTransaction should
634: # already have been called!!!
635: # otherwise it came from the idle set.
636: #
637:
638: if(exists($ActiveTransactions{$Socket})) {
639: delete ($ActiveTransactions{$Socket});
640: }
641: if(exists($ActiveConnections{$Socket})) {
642: delete($ActiveConnections{$Socket});
643: }
644: $ConnectionCount--;
645:
646: # If the connection count has gone to zero and there is work in the
647: # work queue, the work all gets failed with con_lost.
648: #
649: if($ConnectionCount == 0) {
650: EmptyQueue;
651: }
652: }
653:
654: =pod
655:
656: =head2 LondReadable
657:
658: This function is called whenever a lond connection
659: is readable. The action is state dependent:
660:
661: =head3 State=Initialized
662:
663: We''re waiting for the challenge, this is a no-op until the
664: state changes.
665:
666: =head3 State=Challenged
667:
668: The challenge has arrived we need to transition to Writable.
669: The connection must echo the challenge back.
670:
671: =head3 State=ChallengeReplied
672:
673: The challenge has been replied to. The we are receiveing the
674: 'ok' from the partner.
675:
676: =head3 State=RequestingKey
677:
678: The ok has been received and we need to send the request for
679: an encryption key. Transition to writable for that.
680:
681: =head3 State=ReceivingKey
682:
683: The the key has been requested, now we are reading the new key.
684:
685: =head3 State=Idle
686:
687: The encryption key has been negotiated or we have finished
688: reading data from the a transaction. If the callback data has
689: a client as well as the socket iformation, then we are
690: doing a transaction and the data received is relayed to the client
691: before the socket is put on the idle list.
692:
693: =head3 State=SendingRequest
694:
695: I do not think this state can be received here, but if it is,
696: the appropriate thing to do is to transition to writable, and send
697: the request.
698:
699: =head3 State=ReceivingReply
700:
701: We finished sending the request to the server and now transition
702: to readable to receive the reply.
703:
704: The parameter to this function are:
705:
706: The event. Implicit in this is the watcher and its data. The data
707: contains at least the lond connection object and, if a
708: transaction is in progress, the socket attached to the local client.
709:
710: =cut
711:
712: sub LondReadable {
713:
714: my $Event = shift;
715: my $Watcher = $Event->w;
716: my $Socket = $Watcher->data;
717: my $client = undef;
718:
719: &Debug(6,"LondReadable called state = ".$State);
720:
721:
722: my $State = $Socket->GetState(); # All action depends on the state.
723:
724: SocketDump(6, $Socket);
725: my $status = $Socket->Readable();
726: &Debug(2, "Socket->Readable returned: $status");
727:
728: if($status != 0) {
729: # bad return from socket read. Currently this means that
730: # The socket has become disconnected. We fail the transaction.
731:
732: if(exists($ActiveTransactions{$Socket})) {
733: Debug(3,"Lond connection lost failing transaction");
734: FailTransaction($ActiveTransactions{$Socket});
735: }
736: $Watcher->cancel();
737: KillSocket($Socket);
738: return;
739: }
740: SocketDump(6,$Socket);
741:
742: $State = $Socket->GetState(); # Update in case of transition.
743: &Debug(6, "After read, state is ".$State);
744:
745: if($State eq "Initialized") {
746:
747:
748: } elsif ($State eq "ChallengeReceived") {
749: # The challenge must be echoed back; The state machine
750: # in the connection takes care of setting that up. Just
751: # need to transition to writable:
752:
753: $Watcher->cb(\&LondWritable);
754: $Watcher->poll("w");
755:
756: } elsif ($State eq "ChallengeReplied") {
757:
758:
759: } elsif ($State eq "RequestingKey") {
760: # The ok was received. Now we need to request the key
761: # That requires us to be writable:
762:
763: $Watcher->cb(\&LondWritable);
764: $Watcher->poll("w");
765:
766: } elsif ($State eq "ReceivingKey") {
767:
768: } elsif ($State eq "Idle") {
769: # If necessary, complete a transaction and then go into the
770: # idle queue.
771: $Watcher->cancel();
772: if(exists($ActiveTransactions{$Socket})) {
773: Debug(8,"Completing transaction!!");
774: CompleteTransaction($Socket,
775: $ActiveTransactions{$Socket});
776: } else {
777: Log("SUCCESS", "Connection ".$ConnectionCount." to "
778: .$RemoteHost." now ready for action");
779: }
780: ServerToIdle($Socket); # Next work unit or idle.
781:
782: } elsif ($State eq "SendingRequest") {
783: # We need to be writable for this and probably don't belong
784: # here inthe first place.
785:
786: Deubg(6, "SendingRequest state encountered in readable");
787: $Watcher->poll("w");
788: $Watcher->cb(\&LondWritable);
789:
790: } elsif ($State eq "ReceivingReply") {
791:
792:
793: } else {
794: # Invalid state.
795: Debug(4, "Invalid state in LondReadable");
796: }
797: }
798:
799: =pod
800:
801: =head2 LondWritable
802:
803: This function is called whenever a lond connection
804: becomes writable while there is a writeable monitoring
805: event. The action taken is very state dependent:
806:
807: =head3 State = Connected
808:
809: The connection is in the process of sending the 'init' hailing to the
810: lond on the remote end. The connection object''s Writable member is
811: called. On error, ConnectionError is called to destroy the connection
812: and remove it from the ActiveConnections hash
813:
814: =head3 Initialized
815:
816: 'init' has been sent, writability monitoring is removed and
817: readability monitoring is started with LondReadable as the callback.
818:
819: =head3 ChallengeReceived
820:
821: The connection has received the who are you challenge from the remote
822: system, and is in the process of sending the challenge
823: response. Writable is called.
824:
825: =head3 ChallengeReplied
826:
827: The connection has replied to the initial challenge The we switch to
828: monitoring readability looking for the server to reply with 'ok'.
829:
830: =head3 RequestingKey
831:
832: The connection is in the process of requesting its encryption key.
833: Writable is called.
834:
835: =head3 ReceivingKey
836:
837: The connection has sent the request for a key. Switch to readability
838: monitoring to accept the key
839:
840: =head3 SendingRequest
841:
842: The connection is in the process of sending a request to the server.
843: This request is part of a client transaction. All the states until
844: now represent the client setup protocol. Writable is called.
845:
846: =head3 ReceivingReply
847:
848: The connection has sent a request. Now it must receive a reply.
849: Readability monitoring is requested.
850:
851: This function is an event handler and therefore receives as
852: a parameter the event that has fired. The data for the watcher
853: of this event is a reference to a list of one or two elements,
854: depending on state. The first (and possibly only) element is the
855: socket. The second (present only if a request is in progress)
856: is the socket on which to return a reply to the caller.
857:
858: =cut
859:
860: sub LondWritable {
861: my $Event = shift;
862: my $Watcher = $Event->w;
863: my $Socket = $Watcher->data;
864: my $State = $Socket->GetState();
865:
866: Debug(6,"LondWritable State = ".$State."\n");
867:
868:
869: # Figure out what to do depending on the state of the socket:
870:
871:
872:
873:
874: SocketDump(6,$Socket);
875:
876: if ($State eq "Connected") {
877:
878: if ($Socket->Writable() != 0) {
879: # The write resulted in an error.
880: # We'll treat this as if the socket got disconnected:
881: Log("WARNING", "Connection to ".$RemoteHost.
882: " has been disconnected");
883: $Watcher->cancel();
884: KillSocket($Socket);
885: return;
886: }
887: # "init" is being sent...
888:
889:
890: } elsif ($State eq "Initialized") {
891:
892: # Now that init was sent, we switch
893: # to watching for readability:
894:
895: $Watcher->cb(\&LondReadable);
896: $Watcher->poll("r");
897:
898: } elsif ($State eq "ChallengeReceived") {
899: # We received the challenge, now we
900: # are echoing it back. This is a no-op,
901: # we're waiting for the state to change
902:
903: if($Socket->Writable() != 0) {
904:
905: $Watcher->cancel();
906: KillSocket($Socket);
907: return;
908: }
909:
910: } elsif ($State eq "ChallengeReplied") {
911: # The echo was sent back, so we switch
912: # to watching readability.
913:
914: $Watcher->cb(\&LondReadable);
915: $Watcher->poll("r");
916:
917: } elsif ($State eq "RequestingKey") {
918: # At this time we're requesting the key.
919: # again, this is essentially a no-op.
920: # we'll write the next chunk until the
921: # state changes.
922:
923: if($Socket->Writable() != 0) {
924: # Write resulted in an error.
925:
926: $Watcher->cancel();
927: KillSocket($Socket);
928: return;
929:
930: }
931: } elsif ($State eq "ReceivingKey") {
932: # Now we need to wait for the key
933: # to come back from the peer:
934:
935: $Watcher->cb(\&LondReadable);
936: $Watcher->poll("r");
937:
938: } elsif ($State eq "SendingRequest") {
939: # At this time we are sending a request to the
940: # peer... write the next chunk:
941:
942: if($Socket->Writable() != 0) {
943:
944: if(exists($ActiveTransactions{$Socket})) {
945: Debug(3, "Lond connection lost, failing transactions");
946: FailTransaction($ActiveTransactions{$Socket});
947: }
948: $Watcher->cancel();
949: KillSocket($Socket);
950: return;
951:
952: }
953:
954: } elsif ($State eq "ReceivingReply") {
955: # The send has completed. Wait for the
956: # data to come in for a reply.
957: Debug(8,"Writable sent request/receiving reply");
958: $Watcher->cb(\&LondReadable);
959: $Watcher->poll("r");
960:
961: } else {
962: # Control only passes here on an error:
963: # the socket state does not match any
964: # of the known states... so an error
965: # must be logged.
966:
967: &Debug(4, "Invalid socket state ".$State."\n");
968: }
969:
970: }
971: =pod
972:
973: =cut
974: sub QueueDelayed {
975: Debug(3,"QueueDelayed called");
976:
977: my $path = "$perlvar{'lonSockDir'}/delayed";
978:
979: Debug(4, "Delayed path: ".$path);
980: opendir(DIRHANDLE, $path);
981:
982: @alldelayed = grep /\.$RemoteHost$/, readdir DIRHANDLE;
983: Debug(4, "Got ".$alldelayed." delayed files");
984: closedir(DIRHANDLE);
985: my $dfname;
986: my $reqfile;
987: foreach $dfname (sort @alldelayed) {
988: $reqfile = "$path/$dfname";
989: Debug(4, "queueing ".$reqfile);
990: my $Handle = IO::File->new($reqfile);
991: my $cmd = <$Handle>;
992: chomp $cmd; # There may or may not be a newline...
993: $cmd = $cmd."\n"; # now for sure there's exactly one newline.
994: my $Transaction = LondTransaction->new($cmd);
995: $Transaction->SetDeferred($reqfile);
996: QueueTransaction($Transaction);
997: }
998:
999: }
1000:
1001: =pod
1002:
1003: =head2 MakeLondConnection
1004:
1005: Create a new lond connection object, and start it towards its initial
1006: idleness. Once idle, it becomes elligible to receive transactions
1007: from the work queue. If the work queue is not empty when the
1008: connection is completed and becomes idle, it will dequeue an entry and
1009: start off on it.
1010:
1011: =cut
1012:
1013: sub MakeLondConnection {
1014: Debug(4,"MakeLondConnection to ".GetServerHost()." on port "
1015: .GetServerPort());
1016:
1017: my $Connection = LondConnection->new(&GetServerHost(),
1018: &GetServerPort());
1019:
1020: if($Connection == undef) { # Needs to be more robust later.
1021: Log("CRITICAL","Failed to make a connection with lond.");
1022: $ConnectionRetriesLeft--;
1023: return 0; # Failure.
1024: } else {
1025: $ConnectionRetriesLeft = $ConnectionRetries; # success resets the count
1026: # The connection needs to have writability
1027: # monitored in order to send the init sequence
1028: # that starts the whole authentication/key
1029: # exchange underway.
1030: #
1031: my $Socket = $Connection->GetSocket();
1032: if($Socket == undef) {
1033: die "did not get a socket from the connection";
1034: } else {
1035: &Debug(9,"MakeLondConnection got socket: ".$Socket);
1036: }
1037:
1038:
1039: $event = Event->io(fd => $Socket,
1040: poll => 'w',
1041: cb => \&LondWritable,
1042: data => $Connection,
1043: desc => 'Connection to lond server');
1044: $ActiveConnections{$Connection} = $event;
1045:
1046: $ConnectionCount++;
1047: Debug(4, "Connection count = ".$ConnectionCount);
1048: if($ConnectionCount == 1) { # First Connection:
1049: QueueDelayed;
1050: }
1051: Log("SUCESS", "Created connection ".$ConnectionCount
1052: ." to host ".GetServerHost());
1053: return 1; # Return success.
1054: }
1055:
1056: }
1057:
1058: =pod
1059:
1060: =head2 StartRequest
1061:
1062: Starts a lond request going on a specified lond connection.
1063: parameters are:
1064:
1065: =item $Lond
1066:
1067: Connection to the lond that will send the transaction and receive the
1068: reply.
1069:
1070: =item $Client
1071:
1072: Connection to the client that is making this request We got the
1073: request from this socket, and when the request has been relayed to
1074: lond and we get a reply back from lond it will get sent to this
1075: socket.
1076:
1077: =item $Request
1078:
1079: The text of the request to send.
1080:
1081: =cut
1082:
1083: sub StartRequest {
1084: my $Lond = shift;
1085: my $Request = shift; # This is a LondTransaction.
1086:
1087: Debug(6, "StartRequest: ".$Request->getRequest());
1088:
1089: my $Socket = $Lond->GetSocket();
1090:
1091: $Request->Activate($Lond);
1092: $ActiveTransactions{$Lond} = $Request;
1093:
1094: $Lond->InitiateTransaction($Request->getRequest());
1095: $event = Event->io(fd => $Socket,
1096: poll => "w",
1097: cb => \&LondWritable,
1098: data => $Lond,
1099: desc => "lond transaction connection");
1100: $ActiveConnections{$Lond} = $event;
1101: Debug(8," Start Request made watcher data with ".$event->data."\n");
1102: }
1103:
1104: =pod
1105:
1106: =head2 QueueTransaction
1107:
1108: If there is an idle lond connection, it is put to work doing this
1109: transaction. Otherwise, the transaction is placed in the work queue.
1110: If placed in the work queue and the maximum number of connections has
1111: not yet been created, a new connection will be started. Our goal is
1112: to eventually have a sufficient number of connections that the work
1113: queue will typically be empty. parameters are:
1114:
1115: =item Socket
1116:
1117: open on the lonc client.
1118:
1119: =item Request
1120:
1121: data to send to the lond.
1122:
1123: =cut
1124:
1125: sub QueueTransaction {
1126:
1127: my $requestData = shift; # This is a LondTransaction.
1128: my $cmd = $requestData->getRequest();
1129:
1130: Debug(6,"QueueTransaction: ".$cmd);
1131:
1132: my $LondSocket = $IdleConnections->pop();
1133: if(!defined $LondSocket) { # Need to queue request.
1134: Debug(8,"Must queue...");
1135: $WorkQueue->enqueue($requestData);
1136: if($ConnectionCount < $MaxConnectionCount) {
1137: Debug(4,"Starting additional lond connection");
1138: MakeLondConnection();
1139: }
1140: } else { # Can start the request:
1141: Debug(8,"Can start...");
1142: StartRequest($LondSocket, $requestData);
1143: }
1144: }
1145:
1146: #-------------------------- Lonc UNIX socket handling ---------------------
1147:
1148: =pod
1149:
1150: =head2 ClientRequest
1151: Callback that is called when data can be read from the UNIX domain
1152: socket connecting us with an apache server process.
1153:
1154: =cut
1155:
1156: sub ClientRequest {
1157: Debug(6, "ClientRequest");
1158: my $event = shift;
1159: my $watcher = $event->w;
1160: my $socket = $watcher->fd;
1161: my $data = $watcher->data;
1162: my $thisread;
1163:
1164: Debug(9, " Watcher named: ".$watcher->desc);
1165:
1166: my $rv = $socket->recv($thisread, POSIX::BUFSIZ, 0);
1167: Debug(8, "rcv: data length = ".length($thisread)
1168: ." read =".$thisread);
1169: unless (defined $rv && length($thisread)) {
1170: # Likely eof on socket.
1171: Debug(5,"Client Socket closed on lonc for ".$RemoteHost);
1172: close($socket);
1173: $watcher->cancel();
1174: delete($ActiveClients{$socket});
1175: return;
1176: }
1177: Debug(8,"Data: ".$data." this read: ".$thisread);
1178: $data = $data.$thisread; # Append new data.
1179: $watcher->data($data);
1180: if($data =~ /(.*\n)/) { # Request entirely read.
1181: if($data eq "close_connection_exit\n") {
1182: Log("CRITICAL",
1183: "Request Close Connection ... exiting");
1184: CloseAllLondConnections();
1185: exit;
1186: }
1187: Debug(8, "Complete transaction received: ".$data);
1188: my $Transaction = LondTransaction->new($data);
1189: $Transaction->SetClient($socket);
1190: QueueTransaction($Transaction);
1191: $watcher->cancel(); # Done looking for input data.
1192: }
1193:
1194: }
1195:
1196:
1197: =pod
1198:
1199: =head2 NewClient
1200:
1201: Callback that is called when a connection is received on the unix
1202: socket for a new client of lonc. The callback is parameterized by the
1203: event.. which is a-priori assumed to be an io event, and therefore has
1204: an fd member that is the Listener socket. We Accept the connection
1205: and register a new event on the readability of that socket:
1206:
1207: =cut
1208:
1209: sub NewClient {
1210: Debug(6, "NewClient");
1211: my $event = shift; # Get the event parameters.
1212: my $watcher = $event->w;
1213: my $socket = $watcher->fd; # Get the event' socket.
1214: my $connection = $socket->accept(); # Accept the client connection.
1215: Debug(8,"Connection request accepted from "
1216: .GetPeername($connection, AF_UNIX));
1217:
1218:
1219: my $description = sprintf("Connection to lonc client %d",
1220: $ClientConnection);
1221: Debug(9, "Creating event named: ".$description);
1222: Event->io(cb => \&ClientRequest,
1223: poll => 'r',
1224: desc => $description,
1225: data => "",
1226: fd => $connection);
1227: $ActiveClients{$connection} = $ClientConnection;
1228: $ClientConnection++;
1229: }
1230:
1231: =pod
1232:
1233: =head2 GetLoncSocketPath
1234:
1235: Returns the name of the UNIX socket on which to listen for client
1236: connections.
1237:
1238: =cut
1239:
1240: sub GetLoncSocketPath {
1241: return $UnixSocketDir."/".GetServerHost();
1242: }
1243:
1244: =pod
1245:
1246: =head2 GetServerHost
1247:
1248: Returns the host whose lond we talk with.
1249:
1250: =cut
1251:
1252: sub GetServerHost {
1253: return $RemoteHost; # Setup by the fork.
1254: }
1255:
1256: =pod
1257:
1258: =head2 GetServerPort
1259:
1260: Returns the lond port number.
1261:
1262: =cut
1263:
1264: sub GetServerPort {
1265: return $perlvar{londPort};
1266: }
1267:
1268: =pod
1269:
1270: =head2 SetupLoncListener
1271:
1272: Setup a lonc listener event. The event is called when the socket
1273: becomes readable.. that corresponds to the receipt of a new
1274: connection. The event handler established will accept the connection
1275: (creating a communcations channel), that int turn will establish
1276: another event handler to subess requests.
1277:
1278: =cut
1279:
1280: sub SetupLoncListener {
1281:
1282: my $socket;
1283: my $SocketName = GetLoncSocketPath();
1284: unlink($SocketName);
1285: unless ($socket =IO::Socket::UNIX->new(Local => $SocketName,
1286: Listen => 10,
1287: Type => SOCK_STREAM)) {
1288: die "Failed to create a lonc listner socket";
1289: }
1290: Event->io(cb => \&NewClient,
1291: poll => 'r',
1292: desc => 'Lonc listener Unix Socket',
1293: fd => $socket);
1294: }
1295:
1296: =pod
1297:
1298: =head2 ChildStatus
1299:
1300: Child USR1 signal handler to report the most recent status
1301: into the status file.
1302:
1303: =cut
1304: sub ChildStatus {
1305: my $event = shift;
1306: my $watcher = $event->w;
1307:
1308: Debug(2, "Reporting child status because : ".$watcher->data);
1309: my $docdir = $perlvar{'lonDocRoot'};
1310: my $fh = IO::File->new(">>$docdir/lon-status/loncstatus.txt");
1311: print $fh $$."\t".$RemoteHost."\t".$Status."\t".
1312: $RecentLogEntry."\n";
1313: }
1314:
1315: =pod
1316:
1317: =head2 SignalledToDeath
1318:
1319: Called in response to a signal that causes a chid process to die.
1320:
1321: =cut
1322:
1323:
1324: sub SignalledToDeath {
1325: my $event = shift;
1326: my $watcher= $event->w;
1327:
1328: Debug(2,"Signalled to death! via ".$watcher->data);
1329: my ($signal) = @_;
1330: chomp($signal);
1331: Log("CRITICAL", "Abnormal exit. Child $$ for $RemoteHost "
1332: ."died through "."\"$signal\"");
1333: LogPerm("F:lonc: $$ on $RemoteHost signalled to death: "
1334: ."\"$signal\"");
1335: die("Signal abnormal end");
1336: exit 0;
1337:
1338: }
1339: =head2 ChildProcess
1340:
1341: This sub implements a child process for a single lonc daemon.
1342:
1343: =cut
1344:
1345: sub ChildProcess {
1346:
1347:
1348: #
1349: # Signals must be handled by the Event framework...
1350: #
1351: # $SIG{QUIT} = \&SignalledToDeath;
1352: # $SIG{HUP} = \&ChildStatus;
1353: # $SIG{USR1} = IGNORE;
1354: # $SIG{INT} = DEFAULT;
1355: # $SIG{CHLD} = IGNORE;
1356: # $SIG{__DIE__} = \&SignalledToDeath;
1357:
1358: Event->signal(signal => "QUIT",
1359: cb => \&SignalledToDeath,
1360: data => "QUIT");
1361: Event->signal(signal => "HUP",
1362: cb => \&ChildStatus,
1363: data => "HUP");
1364: Event->signal(signal => "USR1",
1365: cb => \&ChildStatus,
1366: data => "USR1");
1367:
1368: SetupTimer();
1369:
1370: SetupLoncListener();
1371:
1372: $Event::Debuglevel = $DebugLevel;
1373:
1374: Debug(9, "Making initial lond connection for ".$RemoteHost);
1375:
1376: # Setup the initial server connection:
1377:
1378: # &MakeLondConnection(); // let first work requirest do it.
1379:
1380:
1381: Debug(9,"Entering event loop");
1382: my $ret = Event::loop(); # Start the main event loop.
1383:
1384:
1385: die "Main event loop exited!!!";
1386: }
1387:
1388: # Create a new child for host passed in:
1389:
1390: sub CreateChild {
1391: my $sigset = POSIX::SigSet->new(SIGINT);
1392: sigprocmask(SIG_BLOCK, $sigset);
1393: my $host = shift;
1394: $RemoteHost = $host;
1395: Log("CRITICAL", "Forking server for ".$host);
1396: $pid = fork;
1397: if($pid) { # Parent
1398: $ChildHash{$pid} = $RemoteHost;
1399: sigprocmask(SIG_UNBLOCK, $sigset);
1400:
1401: } else { # child.
1402: ShowStatus("Connected to ".$RemoteHost);
1403: $SIG{INT} = DEFAULT;
1404: sigprocmask(SIG_UNBLOCK, $sigset);
1405: ChildProcess; # Does not return.
1406: }
1407:
1408: }
1409: #
1410: # Parent process logic pass 1:
1411: # For each entry in the hosts table, we will
1412: # fork off an instance of ChildProcess to service the transactions
1413: # to that host. Each pid will be entered in a global hash
1414: # with the value of the key, the host.
1415: # The parent will then enter a loop to wait for process exits.
1416: # Each exit gets logged and the child gets restarted.
1417: #
1418:
1419: #
1420: # Fork and start in new session so hang-up isn't going to
1421: # happen without intent.
1422: #
1423:
1424:
1425:
1426:
1427:
1428:
1429: ShowStatus("Forming new session");
1430: my $childpid = fork;
1431: if ($childpid != 0) {
1432: sleep 4; # Give child a chacne to break to
1433: exit 0; # a new sesion.
1434: }
1435: #
1436: # Write my pid into the pid file so I can be located
1437: #
1438:
1439: ShowStatus("Parent writing pid file:");
1440: $execdir = $perlvar{'lonDaemons'};
1441: open (PIDSAVE, ">$execdir/logs/lonc.pid");
1442: print PIDSAVE "$$\n";
1443: close(PIDSAVE);
1444:
1445: if (POSIX::setsid() < 0) {
1446: print "Could not create new session\n";
1447: exit -1;
1448: }
1449:
1450: ShowStatus("Forking node servers");
1451:
1452: Log("CRITICAL", "--------------- Starting children ---------------");
1453:
1454: my $HostIterator = LondConnection::GetHostIterator;
1455: while (! $HostIterator->end()) {
1456:
1457: $hostentryref = $HostIterator->get();
1458: CreateChild($hostentryref->[0]);
1459: $HostIterator->next();
1460: }
1461: $RemoteHost = "Parent Server";
1462:
1463: # Maintain the population:
1464:
1465: ShowStatus("Parent keeping the flock");
1466:
1467: #
1468: # Set up parent signals:
1469: #
1470:
1471: $SIG{INT} = \&Terminate;
1472: $SIG{TERM} = \&Terminate;
1473: $SIG{HUP} = \&Restart;
1474: $SIG{USR1} = \&CheckKids;
1475:
1476: while(1) {
1477: $deadchild = wait();
1478: if(exists $ChildHash{$deadchild}) { # need to restart.
1479: $deadhost = $ChildHash{$deadchild};
1480: delete($ChildHash{$deadchild});
1481: Log("WARNING","Lost child pid= ".$deadchild.
1482: "Connected to host ".$deadhost);
1483: Log("INFO", "Restarting child procesing ".$deadhost);
1484: CreateChild($deadhost);
1485: }
1486: }
1487:
1488:
1489:
1490: =pod
1491:
1492: =head1 CheckKids
1493:
1494: Since kids do not die as easily in this implementation
1495: as the previous one, there is no need to restart the
1496: dead ones (all dead kids get restarted when they die!!)
1497: The only thing this function does is to pass USR1 to the
1498: kids so that they report their status.
1499:
1500: =cut
1501:
1502: sub CheckKids {
1503: Debug(2, "Checking status of children");
1504: my $docdir = $perlvar{'lonDocRoot'};
1505: my $fh = IO::File->new(">$docdir/lon-status/loncstatus.txt");
1506: my $now=time;
1507: my $local=localtime($now);
1508: print $fh "LONC status $local - parent $$ \n\n";
1509: foreach $pid (keys %ChildHash) {
1510: Debug(2, "Sending USR1 -> $pid");
1511: kill 'USR1' => $pid; # Tell Child to report status.
1512: sleep 1; # Wait so file doesn't intermix.
1513: }
1514: }
1515:
1516: =pod
1517:
1518: =head1 Restart
1519:
1520: Signal handler for HUP... all children are killed and
1521: we self restart. This is an el-cheapo way to re read
1522: the config file.
1523:
1524: =cut
1525:
1526: sub Restart {
1527: KillThemAll; # First kill all the children.
1528: Log("CRITICAL", "Restarting");
1529: my $execdir = $perlvar{'lonDaemons'};
1530: unlink("$execdir/logs/lonc.pid");
1531: exec("$execdir/lonc");
1532: }
1533:
1534: =pod
1535:
1536: =head1 KillThemAll
1537:
1538: Signal handler that kills all children by sending them a
1539: SIGINT. Responds to sigint and sigterm.
1540:
1541: =cut
1542:
1543: sub KillThemAll {
1544: Debug(2, "Kill them all!!");
1545: local($SIG{CHLD}) = 'IGNORE'; # Our children >will< die.
1546: foreach $pid (keys %ChildHash) {
1547: my $serving = $ChildHash{$pid};
1548: Debug(2, "Killing lonc for $serving pid = $pid");
1549: ShowStatus("Killing lonc for $serving pid = $pid");
1550: Log("CRITICAL", "Killing lonc for $serving pid = $pid");
1551: kill('INT', $pid);
1552: delete($ChildeHash{$pid});
1553: }
1554: my $execdir = $perlvar{'lonDaemons'};
1555: unlink("$execdir/logs/lonc.pid");
1556: ShowStatus("Killing the master process");
1557: Log("CRITICAL", "Killing the master process.");
1558: }
1559:
1560: =pod
1561:
1562: =head1 Terminate
1563:
1564: Terminate the system.
1565:
1566: =cut
1567:
1568: sub Terminate {
1569: KillThemAll;
1570: exit;
1571:
1572: }
1573: =pod
1574:
1575: =head1 Theory
1576:
1577: The event class is used to build this as a single process with an
1578: event driven model. The following events are handled:
1579:
1580: =item UNIX Socket connection Received
1581:
1582: =item Request data arrives on UNIX data transfer socket.
1583:
1584: =item lond connection becomes writable.
1585:
1586: =item timer fires at 1 second intervals.
1587:
1588: All sockets are run in non-blocking mode. Timeouts managed by the timer
1589: handler prevents hung connections.
1590:
1591: Key data structures:
1592:
1593: =item RequestQueue
1594:
1595: A queue of requests received from UNIX sockets that are
1596: waiting for a chance to be forwarded on a lond connection socket.
1597:
1598: =item ActiveConnections
1599:
1600: A hash of lond connections that have transactions in process that are
1601: available to be timed out.
1602:
1603: =item ActiveTransactions
1604:
1605: A hash indexed by lond connections that contain the client reply
1606: socket for each connection that has an active transaction on it.
1607:
1608: =item IdleConnections
1609:
1610: A hash of lond connections that have no work to do. These connections
1611: can be closed if they are idle for a long enough time.
1612:
1613: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>