| 1 | #! /usr/bin/env perl | ||
| 2 | # | ||
| 3 | # | ||
| 4 | # COPYRIGHT UNIVERSITY OF MANCHESTER, 2003 | ||
| 5 | # | ||
| 6 | # Author: Mark Mc Keown | ||
| 7 | # mark.mckeown@man.ac.uk | ||
| 8 | # | ||
| 9 | # LICENCE TERMS | ||
| 10 | # | ||
| 11 | # WSRF::Lite is free software; you can redistribute it | ||
| 12 | # and/or modify it under the same terms as Perl itself. | ||
| 13 | # | ||
| 14 | # | ||
| 15 | # forking Container script - this script forks a new process to | ||
| 16 | # handle each new client connection. Based on HTTP server example | ||
| 17 | # from Perl Cookbook - changes where need to deal with SIGNAL | ||
| 18 | # issues in Perl 5.8 | ||
| 19 | |||
| 20 | use HTTP::Daemon; | ||
| 21 | use Sys::Hostname::Long; | ||
| 22 | use WSRF::Lite; | ||
| 23 | use POSIX ":sys_wait_h"; | ||
| 24 | use strict; | ||
| 25 | |||
| 26 | my $TIMEOUT = "120"; | ||
| 27 | |||
| 28 | $ENV{'TZ'} = "GMT"; | ||
| 29 | $ENV{'PATH'} = ""; | ||
| 30 | |||
| 31 | #Check that the path to the service Modules is set | ||
| 32 | if ( !defined( $ENV{'WSRF_MODULES'} ) ) { | ||
| 33 | die "Enviromental Variable WSRF_MODULES not defined"; | ||
| 34 | } | ||
| 35 | |||
| 36 | my @nsconfig = do $ENV{'WSRF_MODULES'} . "/modnscfg.pl"; | ||
| 37 | for (@nsconfig) { | ||
| 38 | my $ns = $_->{'namespace'}; | ||
| 39 | my $module = $_->{'module'}; | ||
| 40 | $WSRF::Constants::ModuleNamespaceMap{$ns} = $module; | ||
| 41 | } | ||
| 42 | |||
| 43 | print "Mapping of namespaces to modules:\n"; | ||
| 44 | foreach my $ns ( keys %WSRF::Constants::ModuleNamespaceMap ) { | ||
| 45 | print " NS: " . $ns | ||
| 46 | . " => Module: " | ||
| 47 | . $WSRF::Constants::ModuleNamespaceMap{$ns} . "\n"; | ||
| 48 | } | ||
| 49 | |||
| 50 | if ( defined( $ENV{'WSRF_SOCKETS'} ) ) { | ||
| 51 | $WSRF::Constants::SOCKETS_DIRECTORY = $ENV{'WSRF_SOCKETS'}; | ||
| 52 | } | ||
| 53 | |||
| 54 | if ( defined( $ENV{'WSRF_DATA'} ) ) { | ||
| 55 | $WSRF::Constants::Data = $ENV{'WSRF_DATA'}; | ||
| 56 | } else { | ||
| 57 | $WSRF::Constants::Data = $WSRF::Constants::SOCKETS_DIRECTORY . "/data/"; | ||
| 58 | } | ||
| 59 | |||
| 60 | #check the user has set up the directories for holding | ||
| 61 | #the sockets and state files. | ||
| 62 | if ( !-d $WSRF::Constants::SOCKETS_DIRECTORY ) { | ||
| 63 | die "Directory $WSRF::Constants::SOCKETS_DIRECTORY does not exist\n"; | ||
| 64 | } | ||
| 65 | if ( !-d $WSRF::Constants::Data ) { | ||
| 66 | die "Directory $WSRF::Constants::Data does not exist\n"; | ||
| 67 | } | ||
| 68 | |||
| 69 | my $port = 50000; | ||
| 70 | |||
| 71 | my ( $hostname, $daemon ); | ||
| 72 | while ( my $arg = shift @ARGV ) { | ||
| 73 | if ( $arg =~ m/-p/o ) { | ||
| 74 | $port = shift @ARGV; | ||
| 75 | die "No Port number provided with -p option\n" unless defined $port; | ||
| 76 | die "\"$port\" does not look like a port number\n" | ||
| 77 | unless ( $port =~ /\d+/o ); | ||
| 78 | } elsif ( $arg =~ m/-h/o ) { | ||
| 79 | $hostname = shift @ARGV; | ||
| 80 | die "No hostname provided with -h option\n" unless defined $hostname; | ||
| 81 | } elsif ( $arg =~ m/-d/o ) { | ||
| 82 | $daemon = 1; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | # REAPER kills of stray children. | ||
| 87 | sub REAPER { | ||
| 88 | local $!; | ||
| 89 | waitpid( -1, 0 ); | ||
| 90 | $SIG{CHLD} = \&REAPER; # still loathe sysV | ||
| 91 | } | ||
| 92 | |||
| 93 | # set thes signal handler to deal with dead children | ||
| 94 | $SIG{CHLD} = \&REAPER; | ||
| 95 | |||
| 96 | sub HUPPER { | ||
| 97 | local $SIG{HUP} = 'IGNORE'; | ||
| 98 | kill( 1, -$$ ); | ||
| 99 | exit; | ||
| 100 | } | ||
| 101 | |||
| 102 | $SIG{HUP} = \&HUPPER; | ||
| 103 | |||
| 104 | sub become_daemon { | ||
| 105 | my $child = fork; | ||
| 106 | die "Can't fork: $!" unless defined($child); | ||
| 107 | exit(0) if $child; # parent dies; | ||
| 108 | POSIX::setsid(); # become session leader | ||
| 109 | open( STDIN, "</dev/null" ); | ||
| 110 | open( STDOUT, ">/dev/null" ); | ||
| 111 | open( STDERR, '>&STDOUT' ); | ||
| 112 | chdir '/'; # change working directory | ||
| 113 | $ENV{PATH} = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin'; | ||
| 114 | delete @ENV{ 'IFS', 'CDPATH', 'ENV', 'BASH_ENV' }; | ||
| 115 | } | ||
| 116 | |||
| 117 | if ( defined($daemon) ) { | ||
| 118 | print STDOUT "Running as daemon process\n"; | ||
| 119 | become_daemon(); | ||
| 120 | } | ||
| 121 | |||
| 122 | #create the Service Container - just a Web Server | ||
| 123 | my $d = HTTP::Daemon->new( | ||
| 124 | LocalPort => $port, | ||
| 125 | Listen => SOMAXCONN, | ||
| 126 | Reuse => 1 | ||
| 127 | ) | ||
| 128 | || die "ERROR $!\n"; | ||
| 129 | |||
| 130 | #Store the Container Address in the ENV variables - child | ||
| 131 | #processes can then pick it up and use it | ||
| 132 | $hostname = Sys::Hostname::Long::hostname_long() unless defined $hostname; | ||
| 133 | $hostname = "localhost" unless defined $hostname; | ||
| 134 | $ENV{'URL'} = "http://" . $hostname . ":" . $port . "/"; | ||
| 135 | |||
| 136 | #Override the above if you do not like the URL | ||
| 137 | # $d->url has given you | ||
| 138 | # $ENV{'URL'} = "http://localhost:50000/"; | ||
| 139 | |||
| 140 | print "\nContainer Contact Address: " . $ENV{'URL'} . "\n"; | ||
| 141 | |||
| 142 | # HTTP::Daemon ISA IO::Socket::INET so treat like a socket | ||
| 143 | # Thanks to Jonathan Chin for $!{EINTR} | ||
| 144 | while ( my $client = $d->accept || $!{EINTR} ) { | ||
| 145 | next if $!{EINTR}; # just a child exiting, go back to sleep. | ||
| 146 | print "$$ Got Connection\n"; | ||
| 147 | if ( my $pid = fork ) { #fork a process to deal with request | ||
| 148 | print "$$ Parent forked\n"; #parent should go back to accept now | ||
| 149 | print "$$ Closing socket\n"; | ||
| 150 | $client->close; | ||
| 151 | undef $client; | ||
| 152 | print "$$ Going back to accept\n"; | ||
| 153 | } elsif ( defined($pid) ) #child | ||
| 154 | { | ||
| 155 | print "$$ Child created " . scalar( localtime(time) ) . "\n"; | ||
| 156 | print "$$ Closing server socket " . $d->close . " $?\n"; | ||
| 157 | undef($d); | ||
| 158 | |||
| 159 | #clients may want to keep the HTTP connection open | ||
| 160 | #however we put in a timeout | ||
| 161 | while ( my $r = $client->get_request ) { | ||
| 162 | my $crap = alarm 0; | ||
| 163 | my $response = | ||
| 164 | WSRF::Container::handle( $r, $client ); #handle request | ||
| 165 | my $err = $client->send_response($response); | ||
| 166 | print "$$ Sent response $err\n"; | ||
| 167 | alarm($TIMEOUT); | ||
| 168 | } | ||
| 169 | print "$$ Child closing socket\n"; | ||
| 170 | $client->close; | ||
| 171 | undef($client); | ||
| 172 | print "$$ Exiting\n"; | ||
| 173 | exit; | ||
| 174 | } else { #fork failed | ||
| 175 | print "$$ Fork failed\n"; | ||
| 176 | } | ||
| 177 | } | ||
| 178 |