Просмотр исходного кода

Dpkg::IPC: Modify fork_and_exec() to handle environment variables

* scripts/Dpkg/IPC.pm (fork_and_exec): Add a new "env" option
that enables setting environment variables in the child process.
Raphael Hertzog лет назад: 18
Родитель
Сommit
7e278935ff
1 измененных файлов с 17 добавлено и 1 удалено
  1. 17 1
      scripts/Dpkg/IPC.pm

+ 17 - 1
scripts/Dpkg/IPC.pm

@@ -106,6 +106,11 @@ but not the pid.
 Scalar. The child process will chdir in the indicated directory before
 calling exec.
 
+=item env
+
+Hash reference. The child process will populate %ENV with the items of the
+hash before calling exec. This allows exporting environment variables.
+
 =back
 
 =cut
@@ -129,10 +134,14 @@ sub _sanity_check_opts {
     foreach (qw(to_string from_string to_pipe from_pipe)) {
 	if (exists $opts{$_} and
 	    (!ref($opts{$_}) or ref($opts{$_}) ne 'SCALAR')) {
-	    error("paramter $_ must be a scalar reference");
+	    error("parameter $_ must be a scalar reference");
 	}
     }
 
+    if (exists $opts{"env"} and ref($opts{"env"}) ne 'HASH') {
+	error("parameter env must be a hash reference");
+    }
+
     return %opts;
 }
 
@@ -173,6 +182,13 @@ sub fork_and_exec {
     my $pid = fork();
     syserr(_g("fork for %s"), "@prog") unless defined $pid;
     if (not $pid) {
+	# Define environment variables
+	if ($opts{"env"}) {
+	    foreach (keys %{$opts{"env"}}) {
+		$ENV{$_} = $opts{"env"}{$_};
+	    }
+	}
+	# Change the current directory
 	if ($opts{"chdir"}) {
 	    chdir($opts{"chdir"}) || syserr(_g("chdir to %s"), $opts{"chdir"});
 	}