diff options
author | Marc Espie <espie@cvs.openbsd.org> | 2010-10-24 17:04:48 +0000 |
---|---|---|
committer | Marc Espie <espie@cvs.openbsd.org> | 2010-10-24 17:04:48 +0000 |
commit | c6888bafcd63586439ba1ffbdb4242b3f805c608 (patch) | |
tree | 160d3371630959219b4c415c45030bf5a5accebb /usr.sbin | |
parent | b5420765e5b5db7532e0f97cd7e037717799b135 (diff) |
redo ->system in a smarter way (fork+exec) so that we can sneak in a code
reference to be run before the command.
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/State.pm | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/usr.sbin/pkg_add/OpenBSD/State.pm b/usr.sbin/pkg_add/OpenBSD/State.pm index e19e2f1af72..292072eb781 100644 --- a/usr.sbin/pkg_add/OpenBSD/State.pm +++ b/usr.sbin/pkg_add/OpenBSD/State.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: State.pm,v 1.17 2010/10/02 13:33:05 espie Exp $ +# $OpenBSD: State.pm,v 1.18 2010/10/24 17:04:47 espie Exp $ # # Copyright (c) 2007-2010 Marc Espie <espie@openbsd.org> # @@ -388,10 +388,31 @@ sub child_error } } +sub _system +{ + my ($self, $todo) = (shift, shift); + my $r = fork; + if (!defined $r) { + return 1; + } elsif ($r == 0) { + &$todo; + exec {$_[0]} @_ or return 1; + } else { + waitpid($r, 0); + return $?; + } +} + sub system { my $self = shift; - my $r = CORE::system(@_); + my $todo; + if (ref $_[0] eq 'CODE') { + $todo = shift; + } else { + $todo = sub {}; + } + my $r = $self->_system($todo, @_); if ($r != 0) { $self->say("system(#1) failed: #2", join(", ", @_), $self->child_error); @@ -402,9 +423,15 @@ sub system sub verbose_system { my $self = shift; + my $todo; + if (ref $_[0] eq 'CODE') { + $todo = shift; + } else { + $todo = sub {}; + } $self->print("Running #1", join(' ', @_)); - my $r = CORE::system(@_); + my $r = $self->_system($todo, @_); if ($r != 0) { $self->say("... failed: #1", $self->child_error); } else { |