diff options
author | Stuart Henderson <sthen@cvs.openbsd.org> | 2019-07-17 19:00:56 +0000 |
---|---|---|
committer | Stuart Henderson <sthen@cvs.openbsd.org> | 2019-07-17 19:00:56 +0000 |
commit | 249dc6878617b9aa800a6c7b9c5feb02da7ade22 (patch) | |
tree | 463dcc672803594f49cd1d2e80e4640e283ba6b5 /usr.sbin/pkg_add | |
parent | 2b5caba4f1317bfab009f283e381f68e92512bf1 (diff) |
backout previous commits for now; some of this is shared with dpb and
results in problems. hints from pvk@ about what introduced the breakage
we were seeing, who confirms that this backout fixes things; comitting
early to unbreak package builds.
====
a bunch of changes, all related to error-handling:
- have Handle->register also create a proper END block, so that
individual packages don't have to, and explain the issue
- kill old Unlink/Copy code that migrated to State years ago
- commonalize try{} catch {} for pkg_add/delete and pkg_create, so that
debug works the same way in both.
- switch printing command name to the catch handler, so that exceptions
are simpler to handle
and a few comments for the hairy parts...
Members:
OpenBSD/AddCreateDelete.pm:1.44->1.45
OpenBSD/AddDelete.pm:1.87->1.88
OpenBSD/Error.pm:1.33->1.34
OpenBSD/PackageRepository.pm:1.164->1.165
OpenBSD/PkgCreate.pm:1.160->1.161
OpenBSD/State.pm:1.58->1.59
OpenBSD/Temp.pm:1.34->1.35
====
Diffstat (limited to 'usr.sbin/pkg_add')
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/AddCreateDelete.pm | 21 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/AddDelete.pm | 66 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/Error.pm | 133 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/PackageRepository.pm | 14 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/PkgCreate.pm | 58 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/State.pm | 4 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/Temp.pm | 16 |
7 files changed, 188 insertions, 124 deletions
diff --git a/usr.sbin/pkg_add/OpenBSD/AddCreateDelete.pm b/usr.sbin/pkg_add/OpenBSD/AddCreateDelete.pm index 17157bd53a8..59e3aaf5f4a 100644 --- a/usr.sbin/pkg_add/OpenBSD/AddCreateDelete.pm +++ b/usr.sbin/pkg_add/OpenBSD/AddCreateDelete.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: AddCreateDelete.pm,v 1.45 2019/07/14 07:27:18 espie Exp $ +# $OpenBSD: AddCreateDelete.pm,v 1.46 2019/07/17 19:00:55 sthen Exp $ # # Copyright (c) 2007-2014 Marc Espie <espie@openbsd.org> # @@ -187,25 +187,6 @@ sub handle_options $state->handle_options($opt_string, $self, @usage); } -sub try_and_run_command -{ - my ($self, $state) = @_; - if ($state->defines('debug')) { - $self->run_command($state); - } else { - try { - $self->run_command($state); - } catch { - $state->errsay("#1: #2", $state->{cmd}, $_); - OpenBSD::Handler->reset; - if ($_ =~ m/^Caught SIG(\w+)/o) { - kill $1, $$; - } - $state->{bad}++; - }; - } -} - package OpenBSD::InteractiveStub; sub new { diff --git a/usr.sbin/pkg_add/OpenBSD/AddDelete.pm b/usr.sbin/pkg_add/OpenBSD/AddDelete.pm index e7609def601..b0c9377efcc 100644 --- a/usr.sbin/pkg_add/OpenBSD/AddDelete.pm +++ b/usr.sbin/pkg_add/OpenBSD/AddDelete.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: AddDelete.pm,v 1.88 2019/07/14 07:27:18 espie Exp $ +# $OpenBSD: AddDelete.pm,v 1.89 2019/07/17 19:00:55 sthen Exp $ # # Copyright (c) 2007-2010 Marc Espie <espie@openbsd.org> # @@ -63,7 +63,6 @@ use OpenBSD::Error; use OpenBSD::Paths; use OpenBSD::PackageInfo; use OpenBSD::AddCreateDelete; -our @ISA = qw(OpenBSD::AddCreateDelete); sub do_the_main_work { @@ -103,32 +102,49 @@ sub handle_end_tags }); } -sub run_command +sub framework { my ($self, $state) = @_; - lock_db($state->{not}, $state) unless $state->defines('nolock'); - $state->check_root; - $self->process_parameters($state); - my $dielater = $self->do_the_main_work($state); - # cleanup various things - $self->handle_end_tags($state); - $state->{recorder}->cleanup($state); - $state->ldconfig->ensure; - OpenBSD::PackingElement->finish($state); - $state->progress->clear; - $state->log->dump; - $self->finish_display($state); - if ($state->verbose >= 2 || $state->{size_only} || - $state->defines('tally')) { - $state->vstat->tally; + my $do = sub { + lock_db($state->{not}, $state) unless $state->defines('nolock'); + $state->check_root; + $self->process_parameters($state); + my $dielater = $self->do_the_main_work($state); + # cleanup various things + $self->handle_end_tags($state); + $state->{recorder}->cleanup($state); + $state->ldconfig->ensure; + OpenBSD::PackingElement->finish($state); + $state->progress->clear; + $state->log->dump; + $self->finish_display($state); + if ($state->verbose >= 2 || $state->{size_only} || + $state->defines('tally')) { + $state->vstat->tally; + } + $state->say("Extracted #1 from #2", + $state->{stats}{donesize}, + $state->{stats}{totsize}) + if defined $state->{stats} and $state->verbose; + # show any error, and show why we died... + rethrow $dielater; + }; + if ($state->defines('debug')) { + &$do; + } else { + try { + &$do; + } catch { + $state->errsay("#1", $_); + OpenBSD::Handler->reset; + if ($_ =~ m/^Caught SIG(\w+)/o) { + kill $1, $$; + } + $state->{bad}++; + }; } - $state->say("Extracted #1 from #2", - $state->{stats}{donesize}, - $state->{stats}{totsize}) - if defined $state->{stats} and $state->verbose; - # show any error, and show why we died... - rethrow $dielater; + } sub parse_and_run @@ -158,7 +174,7 @@ sub parse_and_run } } - $self->try_and_run_command($state); + $self->framework($state); if (defined $lflag) { $termios->setlflag($lflag); diff --git a/usr.sbin/pkg_add/OpenBSD/Error.pm b/usr.sbin/pkg_add/OpenBSD/Error.pm index 90451d1573c..2a6aad4893b 100644 --- a/usr.sbin/pkg_add/OpenBSD/Error.pm +++ b/usr.sbin/pkg_add/OpenBSD/Error.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: Error.pm,v 1.34 2019/07/14 07:27:18 espie Exp $ +# $OpenBSD: Error.pm,v 1.35 2019/07/17 19:00:55 sthen Exp $ # # Copyright (c) 2004-2010 Marc Espie <espie@openbsd.org> # @@ -17,8 +17,6 @@ use strict; use warnings; -# this is a set of common classes related to error handling in pkg land - package OpenBSD::Auto; sub cache(*&) { @@ -32,55 +30,32 @@ sub cache(*&) *{$callpkg."::$sym"} = $actual; } -# a bunch of other modules create persistent state that must be cleaned up -# on exit (temporary files, network connections to abort properly...) -# END blocks would do that (but see below...) but sig handling bypasses that, -# so we MUST install SIG handlers. - -# note that END will be run for *each* process, so beware! -# (temp files are registered per pid, for instance, so they only -# get cleaned when the proper pid is used) package OpenBSD::Handler; -# hash of code to run on ANY exit -my $cleanup = {}; - -sub cleanup -{ - my ($class, $sig) = @_; - # XXX note that order of cleanup is "unpredictable" - for my $v (values %$cleanup) { - &$v($sig); - } -} +my $list = []; -END { - # XXX localize $? so that cleanup doesn't fuck up our exit code - local $?; - cleanup(); -} - -# register each code block "by name" so that we can re-register each -# block several times sub register { my ($class, $code) = @_; - $cleanup->{$code} = $code; + push(@$list, $code); } my $handler = sub { my $sig = shift; - __PACKAGE__->cleanup($sig); - # after cleanup, just propagate the signal + for my $c (@$list) { + &$c($sig); + } $SIG{$sig} = 'DEFAULT'; kill $sig, $$; }; sub reset { - for my $sig (qw(INT QUIT HUP KILL TERM)) { - $SIG{$sig} = $handler; - } + $SIG{'INT'} = $handler; + $SIG{'QUIT'} = $handler; + $SIG{'HUP'} = $handler; + $SIG{'KILL'} = $handler; + $SIG{'TERM'} = $handler; } __PACKAGE__->reset; @@ -88,11 +63,95 @@ __PACKAGE__->reset; package OpenBSD::Error; require Exporter; our @ISA=qw(Exporter); -our @EXPORT=qw(try throw catch catchall rethrow); +our @EXPORT=qw(Copy Unlink try throw catch catchall rethrow); our ($FileName, $Line, $FullMessage); +my @signal_name = (); + use Carp; + +sub fillup_names +{ + { + # XXX force autoload + package verylocal; + + require POSIX; + POSIX->import(qw(signal_h)); + } + + for my $sym (keys %POSIX::) { + next unless $sym =~ /^SIG([A-Z].*)/; + my $i = eval "&POSIX::$sym()"; + next unless defined $i; + $signal_name[$i] = $1; + } + # extra BSD signals + $signal_name[5] = 'TRAP'; + $signal_name[7] = 'IOT'; + $signal_name[10] = 'BUS'; + $signal_name[12] = 'SYS'; + $signal_name[16] = 'URG'; + $signal_name[23] = 'IO'; + $signal_name[24] = 'XCPU'; + $signal_name[25] = 'XFSZ'; + $signal_name[26] = 'VTALRM'; + $signal_name[27] = 'PROF'; + $signal_name[28] = 'WINCH'; + $signal_name[29] = 'INFO'; +} + +sub find_signal +{ + my $number = shift; + + if (@signal_name == 0) { + fillup_names(); + } + + return $signal_name[$number] || $number; +} + +sub child_error +{ + my $error = shift // $?; + + my $extra = ""; + + if ($error & 128) { + $extra = " (core dumped)"; + } + if ($error & 127) { + return "killed by signal ". find_signal($error & 127).$extra; + } else { + return "exit(". ($error >> 8) . ")$extra"; + } +} + +sub Copy +{ + require File::Copy; + + my $r = File::Copy::copy(@_); + if (!$r) { + print "copy(", join(',', @_),") failed: $!\n"; + } + return $r; +} + +sub Unlink +{ + my $verbose = shift; + my $r = unlink @_; + if ($r != @_) { + print "rm @_ failed: removed only $r targets, $!\n"; + } elsif ($verbose) { + print "rm @_\n"; + } + return $r; +} + sub dienow { my ($error, $handler) = @_; diff --git a/usr.sbin/pkg_add/OpenBSD/PackageRepository.pm b/usr.sbin/pkg_add/OpenBSD/PackageRepository.pm index 25bd6b9a401..087b37ccb78 100644 --- a/usr.sbin/pkg_add/OpenBSD/PackageRepository.pm +++ b/usr.sbin/pkg_add/OpenBSD/PackageRepository.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: PackageRepository.pm,v 1.165 2019/07/14 07:27:18 espie Exp $ +# $OpenBSD: PackageRepository.pm,v 1.166 2019/07/17 19:00:55 sthen Exp $ # # Copyright (c) 2003-2010 Marc Espie <espie@openbsd.org> # @@ -82,12 +82,18 @@ sub unique return $o; } -OpenBSD::Handler->register( - sub { +my $cleanup = sub { for my $repo (values %$cache) { $repo->cleanup; } - }); +}; +END { + my $a = $?; + &$cleanup; + $? = $a; +} + +OpenBSD::Handler->register($cleanup); sub parse_fullurl { diff --git a/usr.sbin/pkg_add/OpenBSD/PkgCreate.pm b/usr.sbin/pkg_add/OpenBSD/PkgCreate.pm index ecebae95f13..388383c6c9b 100644 --- a/usr.sbin/pkg_add/OpenBSD/PkgCreate.pm +++ b/usr.sbin/pkg_add/OpenBSD/PkgCreate.pm @@ -1,6 +1,6 @@ #! /usr/bin/perl # ex:ts=8 sw=4: -# $OpenBSD: PkgCreate.pm,v 1.161 2019/07/14 07:27:18 espie Exp $ +# $OpenBSD: PkgCreate.pm,v 1.162 2019/07/17 19:00:55 sthen Exp $ # # Copyright (c) 2003-2014 Marc Espie <espie@openbsd.org> # @@ -1587,9 +1587,27 @@ sub save_history return $l; } -sub run_command +sub parse_and_run { - my ($self, $state) = @_; + my ($self, $cmd) = @_; + + my $regen_package = 0; + my $sign_only = 0; + my $rc = 0; + + my $state = OpenBSD::PkgCreate::State->new($cmd); + $state->handle_options; + + if (@ARGV == 0) { + $regen_package = 1; + } elsif (@ARGV != 1) { + if (defined $state->{contents} || + !defined $state->{signature_params}) { + $state->usage("Exactly one single package name is required: #1", join(' ', @ARGV)); + } + } + + try { if (defined $state->opt('Q')) { $state->{opt}{q} = 1; } @@ -1599,7 +1617,7 @@ sub run_command } my $plist; - if ($state->{regen_package}) { + if ($regen_package) { if (!defined $state->{contents} || @{$state->{contents}} > 1) { $state->usage("Exactly one single packing-list is required"); } @@ -1623,7 +1641,7 @@ sub run_command $plist->stub_digest($ordered); } else { $state->set_status("checksumming"); - if ($state->{regen_package}) { + if ($regen_package) { $state->progress->visit_with_count($plist, 'verify_checksum'); } else { @@ -1666,7 +1684,7 @@ sub run_command $state->{bad} = 0; my $wname; - if ($state->{regen_package}) { + if ($regen_package) { $wname = $plist->pkgname.".tgz"; } else { $plist->save or $state->fatal("can't write packing-list"); @@ -1683,29 +1701,11 @@ sub run_command if (!$state->defines("stub")) { $self->finish_manpages($state, $plist); } -} - -sub parse_and_run -{ - my ($self, $cmd) = @_; - - my $sign_only = 0; - my $rc = 0; - - my $state = OpenBSD::PkgCreate::State->new($cmd); - $state->handle_options; - - if (@ARGV == 0) { - $state->{regen_package} = 1; - } elsif (@ARGV != 1) { - if (defined $state->{contents} || - !defined $state->{signature_params}) { - $state->usage("Exactly one single package name is required: #1", join(' ', @ARGV)); - } - } - - $self->try_and_run_command($state); - return $state->{bad} != 0; + } catch { + $state->errsay("#1", $_); + $rc = 1; + }; + return $rc; } 1; diff --git a/usr.sbin/pkg_add/OpenBSD/State.pm b/usr.sbin/pkg_add/OpenBSD/State.pm index 1c3a752723d..92d83ccb923 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.59 2019/07/14 07:27:18 espie Exp $ +# $OpenBSD: State.pm,v 1.60 2019/07/17 19:00:55 sthen Exp $ # # Copyright (c) 2007-2014 Marc Espie <espie@openbsd.org> # @@ -214,7 +214,7 @@ sub _fatal # the way is to eval { croak @_}; and decide what to do with $@. delete $SIG{__DIE__}; $self->sync_display; - croak @_, "\n"; + croak $self->{cmd}, ": ", @_, "\n"; } sub fatal diff --git a/usr.sbin/pkg_add/OpenBSD/Temp.pm b/usr.sbin/pkg_add/OpenBSD/Temp.pm index db695c1e158..6a9096a36a4 100644 --- a/usr.sbin/pkg_add/OpenBSD/Temp.pm +++ b/usr.sbin/pkg_add/OpenBSD/Temp.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: Temp.pm,v 1.35 2019/07/14 07:27:18 espie Exp $ +# $OpenBSD: Temp.pm,v 1.36 2019/07/17 19:00:55 sthen Exp $ # # Copyright (c) 2003-2005 Marc Espie <espie@openbsd.org> # @@ -26,24 +26,26 @@ use OpenBSD::Error; our $tempbase = $ENV{'PKG_TMPDIR'} || OpenBSD::Paths->vartmp; -# stuff that should be cleaned up on exit, registered by pid, -# so that it gets cleaned on exit from the correct process - my $dirs = {}; my $files = {}; my ($lastname, $lasterror, $lasttype); -OpenBSD::Handler->register( - sub { +my $cleanup = sub { while (my ($name, $pid) = each %$files) { unlink($name) if $pid == $$; } while (my ($dir, $pid) = each %$dirs) { OpenBSD::Error->rmtree([$dir]) if $pid == $$; } - }); +}; +END { + my $r = $?; + &$cleanup; + $? = $r; +} +OpenBSD::Handler->register($cleanup); sub dir { |