Index | Thread | Search

From:
Marc Espie <marc.espie.openbsd@gmail.com>
Subject:
[patch] make pkg_add internals a bit more regular
To:
tech@openbsd.org
Cc:
afresh1@openbsd.org, sthen@openbsd.org, bentley@openbsd.org
Date:
Tue, 17 Sep 2024 15:10:59 +0200

Download raw body.

Thread
I got bitten recently by the fact that
Log->system does not support the same features as
BaseState->system

(namely, passing some code values in child/father, which is intended
to eventually enable privsep in tags)

So, the following stand-alone patch should go in.

All this does is rewrite the pipeopen to allow code snippets to get
in the middle.

Please test and comment.

Index: OpenBSD/Log.pm
===================================================================
RCS file: /vide/cvs/src/usr.sbin/pkg_add/OpenBSD/Log.pm,v
diff -u -p -r1.10 Log.pm
--- OpenBSD/Log.pm	13 Jun 2023 09:07:17 -0000	1.10
+++ OpenBSD/Log.pm	17 Sep 2024 13:07:59 -0000
@@ -104,7 +104,24 @@ sub fatal($self, @p)
 
 sub system($self, @p)
 {
-	if (open(my $grab, "-|", @p)) {
+	my ($todo, $todo2);
+	if (ref $p[0] eq 'CODE') {
+		$todo = shift @p;
+	} else {
+		$todo = sub() {};
+	}
+	if (ref $p[0] eq 'CODE') {
+		$todo2 = shift @p;
+	} else {
+		$todo2 = sub() {};
+	}
+	my $child_pid = open(my $grab, "-|");
+	if (!defined $child_pid) {
+		$self->{p}->say("system(#1) was not run: #2 #3",
+		    join(", ", @p), $!, $self->{p}->child_error);
+	}
+	if ($child_pid) {
+		&$todo2();
 		while (<$grab>) {
 			$self->{p}->_print($_);
 		}
@@ -115,8 +132,9 @@ sub system($self, @p)
 		}
 		return $?;
 	} else {
-		$self->{p}->say("system(#1) was not run: #2 #3",
-		    join(", ", @p), $!, $self->{p}->child_error);
+		$DB::inhibit_exit = 0;
+		&$todo();
+		exec {$p[0]} (@p) or exit 1;
 	}
 }