summaryrefslogtreecommitdiffstats
path: root/perl-install/run_program.pm
blob: 7e91437e72417344a0f94026ca13f6a6d32eda09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package run_program;

use diagnostics;
use strict;

use log;

1;

sub run($@) { rooted('', @_) }

sub rooted {
    my ($root, $name, @args) = @_;
    my $str = ref $name ? $name->[0] : $name;
    log::l("running: $str @args" . ($root ? " with root $root" : ""));
    $root ? $root .= '/' : ($root = '');

    fork and wait, return $? == 0;
    {
	my ($stdout, $stdoutm, $stderr, $stderrm);
	($stdoutm, $stdout, @args) = @args if $args[0] =~ /^>>?$/;
	($stderrm, $stderr, @args) = @args if $args[0] =~ /^2>>?$/;

	open STDIN, "/dev/null" or die "can't open /dev/null as stdin";

	if ($stderr) {
	    $stderrm =~ s/2//;
	    open STDERR, "$stderrm $root$stderr" or die "run_program can't output in $root$stderr (mode `$stderrm')";
	} else {
	    open STDERR, ">> /dev/tty7" or open STDERR, ">> /tmp/exec.log" or die "run_program can't log :(";
	}
	if ($stdout) {
	    open STDOUT, "$stdoutm $root$stdout" or die "run_program can't output in $root$stdout (mode `$stdoutm')";
	} else {
	    open STDOUT, ">> /dev/tty7" or open STDOUT, ">> /tmp/exec.log" or die "run_program can't log :(";
	}

	$root and chroot $root;
	chdir "/";

	if (ref $name) {
	    unless (exec { $name->[0] } $name->[1], @args) {
		log::l("exec of $name->[0] failed: $!");
		exec('false') or exit(1);
	    }
	} else {
	    unless (exec $name, @args) {
		log::l("exec of $name failed: $!");
		exec('false') or exit(1);
	    }

	}
    }

}