about summary refs log tree commit homepage
path: root/lib/PublicInbox/Qspawn.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2019-03-11 23:53:02 +0000
committerEric Wong <e@80x24.org>2019-04-04 09:13:58 +0000
commit89be7a1b89a60446fc0a385c5c1cfaeec2f92c88 (patch)
tree529a58f8cf4a2c3e119e71a494bae38e1646c8f4 /lib/PublicInbox/Qspawn.pm
parent411dc580077c5240e9cfd4808c06623345435b69 (diff)
downloadpublic-inbox-89be7a1b89a60446fc0a385c5c1cfaeec2f92c88.tar.gz
This allows users to configure RLIMIT_{CORE,CPU,DATA} using
our "limiter" config directive when spawning external processes.
Diffstat (limited to 'lib/PublicInbox/Qspawn.pm')
-rw-r--r--lib/PublicInbox/Qspawn.pm41
1 files changed, 39 insertions, 2 deletions
diff --git a/lib/PublicInbox/Qspawn.pm b/lib/PublicInbox/Qspawn.pm
index 509a4412..79cdae71 100644
--- a/lib/PublicInbox/Qspawn.pm
+++ b/lib/PublicInbox/Qspawn.pm
@@ -43,10 +43,18 @@ sub new ($$$;) {
 sub _do_spawn {
         my ($self, $cb) = @_;
         my $err;
+        my ($cmd, $env, $opts) = @{$self->{args}};
+        my %opts = %{$opts || {}};
+        my $limiter = $self->{limiter};
+        foreach my $k (PublicInbox::Spawn::RLIMITS()) {
+                if (defined(my $rlimit = $limiter->{$k})) {
+                        $opts{$k} = $rlimit;
+                }
+        }
 
-        ($self->{rpipe}, $self->{pid}) = popen_rd(@{$self->{args}});
+        ($self->{rpipe}, $self->{pid}) = popen_rd($cmd, $env, \%opts);
         if (defined $self->{pid}) {
-                $self->{limiter}->{running}++;
+                $limiter->{running}++;
         } else {
                 $self->{err} = $!;
         }
@@ -251,9 +259,38 @@ sub new {
                 max => $max || 32,
                 running => 0,
                 run_queue => [],
+                # RLIMIT_CPU => undef,
+                # RLIMIT_DATA => undef,
+                # RLIMIT_CORE => undef,
         }, $class;
 }
 
+sub setup_rlimit {
+        my ($self, $name, $config) = @_;
+        foreach my $rlim (PublicInbox::Spawn::RLIMITS()) {
+                my $k = lc($rlim);
+                $k =~ tr/_//d;
+                $k = "publicinboxlimiter.$name.$k";
+                defined(my $v = $config->{$k}) or next;
+                my @rlimit = split(/\s*,\s*/, $v);
+                if (scalar(@rlimit) == 1) {
+                        push @rlimit, $rlimit[0];
+                } elsif (scalar(@rlimit) != 2) {
+                        warn "could not parse $k: $v\n";
+                }
+                eval { require BSD::Resource };
+                if ($@) {
+                        warn "BSD::Resource missing for $rlim";
+                        next;
+                }
+                foreach my $i (0..$#rlimit) {
+                        next if $rlimit[$i] ne 'INFINITY';
+                        $rlimit[$i] = BSD::Resource::RLIM_INFINITY();
+                }
+                $self->{$rlim} = \@rlimit;
+        }
+}
+
 # captures everything into a buffer and executes a callback when done
 package PublicInbox::Qspawn::Qx;
 use strict;