[linux] 02/08: linux-image: Make user hook failure handling consistent across maintainer scripts

debian-kernel at lists.debian.org debian-kernel at lists.debian.org
Wed Jan 27 19:54:57 UTC 2016


This is an automated email from the git hooks/post-receive script.

benh pushed a commit to branch master
in repository linux.

commit 78007e962c408d908c5df1ba0ee1560ed40a6f66
Author: Ben Hutchings <ben at decadent.org.uk>
Date:   Wed Jan 27 00:29:27 2016 +0000

    linux-image: Make user hook failure handling consistent across maintainer scripts
    
    - Use system_failure_message() to generate the error message
    - Exit using "die", not passing up the exit code (which will be 0 in case the
      process was terminated by signal!)
    - Remove redundant path lookup since system() already does that
---
 debian/templates/image.plain.postinst.in | 17 ++--------
 debian/templates/image.plain.postrm.in   | 50 ++++++++----------------------
 debian/templates/image.plain.preinst.in  | 53 ++++++++------------------------
 debian/templates/image.plain.prerm.in    | 53 ++++++++------------------------
 4 files changed, 42 insertions(+), 131 deletions(-)

diff --git a/debian/templates/image.plain.postinst.in b/debian/templates/image.plain.postinst.in
index a78f6e7..7c4e948 100755
--- a/debian/templates/image.plain.postinst.in
+++ b/debian/templates/image.plain.postinst.in
@@ -551,25 +551,14 @@ if (($arch eq "mips" || $arch eq "mipsel")
 
 # set the env var stem
 $ENV{'STEM'} = "linux";
+
 sub run_hook {
   my $type   = shift;
   my $script = shift;
 
   print STDERR "Running $script.\n";
-  system ("$script $version $realimageloc$kimage-$version") &&
-    print STDERR "User $type hook script [$script] ";
-  if ($?) {
-    if ($? == -1) {
-      print STDERR "failed to execute: $!\n";
-    }
-    elsif ($? & 127) {
-      printf STDERR "died with signal %d, %s coredump\n",
-        ($? & 127),  ($? & 128) ? 'with' : 'without';
-    }
-    else {
-      printf STDERR "exited with value %d\n", $? >> 8;
-    }
-    exit $? >> 8;
+  if (system ("$script $version $realimageloc$kimage-$version")) {
+    die ("$script failed: " . system_failure_message());
   }
 }
 
diff --git a/debian/templates/image.plain.postrm.in b/debian/templates/image.plain.postrm.in
index 3491dfa..7b13b76 100755
--- a/debian/templates/image.plain.postrm.in
+++ b/debian/templates/image.plain.postrm.in
@@ -3,6 +3,7 @@
 use strict;
 use warnings;
 use Cwd 'abs_path';
+use POSIX ();
 
 # Debconf may not be around here.
 my $have_debconf = 0;
@@ -146,48 +147,23 @@ sub image_magic {
 # set the env var stem
 $ENV{'STEM'} = "linux";
 
-sub exec_script {
-  my $type   = shift;
-  my $script = shift;
-  print STDERR "Running $type hook script $script.\n";
-  system ("$script $version $realimageloc$kimage-$version") &&
-    print STDERR "User $type hook script [$script] ";
-  if ($?) {
-    if ($? == -1) {
-      print STDERR "failed to execute: $!\n";
-    }
-    elsif ($? & 127) {
-      printf STDERR "died with signal %d, %s coredump\n",
-        ($? & 127),  ($? & 128) ? 'with' : 'without';
-    }
-    else {
-      printf STDERR "exited with value %d\n", $? >> 8;
-    }
+sub system_failure_message {
+  if ($? < 0) {
+    return "$!";
+  } elsif (POSIX::WIFSIGNALED($?)) {
+    return sprintf('signal %d', POSIX::WTERMSIG($?));
+  } else {
+    return sprintf('exit code %d', POSIX::WEXITSTATUS($?));
   }
 }
+
 sub run_hook {
   my $type   = shift;
   my $script = shift;
-  if ($script =~ m,^/,) {
-    # Full path provided for the hook script
-    if (-x "$script") {
-      &exec_script($type,$script);
-    }
-    else {
-      warn "The provided $type hook script [$script] could not be run.\n";
-    }
-  }
-  else {
-    # Look for it in a safe path
-    for my $path ('/bin', '/sbin', '/usr/bin', '/usr/sbin') {
-      if (-x "$path/$script") {
-        &exec_script($type, "$path/$script");
-        return 0;
-      }
-    }
-    # No luck
-    print STDERR "Could not find $type hook script [$script].\n";
-    warn "Looked in: '/bin', '/sbin', '/usr/bin', '/usr/sbin'\n";
+
+  print STDERR "Running $script.\n";
+  if (system ("$script $version $realimageloc$kimage-$version")) {
+    die ("$script failed: " . system_failure_message());
   }
 }
 
diff --git a/debian/templates/image.plain.preinst.in b/debian/templates/image.plain.preinst.in
index 608a34c..0d0916f 100755
--- a/debian/templates/image.plain.preinst.in
+++ b/debian/templates/image.plain.preinst.in
@@ -2,6 +2,7 @@
 #
 use strict;
 use warnings;
+use POSIX ();
 
 use Debconf::Client::ConfModule qw(:all);
 version('2.0');
@@ -39,57 +40,29 @@ if (-r "$CONF_LOC" && -f "$CONF_LOC"  ) {
   }
 }
 
-
 # set the env var stem
 $ENV{'STEM'} = "linux";
 
-sub exec_script {
-  my $type   = shift;
-  my $script = shift;
-  print STDERR "Running $type hook script $script.\n";
-  system ("$script $version $realimageloc$kimage-$version") &&
-    print STDERR "User $type hook script [$script] ";
-  if ($?) {
-    if ($? == -1) {
-      print STDERR "failed to execute: $!\n";
-    }
-    elsif ($? & 127) {
-      printf STDERR "died with signal %d, %s coredump\n",
-        ($? & 127),  ($? & 128) ? 'with' : 'without';
-    }
-    else {
-      printf STDERR "exited with value %d\n", $? >> 8;
-    }
-    exit $? >> 8;
+sub system_failure_message {
+  if ($? < 0) {
+    return "$!";
+  } elsif (POSIX::WIFSIGNALED($?)) {
+    return sprintf('signal %d', POSIX::WTERMSIG($?));
+  } else {
+    return sprintf('exit code %d', POSIX::WEXITSTATUS($?));
   }
 }
+
 sub run_hook {
   my $type   = shift;
   my $script = shift;
-  if ($script =~ m,^/,) {
-    # Full path provided for the hook script
-    if (-x "$script") {
-      &exec_script($type,$script);
-    }
-    else {
-      die "The provided $type hook script [$script] could not be run.\n";
-    }
-  }
-  else {
-    # Look for it in a safe path
-    for my $path ('/bin', '/sbin', '/usr/bin', '/usr/sbin') {
-      if (-x "$path/$script") {
-        &exec_script($type, "$path/$script");
-        return 0;
-      }
-    }
-    # No luck
-    print STDERR "Could not find $type hook script [$script].\n";
-    die "Looked in: '/bin', '/sbin', '/usr/bin', '/usr/sbin'\n";
+
+  print STDERR "Running $script.\n";
+  if (system ("$script $version $realimageloc$kimage-$version")) {
+    die ("$script failed: " . system_failure_message());
   }
 }
 
-
 my $options;
 for (@ARGV) {
 	s,','\\'',g;
diff --git a/debian/templates/image.plain.prerm.in b/debian/templates/image.plain.prerm.in
index bedb503..45f7cff 100755
--- a/debian/templates/image.plain.prerm.in
+++ b/debian/templates/image.plain.prerm.in
@@ -2,6 +2,7 @@
 # 
 use strict;
 use warnings;
+use POSIX ();
 use Debconf::Client::ConfModule qw(:all);
 version('2.0');
 my $capb=capb("backup");
@@ -80,57 +81,29 @@ if ($running eq $version) {
 
 chdir("/") or die "could not chdir to /:$!\n";
 
-
 # set the env var stem
 $ENV{'STEM'} = "linux";
 
-sub exec_script {
-  my $type   = shift;
-  my $script = shift;
-  print STDERR "Running $type hook script $script.\n";
-  system ("$script $version $realimageloc$kimage-$version") &&
-    print STDERR "User $type hook script [$script] ";
-  if ($?) {
-    if ($? == -1) {
-      print STDERR "failed to execute: $!\n";
-    }
-    elsif ($? & 127) {
-      printf STDERR "died with signal %d, %s coredump\n",
-        ($? & 127),  ($? & 128) ? 'with' : 'without';
-    }
-    else {
-      printf STDERR "exited with value %d\n", $? >> 8;
-    }
-    exit $? >> 8;
+sub system_failure_message {
+  if ($? < 0) {
+    return "$!";
+  } elsif (POSIX::WIFSIGNALED($?)) {
+    return sprintf('signal %d', POSIX::WTERMSIG($?));
+  } else {
+    return sprintf('exit code %d', POSIX::WEXITSTATUS($?));
   }
 }
+
 sub run_hook {
   my $type   = shift;
   my $script = shift;
-  if ($script =~ m,^/,) {
-    # Full path provided for the hook script
-    if (-x "$script") {
-      &exec_script($type,$script);
-    }
-    else {
-      die "The provided $type hook script [$script] could not be run.\n";
-    }
-  }
-  else {
-    # Look for it in a safe path
-    for my $path ('/bin', '/sbin', '/usr/bin', '/usr/sbin') {
-      if (-x "$path/$script") {
-        &exec_script($type, "$path/$script");
-        return 0;
-      }
-    }
-    # No luck
-    print STDERR "Could not find $type hook script [$script].\n";
-    die "Looked in: '/bin', '/sbin', '/usr/bin', '/usr/sbin'\n";
+
+  print STDERR "Running $script.\n";
+  if (system ("$script $version $realimageloc$kimage-$version")) {
+    die ("$script failed: " . system_failure_message());
   }
 }
 
-
 my $options;
 for (@ARGV) {
 	s,','\\'',g;

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/kernel/linux.git



More information about the Kernel-svn-changes mailing list