| 1 | #! /usr/bin/perl -w | ||
| 2 | # | ||
| 3 | # | ||
| 4 | # COPYRIGHT UNIVERSITY OF MANCHESTER, 2003 | ||
| 5 | # | ||
| 6 | # Author: Mark Mc Keown | ||
| 7 | # mark.mckeown@man.ac.uk | ||
| 8 | # | ||
| 9 | |||
| 10 | use HTTP::Daemon; | ||
| 11 | use WSRF::Lite; | ||
| 12 | use POSIX; | ||
| 13 | use File::Temp; | ||
| 14 | use WSRF::SSLDaemon; | ||
| 15 | |||
| 16 | $ENV{PATH}= ""; | ||
| 17 | |||
| 18 | #$ENV{'WSRF_MODULES'} = "/tmp/WSRF-Lite/modules"; | ||
| 19 | |||
| 20 | #Check that the path to the Grid Service Modules is set | ||
| 21 | if ( !defined($ENV{'WSRF_MODULES'}) ) | ||
| 22 | { | ||
| 23 | die "Enviromental Variable WSRF_MODULES not defined"; | ||
| 24 | } | ||
| 25 | |||
| 26 | |||
| 27 | # This is our lock file - we use File::Temp because POSIX::tmpnam is bad!! | ||
| 28 | # The UNLINK option means the file will be destroyed on exit. | ||
| 29 | my $file = new File::Temp(); | ||
| 30 | |||
| 31 | print "Temp File= $file\n"; | ||
| 32 | |||
| 33 | |||
| 34 | #ignore broken pipes | ||
| 35 | $SIG{PIPE} = 'IGNORE'; | ||
| 36 | |||
| 37 | #create the Service Container - just a Web Server | ||
| 38 | #my $server = HTTP::Daemon->new( | ||
| 39 | # LocalPort => '50000', | ||
| 40 | # Listen => SOMAXCONN, | ||
| 41 | # Reuse => 1 | ||
| 42 | # ) || die "ERROR $!\n"; | ||
| 43 | |||
| 44 | my $server = WSRF::SSLDaemon->new( | ||
| 45 | LocalPort => '50000', | ||
| 46 | Listen => SOMAXCONN, | ||
| 47 | Reuse => 1, | ||
| 48 | SSL_key_file => '/home/zzcgumk/.globus/hostkey.pem', | ||
| 49 | SSL_cert_file => '/home/zzcgumk/.globus/hostcert.pem', | ||
| 50 | SSL_ca_file => '/etc/grid-security/certificates/01621954.0', | ||
| 51 | SSL_verify_mode => 0x00 | ||
| 52 | ) || die "ERROR $!\n"; | ||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | #Store the Container Address in the ENV variables - child | ||
| 58 | #processes can then pick it up and use it | ||
| 59 | $ENV{'URL'} = $server->url; | ||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | #Global Variables | ||
| 64 | $PREFORK = 10; # number of children to maintain | ||
| 65 | $MAX_CLIENTS_PER_CHILD = 1000; # number of clients each child should process | ||
| 66 | %children = (); # keys are current child process IDs | ||
| 67 | $children = 0; # current number of children | ||
| 68 | |||
| 69 | |||
| 70 | sub REAPER { # takes care of dead children | ||
| 71 | local $!; | ||
| 72 | while( defined( my $pid = waitpid(-1,POSIX::WNOHANG()) ) ) | ||
| 73 | { | ||
| 74 | last unless $pid > 0; | ||
| 75 | $children --; | ||
| 76 | print "Child $pid died\n"; | ||
| 77 | delete $children{$pid}; | ||
| 78 | } | ||
| 79 | $SIG{CHLD} = \&REAPER; | ||
| 80 | } | ||
| 81 | |||
| 82 | $SIG{PIPE} = 'IGNORE'; | ||
| 83 | |||
| 84 | sub HUNTSMAN { # signal handler for SIGINT | ||
| 85 | local($SIG{CHLD}) = 'IGNORE'; # we're going to kill our children | ||
| 86 | foreach my $key ( keys %children) | ||
| 87 | { | ||
| 88 | print " Killing $key\n"; | ||
| 89 | kill 'INT' => $key; | ||
| 90 | } | ||
| 91 | exit; # clean up with dignity | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | # Fork off our children. | ||
| 96 | for (1 .. $PREFORK) { | ||
| 97 | make_new_child(); | ||
| 98 | } | ||
| 99 | |||
| 100 | # Install signal handlers. | ||
| 101 | $SIG{CHLD} = \&REAPER; | ||
| 102 | $SIG{INT} = \&HUNTSMAN; | ||
| 103 | |||
| 104 | # And maintain the population. | ||
| 105 | while (1) { | ||
| 106 | sleep; # wait for a signal (i.e., child's death) | ||
| 107 | for ($i = $children; $i < $PREFORK; $i++) { | ||
| 108 | make_new_child(); # top up the child pool | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | print "\nContainer Contact Address: ", $server->url, "\n"; | ||
| 113 | |||
| 114 | sub make_new_child { | ||
| 115 | my $pid; | ||
| 116 | my $sigset; | ||
| 117 | |||
| 118 | # block signal for fork | ||
| 119 | $sigset = POSIX::SigSet->new(SIGINT); | ||
| 120 | sigprocmask(SIG_BLOCK, $sigset) | ||
| 121 | or die "Can't block SIGINT for fork: $!\n"; | ||
| 122 | |||
| 123 | |||
| 124 | die "fork: $!" unless defined ($pid = fork); | ||
| 125 | |||
| 126 | if ($pid) { | ||
| 127 | # Parent records the child's birth and returns. | ||
| 128 | sigprocmask(SIG_UNBLOCK, $sigset) | ||
| 129 | or die "Can't unblock SIGINT for fork: $!\n"; | ||
| 130 | $children{$pid} = 1; | ||
| 131 | $children++; | ||
| 132 | print "Parent $$ created Client handler - $pid\n"; | ||
| 133 | return; | ||
| 134 | } else { | ||
| 135 | # Child can *not* return from this subroutine. | ||
| 136 | $SIG{INT} = 'DEFAULT'; # make SIGINT kill us as it did before | ||
| 137 | |||
| 138 | # unblock signals | ||
| 139 | sigprocmask(SIG_UNBLOCK, $sigset) | ||
| 140 | or die "Can't unblock SIGINT for fork: $!\n"; | ||
| 141 | |||
| 142 | # handle connections until we've reached $MAX_CLIENTS_PER_CHILD | ||
| 143 | for ($i=0; $i < $MAX_CLIENTS_PER_CHILD; $i++) { | ||
| 144 | my ($client); | ||
| 145 | |||
| 146 | # local *LOCK; | ||
| 147 | # open(LOCK,"$file") or die "Cannot open lock file $file\n"; | ||
| 148 | # flock(LOCK,Fcntl::LOCK_EX()) or die "Couldn't get lock on file $file\n"; | ||
| 149 | |||
| 150 | print "$$ got lock\n"; | ||
| 151 | $client = $server->accept(); | ||
| 152 | |||
| 153 | # flock(LOCK,Fcntl::LOCK_UN()); | ||
| 154 | next if $!{EINTR}; | ||
| 155 | next if !$client; | ||
| 156 | print "$$ client= $client\n"; | ||
| 157 | |||
| 158 | my $resp = WSRF::Container::handle($client->get_request); | ||
| 159 | my $result = $client->send_response($resp); | ||
| 160 | print "result= $result\n"; | ||
| 161 | |||
| 162 | print "closing <$client>\n"; | ||
| 163 | $client->close; | ||
| 164 | undef $client; | ||
| 165 | } | ||
| 166 | |||
| 167 | exit; | ||
| 168 | } | ||
| 169 | } | ||
| 170 |