dumping ground for random patches and texts
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: spew@80x24.org
Subject: [PATCH] WIP-limiter-rlimit
Date: Thu, 30 Nov 2023 19:33:53 +0000	[thread overview]
Message-ID: <20231130193353.1105570-1-e@80x24.org> (raw)

---
 lib/PublicInbox/Limiter.pm | 15 +++++++++------
 lib/PublicInbox/Spawn.pm   | 31 ++++++++++++++++++++++++-------
 t/spawn.t                  |  7 +++++++
 3 files changed, 40 insertions(+), 13 deletions(-)

diff --git a/lib/PublicInbox/Limiter.pm b/lib/PublicInbox/Limiter.pm
index 48a2b6a3..a8d08fc3 100644
--- a/lib/PublicInbox/Limiter.pm
+++ b/lib/PublicInbox/Limiter.pm
@@ -31,14 +31,17 @@ sub setup_rlimit {
 		} elsif (scalar(@rlimit) != 2) {
 			warn "could not parse $k: $v\n";
 		}
-		eval { require BSD::Resource };
-		if ($@) {
-			warn "BSD::Resource missing for $rlim";
-			next;
-		}
+		my $inf = $v =~ /\binfinity\b/i ?
+			$PublicInbox::Spawn::RLIMITS{RLIM_INFINITY} // eval {
+				require BSD::Resource;
+				BSD::Resource::RLIM_INFINITY();
+			} // do {
+				warn "BSD::Resource missing for $rlim";
+				next;
+			} : undef;
 		for my $i (0..$#rlimit) {
 			next if $rlimit[$i] ne 'INFINITY';
-			$rlimit[$i] = BSD::Resource::RLIM_INFINITY();
+			$rlimit[$i] = $inf;
 		}
 		$self->{$rlim} = \@rlimit;
 	}
diff --git a/lib/PublicInbox/Spawn.pm b/lib/PublicInbox/Spawn.pm
index b09bb536..aed993db 100644
--- a/lib/PublicInbox/Spawn.pm
+++ b/lib/PublicInbox/Spawn.pm
@@ -21,10 +21,11 @@ use IO::Handle ();
 use Carp qw(croak);
 use PublicInbox::IO;
 our @EXPORT_OK = qw(which spawn popen_rd popen_wr run_die run_wait run_qx);
-our @RLIMITS = qw(RLIMIT_CPU RLIMIT_CORE RLIMIT_DATA);
+our (@RLIMITS, %RLIMITS);
 use autodie qw(close open pipe seek sysseek truncate);
 
 BEGIN {
+	@RLIMITS = qw(RLIMIT_CPU RLIMIT_CORE RLIMIT_DATA RLIMIT_FSIZE);
 	my $all_libc = <<'ALL_LIBC'; # all *nix systems we support
 #include <sys/resource.h>
 #include <sys/socket.h>
@@ -292,14 +293,28 @@ void recv_cmd4(PerlIO *s, SV *buf, STRLEN n)
 	Inline_Stack_Done;
 }
 #endif /* defined(CMSG_SPACE) && defined(CMSG_LEN) */
-ALL_LIBC
 
+void rlimit_map()
+{
+	Inline_Stack_Vars;
+	Inline_Stack_Reset;
+ALL_LIBC
 	my $inline_dir = $ENV{PERL_INLINE_DIRECTORY} // (
 			$ENV{XDG_CACHE_HOME} //
 			( ($ENV{HOME} // '/nonexistent').'/.cache' )
 		).'/public-inbox/inline-c';
 	undef $all_libc unless -d $inline_dir;
 	if (defined $all_libc) {
+		for (@RLIMITS, 'RLIM_INFINITY') {
+			$all_libc .= <<EOM;
+	Inline_Stack_Push(sv_2mortal(newSVpvs("$_")));
+	Inline_Stack_Push(sv_2mortal(newSViv($_)));
+EOM
+		}
+		$all_libc .= <<EOM;
+	Inline_Stack_Done;
+} // rlimit_map
+EOM
 		local $ENV{PERL_INLINE_DIRECTORY} = $inline_dir;
 		# CentOS 7.x ships Inline 0.53, 0.64+ has built-in locking
 		my $lk = PublicInbox::Lock->new($inline_dir.
@@ -325,6 +340,7 @@ ALL_LIBC
 	}
 	if (defined $all_libc) { # set for Gcf2
 		$ENV{PERL_INLINE_DIRECTORY} = $inline_dir;
+		%RLIMITS = rlimit_map();
 	} else {
 		require PublicInbox::SpawnPP;
 		*pi_fork_exec = \&PublicInbox::SpawnPP::pi_fork_exec
@@ -370,11 +386,12 @@ sub spawn ($;$$) {
 	my $rlim = [];
 	foreach my $l (@RLIMITS) {
 		my $v = $opt->{$l} // next;
-		my $r = eval "require BSD::Resource; BSD::Resource::$l();";
-		unless (defined $r) {
-			warn "$l undefined by BSD::Resource: $@\n";
-			next;
-		}
+		my $r = $RLIMITS{$l} //
+			eval "require BSD::Resource; BSD::Resource::$l();" //
+			do {
+				warn "$l undefined by BSD::Resource: $@\n";
+				next;
+			};
 		push @$rlim, $r, @$v;
 	}
 	my $cd = $opt->{'-C'} // ''; # undef => NULL mapping doesn't work?
diff --git a/t/spawn.t b/t/spawn.t
index 48f541b8..344957af 100644
--- a/t/spawn.t
+++ b/t/spawn.t
@@ -260,4 +260,11 @@ SKIP: {
 	is(readline($fh), "hihi\n", 'env+chdir SpawnPP under (faked) MOD_PERL');
 }
 
+my $rlimit_map = PublicInbox::Spawn->can('rlimit_map');
+diag $rlimit_map;
+if ($rlimit_map) {
+	my %rlim = $rlimit_map->();
+	diag explain(\%rlim);
+}
+
 done_testing();

                 reply	other threads:[~2023-11-30 19:33 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231130193353.1105570-1-e@80x24.org \
    --to=e@80x24.org \
    --cc=spew@80x24.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).