# signal handler for child die events $SIG{TERM} = $SIG{INT} = \&do_term; $SIG{HUP} = \&do_hup; our($listen_socket); $listen_socket = HTTP::Daemon->new(LocalPort => PORT) or die "Can't create a listening socket: $@" unless $listen_socket; my $pid = init_server(PIDFILE, USER, @groups); my $port = PORT; log_notice("App Server accepting connections on port $port\n"); while (my $connection = $listen_socket->accept) { my $host = $connection->peerhost; my $child = launch_child(undef, undef); if ($child == 0) { $listen_socket->close; log_notice("App Srv : Accepting a connection from $host\n"); my ($fnm, $prg) = launch_app($connection); my $child = launch_child(undef, undef); if ($child == 0) { log_notice("Otools App Srv : Running $prg\n"); ($<, $>) = ($>, $<); # Switch EUid & RUid. $> = $<; log_notice("Uid = $< : Eid = $>\n"); log_notice("Gid = $( : EGid = $)\n"); exec($prg, $fnm, $host) or log_die("App Srv : Exec failed. Died"); } log_notice("App Srv : Connection from $host finished\n"); exit 0; } } continue { $connection->close; } #### sub become_daemon { croak "Can't fork" unless defined (my $child = fork); exit 0 if $child; # parent dies; POSIX::setsid(); # become session leader open(STDIN,"/dev/null"); open(STDERR,">&STDOUT"); $CWD = getcwd; # remember working directory chdir '/'; # change working directory umask(0); # forget file mode creation mask $ENV{PATH} = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin'; delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'}; $SIG{CHLD} = \&reap_child; }