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
6 use PublicInbox::TestCommon;
7 use Errno qw(EADDRINUSE);
10 use autodie qw(close);
11 require_mods(qw(Plack::Util Plack::Builder HTTP::Date HTTP::Status));
14 require PublicInbox::Sigfd;
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({}) ? 1 : $ENV{TEST_UNRELIABLE};
103 PublicInbox::DS::Reset() if $has_sigfd;
106 my ($cond, $msg) = @_;
111 } until (time > $end);
112 Carp::confess($msg // 'condition failed');
116 require_mods('Net::Server::Daemonize', 52);
117 $has_sigfd or skip('signalfd / EVFILT_SIGNAL not available', 52);
118 my $pid_file = "$tmpdir/pid";
121 open my $fh, '<', $f or die "open $f failed: $!";
122 my $pid = do { local $/; <$fh> };
123 chomp($pid) or die("pid file not ready $!");
127 for my $w (qw(-W0 -W1)) {
128 my ($p0, $p1) = quit_waiter_pipe;
129 # wait for daemonization
130 $spawn_httpd->("-l$unix", '-D', '-P', $pid_file, $w);
133 is($?, 0, "daemonized $w process");
135 ok(-s $pid_file, "$w pid file written");
136 my $pid = $read_pid->($pid_file);
137 no_pollerfd($pid) if $w eq '-W1';
138 is(kill('TERM', $pid), 1, "signaled daemonized $w process");
139 delete $td->{-extra}; # drop tail(1) process
140 wait_for_eof($p0, "httpd $w quit pipe");
141 ok(!-e $pid_file, "$w pid file unlinked at exit");
144 my $httpd = abs_path('blib/script/public-inbox-httpd');
145 $psgi = abs_path($psgi);
146 my $opt = { run_mode => 0 };
147 my @args = ("-l$unix", '-D', '-P', $pid_file, -1, $out, -2, $err);
149 if ('USR2 upgrades with workers') {
150 my ($p0, $p1) = quit_waiter_pipe;
151 $td = start_script([$httpd, @args, $psgi], undef, $opt);
154 is($?, 0, "daemonized process again");
156 ok(-s $pid_file, 'pid file written');
157 my $pid = $read_pid->($pid_file);
159 # stop worker to ensure check_sock below hits $new_pid
160 kill('TTOU', $pid) or die "TTOU failed: $!";
162 kill('USR2', $pid) or die "USR2 failed: $!";
164 $pid != (eval { $read_pid->($pid_file) } // $pid)
166 my $new_pid = $read_pid->($pid_file);
167 isnt($new_pid, $pid, 'new child started');
168 ok($new_pid > 0, '$new_pid valid');
169 delay_until(sub { -s "$pid_file.oldbin" });
170 my $old_pid = $read_pid->("$pid_file.oldbin");
171 is($old_pid, $pid, '.oldbin pid file written');
172 ok($old_pid > 0, '$old_pid valid');
174 check_sock($unix); # ensures $new_pid is ready to receive signals
176 # first, back out of the upgrade
177 kill('QUIT', $new_pid) or die "kill new PID failed: $!";
179 $pid == (eval { $read_pid->($pid_file) } // 0)
182 delay_until(sub { !kill(0, $new_pid) }, 'new PID really died');
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) }, 'old PID really died');
201 ok(!-f "$pid_file.oldbin", '.oldbin PID file gone');
205 kill('QUIT', $new_pid) or die "QUIT failed: $!";
207 wait_for_eof($p0, 'new process');
208 ok(!-f $pid_file, 'PID file is gone');
211 if ('try USR2 without workers (-W0)') {
212 my ($p0, $p1) = quit_waiter_pipe;
213 $td = start_script([$httpd, @args, '-W0', $psgi], undef, $opt);
216 is($?, 0, 'daemonized w/o workers');
217 $register_exit_fifo->($unix, $f1);
218 my $pid = $read_pid->($pid_file);
220 # replace running process
221 kill('USR2', $pid) or xbail "USR2 failed: $!";
222 open my $fh, '<', $f1 or xbail "open($f1): $!";
223 is(my $bye = <$fh>, "bye from $pid\n", 'got FIFO bye');
226 $pid = $read_pid->($pid_file);
227 kill('QUIT', $pid) or xbail "USR2 failed: $!";
229 wait_for_eof($p0, '-W0 USR2 test pipe');
230 ok(!-f $pid_file, 'PID file is gone');