1 # Copyright (C) 2016-2019 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);
9 require_mods(qw(Plack::Util Plack::Builder HTTP::Date HTTP::Status));
11 my ($tmpdir, $for_destroy) = tmpdir();
12 my $unix = "$tmpdir/unix.sock";
13 my $psgi = './t/httpd-corner.psgi';
14 my $out = "$tmpdir/out.log";
15 my $err = "$tmpdir/err.log";
18 my $spawn_httpd = sub {
20 my $cmd = [ '-httpd', @args, "--stdout=$out", "--stderr=$err", $psgi ];
21 $td = start_script($cmd);
25 require PublicInbox::Daemon;
26 my $l = "$tmpdir/named.sock";
27 my $s = IO::Socket::UNIX->new(Listen => 5, Local => $l,
29 is(PublicInbox::Daemon::sockname($s), $l, 'sockname works for UNIX');
32 ok(!-S $unix, 'UNIX socket does not exist, yet');
33 $spawn_httpd->("-l$unix", '-W0');
34 my %o = (Peer => $unix, Type => SOCK_STREAM);
36 last if -S $unix && IO::Socket::UNIX->new(%o);
37 select undef, undef, undef, 0.02
40 ok(-S $unix, 'UNIX socket was bound by -httpd');
43 my $sock = IO::Socket::UNIX->new(Peer => $unix, Type => SOCK_STREAM);
44 warn "E: $! connecting to $unix\n" unless defined $sock;
45 ok($sock, 'client UNIX socket connected');
46 ok($sock->write("GET /host-port HTTP/1.0\r\n\r\n"),
47 'wrote req to server');
48 ok($sock->read(my $buf, 4096), 'read response');
49 like($buf, qr!\r\n\r\n127\.0\.0\.1:0\z!,
50 'set REMOTE_ADDR and REMOTE_PORT for Unix socket');
55 { # do not clobber existing socket
56 my %err = ( 'linux' => EADDRINUSE, 'freebsd' => EADDRINUSE );
57 open my $out, '>>', "$tmpdir/1" or die "redirect failed: $!";
58 open my $err, '>>', "$tmpdir/2" or die "redirect failed: $!";
59 my $cmd = ['-httpd', '-l', $unix, '-W0', $psgi];
60 my $ftd = start_script($cmd, undef, { 1 => $out, 2 => $err });
62 isnt($?, 0, 'httpd failure set $?');
65 skip("not sure if $^O fails with EADDRINUSE", 1);
66 is($? >> 8, $ec, 'httpd failed with EADDRINUSE');
68 open my $fh, "$tmpdir/2" or die "failed to open $tmpdir/2: $!";
71 like($e, qr/no listeners bound/i, 'got error message');
72 is(-s "$tmpdir/1", 0, 'stdout was empty');
76 is($td->kill, 1, 'terminate existing process');
78 is($?, 0, 'existing httpd exited successfully');
79 ok(-S $unix, 'unix socket still exists');
83 require_mods('Net::Server::Daemonize', 20);
84 my $pid_file = "$tmpdir/pid";
85 for my $w (qw(-W0 -W1)) {
86 # wait for daemonization
87 $spawn_httpd->("-l$unix", '-D', '-P', $pid_file, $w);
89 is($?, 0, "daemonized $w process");
92 ok(-f $pid_file, "$w pid file written");
93 open my $fh, '<', "$tmpdir/pid" or die "open failed: $!";
94 my $rpid = do { local $/; <$fh> };
96 like($rpid, qr/\A\d+\z/s, "$w pid file looks like a pid");
97 is(kill('TERM', $rpid), 1, "signaled daemonized $w process");
99 kill(0, $rpid) or last;
100 select undef, undef, undef, 0.02;
102 is(kill(0, $rpid), 0, "daemonized $w process exited");
103 ok(!-e $pid_file, "$w pid file unlinked at exit");