2 # Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
5 my $usage = "$0 [-j JOBS] [-s SLOW_THRESHOLD] URL_OF_INBOX\n";
8 use File::Temp qw(tempfile);
10 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
13 use POSIX qw(:sys_wait_h);
14 use Time::HiRes qw(gettimeofday tv_interval);
20 '-j|jobs=i' => \$nproc,
21 '-s|slow-threshold=f' => \$slow,
23 GetOptions(%opts) or die "bad command-line args\n$usage";
24 my $root_url = shift or die $usage;
27 $SIG{TERM} = sub { exit 0 };
30 my $pid = waitpid(-1, WNOHANG);
31 return if !defined $pid || $pid <= 0;
32 my $p = delete $workers{$pid} || '(unknown)';
33 warn("$pid [$p] exited with $?\n") if $?;
37 my @todo = IO::Socket->socketpair(AF_UNIX, SOCK_SEQPACKET, 0);
38 die "socketpair failed: $!" unless $todo[1];
39 my @done = IO::Socket->socketpair(AF_UNIX, SOCK_SEQPACKET, 0);
40 die "socketpair failed: $!" unless $done[1];
43 foreach my $p (1..$nproc) {
45 die "fork failed: $!\n" unless defined $pid;
51 worker_loop($todo[0], $done[1]);
55 my ($fh, $tmp) = tempfile('www-check-XXXXXXXX',
56 SUFFIX => '.gdbm', UNLINK => 1, TMPDIR => 1);
57 my $gdbm = tie my %seen, 'GDBM_File', $tmp, &GDBM_WRCREAT, 0600;
58 defined $gdbm or die "gdbm open failed: $!\n";
63 $todo[1]->blocking(0);
64 $done[0]->blocking(0);
68 my @queue = ($root_url);
69 my $timeout = $slow * 4;
70 while (keys %workers) { # reacts to SIGCHLD
73 vec($rvec, fileno($done[0]), 1) = 1;
75 vec($wvec, fileno($todo[1]), 1) = 1;
76 } elsif ($ndone == $nsent) {
77 kill 'TERM', keys %workers;
80 if (!select($rvec, $wvec, undef, $timeout)) {
81 while (my ($k, $v) = each %seen) {
83 print "WAIT ($ndone/$nsent) <$k>\n";
86 while ($u = shift @queue) {
87 my $s = $todo[1]->send($u, MSG_EOR);
95 $r = $done[0]->recv($u, 65535, 0);
96 } while (!defined $r && $!{EINTR});
98 if ($u =~ s/\ADONE\t//) {
110 my ($todo_rd, $done_wr) = @_;
111 my $m = WWW::Mechanize->new(autocheck => 0);
112 my $cc = LWP::ConnCache->new;
115 $todo_rd->recv(my $u, 65535, 0);
118 my $t = [ gettimeofday ];
120 $t = tv_interval($t);
121 printf "SLOW %0.06f % 5d %s\n", $t, $$, $u if $t > $slow;
123 if ($r->is_success) {
125 (split('#', $_->URI->abs->as_string))[0] => 1;
127 $_->tag && $_->url !~ /:/
129 @links = keys %links;
130 } elsif ($r->code != 300) {
131 warn "W: ".$r->code . " $u\n"
136 foreach my $l (@links, "DONE\t$u") {
139 $s = $done_wr->send($l, MSG_EOR);
140 } while (!defined $s && $!{EINTR});
141 die "$$ send $!\n" unless defined $s;
143 die "$$ send truncated $s < $n\n" if $s != $n;
146 # make sure the HTML source doesn't screw up terminals
147 # when people curl the source (not remotely an expert
148 # on languages or encodings, here).
149 next if $r->header('Content-Type') !~ m!\btext/html\b!;
150 my $dc = $r->decoded_content;
151 if ($dc =~ /([\x00-\x08\x0d-\x1f\x7f-\x{99999999}]+)/s) {