1 # Copyright (C) 2016-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 # Tests for binding Unix domain sockets
7 use PublicInbox::TestCommon;
8 use Errno qw(EADDRINUSE);
11 require_mods(qw(Plack::Util Plack::Builder HTTP::Date HTTP::Status));
13 my ($tmpdir, $for_destroy) = tmpdir();
14 my $unix = "$tmpdir/unix.sock";
15 my $psgi = './t/httpd-corner.psgi';
16 my $out = "$tmpdir/out.log";
17 my $err = "$tmpdir/err.log";
20 my $spawn_httpd = sub {
22 my $cmd = [ '-httpd', @args, "--stdout=$out", "--stderr=$err", $psgi ];
23 $td = start_script($cmd);
27 require PublicInbox::Daemon;
28 my $l = "$tmpdir/named.sock";
29 my $s = IO::Socket::UNIX->new(Listen => 5, Local => $l,
31 is(PublicInbox::Daemon::sockname($s), $l, 'sockname works for UNIX');
34 ok(!-S $unix, 'UNIX socket does not exist, yet');
35 $spawn_httpd->("-l$unix", '-W0');
36 my %o = (Peer => $unix, Type => SOCK_STREAM);
38 last if -S $unix && IO::Socket::UNIX->new(%o);
39 select undef, undef, undef, 0.02
42 ok(-S $unix, 'UNIX socket was bound by -httpd');
45 my $sock = IO::Socket::UNIX->new(Peer => $unix, Type => SOCK_STREAM);
46 warn "E: $! connecting to $unix\n" unless defined $sock;
47 ok($sock, 'client UNIX socket connected');
48 ok($sock->write("GET /host-port HTTP/1.0\r\n\r\n"),
49 'wrote req to server');
50 ok($sock->read(my $buf, 4096), 'read response');
51 like($buf, qr!\r\n\r\n127\.0\.0\.1:0\z!,
52 'set REMOTE_ADDR and REMOTE_PORT for Unix socket');
57 { # do not clobber existing socket
58 my %err = ( 'linux' => EADDRINUSE, 'freebsd' => EADDRINUSE );
59 open my $out, '>>', "$tmpdir/1" or die "redirect failed: $!";
60 open my $err, '>>', "$tmpdir/2" or die "redirect failed: $!";
61 my $cmd = ['-httpd', '-l', $unix, '-W0', $psgi];
62 my $ftd = start_script($cmd, undef, { 1 => $out, 2 => $err });
64 isnt($?, 0, 'httpd failure set $?');
67 skip("not sure if $^O fails with EADDRINUSE", 1);
68 is($? >> 8, $ec, 'httpd failed with EADDRINUSE');
70 open my $fh, "$tmpdir/2" or die "failed to open $tmpdir/2: $!";
73 like($e, qr/no listeners bound/i, 'got error message');
74 is(-s "$tmpdir/1", 0, 'stdout was empty');
78 is($td->kill, 1, 'terminate existing process');
80 is($?, 0, 'existing httpd exited successfully');
81 ok(-S $unix, 'unix socket still exists');
88 select undef, undef, undef, 0.012;
90 Carp::croak('condition failed');
94 require_mods('Net::Server::Daemonize', 20);
95 my $pid_file = "$tmpdir/pid";
98 open my $fh, '<', $f or die "open $f failed: $!";
99 my $pid = do { local $/; <$fh> };
104 for my $w (qw(-W0 -W1)) {
105 # wait for daemonization
106 $spawn_httpd->("-l$unix", '-D', '-P', $pid_file, $w);
108 is($?, 0, "daemonized $w process");
111 ok(-f $pid_file, "$w pid file written");
112 my $pid = $read_pid->($pid_file);
113 is(kill('TERM', $pid), 1, "signaled daemonized $w process");
114 delay_until(sub { !kill(0, $pid) });
115 is(kill(0, $pid), 0, "daemonized $w process exited");
116 ok(!-e $pid_file, "$w pid file unlinked at exit");
119 # try a USR2 upgrade with workers:
120 my $httpd = abs_path('blib/script/public-inbox-httpd');
121 $psgi = abs_path($psgi);
122 my $opt = { run_mode => 0 };
124 my @args = ("-l$unix", '-D', '-P', $pid_file, -1, $out, -2, $err);
125 $td = start_script([$httpd, @args, $psgi], undef, $opt);
127 is($?, 0, "daemonized process again");
129 my $pid = $read_pid->($pid_file);
131 # stop worker to ensure check_sock below hits $new_pid
132 kill('TTOU', $pid) or die "TTOU failed: $!";
134 kill('USR2', $pid) or die "USR2 failed: $!";
136 $pid != (eval { $read_pid->($pid_file) } // $pid)
138 my $new_pid = $read_pid->($pid_file);
139 isnt($new_pid, $pid, 'new child started');
140 my $old_pid = $read_pid->("$pid_file.oldbin");
141 is($old_pid, $pid, '.oldbin pid file written');
143 check_sock($unix); # ensures $new_pid is ready to receive signals
145 # first, back out of the upgrade
146 kill('QUIT', $new_pid) or die "kill new PID failed: $!";
148 $pid == (eval { $read_pid->($pid_file) } // 0)
150 is($read_pid->($pid_file), $pid, 'old PID file restored');
151 ok(!-f "$pid_file.oldbin", '.oldbin PID file gone');
154 kill('USR2', $pid) or die "USR2 failed: $!";
156 $pid != (eval { $read_pid->($pid_file) } // $pid)
158 $new_pid = $read_pid->($pid_file);
159 isnt($new_pid, $pid, 'new child started again');
160 $old_pid = $read_pid->("$pid_file.oldbin");
161 is($old_pid, $pid, '.oldbin pid file written');
163 # drop the old parent
164 kill('QUIT', $old_pid) or die "QUIT failed: $!";
165 delay_until(sub { !kill(0, $old_pid) });
169 kill('QUIT', $new_pid) or die "QUIT failed: $!";
170 delay_until(sub { !kill(0, $new_pid) });
171 ok(!-f $pid_file, 'PID file is gone');
174 # try USR2 without workers (-W0)
175 $td = start_script([$httpd, @args, '-W0', $psgi], undef, $opt);
177 is($?, 0, 'daemonized w/o workers');
179 $pid = $read_pid->($pid_file);
181 # replace running process
182 kill('USR2', $pid) or die "USR2 failed: $!";
183 delay_until(sub { !kill(0, $pid) });
186 $pid = $read_pid->($pid_file);
187 kill('QUIT', $pid) or die "USR2 failed: $!";
188 delay_until(sub { !kill(0, $pid) });
189 ok(!-f $pid_file, 'PID file is gone');