2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Tests for binding Unix domain sockets
7 use PublicInbox::TestCommon;
8 use Errno qw(EADDRINUSE);
11 use Fcntl qw(FD_CLOEXEC F_SETFD);
12 require_mods(qw(Plack::Util Plack::Builder HTTP::Date HTTP::Status));
15 my ($tmpdir, $for_destroy) = tmpdir();
16 my $unix = "$tmpdir/unix.sock";
17 my $psgi = './t/httpd-corner.psgi';
18 my $out = "$tmpdir/out.log";
19 my $err = "$tmpdir/err.log";
22 my $register_exit_fifo = sub {
24 my $sock = new_sock($s);
25 ok($sock->write("GET /exit-fifo$f HTTP/1.0\r\n\r\n"),
27 ok($sock->read(my $buf, 4096), 'read exit-fifo response');
28 like($buf, qr!\r\n\r\nfifo \Q$f\E registered\z!, 'set exit fifo');
31 my $spawn_httpd = sub {
33 my $cmd = [ '-httpd', @args, "--stdout=$out", "--stderr=$err", $psgi ];
34 $td = start_script($cmd);
38 require PublicInbox::Daemon;
39 my $l = "$tmpdir/named.sock";
40 my $s = IO::Socket::UNIX->new(Listen => 5, Local => $l,
42 is(PublicInbox::Daemon::sockname($s), $l, 'sockname works for UNIX');
45 ok(!-S $unix, 'UNIX socket does not exist, yet');
46 my $f1 = "$tmpdir/f1";
49 local $ENV{TEST_OPEN_FIFO} = $f1;
50 $spawn_httpd->("-l$unix", '-W0');
51 open my $fh, '<', $f1 or xbail "open($f1): $!";
52 is(my $hi = <$fh>, "hi\n", 'got FIFO greeting');
54 ok(-S $unix, 'UNIX socket was bound by -httpd');
57 IO::Socket::UNIX->new(Peer => $_[0], Type => SOCK_STREAM)
58 // xbail "E: $! connecting to $_[0]";
63 my $sock = new_sock($unix);
64 ok($sock->write("GET /host-port HTTP/1.0\r\n\r\n"),
65 'wrote req to server');
66 ok($sock->read(my $buf, 4096), 'read response');
67 like($buf, qr!\r\n\r\n127\.0\.0\.1 0\z!,
68 'set REMOTE_ADDR and REMOTE_PORT for Unix socket');
73 { # do not clobber existing socket
74 my %err = ( 'linux' => EADDRINUSE, 'freebsd' => EADDRINUSE );
75 open my $out, '>>', "$tmpdir/1" or die "redirect failed: $!";
76 open my $err, '>>', "$tmpdir/2" or die "redirect failed: $!";
77 my $cmd = ['-httpd', '-l', $unix, '-W0', $psgi];
78 my $ftd = start_script($cmd, undef, { 1 => $out, 2 => $err });
80 isnt($?, 0, 'httpd failure set $?');
83 skip("not sure if $^O fails with EADDRINUSE", 1);
84 is($? >> 8, $ec, 'httpd failed with EADDRINUSE');
86 open my $fh, "$tmpdir/2" or die "failed to open $tmpdir/2: $!";
89 like($e, qr/no listeners bound/i, 'got error message');
90 is(-s "$tmpdir/1", 0, 'stdout was empty');
94 is($td->kill, 1, 'terminate existing process');
96 is($?, 0, 'existing httpd exited successfully');
97 ok(-S $unix, 'unix socket still exists');
100 # portable Perl can delay or miss signal dispatches due to races,
101 # so disable some tests on systems lacking signalfd(2) or EVFILT_SIGNAL
102 my $has_sigfd = PublicInbox::Sigfd->new({}, 0) ? 1 : $ENV{TEST_UNRELIABLE};
110 } until (time > $end);
111 Carp::confess('condition failed');
115 require_mods('Net::Server::Daemonize', 52);
116 $has_sigfd or skip('signalfd / EVFILT_SIGNAL not available', 52);
117 my $pid_file = "$tmpdir/pid";
120 open my $fh, '<', $f or die "open $f failed: $!";
121 my $pid = do { local $/; <$fh> };
122 chomp($pid) or die("pid file not ready $!");
126 for my $w (qw(-W0 -W1)) {
127 pipe(my ($p0, $p1)) or xbail "pipe: $!";
128 fcntl($p1, F_SETFD, 0) or xbail "fcntl: $!"; # clear FD_CLOEXEC
129 # wait for daemonization
130 $spawn_httpd->("-l$unix", '-D', '-P', $pid_file, $w);
131 close $p1 or xbail "close: $!";
133 is($?, 0, "daemonized $w process");
135 ok(-s $pid_file, "$w pid file written");
136 my $pid = $read_pid->($pid_file);
137 is(kill('TERM', $pid), 1, "signaled daemonized $w process");
138 vec(my $rvec = '', fileno($p0), 1) = 1;
139 delete $td->{-extra}; # drop tail(1) process
140 is(select($rvec, undef, undef, 1), 1, 'timeout for pipe HUP');
141 is(my $undef = <$p0>, undef, 'process closed pipe writer at exit');
142 ok(!-e $pid_file, "$w pid file unlinked at exit");
145 my $httpd = abs_path('blib/script/public-inbox-httpd');
146 $psgi = abs_path($psgi);
147 my $opt = { run_mode => 0 };
148 my @args = ("-l$unix", '-D', '-P', $pid_file, -1, $out, -2, $err);
150 if ('USR2 upgrades with workers') {
151 pipe(my ($p0, $p1)) or xbail "pipe: $!";
152 fcntl($p1, F_SETFD, 0) or xbail "fcntl: $!"; # clear FD_CLOEXEC
154 $td = start_script([$httpd, @args, $psgi], undef, $opt);
155 close($p1) or xbail "close: $!";
157 is($?, 0, "daemonized process again");
159 ok(-s $pid_file, 'pid file written');
160 my $pid = $read_pid->($pid_file);
162 # stop worker to ensure check_sock below hits $new_pid
163 kill('TTOU', $pid) or die "TTOU failed: $!";
165 kill('USR2', $pid) or die "USR2 failed: $!";
167 $pid != (eval { $read_pid->($pid_file) } // $pid)
169 my $new_pid = $read_pid->($pid_file);
170 isnt($new_pid, $pid, 'new child started');
171 ok($new_pid > 0, '$new_pid valid');
172 delay_until(sub { -s "$pid_file.oldbin" });
173 my $old_pid = $read_pid->("$pid_file.oldbin");
174 is($old_pid, $pid, '.oldbin pid file written');
175 ok($old_pid > 0, '$old_pid valid');
177 check_sock($unix); # ensures $new_pid is ready to receive signals
179 # first, back out of the upgrade
180 kill('QUIT', $new_pid) or die "kill new PID failed: $!";
182 $pid == (eval { $read_pid->($pid_file) } // 0)
184 is($read_pid->($pid_file), $pid, 'old PID file restored');
185 ok(!-f "$pid_file.oldbin", '.oldbin PID file gone');
188 kill('USR2', $pid) or die "USR2 failed: $!";
190 $pid != (eval { $read_pid->($pid_file) } // $pid)
192 $new_pid = $read_pid->($pid_file);
193 isnt($new_pid, $pid, 'new child started again');
194 $old_pid = $read_pid->("$pid_file.oldbin");
195 is($old_pid, $pid, '.oldbin pid file written');
197 # drop the old parent
198 kill('QUIT', $old_pid) or die "QUIT failed: $!";
199 delay_until(sub { !kill(0, $old_pid) }); # UGH
201 ok(!-f "$pid_file.oldbin", '.oldbin PID file gone');
205 kill('QUIT', $new_pid) or die "QUIT failed: $!";
207 vec(my $rvec = '', fileno($p0), 1) = 1;
208 is(select($rvec, undef, undef, 1), 1, 'timeout for pipe HUP');
209 is(my $u = <$p0>, undef, 'process closed pipe writer at exit');
211 ok(!-f $pid_file, 'PID file is gone');
214 if ('try USR2 without workers (-W0)') {
215 pipe(my ($p0, $p1)) or xbail "pipe: $!";
216 fcntl($p1, F_SETFD, 0) or xbail "fcntl: $!"; # clear FD_CLOEXEC
217 $td = start_script([$httpd, @args, '-W0', $psgi], undef, $opt);
218 close $p1 or xbail "close: $!";
220 is($?, 0, 'daemonized w/o workers');
221 $register_exit_fifo->($unix, $f1);
222 my $pid = $read_pid->($pid_file);
224 # replace running process
225 kill('USR2', $pid) or xbail "USR2 failed: $!";
226 open my $fh, '<', $f1 or xbail "open($f1): $!";
227 is(my $bye = <$fh>, "bye from $pid\n", 'got FIFO bye');
230 $pid = $read_pid->($pid_file);
231 kill('QUIT', $pid) or xbail "USR2 failed: $!";
233 vec(my $rvec = '', fileno($p0), 1) = 1;
234 is(select($rvec, undef, undef, 1), 1, 'timeout for pipe HUP');
235 is(my $u = <$p0>, undef, 'process closed pipe writer at exit');
236 ok(!-f $pid_file, 'PID file is gone');