2 # Copyright (C) 2016-2019 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);
18 # we want to use vfork+exec with spawn, WWW::Mechanize can use too much
19 # memory and fork(2) fails
20 use PublicInbox::Spawn qw(spawn which);
21 $ENV{PERL_INLINE_DIRECTORY} or warn "PERL_INLINE_DIRECTORY unset, may OOM\n";
27 '-j|jobs=i' => \$nproc,
28 '-s|slow-threshold=f' => \$slow,
30 GetOptions(%opts) or die "bad command-line args\n$usage";
31 my $root_url = shift or die $usage;
33 chomp(my $xmlstarlet = which('xmlstarlet'));
34 my $atom_check = eval {
35 my $cmd = [ qw(xmlstarlet val -e -) ];
37 my ($in, $out, $err) = @_;
39 open my $in_fh, '+>', undef;
40 open my $out_fh, '+>', undef;
41 open my $err_fh, '+>', undef;
44 sysseek($in_fh, 0, 0);
50 my $pid = spawn($cmd, undef, $rdr);
51 defined $pid or die "spawn failure: $!";
52 while (waitpid($pid, 0) != $pid) {
54 warn "waitpid(xmlstarlet, $pid) $!";
57 sysseek($out_fh, 0, 0);
58 sysread($out_fh, $$out, -s $out_fh);
59 sysseek($err_fh, 0, 0);
60 sysread($err_fh, $$err, -s $err_fh);
66 $SIG{INT} = sub { exit 130 };
67 $SIG{TERM} = sub { exit 0 };
70 my $pid = waitpid(-1, WNOHANG);
71 return if !defined $pid || $pid <= 0;
72 my $p = delete $workers{$pid} || '(unknown)';
73 warn("$pid [$p] exited with $?\n") if $?;
77 my @todo = IO::Socket->socketpair(AF_UNIX, SOCK_SEQPACKET, 0);
78 die "socketpair failed: $!" unless $todo[1];
79 my @done = IO::Socket->socketpair(AF_UNIX, SOCK_SEQPACKET, 0);
80 die "socketpair failed: $!" unless $done[1];
83 foreach my $p (1..$nproc) {
85 die "fork failed: $!\n" unless defined $pid;
91 worker_loop($todo[0], $done[1]);
95 my ($fh, $tmp) = tempfile('www-check-XXXXXXXX',
96 SUFFIX => '.gdbm', UNLINK => 1, TMPDIR => 1);
97 my $gdbm = tie my %seen, 'GDBM_File', $tmp, &GDBM_WRCREAT, 0600;
98 defined $gdbm or die "gdbm open failed: $!\n";
103 $todo[1]->blocking(0);
104 $done[0]->blocking(0);
105 $seen{$root_url} = 1;
108 my @queue = ($root_url);
109 my $timeout = $slow * 4;
110 while (keys %workers) { # reacts to SIGCHLD
113 vec($rvec, fileno($done[0]), 1) = 1;
115 vec($wvec, fileno($todo[1]), 1) = 1;
116 } elsif ($ndone == $nsent) {
117 kill 'TERM', keys %workers;
120 if (!select($rvec, $wvec, undef, $timeout)) {
121 while (my ($k, $v) = each %seen) {
123 print "WAIT ($ndone/$nsent) <$k>\n";
126 while ($u = shift @queue) {
127 my $s = $todo[1]->send($u, MSG_EOR);
135 $r = $done[0]->recv($u, 65535, 0);
136 } while (!defined $r && $!{EINTR});
138 if ($u =~ s/\ADONE\t//) {
150 my ($todo_rd, $done_wr) = @_;
151 $SIG{CHLD} = 'DEFAULT';
152 my $m = WWW::Mechanize->new(autocheck => 0);
153 my $cc = LWP::ConnCache->new;
154 $m->stack_depth(0); # no history
157 $todo_rd->recv(my $u, 65535, 0);
160 my $t = [ gettimeofday ];
162 $t = tv_interval($t);
163 printf "SLOW %0.06f % 5d %s\n", $t, $$, $u if $t > $slow;
165 if ($r->is_success) {
167 (split('#', $_->URI->abs->as_string))[0] => 1;
169 $_->tag && $_->url !~ /:/
171 @links = keys %links;
172 } elsif ($r->code != 300) {
173 warn "W: ".$r->code . " $u\n"
178 foreach my $l (@links, "DONE\t$u") {
179 next if $l eq '' || $l =~ /\.mbox(?:\.gz)\z/;
181 $s = $done_wr->send($l, MSG_EOR);
182 } while (!defined $s && $!{EINTR});
183 die "$$ send $!\n" unless defined $s;
185 die "$$ send truncated $s < $n\n" if $s != $n;
188 # make sure the HTML source doesn't screw up terminals
189 # when people curl the source (not remotely an expert
190 # on languages or encodings, here).
191 my $ct = $r->header('Content-Type') || '';
192 warn "no Content-Type: $u\n" if $ct eq '';
194 if ($atom_check && $ct =~ m!\bapplication/atom\+xml\b!) {
195 my $raw = $r->decoded_content;
196 my ($out, $err) = ('', '');
197 my $fail = $atom_check->(\$raw, \$out, \$err);
198 warn "Atom ($fail) - $u - <1:$out> <2:$err>\n" if $fail;
201 next if $ct !~ m!\btext/html\b!;
202 my $dc = $r->decoded_content;
203 if ($dc =~ /([\x00-\x08\x0d-\x1f\x7f-\x{99999999}]+)/s) {