4 our @ISA = qw(Exporter);
5 our @EXPORT = qw(initialisation_complete);
8 use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
9 use POSIX qw(_exit setsid);
14 our $env_name = 'PREFORK_INTERP';
21 print STDERR "$0: prefork [$$]: $m\n";
28 syslog(LOG_INFO, "$0 prefork [$$]: $m, quitting");
32 # Returns in the executor process
33 sub become_monitor () {
34 my $child = fork // fail("fork executor: $!");
37 open ::STDIN , "<& $call_fds[0]" or fail("dup for fd0");
38 open ::STDOUT, ">& $call_fds[1]" or fail("dup for fd1");
39 open ::STDERR, ">& $call_fds[2]" or fail("dup for fd2");
40 POSIX::close($_) foreach @call_fds;
45 #---- monitor [2] ----
46 my $got = waitpid $child, 0 // fail("wait for executor: $!");
47 $got == $child or fail("wait for esecutor gave $got, expected $child");
49 protocol_write(pack "L", $?);
53 sub protocol_write ($) {
55 return if (print CALL $d and flush CALL);
56 _exit(0) if $!==EPIPE || $!==ECONNRESET;
57 fail("protocol write: $!");
64 return $r if defined $r;
70 sub protocol_read_fail ($) {
72 _exit(0) if $!==ECONNRESET;
73 fail("recv $what: $!");
76 sub protocol_exchange () {
83 $r = IO::FDPass::recv(fileno(CALL));
86 protocol_read_fail("fd $_");
92 $r = read(CALL, $len, 4) // protocol_read_fail("message length");
95 $len = unpack "L", $len;
97 $r = read(CALL, $data, $len) // protocol_read_fail("message data ($len)");
98 $r == $len or _exit(0);
100 @ARGV = split /\0/, $data;
101 @ARGV >= 2 or fail("message data has too few strings");
102 length(pop(@ARGV)) and fail("message data missing trailing nul");
104 while (my $s = shift @ARGV) {
106 $s =~ m/=/ or fail("message data env var missing equals");
111 sub initialisation_complete {
114 # if env var not set, we're not running under prefork-interp
115 my @env_params = split /,/, ($ENV{$env_name} // return), 5;
116 croak "$env_name has too few entries" unless @env_params == 5;
118 $num_servers = $opts{max_servers} // 4;
120 #---- setup (pm) [1] ----
122 $socket_path = pop @env_params;
123 foreach (@env_params) {
124 $_ eq ($_+0) or croak "$env_name contains $_, not a number";
126 open LISTEN, "+>&=$env_params[0]" or croak "listen fd: $!";
127 open CALL, "+>&=$env_params[1]" or croak "listen fd: $!";
128 @call_fds = ($env_params[2], $env_params[3], 2);
130 if (!$opts{no_openlog}) {
131 openlog("prefork-interp $0", 'ndelay,nofatal,pid',
132 $opts{log_facility} // 'log_user');
135 open NULL, "+>/dev/null" or croak "open /dev/null: $!";
137 #---- fork for server ----
139 my $child = fork // croak "first fork failed: $!";
141 #---- setup (pm) [2], exits ----
145 #---- server(pm) [1] ----
147 $child = fork // croak "second fork failed: $!";
149 # we are the child, i.e. the one fa-monitor
150 return become_monitor();
154 $children{$child} = 1;
156 # --- server(pm) [2] ----
158 setsid() > 0 or fail("setsid: $!");
159 open STDIN, "<&NULL" or fail("dup null onto stdin: $!");
160 open STDOUT, ">&NULL" or fail("dup null onto stdout: $!");
161 open STDERR, ">&NULL" or fail("dup null onto stderr: $!");
164 POSIX::close($_) foreach @call_fds;
167 my $flags = fcntl(LISTEN, F_GETFL, 0) or fail("F_GETFL listen socket: $!");
168 $flags |= O_NONBLOCK;
169 fcntl(LISTEN, F_SETFL, $flags) or fail("F_SETFL listen socket: $!");
176 my $full = %children >= $num_servers;
177 my $got = waitpid -1, ($full ? 0 : WNOHANG);
180 "$0 prefork [$$]: monitor process [$got] failed with wait status $?");
182 if (!exists $children{$got}) {
184 "$0 prefork [$$]: monitor process [$got] wasn't one of ours?!");
186 delete $children{$got};
190 # select for accepting or housekeeping timeout
192 vec($rbits, fileno(LISTEN), 1) = 1;
194 my $nfound = select($rbits, '', $ebits, ($opts{check_interval} // 300));
197 if (accept(CALL, LISTEN)) {
198 $child = fork // fail("fork for accepted call failed: $!");
200 #---- monitor [1] ----
203 return become_monitor();
207 $children{$child} = 1;
208 } elsif ($! == EINTR || $! == EAGAIN || $! == EWOULDBLOCK) {
210 syslog(LOG_WARNING, "$0 prefork [$$]: accept failed: $!");
211 if ($errcount > ($opts{max_errors} // 100)) {
213 "$0 prefork [$$]: too many accept failures, quitting");
221 my @st_listen = stat(LISTEN) // fail("fstat listening socket: $!");
222 my @st_socket = stat($socket_path) // do {
223 if ($! == ENOENT) { server_quit("socket $socket is ENOENT"); }
224 fail("stat socket $socket: $!");
226 if ("@st_listen[0..2]" ne "@st_socket[0..2]") {
227 server_quit("socket $socket is no longer ours");