6 use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
10 our $env_name = 'PREFORK_INTERP';
17 syslog(LOG_INFO, "$0 prefork [$$]: $m, quitting");
21 # Returns in the executor process
22 sub become_monitor () {
23 my $child = fork // fail("fork executor: $!");or
26 open ::STDIN , "<& $call_fds[0]" or fail("dup for fd0");
27 open ::STDOUT, ">& $call_fds[1]" or fail("dup for fd1");
28 open ::STDERR, ">& $call_fds[2]" or fail("dup for fd2");
29 POSIX::close($_) foreach @call_fds;
34 #---- monitor [2] ----
35 my $got = waitpid $child, 0 // fail("wait for executor: $!");
36 $got == $child or fail("wait for esecutor gave $got, expected $child");
38 protocol_write(pack "L", $?);
42 sub protocol_write ($) {
44 return if (print CALL $d and flush CALL);
45 _exit(0) if $!==EPIPE || $!==ECONNRESET;
46 fail("protocol write: $!");
53 return $r if defined $r;
59 sub protocol_read_fail ($) {
61 _exit(0) if $!==ECONNRESET;
62 fail("recv $what: $!");
65 sub protocol_exchange () {
72 $r = IO::FDPass::recv(fileno(CALL));
75 protocol_read_fail("fd $_");
81 $r = read(CALL, $len, 4) // protocol_read_fail("message length");
84 $len = unpack "L", $len;
86 $r = read(CALL, $data, $len) // protocol_read_fail("message data ($len)");
87 $r == $len or _exit(0);
89 @ARGV = split /\0/, $data;
90 @ARGV >= 2 or fail("message data has too few strings");
91 length(pop(@ARGV)) and fail("message data missing trailing nul");
93 while (my $s = shift @ARGV) {
95 $s =~ m/=/ or fail("message data env var missing equals");
100 sub initialisation_complete {
103 # if env var not set, we're not running under prefork-interp
104 my @env_params = split /,/, ($ENV{$env_name} // return), 5;
105 croak "$env_name has too few entries" unless @env_params == 5;
107 $num_servers = $opts{max_servers} // 4;
109 #---- setup (pm) [1] ----
111 $socket_path = pop @env_params;
112 foreach (@env_params) {
113 $_ eq ($_+0) or croak "$env_name contains $_, not a number";
115 open LISTEN, "<>&=$env_params[0]" or croak "listen fd: $!";
116 open CALL, "<>&=$env_params[1]" or croak "listen fd: $!";
117 @call_fds = ($env_params[2], $env_params[3], 2);
119 if (!$opts{no_openlog}) {
120 openlog("prefork-interp $0", 'ndelay,nofatal,pid',
121 $opts{log_facility} // 'log_user');
124 open NULL, "<>/dev/null" or croak "open /dev/null: $!";
126 #---- fork for server ----
128 my $child = fork // croak "first fork failed: $!";
130 #---- setup (pm) [2], exits ----
134 #---- server(pm) [1] ----
136 $child = fork // croak "second fork failed: $!";
138 # we are the child, i.e. the one fa-monitor
139 return become_monitor();
143 $children{$child} = 1;
145 # --- server(pm) [2] ----
147 setsid() > 0 or fail("setsid: $!");
148 open STDIN, "<&NULL" or fail("dup null onto stdin: $!");
149 open STDOUT, ">&NULL" or fail("dup null onto stdout: $!");
150 open STDERR, ">&NULL" or fail("dup null onto stderr: $!");
153 POSIX::close($_) foreach @call_fds;
156 my $flags = fcntl(LISTEN, F_GETFL, 0) or fail("F_GETFL listen socket: $!");
157 $flags |= O_NONBLOCK;
158 fcntl(LISTEN, F_SETFL, $flags) or fail("F_SETFL listen socket: $!");
165 my $full = %children >= $num_servers;
166 my $got = waitpid -1, ($full ? 0 : WNOHANG);
169 "$0 prefork [$$]: monitor process [$got] failed with wait status $?");
171 if (!exists $children{$got}) {
173 "$0 prefork [$$]: monitor process [$got] wasn't one of ours?!");
175 delete $children{$got};
179 # select for accepting or housekeeping timeout
181 vec($rbits, fileno(LISTEN), 1) = 1;
183 my $nfound = select($rbits, '', $ebits, ($opts{check_interval} // 300));
186 if (accept(CALL, LISTEN)) {
187 $child = fork // fail("fork for accepted call failed: $!");
189 #---- monitor [1] ----
192 return become_monitor();
196 $children{$child} = 1;
197 } elsif ($! == EINTR || $! == EAGAIN || $! == EWOULDBLOCK) {
199 syslog(LOG_WARNING, "$0 prefork [$$]: accept failed: $!");
200 if ($errcount > ($opts{max_errors} // 100)) {
202 "$0 prefork [$$]: too many accept failures, quitting");
210 my @st_listen = stat(LISTEN) // fail("fstat listening socket: $!");
211 my @st_socket = stat($socket_path) // do {
212 if ($! == ENOENT) { server_quit("socket $socket is ENOENT"); }
213 fail("stat socket $socket: $!");
215 if ("@st_listen[0..2]" ne "@st_socket[0..2]") {
216 server_quit("socket $socket is no longer ours");