2022-01-23 16:49:23 +01:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
2022-01-30 00:51:39 +01:00
|
|
|
# File: vm-server
|
2022-01-23 16:49:23 +01:00
|
|
|
#
|
2022-02-10 19:58:56 +01:00
|
|
|
# Purpose: The PBot Host Server manages the guest virtual machine state and
|
|
|
|
# listens for incoming commands from vm-client. Invokes vm-exec to send
|
|
|
|
# commands to the PBot Guest Server (guest-server).
|
|
|
|
|
2022-01-23 16:49:23 +01:00
|
|
|
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2022-02-07 05:01:56 +01:00
|
|
|
use 5.020;
|
|
|
|
|
2022-01-23 16:49:23 +01:00
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
2022-02-07 05:01:56 +01:00
|
|
|
use feature qw(signatures);
|
|
|
|
no warnings qw(experimental::signatures);
|
|
|
|
|
2022-01-23 16:49:23 +01:00
|
|
|
use IO::Socket;
|
|
|
|
use Net::hostent;
|
|
|
|
use IPC::Shareable;
|
|
|
|
use Time::HiRes qw/gettimeofday/;
|
|
|
|
use Encode;
|
|
|
|
|
2022-01-29 21:22:48 +01:00
|
|
|
use constant {
|
2022-02-07 05:01:56 +01:00
|
|
|
SERVER_PORT => $ENV{PBOTVM_SERVER} // 9000,
|
|
|
|
HEARTBEAT_PORT => $ENV{PBOTVM_HEART} // 5556,
|
|
|
|
DOMAIN_NAME => $ENV{PBOTVM_DOMAIN} // 'pbot-vm',
|
|
|
|
COMPILE_TIMEOUT => $ENV{PBOTVM_TIMEOUT} // 10,
|
2022-01-29 21:22:48 +01:00
|
|
|
};
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-07 05:01:56 +01:00
|
|
|
sub vm_revert() {
|
|
|
|
return if $ENV{PBOTVM_NOREVERT};
|
|
|
|
print "Reverting vm...\n";
|
|
|
|
system('time virsh snapshot-revert '.DOMAIN_NAME.' 1');
|
|
|
|
print "Reverted.\n";
|
2022-01-23 16:49:23 +01:00
|
|
|
}
|
|
|
|
|
2022-02-08 05:33:24 +01:00
|
|
|
sub execute($command) {
|
|
|
|
print "execute ($command)\n";
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-10 19:58:56 +01:00
|
|
|
# to get $? from pipe
|
|
|
|
local $SIG{CHLD} = 'DEFAULT';
|
|
|
|
|
2023-03-13 00:06:04 +01:00
|
|
|
my $pid = open(my $fh, '-|', split / /, encode('UTF-8', $command));
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-07 05:01:56 +01:00
|
|
|
if (not defined $pid) {
|
|
|
|
print "Couldn't fork: $!\n";
|
|
|
|
return (-13, "[Fatal error]");
|
|
|
|
}
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-10 19:58:56 +01:00
|
|
|
my $result = eval {
|
|
|
|
my $output = '';
|
|
|
|
local $SIG{ALRM} = sub { kill 9, $pid; die "Timed-out: $output\n"; };
|
2022-02-07 05:01:56 +01:00
|
|
|
alarm(COMPILE_TIMEOUT);
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2023-03-13 00:06:04 +01:00
|
|
|
while (my $line = decode('UTF-8', <$fh>)) {
|
2022-02-10 19:58:56 +01:00
|
|
|
$output .= $line;
|
2022-02-07 05:01:56 +01:00
|
|
|
}
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-10 19:58:56 +01:00
|
|
|
return $output;
|
2022-02-07 05:01:56 +01:00
|
|
|
};
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-07 05:01:56 +01:00
|
|
|
alarm 0;
|
2022-02-10 19:58:56 +01:00
|
|
|
close $fh;
|
|
|
|
|
|
|
|
my $ret = $? >> 8;
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-07 05:01:56 +01:00
|
|
|
if (my $exception = $@) {
|
2022-02-08 05:33:24 +01:00
|
|
|
# handle time-out exception
|
2022-02-07 05:01:56 +01:00
|
|
|
if ($exception =~ /Timed-out: (.*)/) {
|
2022-01-23 16:49:23 +01:00
|
|
|
return (-13, "[Timed-out] $1");
|
|
|
|
}
|
|
|
|
|
2022-02-08 05:33:24 +01:00
|
|
|
# propagate unhandled exception
|
2022-02-07 05:01:56 +01:00
|
|
|
die $exception;
|
2022-01-23 16:49:23 +01:00
|
|
|
}
|
2022-02-07 05:01:56 +01:00
|
|
|
|
|
|
|
return ($ret, $result);
|
2022-01-23 16:49:23 +01:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:01:56 +01:00
|
|
|
sub connect_to_heartbeat() {
|
2022-01-23 16:49:23 +01:00
|
|
|
my $heartbeat;
|
|
|
|
my $attempts = 15;
|
|
|
|
|
|
|
|
while (!$heartbeat && $attempts > 0) {
|
2022-02-07 05:01:56 +01:00
|
|
|
print "Connecting to heartbeat on port ".HEARTBEAT_PORT." ... ";
|
2022-01-23 16:49:23 +01:00
|
|
|
|
|
|
|
$heartbeat = IO::Socket::INET->new (
|
|
|
|
PeerAddr => '127.0.0.1',
|
2022-01-29 21:22:48 +01:00
|
|
|
PeerPort => HEARTBEAT_PORT,
|
2022-01-23 16:49:23 +01:00
|
|
|
Proto => 'tcp',
|
|
|
|
Type => SOCK_STREAM,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$heartbeat) {
|
|
|
|
print "failed.\n";
|
|
|
|
--$attempts;
|
|
|
|
print "Trying again in 2 seconds ($attempts attempts remaining) ...\n" if $attempts > 0;
|
|
|
|
sleep 2;
|
|
|
|
} else {
|
|
|
|
print "success!\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $heartbeat;
|
|
|
|
}
|
|
|
|
|
2022-02-10 19:58:56 +01:00
|
|
|
sub do_heartbeat() {
|
|
|
|
tie my $heartbeat, 'IPC::Shareable', { key => 'heartbeat' };
|
|
|
|
tie my $running, 'IPC::Shareable', { key => 'running' };
|
|
|
|
|
|
|
|
while ($running) {
|
|
|
|
my $heartbeat_monitor = connect_to_heartbeat();
|
|
|
|
|
|
|
|
while ($running and <$heartbeat_monitor>) {
|
|
|
|
$heartbeat = time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:01:56 +01:00
|
|
|
sub server_listen($port) {
|
2022-01-23 16:49:23 +01:00
|
|
|
my $server = IO::Socket::INET->new (
|
|
|
|
Proto => 'tcp',
|
|
|
|
LocalPort => $port,
|
|
|
|
Listen => SOMAXCONN,
|
|
|
|
ReuseAddr => 1,
|
|
|
|
Reuse => 1,
|
|
|
|
);
|
|
|
|
die "Can't setup server: $!" unless $server;
|
2022-02-08 05:33:24 +01:00
|
|
|
print "Server $0 accepting clients at :$port\n";
|
2022-01-23 16:49:23 +01:00
|
|
|
return $server;
|
|
|
|
}
|
|
|
|
|
2022-02-10 19:58:56 +01:00
|
|
|
sub do_server() {
|
|
|
|
tie my $heartbeat, 'IPC::Shareable', { key => 'heartbeat' };
|
|
|
|
tie my $running, 'IPC::Shareable', { key => 'running' };
|
|
|
|
|
|
|
|
print "Starting PBot VM Server on port " . SERVER_PORT . "\n";
|
|
|
|
my $server = eval { server_listen(SERVER_PORT) };
|
|
|
|
|
|
|
|
if ($@) {
|
|
|
|
print STDERR $@;
|
|
|
|
$running = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ($running and my $client = $server->accept) {
|
|
|
|
print '-' x 20, "\n";
|
|
|
|
my $hostinfo = gethostbyaddr($client->peeraddr);
|
|
|
|
print "Connect from ", $client->peerhost, " at ", scalar localtime, "\n";
|
|
|
|
handle_client($client, $heartbeat);
|
|
|
|
}
|
|
|
|
|
|
|
|
print "Shutting down server.\n";
|
|
|
|
}
|
|
|
|
|
2022-02-08 05:33:24 +01:00
|
|
|
sub handle_client($client, $heartbeat) {
|
|
|
|
my ($timed_out, $killed) = (0, 0);
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-10 19:58:56 +01:00
|
|
|
my $r = fork;
|
|
|
|
|
|
|
|
if (not defined $r) {
|
|
|
|
print "Could not fork to handle client: $!\n";
|
|
|
|
print $client "Fatal error.\n";
|
|
|
|
close $client;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($r > 0) {
|
|
|
|
# nothing for parent to do with client
|
|
|
|
close $client;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$client->autoflush(1);
|
|
|
|
|
2022-02-08 05:33:24 +01:00
|
|
|
eval {
|
|
|
|
# give client 5 seconds to send a line
|
|
|
|
local $SIG{ALRM} = sub { die "Client I/O timed-out\n"; };
|
|
|
|
alarm 5;
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2023-03-13 00:06:04 +01:00
|
|
|
while (my $line = decode('UTF-8', <$client>)) {
|
2022-02-08 05:33:24 +01:00
|
|
|
$line =~ s/[\r\n]+$//;
|
|
|
|
next if $line =~ m/^\s*$/;
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-08 05:33:24 +01:00
|
|
|
# give client 5 more seconds
|
|
|
|
alarm 5;
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-10 19:58:56 +01:00
|
|
|
print "[$$] Read [$line]\n";
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-08 05:33:24 +01:00
|
|
|
if (time - $heartbeat > 5) {
|
2022-02-10 19:58:56 +01:00
|
|
|
print "[$$] Lost heartbeat, ignoring compile attempt.\n";
|
2022-02-13 01:06:04 +01:00
|
|
|
print $client "Virtual machine is resetting, try again soon.\n";
|
2022-02-08 05:33:24 +01:00
|
|
|
last;
|
|
|
|
}
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-08 05:33:24 +01:00
|
|
|
# disable client time-out
|
|
|
|
alarm 0;
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-08 05:33:24 +01:00
|
|
|
my ($ret, $result) = execute("perl vm-exec $line");
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-08 05:33:24 +01:00
|
|
|
$result =~ s/\s+$//;
|
|
|
|
print "Ret: $ret; result: [$result]\n";
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-08 05:33:24 +01:00
|
|
|
if ($result =~ m/\[Killed\]$/) {
|
|
|
|
$killed = 1;
|
|
|
|
$ret = -14;
|
|
|
|
}
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-08 05:33:24 +01:00
|
|
|
if ($ret == -13 && $result =~ m/\[Timed-out\]/) {
|
|
|
|
$timed_out = 1;
|
|
|
|
}
|
2022-02-07 05:01:56 +01:00
|
|
|
|
2023-03-13 00:06:04 +01:00
|
|
|
print $client encode('UTF-8', $result . "\n");
|
2022-02-08 05:33:24 +01:00
|
|
|
last;
|
2022-01-23 16:49:23 +01:00
|
|
|
}
|
2022-02-08 05:33:24 +01:00
|
|
|
};
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-08 05:33:24 +01:00
|
|
|
# print client time-out exception
|
2022-02-10 19:58:56 +01:00
|
|
|
print "[$$] $@" if $@;
|
2022-02-07 05:01:56 +01:00
|
|
|
|
2022-02-08 05:33:24 +01:00
|
|
|
alarm 0;
|
|
|
|
close $client;
|
2022-02-07 05:01:56 +01:00
|
|
|
|
2022-02-10 19:58:56 +01:00
|
|
|
print "[$$] timed out: $timed_out; killed: $killed\n";
|
2022-02-07 05:01:56 +01:00
|
|
|
|
2022-02-10 19:58:56 +01:00
|
|
|
if ($timed_out || $killed) {
|
|
|
|
vm_revert();
|
2022-02-08 05:33:24 +01:00
|
|
|
}
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-10 19:58:56 +01:00
|
|
|
# child done
|
|
|
|
print "[$$] client exiting\n";
|
|
|
|
print "=" x 20, "\n";
|
2022-02-08 05:33:24 +01:00
|
|
|
exit;
|
|
|
|
}
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-08 05:33:24 +01:00
|
|
|
sub main() {
|
2023-03-13 00:06:04 +01:00
|
|
|
binmode(STDOUT, ':utf8');
|
|
|
|
binmode(STDERR, ':utf8');
|
|
|
|
|
2023-01-24 21:34:03 +01:00
|
|
|
# let OS clean-up child exits
|
|
|
|
$SIG{CHLD} = 'IGNORE';
|
|
|
|
|
2022-02-08 05:33:24 +01:00
|
|
|
tie my $heartbeat, 'IPC::Shareable', { key => 'heartbeat', create => 1, destroy => 1 };
|
|
|
|
tie my $running, 'IPC::Shareable', { key => 'running', create => 1, destroy => 1 };
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-08 05:33:24 +01:00
|
|
|
$running = 1;
|
|
|
|
$heartbeat = 0;
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-08 05:33:24 +01:00
|
|
|
my $heartbeat_pid = fork // die "Heartbeat fork failed: $!";
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-08 05:33:24 +01:00
|
|
|
if ($heartbeat_pid == 0) {
|
|
|
|
do_heartbeat();
|
|
|
|
} else {
|
|
|
|
do_server();
|
2022-01-23 16:49:23 +01:00
|
|
|
}
|
|
|
|
|
2022-02-08 05:33:24 +01:00
|
|
|
print "Waiting for heart to stop...\n";
|
2022-01-23 16:49:23 +01:00
|
|
|
waitpid($heartbeat_pid, 0);
|
2022-02-08 05:33:24 +01:00
|
|
|
print "Heart stopped.\n";
|
2022-01-23 16:49:23 +01:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:01:56 +01:00
|
|
|
main();
|