unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH] spawn_pp: use `which()' properly for pure-Perl spawn
Date: Sun, 29 Jan 2023 09:45:11 +0000	[thread overview]
Message-ID: <20230129094511.3218368-1-e@80x24.org> (raw)

I have no idea if mod_perl/mod_perl2 is used nowadays, but
we're stuck supporting it as long as mod_perl exists.  So
add some tests and make minor updates to existing ones to
ensure it stays working.
---
 lib/PublicInbox/SpawnPP.pm |  5 ++++-
 t/spawn.t                  | 42 ++++++++++++++++++++++++++++++--------
 2 files changed, 37 insertions(+), 10 deletions(-)

diff --git a/lib/PublicInbox/SpawnPP.pm b/lib/PublicInbox/SpawnPP.pm
index 7a5793f6..73859e9b 100644
--- a/lib/PublicInbox/SpawnPP.pm
+++ b/lib/PublicInbox/SpawnPP.pm
@@ -3,9 +3,11 @@
 
 # Pure-Perl implementation of "spawn".  This can't take advantage
 # of vfork, so no speedups under Linux for spawning from large processes.
+# Do not require this directly, only use from PublicInbox::Spawn
 package PublicInbox::SpawnPP;
 use v5.12;
 use POSIX qw(dup2 _exit setpgid :signal_h);
+use PublicInbox::Spawn qw(which);
 
 # Pure Perl implementation for folks that do not use Inline::C
 sub pi_fork_exec ($$$$$$$) {
@@ -46,7 +48,8 @@ sub pi_fork_exec ($$$$$$$) {
 		sigprocmask(SIG_SETMASK, $old) or die "SIG_SETMASK ~CHLD: $!";
 		$cmd->[0] = $f;
 		if ($ENV{MOD_PERL}) {
-			@$cmd = (which('env'), '-i', @$env, @$cmd);
+			$f = which('env');
+			@$cmd = ('env', '-i', @$env, @$cmd);
 		} else {
 			%ENV = map { split(/=/, $_, 2) } @$env;
 		}
diff --git a/t/spawn.t b/t/spawn.t
index c22cfcfc..ff95ae8e 100644
--- a/t/spawn.t
+++ b/t/spawn.t
@@ -1,10 +1,11 @@
-# Copyright (C) 2015-2021 all contributors <meta@public-inbox.org>
+#!perl -w
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
-use strict;
-use warnings;
+use v5.12;
 use Test::More;
 use PublicInbox::Spawn qw(which spawn popen_rd);
-use PublicInbox::Sigfd;
+require PublicInbox::Sigfd;
+require PublicInbox::DS;
 
 {
 	my $true = which('true');
@@ -38,9 +39,8 @@ SKIP: {
 	$pid = eval { spawn(['true'], undef, { pgid => $wrong_pgid, 2 => $w }) };
 	close $w;
 	my $err = do { local $/; <$r> };
-	# diag "$err ($@)";
 	if (defined $pid) {
-		waitpid($pid, 0) if defined $pid;
+		waitpid($pid, 0);
 		isnt($?, 0, 'child error (pure-Perl)');
 	} else {
 		ok($@, 'exception raised');
@@ -65,7 +65,7 @@ EOF
 	my $rd = popen_rd([$^X, '-e', $script]);
 	diag 'waiting for child to reap grandchild...';
 	chomp(my $line = readline($rd));
-	my ($rdy, $pid) = split(' ', $line);
+	my ($rdy, $pid) = split(/ /, $line);
 	is($rdy, 'RDY', 'got ready signal, waitpid(-1) works in child');
 	ok(kill('CHLD', $pid), 'sent SIGCHLD to child');
 	is(readline($rd), "HI\n", '$SIG{CHLD} works in child');
@@ -199,6 +199,30 @@ SKIP: {
 	isnt($?, 0, 'non-zero exit status');
 }
 
-done_testing();
+SKIP: {
+	require PublicInbox::SpawnPP;
+	require File::Temp;
+	my $tmp = File::Temp->newdir('spawnpp-XXXX', TMPDIR => 1);
+	my $cmd = [ qw(/bin/sh -c), 'echo $HI >foo' ];
+	my $env = [ 'HI=hihi' ];
+	my $rlim = [];
+	my $pgid = -1;
+	my $pid = PublicInbox::SpawnPP::pi_fork_exec([], '/bin/sh', $cmd, $env,
+						$rlim, "$tmp", $pgid);
+	is(waitpid($pid, 0), $pid, 'spawned process exited');
+	is($?, 0, 'no error');
+	open my $fh, '<', "$tmp/foo" or die "open: $!";
+	is(readline($fh), "hihi\n", 'env+chdir worked for SpawnPP');
+	close $fh;
+	unlink("$tmp/foo") or die "unlink: $!";
+	{
+		local $ENV{MOD_PERL} = 1;
+		$pid = PublicInbox::SpawnPP::pi_fork_exec([],
+				'/bin/sh', $cmd, $env, $rlim, "$tmp", $pgid);
+	}
+	is(waitpid($pid, 0), $pid, 'spawned process exited');
+	open $fh, '<', "$tmp/foo" or die "open: $!";
+	is(readline($fh), "hihi\n", 'env+chdir SpawnPP under (faked) MOD_PERL');
+}
 
-1;
+done_testing();

                 reply	other threads:[~2023-01-29  9:45 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

  List information: https://public-inbox.org/README

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

  git send-email \
    --in-reply-to=20230129094511.3218368-1-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.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).