sigtrap 0
BSD::Resource 0
+ Expect::Simple 0
File::Slurp 0
Gruntmaster::Data 0
IPC::Signal 0
JSON 0
List::MoreUtils 0
Log::Log4perl 0
+ String::ShellQuote 0
Try::Tiny 0/
},
META_MERGE => {
our $VERSION = '5999.000_004';
use Gruntmaster::Daemon::Constants qw/ERR/;
-use Gruntmaster::Daemon::Format qw/prepare_files/;
+use Gruntmaster::Daemon::Format qw/prepare_files stopvms/;
use File::Basename qw/fileparse/;
use File::Temp qw/tempdir/;
$meta->{result} = -1;
$meta->{result_text} = $_;
};
+ stopvms;
get_logger->info("Job result: " . $meta->{result_text});
return unless $jobr;
}
sub run{
+ warn "No GRUNTMASTER_VM environment variable. Running without a VM is a security risk.\n" unless $ENV{GRUNTMASTER_VM};
require Gruntmaster::Data;
$db = Gruntmaster::Data->connect('dbi:Pg:');
Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf');
use parent qw/Exporter/;
no if $] > 5.017011, warnings => 'experimental::smartmatch';
-use POSIX qw//;
+use Expect::Simple;
use File::Basename qw/fileparse/;
use File::Slurp qw/read_file write_file/;
-use Gruntmaster::Daemon::Constants qw/TLE OLE DIED NZX/;
-use Time::HiRes qw/alarm/;
use List::MoreUtils qw/natatime/;
use Log::Log4perl qw/get_logger/;
-use IPC::Signal qw/sig_name sig_num/;
+use POSIX qw/mkfifo/;
+use String::ShellQuote qw/shell_quote/;
use Try::Tiny;
our $VERSION = "5999.000_004";
-our @EXPORT_OK = qw/prepare_files/;
+our @EXPORT_OK = qw/prepare_files stopvms/;
##################################################
+our (%vm);
+
+sub runvm {
+ my ($name) = @_;
+ return unless $ENV{GRUNTMASTER_VM};
+ mkfifo "$name.in", 0600;
+ mkfifo "$name.out", 0600;
+ get_logger->trace("Starting VM $name");
+ $vm{$name} = Expect::Simple->new({
+ Cmd => "$ENV{GRUNTMASTER_VM} $name",
+ Prompt => '# ',
+ DisconnectCmd => 'exit',
+ RawPty => 1,
+ Timeout => 10,
+ });
+}
+
+sub stopvms { %vm = () }
+
sub execlist {
- my (@args) = @_;
- my $ret = fork // die 'Cannot fork';
- if ($ret) {
- waitpid $ret, 0;
- die "gruntmaster-exec died\n" if -z 'exec-result';
- my ($excode, $exmsg) = read_file 'exec-result';
- unlink 'exec-result';
- chomp ($excode, $exmsg);
- die [$excode, $exmsg] if $excode > 0;
+ my ($vm, @args) = @_;
+ my $er = "exec-result-$vm";
+ if ($vm{$vm}) {
+ my $cmd = ">$er " . shell_quote 'gruntmaster-exec', @args;
+ get_logger->trace("Running in VM $vm: $cmd");
+ $vm{$vm}->send($cmd);
} else {
- open STDOUT, '>exec-result';
- exec 'gruntmaster-exec', @args;
+ my $ret = fork // die 'Cannot fork';
+ if ($ret) {
+ waitpid $ret, 0;
+ } else {
+ open STDOUT, ">$er";
+ exec 'gruntmaster-exec', @args;
+ }
}
+
+ die "gruntmaster-exec died\n" if -z $er;
+ my ($excode, $exmsg) = read_file $er;
+ unlink $er;
+ chomp ($excode, $exmsg);
+ get_logger->trace("Exec result: $excode $exmsg");
+ die [$excode, $exmsg] if $excode > 0;
}
sub command_and_args{
while (my ($fd, $file) = $it->()) {
push @args, "--fd=$fd $file";
}
- execlist @args, command_and_args($format, $basename);
+ execlist $basename, @args, command_and_args($format, $basename);
}
}
get_logger->trace("Preparing file $name...");
try {
- execlist '--fd=1 >>errors', '--fd=2 >>errors', 'gruntmaster-compile', $format, $name;
+ execlist prog => '--fd=1 >>errors', '--fd=2 >>errors', 'gruntmaster-compile', $format, $name;
} catch {
die "Compile error\n"
} finally {
sub prepare_files{
my $meta = shift;
+ runvm $_ for keys %{$meta->{files}};
for my $file (values %{$meta->{files}}) {
my ($format, $name, $content) = @{$file}{qw/format name content/};
mkfifo 'fifo1', 0600 or die $! unless -e 'fifo1';
mkfifo 'fifo2', 0600 or die $! unless -e 'fifo2';
+ if ($test == 1 && $ENV{GRUNTMASTER_VM}) {
+ exec 'cat <prog.out >ver.in' if fork;
+ exec 'cat <ver.out >prog.in' if fork;
+ }
+
my $ret = fork // get_logger->logdie("Fork failed: $!");
if ($ret) {
try {
- $meta->{files}{prog}{run}->($meta->{files}{prog}{name}, fds => [qw/0 fifo1 1 >fifo2/], map {defined $meta->{$_} ? ($_ => $meta->{$_}) : () } qw/timeout mlimit/);
+ my @fds = $ENV{GRUNTMASTER_VM} ? qw,0 /dev/ttyS1 1 >/dev/ttyS1, : qw/0 fifo1 1 >fifo2/;
+ $meta->{files}{prog}{run}->($meta->{files}{prog}{name}, fds => \@fds, map {defined $meta->{$_} ? ($_ => $meta->{$_}) : () } qw/timeout mlimit/);
} catch {
die $_
} finally {
die [WA, "Wrong Answer"] if $?;
} else {
try {
- $meta->{files}{ver}{run}->($meta->{files}{ver}{name}, fds => [qw/1 >fifo1 0 fifo2 4 >result/], args => [$test], map {defined $meta->{$_} ? ($_ => $meta->{$_}) : () } qw/timeout mlimit/);
+ my @fds = $ENV{GRUNTMASTER_VM} ? qw,1 >/dev/ttyS1 0 /dev/ttyS1, : qw/1 >fifo1 0 fifo2/;
+ $meta->{files}{ver}{run}->($meta->{files}{ver}{name}, fds => [@fds, qw,4 >result,], args => [$test], map {defined $meta->{$_} ? ($_ => $meta->{$_}) : () } qw/timeout mlimit/);
} catch {
exit 1;
};
--- /dev/null
+#!/bin/bash
+[ `id -u` -eq 0 ] || echo 'This script must be run as root' && exit 1
+rm -rf vm/ vm.squashfs
+multistrap -f vm.conf
+ln -s vm/usr/bin/mawk vm/bin/awk
+echo 'virtfs /mnt 9p trans=virtio,auto 0 0' > vm/etc/fstab
+echo '/sbin/poweroff -f' > vm/.bash_logout
+cat > vm/.profile <<EOF
+mount -a
+stty -F /dev/ttyS1 -echo
+cd /mnt
+export PS1="# "
+EOF
+cat > vm/etc/initramfs-tools/modules <<EOF
+squashfs
+9p
+9pnet
+9pnet_virtio
+EOF
+cp gruntmaster-exec gruntmaster-compile vm/usr/bin/
+chroot vm update-initramfs -d -k 3.2.0-4-amd64
+chroot vm update-initramfs -c -k 3.2.0-4-amd64
+mksquashfs vm vm.squashfs -comp lzo
--- /dev/null
+#!/bin/bash
+ROOT=/home/marius/git/gruntmaster-daemon
+
+qemu-system-x86_64 \
+ -enable-kvm -cpu host -m 256MB \
+ -nodefaults -nographic \
+ -kernel $ROOT/vm/boot/vmlinuz-3.2.0-4-amd64 \
+ -initrd $ROOT/vm/boot/initrd.img-3.2.0-4-amd64 \
+ -append 'root=/dev/vda console=ttyS0,38400 quiet init=/bin/bash --login' \
+ -drive file=$ROOT/vm.squashfs,if=virtio,readonly \
+ -virtfs local,id=virtfs0,path=.,security_model=none,mount_tag=virtfs \
+ -chardev stdio,id=stdio,signal=on \
+ -chardev pipe,id=hostpipe,path=$1 \
+ -serial chardev:stdio \
+ -serial chardev:hostpipe
--- /dev/null
+[General]
+directory=vm/
+cleanup=true
+bootstrap=Debian
+aptsources=Debian
+
+[Debian]
+packages=linux-image-3.2.0-4-amd64 mawk libbsd-resource-perl libipc-signal-perl g++
+source=http://ftp.ro.debian.org/debian
+keyring=debian-archive-keyring
+suite=wheezy
+components=main
+omitdebsrc=true