]> Sergey Matveev's repositories - public-inbox.git/blob - t/httpd-unix.t
t/httpd-unix.t: stop tail(1) before stopping server
[public-inbox.git] / t / httpd-unix.t
1 #!perl -w
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
5 use strict;
6 use Test::More;
7 use PublicInbox::TestCommon;
8 use Errno qw(EADDRINUSE);
9 use Cwd qw(abs_path);
10 use Carp qw(croak);
11 use Fcntl qw(FD_CLOEXEC F_SETFD);
12 require_mods(qw(Plack::Util Plack::Builder HTTP::Date HTTP::Status));
13 use IO::Socket::UNIX;
14 use POSIX qw(mkfifo);
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";
20 my $td;
21
22 my $register_exit_fifo = sub {
23         my ($s, $f) = @_;
24         my $sock = new_sock($s);
25         ok($sock->write("GET /exit-fifo$f HTTP/1.0\r\n\r\n"),
26                 'request exit-fifo');
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');
29 };
30
31 my $spawn_httpd = sub {
32         my (@args) = @_;
33         my $cmd = [ '-httpd', @args, "--stdout=$out", "--stderr=$err", $psgi ];
34         $td = start_script($cmd);
35 };
36
37 {
38         require PublicInbox::Daemon;
39         my $l = "$tmpdir/named.sock";
40         my $s = IO::Socket::UNIX->new(Listen => 5, Local => $l,
41                                         Type => SOCK_STREAM);
42         is(PublicInbox::Daemon::sockname($s), $l, 'sockname works for UNIX');
43 }
44
45 ok(!-S $unix, 'UNIX socket does not exist, yet');
46 my $f1 = "$tmpdir/f1";
47 mkfifo($f1, 0600);
48 {
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');
53 }
54 ok(-S $unix, 'UNIX socket was bound by -httpd');
55
56 sub new_sock ($) {
57         IO::Socket::UNIX->new(Peer => $_[0], Type => SOCK_STREAM)
58                 // xbail "E: $! connecting to $_[0]";
59 }
60
61 sub check_sock ($) {
62         my ($unix) = @_;
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');
69 }
70
71 check_sock($unix);
72
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 });
79         $ftd->join;
80         isnt($?, 0, 'httpd failure set $?');
81         SKIP: {
82                 my $ec = $err{$^O} or
83                         skip("not sure if $^O fails with EADDRINUSE", 1);
84                 is($? >> 8, $ec, 'httpd failed with EADDRINUSE');
85         };
86         open my $fh, "$tmpdir/2" or die "failed to open $tmpdir/2: $!";
87         local $/;
88         my $e = <$fh>;
89         like($e, qr/no listeners bound/i, 'got error message');
90         is(-s "$tmpdir/1", 0, 'stdout was empty');
91 }
92
93 {
94         is($td->kill, 1, 'terminate existing process');
95         $td->join;
96         is($?, 0, 'existing httpd exited successfully');
97         ok(-S $unix, 'unix socket still exists');
98 }
99
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};
103
104 sub delay_until {
105         my $cond = shift;
106         my $end = time + 30;
107         do {
108                 return if $cond->();
109                 tick(0.012);
110         } until (time > $end);
111         Carp::confess('condition failed');
112 }
113
114 SKIP: {
115         require_mods('Net::Server::Daemonize', 52);
116         $has_sigfd or skip('signalfd / EVFILT_SIGNAL not available', 52);
117         my $pid_file = "$tmpdir/pid";
118         my $read_pid = sub {
119                 my $f = shift;
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 $!");
123                 $pid;
124         };
125
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: $!";
132                 $td->join;
133                 is($?, 0, "daemonized $w process");
134                 check_sock($unix);
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");
143         }
144
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);
149
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
153
154                 $td = start_script([$httpd, @args, $psgi], undef, $opt);
155                 close($p1) or xbail "close: $!";
156                 $td->join;
157                 is($?, 0, "daemonized process again");
158                 check_sock($unix);
159                 ok(-s $pid_file, 'pid file written');
160                 my $pid = $read_pid->($pid_file);
161
162                 # stop worker to ensure check_sock below hits $new_pid
163                 kill('TTOU', $pid) or die "TTOU failed: $!";
164
165                 kill('USR2', $pid) or die "USR2 failed: $!";
166                 delay_until(sub {
167                         $pid != (eval { $read_pid->($pid_file) } // $pid)
168                 });
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');
176
177                 check_sock($unix); # ensures $new_pid is ready to receive signals
178
179                 # first, back out of the upgrade
180                 kill('QUIT', $new_pid) or die "kill new PID failed: $!";
181                 delay_until(sub {
182                         $pid == (eval { $read_pid->($pid_file) } // 0)
183                 });
184                 is($read_pid->($pid_file), $pid, 'old PID file restored');
185                 ok(!-f "$pid_file.oldbin", '.oldbin PID file gone');
186
187                 # retry USR2 upgrade
188                 kill('USR2', $pid) or die "USR2 failed: $!";
189                 delay_until(sub {
190                         $pid != (eval { $read_pid->($pid_file) } // $pid)
191                 });
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');
196
197                 # drop the old parent
198                 kill('QUIT', $old_pid) or die "QUIT failed: $!";
199                 delay_until(sub { !kill(0, $old_pid) }); # UGH
200
201                 ok(!-f "$pid_file.oldbin", '.oldbin PID file gone');
202
203                 # drop the new child
204                 check_sock($unix);
205                 kill('QUIT', $new_pid) or die "QUIT failed: $!";
206
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');
210
211                 ok(!-f $pid_file, 'PID file is gone');
212         }
213
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: $!";
219                 $td->join;
220                 is($?, 0, 'daemonized w/o workers');
221                 $register_exit_fifo->($unix, $f1);
222                 my $pid = $read_pid->($pid_file);
223
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');
228
229                 check_sock($unix);
230                 $pid = $read_pid->($pid_file);
231                 kill('QUIT', $pid) or xbail "USR2 failed: $!";
232
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');
237         }
238 }
239
240 done_testing();