2022-02-10 19:58:56 +01:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
|
|
|
# File: guest-server
|
|
|
|
#
|
|
|
|
# Purpose: PBot VM Guest server. Runs inside PBot VM Guest and processes
|
|
|
|
# incoming VM commands from vm-exec.
|
|
|
|
|
|
|
|
# SPDX-FileCopyrightText: 2022 Pragmatic Software <pragma78@gmail.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
use 5.020;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
use feature qw/signatures/;
|
|
|
|
no warnings qw(experimental::signatures);
|
|
|
|
|
|
|
|
use constant {
|
2022-02-13 01:06:04 +01:00
|
|
|
USERNAME => 'vm',
|
|
|
|
MOD_DIR => '/usr/local/share/pbot-vm/',
|
2022-02-10 19:58:56 +01:00
|
|
|
SERIAL => '/dev/ttyS1',
|
|
|
|
HEARTBEAT => '/dev/ttyS2',
|
2022-02-13 01:06:04 +01:00
|
|
|
VPORT => $ENV{PBOTVM_VPORT} // 5555,
|
2022-02-10 19:58:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
use lib MOD_DIR;
|
2022-02-13 01:06:04 +01:00
|
|
|
use lib MOD_DIR . "Languages";
|
|
|
|
|
|
|
|
use Guest;
|
|
|
|
|
|
|
|
use File::Basename;
|
|
|
|
use IPC::Shareable;
|
2022-02-10 19:58:56 +01:00
|
|
|
|
|
|
|
my %languages;
|
|
|
|
|
|
|
|
sub load_modules() {
|
2022-02-13 01:06:04 +01:00
|
|
|
my @files = glob MOD_DIR . "Languages/*.pm";
|
|
|
|
|
2022-02-10 19:58:56 +01:00
|
|
|
foreach my $mod (@files){
|
|
|
|
print "Loading module $mod\n";
|
2022-02-13 01:06:04 +01:00
|
|
|
|
2022-02-10 19:58:56 +01:00
|
|
|
my $filename = basename($mod);
|
2022-02-13 01:06:04 +01:00
|
|
|
|
2022-02-10 19:58:56 +01:00
|
|
|
require $filename;
|
2022-02-13 01:06:04 +01:00
|
|
|
|
2022-02-10 19:58:56 +01:00
|
|
|
$filename =~ s/\.pm$//;
|
2022-02-13 01:06:04 +01:00
|
|
|
|
2022-02-10 19:58:56 +01:00
|
|
|
$languages{$filename} = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
sub vsock_server() {
|
|
|
|
print "Starting VSOCK server on PID $$\n";
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
system("socat VSOCK-LISTEN:".VPORT.",reuseaddr,fork EXEC:accept-vsock-client");
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
print "VSOCK server shutdown.\n";
|
|
|
|
exit; # exit child process
|
|
|
|
}
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
sub serial_server() {
|
|
|
|
print "Starting serial server on PID $$\n";
|
|
|
|
|
|
|
|
# set serial to 115200 baud instead of 9600
|
|
|
|
system('stty -F '.SERIAL.' 115200');
|
|
|
|
|
|
|
|
open(my $input, '<', SERIAL) or die $!;
|
|
|
|
open(my $output, '>', SERIAL) or die $!;
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
tie my $running, 'IPC::Shareable', { key => 'running' };
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
my $buffer = '';
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
while ($running) {
|
|
|
|
my $command = Guest::read_input($input, \$buffer, 'Serial');
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
if (not defined $command) {
|
|
|
|
# recoverable error while reading, try again
|
|
|
|
next;
|
|
|
|
}
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
if ($command == 0) {
|
|
|
|
# serial closed, exit child process
|
|
|
|
exit;
|
|
|
|
}
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
if (not exists $languages{$command->{lang}}) {
|
|
|
|
$command->{lang} = '_default';
|
|
|
|
}
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
my $mod = $command->{lang}->new(%$command);
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
my $result = Guest::process_command($command, $mod, USERNAME, 'Serial');
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
if (not defined $result) {
|
|
|
|
$result = "[Fatal error]";
|
|
|
|
}
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
if ($result) {
|
|
|
|
Guest::send_output($output, $result, 'Serial');
|
|
|
|
exit; # exit child process
|
|
|
|
}
|
2022-02-10 19:58:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
sub do_server() {
|
2022-02-10 19:58:56 +01:00
|
|
|
my $pid = fork;
|
|
|
|
|
|
|
|
if (not defined $pid) {
|
2022-02-13 01:06:04 +01:00
|
|
|
print STDERR "Could not fork server: $!\n";
|
|
|
|
die;
|
2022-02-10 19:58:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($pid == 0) {
|
2022-02-13 01:06:04 +01:00
|
|
|
vsock_server();
|
|
|
|
} else {
|
|
|
|
serial_server();
|
|
|
|
}
|
|
|
|
}
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
sub do_heartbeat() {
|
|
|
|
open(my $heartbeat, '>', HEARTBEAT) or die $!;
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
tie my $running, 'IPC::Shareable', { key => 'running' };
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
print "Heart beating on PID $$...\n";
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
while ($running) {
|
|
|
|
print $heartbeat "\n";
|
|
|
|
sleep 5;
|
2022-02-10 19:58:56 +01:00
|
|
|
}
|
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
print "Heart beat stopped.\n";
|
|
|
|
exit; # exit child process
|
|
|
|
}
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
sub install_signal_handlers() {
|
|
|
|
use POSIX qw(:signal_h :errno_h :sys_wait_h);
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
$SIG{CHLD} = \&REAPER;
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
sub REAPER {
|
|
|
|
my $pid = waitpid(-1, &WNOHANG);
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
if ($pid == -1) {
|
|
|
|
# no child waiting. Ignore it.
|
|
|
|
} elsif (WIFEXITED($?)) {
|
|
|
|
print "Process $pid exited.\n";
|
2022-02-10 19:58:56 +01:00
|
|
|
}
|
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
$SIG{CHLD} = \&REAPER; # in case of unreliable signals
|
2022-02-10 19:58:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub main() {
|
2022-02-13 01:06:04 +01:00
|
|
|
print "Starting PBot VM Guest server on PID $$\n";
|
|
|
|
|
2022-02-10 19:58:56 +01:00
|
|
|
load_modules();
|
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
install_signal_handlers();
|
2022-02-10 19:58:56 +01:00
|
|
|
|
2022-02-13 01:06:04 +01:00
|
|
|
tie my $running, 'IPC::Shareable', { key => 'running', create => 1, destroy => 1 };
|
|
|
|
|
|
|
|
$running = 1;
|
2022-02-10 19:58:56 +01:00
|
|
|
|
|
|
|
my $pid = fork // die "Fork failed: $!";
|
|
|
|
|
|
|
|
if ($pid == 0) {
|
2022-02-13 01:06:04 +01:00
|
|
|
do_heartbeat();
|
2022-02-10 19:58:56 +01:00
|
|
|
} else {
|
2022-02-13 01:06:04 +01:00
|
|
|
do_server();
|
2022-02-10 19:58:56 +01:00
|
|
|
}
|
2022-02-13 01:06:04 +01:00
|
|
|
|
|
|
|
print "PBot VM Guest server shutdown.\n";
|
2022-02-10 19:58:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
main();
|