This adds the --setup-hook command line argument that allows a
program to be run from inside the chroot just before the build
started.  All the scalar build and config settings are exported
to the environment so the script can examine them to take any
needed actions.

Signed-off-by: Kees Cook <kees.c...@canonical.com>
---
 lib/Sbuild/Build.pm   |   43 +++++++++++++++++++++++++++++++++++++++++++
 lib/Sbuild/Conf.pm    |    5 +++++
 lib/Sbuild/Options.pm |    3 +++
 3 files changed, 51 insertions(+), 0 deletions(-)

diff --git a/lib/Sbuild/Build.pm b/lib/Sbuild/Build.pm
index d2f6e0b..f6d8b38 100644
--- a/lib/Sbuild/Build.pm
+++ b/lib/Sbuild/Build.pm
@@ -273,6 +273,25 @@ sub run {
        goto cleanup_close;
     }
 
+    # Run setup-hook before processing deps and build
+    if ($self->get_conf('SETUP_HOOK')) {
+       my $pipe = $session->pipe_command(
+           { COMMAND => [$self->get_conf('SETUP_HOOK')],
+             ENV => $self->get_env('SBUILD_BUILD_'),
+             USER => "root",
+             PRIORITY => 0,
+             CHROOT => 1 });
+       while(<$pipe>) {
+           $self->log($_);
+       }
+       close($pipe);
+       if ($?) {
+           $self->log("setup-hook failed\n");
+           $self->set_status('skipped');
+           goto cleanup_close;
+       }
+    }
+
     $self->set('Pkg Fail Stage', 'install-deps');
     if (!$self->install_deps()) {
        $self->log("Source-dependencies not satisfied; skipping " .
@@ -1539,6 +1558,30 @@ sub check_dependencies {
     return $fail;
 }
 
+# Produce a hash suitable for ENV export
+sub get_env ($$) {
+    my $self = shift;
+    my $prefix = shift;
+
+    sub _env_loop ($$$$) {
+       my ($env,$ref,$keysref,$prefix) = @_;
+
+       foreach my $key (keys( %{ $keysref } )) {
+           my $value = $ref->get($key);
+           next if (!defined($value));
+           next if (ref($value));
+           my $name = "${prefix}${key}";
+           $name =~ s/ /_/g;
+           $env->{$name} = $value;
+        }
+    }
+
+    my $envlist = {};
+    _env_loop($envlist, $self, $self, $prefix);
+    _env_loop($envlist, $self->get('Config'), $self->get('Config')->{'KEYS'}, 
"${prefix}CONF_");
+    return $envlist;
+}
+
 sub get_apt_policy {
     my $self = shift;
     my @interest = @_;
diff --git a/lib/Sbuild/Conf.pm b/lib/Sbuild/Conf.pm
index 3187dbb..cbf084a 100644
--- a/lib/Sbuild/Conf.pm
+++ b/lib/Sbuild/Conf.pm
@@ -302,6 +302,9 @@ sub init_allowed_keys {
        'SBUILD_MODE'                           => {
            DEFAULT => 'user'
        },
+       'SETUP_HOOK'                            => {
+           DEFAULT => undef
+       },
        'FORCE_ORIG_SOURCE'                     => {
            DEFAULT => 0
        },
@@ -492,6 +495,7 @@ sub read_config {
     my $sbuild_mode = undef;
     my $debug = undef;
     my $force_orig_source = undef;
+    my $setup_hook = undef;
     my %individual_stalled_pkg_timeout;
     undef %individual_stalled_pkg_timeout;
     my $path = undef;
@@ -569,6 +573,7 @@ sub read_config {
     $self->set('CHROOT_SPLIT', $chroot_split);
     $self->set('SBUILD_MODE', $sbuild_mode);
     $self->set('FORCE_ORIG_SOURCE', $force_orig_source);
+    $self->set('SETUP_HOOK', $setup_hook);
     $self->set('INDIVIDUAL_STALLED_PKG_TIMEOUT',
               \%individual_stalled_pkg_timeout)
        if (%individual_stalled_pkg_timeout);
diff --git a/lib/Sbuild/Options.pm b/lib/Sbuild/Options.pm
index f3701ac..e5127c5 100644
--- a/lib/Sbuild/Options.pm
+++ b/lib/Sbuild/Options.pm
@@ -142,6 +142,9 @@ sub set_options {
                       "stats-dir=s" => sub {
                           $self->set_conf('STATS_DIR', $_[1]);
                       },
+                      "setup-hook=s" => sub {
+                          $self->set_conf('SETUP_HOOK', $_[1]);
+                      },
                       "use-snapshot" => sub {
                           $self->set_conf('GCC_SNAPSHOT', 1);
                           $self->set_conf('LD_LIBRARY_PATH',
-- 
1.6.2.4

-- 
Kees Cook



-- 
To UNSUBSCRIBE, email to debian-bugs-dist-requ...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org

Reply via email to