Extend Log->system to support the same features as BaseState->system
authortb <tb@openbsd.org>
Tue, 1 Oct 2024 18:48:29 +0000 (18:48 +0000)
committertb <tb@openbsd.org>
Tue, 1 Oct 2024 18:48:29 +0000 (18:48 +0000)
In order to support privsep in tags, we need to be able to pass some
code values in child/parent.

from espie, tested by sthen, ok giovanni

usr.sbin/pkg_add/OpenBSD/Log.pm

index 9eb4570..79b381a 100644 (file)
@@ -1,5 +1,5 @@
 # ex:ts=8 sw=4:
-# $OpenBSD: Log.pm,v 1.10 2023/06/13 09:07:17 espie Exp $
+# $OpenBSD: Log.pm,v 1.11 2024/10/01 18:48:29 tb Exp $
 #
 # Copyright (c) 2007-2010 Marc Espie <espie@openbsd.org>
 #
@@ -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;
        }
 }