1 27363023 2022-08-25 op #!/usr/bin/env perl
3 1bcb9899 2022-08-29 op # pe was written by Omar Polo <op@openbsd.org> and is placed in the
4 1bcb9899 2022-08-29 op # public domain. The author hereby disclaims copyright to this source
7 27363023 2022-08-25 op use open ":std", ":encoding(UTF-8)";
12 70b47196 2023-03-29 op use Digest::SHA;
13 70b47196 2023-03-29 op use Encode qw(encode);
14 27363023 2022-08-25 op use IO::Poll qw(POLLOUT);
16 38232a0a 2023-05-07 op use SMArc qw(parse);
18 27363023 2022-08-25 op my $jobs = $ENV{'MAKE_JOBS'} // 1;
20 70b47196 2023-03-29 op my $csumdir = $ENV{'CSUMDIR'};
21 70b47196 2023-03-29 op die '$CSUMDIR undefined' unless defined $csumdir;
23 27363023 2022-08-25 op my $poll = IO::Poll->new();
24 27363023 2022-08-25 op for (1..$jobs) {
25 f43c847e 2023-05-05 op open(my $kid, '|-', 'mexp') or die "can't exec mexp: $!";
26 27363023 2022-08-25 op $poll->mask($kid => POLLOUT);
29 70b47196 2023-03-29 op # get current thread checksum
31 70b47196 2023-03-29 op my $sha = Digest::SHA->new(256);
32 70b47196 2023-03-29 op $sha->add(encode('UTF-8', $_)) for @_;
33 70b47196 2023-03-29 op return $sha->hexdigest;
36 70b47196 2023-03-29 op # get stored thread checksum
38 70b47196 2023-03-29 op my $tid = shift;
39 70b47196 2023-03-29 op open my $fh, '<', "$csumdir/$tid" or return "";
40 70b47196 2023-03-29 op my $sum = <$fh>;
45 70b47196 2023-03-29 op # save thread checksum
47 70b47196 2023-03-29 op my ($tid, $sum) = @_;
48 70b47196 2023-03-29 op open my $fh, '>', "$csumdir/$tid"
49 70b47196 2023-03-29 op or die "can't open checksum file $csumdir/$tid: $!";
55 70b47196 2023-03-29 op my @entries = @_;
57 70b47196 2023-03-29 op return unless @entries;
59 6b36ff28 2023-04-01 op my $mail = parse $entries[0];
60 bbdbef1a 2023-04-01 op die "wtf?" if $mail->{level} != 0;
62 bbdbef1a 2023-04-01 op my $tid = $mail->{mid};
63 70b47196 2023-03-29 op my $thrsum = thrsum @_;
64 70b47196 2023-03-29 op my $oldsum = oldsum $tid;
65 70b47196 2023-03-29 op return if $thrsum eq $oldsum;
67 27363023 2022-08-25 op die "poll: $!" if $poll->poll() == -1;
68 27363023 2022-08-25 op my @handles = $poll->handles(POLLOUT) or die "no procs ready?";
69 27363023 2022-08-25 op my $handle = $handles[int(rand(@handles))];
70 70b47196 2023-03-29 op print $handle $_ foreach @entries;
72 70b47196 2023-03-29 op savesum($tid, $thrsum);
75 1d565e04 2023-05-06 op if (`uname` =~ "OpenBSD") {
76 1d565e04 2023-05-06 op use OpenBSD::Pledge;
77 1d565e04 2023-05-06 op use OpenBSD::Unveil;
79 1d565e04 2023-05-06 op unveil($csumdir, "rwc") or die "unveil $csumdir: $!";
80 1d565e04 2023-05-06 op pledge("stdio rpath wpath cpath") or die "pledge: $!";
85 27363023 2022-08-25 op print; # continue the pipeline
87 de557185 2022-08-26 op my $new_thread = m/^-/;
88 de557185 2022-08-26 op if ($new_thread && @thread) {
89 27363023 2022-08-25 op process @thread;
93 27363023 2022-08-25 op push @thread, $_;
96 27363023 2022-08-25 op process @thread if @thread;