Blame


1 27363023 2022-08-25 op #!/usr/bin/env perl
2 1bcb9899 2022-08-29 op #
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
5 1bcb9899 2022-08-29 op # code.
6 27363023 2022-08-25 op
7 27363023 2022-08-25 op use open ":std", ":encoding(UTF-8)";
8 27363023 2022-08-25 op use strict;
9 27363023 2022-08-25 op use warnings;
10 27363023 2022-08-25 op use v5.32;
11 70b47196 2023-03-29 op
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);
15 27363023 2022-08-25 op
16 38232a0a 2023-05-07 op use SMArc qw(parse);
17 70b47196 2023-03-29 op
18 27363023 2022-08-25 op my $jobs = $ENV{'MAKE_JOBS'} // 1;
19 27363023 2022-08-25 op
20 70b47196 2023-03-29 op my $csumdir = $ENV{'CSUMDIR'};
21 70b47196 2023-03-29 op die '$CSUMDIR undefined' unless defined $csumdir;
22 70b47196 2023-03-29 op
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);
27 27363023 2022-08-25 op }
28 27363023 2022-08-25 op
29 70b47196 2023-03-29 op # get current thread checksum
30 70b47196 2023-03-29 op sub thrsum {
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;
34 70b47196 2023-03-29 op }
35 70b47196 2023-03-29 op
36 70b47196 2023-03-29 op # get stored thread checksum
37 70b47196 2023-03-29 op sub oldsum {
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>;
41 70b47196 2023-03-29 op chomp $sum;
42 70b47196 2023-03-29 op return $sum;
43 70b47196 2023-03-29 op }
44 70b47196 2023-03-29 op
45 70b47196 2023-03-29 op # save thread checksum
46 70b47196 2023-03-29 op sub savesum {
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: $!";
50 70b47196 2023-03-29 op say $fh $sum;
51 70b47196 2023-03-29 op close $fh;
52 70b47196 2023-03-29 op }
53 70b47196 2023-03-29 op
54 27363023 2022-08-25 op sub process {
55 70b47196 2023-03-29 op my @entries = @_;
56 70b47196 2023-03-29 op
57 70b47196 2023-03-29 op return unless @entries;
58 70b47196 2023-03-29 op
59 6b36ff28 2023-04-01 op my $mail = parse $entries[0];
60 bbdbef1a 2023-04-01 op die "wtf?" if $mail->{level} != 0;
61 70b47196 2023-03-29 op
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;
66 70b47196 2023-03-29 op
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;
71 70b47196 2023-03-29 op
72 70b47196 2023-03-29 op savesum($tid, $thrsum);
73 27363023 2022-08-25 op }
74 27363023 2022-08-25 op
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;
78 054f3fd4 2022-08-25 op
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: $!";
81 1d565e04 2023-05-06 op }
82 1d565e04 2023-05-06 op
83 27363023 2022-08-25 op my @thread;
84 27363023 2022-08-25 op while (<>) {
85 27363023 2022-08-25 op print; # continue the pipeline
86 27363023 2022-08-25 op
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;
90 27363023 2022-08-25 op @thread = ();
91 27363023 2022-08-25 op }
92 27363023 2022-08-25 op
93 27363023 2022-08-25 op push @thread, $_;
94 27363023 2022-08-25 op }
95 27363023 2022-08-25 op
96 27363023 2022-08-25 op process @thread if @thread;