root/honeyclient/tags/exp/PRE-kindlund-firefox/bin/StartAgent.pl

Revision 409, 2.5 kB (checked in by kindlund, 2 years ago)

Merged kindlund-filesystem branch back into trunk.

  • Property svn:executable set to *
  • Property svn:keywords set to Id "$file"
Line 
1 #!perl -w -Ilib
2
3 # $Id$
4
5 use strict;
6 use warnings;
7 use Carp ();
8
9 use HoneyClient::Util::Config qw(getVar);
10 use HoneyClient::Agent;
11 use HoneyClient::Util::SOAP qw(getClientHandle);
12 use Data::Dumper;
13 use MIME::Base64 qw(decode_base64 encode_base64);
14 use Storable qw(thaw nfreeze);
15 use Log::Log4perl qw(:easy);
16
17 # The global logging object.
18 our $LOG = get_logger();
19
20 our ($stub, $som);
21 our $URL = HoneyClient::Agent->init();
22
23 our $agentState = undef;
24 my $tempState = undef;
25 our $faultDetected = 0;
26
27 print "URL: " . $URL. "\n";
28
29 sub _watchdogFaultHandler {
30
31     # Extract arguments.
32     my ($class, $res) = @_;
33
34     # Construct error message.
35     # Figure out if the error occurred in transport or over
36     # on the other side.
37     my $errMsg = $class->transport->status; # Assume transport error.
38
39     if (ref $res) {
40         $errMsg = $res->faultcode . ": ".  $res->faultstring . "\n";
41     }
42
43     if (!$faultDetected) {
44         $LOG->error("Watchdog fault detected, recovering Agent daemon.");
45         $faultDetected = 1;
46     }
47     # XXX: Reenable this, eventually.
48     $LOG->error(__PACKAGE__ . "->_watchdogFaultHandler(): Error occurred during processing.\n" . $errMsg);
49     Carp::carp __PACKAGE__ . "->_watchdogFaultHandler(): Error occurred during processing.\n" . $errMsg;
50
51
52     # Regardless of the error, destroy the Agent process and reinitialize it.
53     # XXX: Sanity check this, eventually.
54     HoneyClient::Agent->destroy();
55
56     # Wait for a small amount of time, in order for the killed process to release
57     # its control of the bound TCP port.
58     sleep 5;
59
60     $URL = HoneyClient::Agent->init();
61
62     # Restore state information.
63     $som = $stub->updateState(encode_base64(nfreeze($agentState)));
64 }
65
66 $stub = getClientHandle(address   => 'localhost',
67                         namespace => 'HoneyClient::Agent',
68                         fault_handler => \&_watchdogFaultHandler);
69                
70 for (;;) {
71     # TODO: Make this a programmatic value.
72     sleep (5);
73     $som = $stub->getState();
74     if (defined($som) and (ref($som) eq "SOAP::SOM")) {
75         $tempState = $som->result();
76         if (defined($tempState)) {
77             # Make sure the new state is parsable, before saving it.
78             eval {
79                 $tempState = thaw(decode_base64($tempState));
80             };
81             if (!$@) {
82                 $agentState = $tempState;
83             }
84         }
85     }
86 }
87
88 HoneyClient::Agent->destroy();
Note: See TracBrowser for help on using the browser.