2022-01-23 16:49:23 +01:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
use English;
|
|
|
|
use Encode;
|
|
|
|
use File::Basename;
|
|
|
|
use JSON::XS;
|
2022-02-06 20:33:21 +01:00
|
|
|
use Data::Dumper;
|
2022-01-23 16:49:23 +01:00
|
|
|
|
|
|
|
my $USERNAME = 'vm'; # variable for easier string interpolation
|
|
|
|
|
2022-01-30 00:51:39 +01:00
|
|
|
use constant {
|
|
|
|
MOD_DIR => '/usr/local/share/pbot-vm/Languages',
|
|
|
|
SERIAL => '/dev/ttyS1',
|
|
|
|
HEARTBEAT => '/dev/ttyS2',
|
|
|
|
INPUT => '/dev/stdin',
|
|
|
|
OUTPUT => '/dev/stdout',
|
|
|
|
};
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-06 20:33:21 +01:00
|
|
|
my $USE_LOCAL = $ENV{'CC_LOCAL'} // 0;
|
|
|
|
|
2022-01-23 16:49:23 +01:00
|
|
|
use lib MOD_DIR;
|
|
|
|
|
|
|
|
my %languages;
|
|
|
|
|
|
|
|
sub load_modules {
|
|
|
|
my @files = glob MOD_DIR . "/*.pm";
|
|
|
|
foreach my $mod (@files){
|
|
|
|
print "Loading module $mod\n";
|
|
|
|
my $filename = basename($mod);
|
|
|
|
require $filename;
|
|
|
|
$filename =~ s/\.pm$//;
|
|
|
|
$languages{$filename} = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub run_server {
|
|
|
|
my ($input, $output, $heartbeat);
|
|
|
|
|
2022-02-06 20:33:21 +01:00
|
|
|
if (not $USE_LOCAL) {
|
2022-01-23 16:49:23 +01:00
|
|
|
# set serial to 115200 baud instead of 9600
|
|
|
|
system('stty -F ' . SERIAL . ' 115200');
|
|
|
|
|
|
|
|
open($input, '<', SERIAL) or die $!;
|
|
|
|
open($output, '>', SERIAL) or die $!;
|
|
|
|
open($heartbeat, '>', HEARTBEAT) or die $!;
|
|
|
|
} else {
|
2022-01-25 03:12:59 +01:00
|
|
|
open($input, '<', INPUT) or die $!;
|
|
|
|
open($output, '>', OUTPUT) or die $!;
|
2022-01-23 16:49:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
my $date;
|
|
|
|
my $lang;
|
|
|
|
my $sourcefile;
|
|
|
|
my $execfile;
|
|
|
|
my $code;
|
|
|
|
my $cmdline;
|
|
|
|
my $user_input;
|
|
|
|
|
|
|
|
my $pid = fork;
|
|
|
|
die "Fork failed: $!" if not defined $pid;
|
|
|
|
|
2022-02-06 20:33:21 +01:00
|
|
|
if ($pid == 0) {
|
|
|
|
my $buffer = '';
|
2022-01-23 16:49:23 +01:00
|
|
|
my $line;
|
|
|
|
my $total_read = 0;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
print "Waiting for input...\n";
|
2022-02-06 20:33:21 +01:00
|
|
|
my $ret = sysread($input, my $buf, 16384);
|
2022-01-23 16:49:23 +01:00
|
|
|
|
|
|
|
if (not defined $ret) {
|
|
|
|
print "Error reading: $!\n";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
$total_read += $ret;
|
|
|
|
|
|
|
|
if ($ret == 0) {
|
2022-02-06 20:33:21 +01:00
|
|
|
print "Input closed; exiting...\n";
|
2022-01-23 16:49:23 +01:00
|
|
|
print "got buffer [$buffer]\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
chomp $buf;
|
|
|
|
print "read $ret bytes [$total_read so far] [$buf]\n";
|
|
|
|
$buffer.= $buf;
|
|
|
|
|
2022-02-06 20:33:21 +01:00
|
|
|
next if $buffer !~ s/\s*:end:\s*$//m;
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-06 20:33:21 +01:00
|
|
|
$line = $buffer;
|
2022-01-23 16:49:23 +01:00
|
|
|
chomp $line;
|
|
|
|
|
2022-02-06 20:33:21 +01:00
|
|
|
$buffer = '';
|
|
|
|
$total_read = 0;
|
2022-01-23 16:49:23 +01:00
|
|
|
|
|
|
|
$line = encode('UTF-8', $line);
|
|
|
|
|
2022-02-06 20:33:21 +01:00
|
|
|
print "-" x 40, "\n";
|
|
|
|
print "Got [$line]\n";
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-06 20:33:21 +01:00
|
|
|
my $compile_in = decode_json($line);
|
2022-01-23 16:49:23 +01:00
|
|
|
|
|
|
|
print Dumper $compile_in;
|
|
|
|
|
2022-02-06 20:33:21 +01:00
|
|
|
$compile_in->{arguments} //= '';
|
|
|
|
$compile_in->{input} //= '';
|
|
|
|
|
2022-01-23 16:49:23 +01:00
|
|
|
my $pid = fork;
|
|
|
|
|
|
|
|
if (not defined $pid) {
|
|
|
|
print "fork failed: $!\n";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($pid == 0) {
|
|
|
|
my ($uid, $gid, $home) = (getpwnam $USERNAME)[2, 3, 7];
|
2022-02-06 20:33:21 +01:00
|
|
|
|
2022-01-23 16:49:23 +01:00
|
|
|
if (not $uid and not $gid) {
|
|
|
|
print "Could not find user $USERNAME: $!\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($compile_in->{'persist-key'}) {
|
|
|
|
system ("rm -rf \"/home/$USERNAME/$compile_in->{'persist-key'}\"");
|
|
|
|
system("mount /dev/vdb1 /root/factdata");
|
|
|
|
system("mkdir -p \"/root/factdata/$compile_in->{'persist-key'}\"");
|
|
|
|
system("cp -R -p \"/root/factdata/$compile_in->{'persist-key'}\" \"/home/$USERNAME/$compile_in->{'persist-key'}\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
system("chmod -R 755 /home/$USERNAME");
|
|
|
|
system("chown -R $USERNAME /home/$USERNAME");
|
|
|
|
system("chgrp -R $USERNAME /home/$USERNAME");
|
|
|
|
system("rm -rf /home/$USERNAME/prog*");
|
|
|
|
system("pkill -u $USERNAME");
|
|
|
|
|
2022-02-06 09:24:04 +01:00
|
|
|
system("date -s \@$compile_in->{date}");
|
|
|
|
|
2022-01-23 16:49:23 +01:00
|
|
|
$ENV{USER} = $USERNAME;
|
|
|
|
$ENV{LOGNAME} = $USERNAME;
|
|
|
|
$ENV{HOME} = $home;
|
|
|
|
|
|
|
|
$GID = $gid;
|
|
|
|
$EGID = "$gid $gid";
|
|
|
|
$EUID = $UID = $uid;
|
|
|
|
|
2022-02-06 20:33:21 +01:00
|
|
|
chdir("/home/$USERNAME");
|
|
|
|
|
2022-01-23 16:49:23 +01:00
|
|
|
my $result = interpret(%$compile_in);
|
|
|
|
|
2022-01-29 21:22:48 +01:00
|
|
|
$GID = 0;
|
|
|
|
$UID = 0;
|
|
|
|
|
2022-01-23 16:49:23 +01:00
|
|
|
my $compile_out = { result => $result };
|
2022-02-06 20:33:21 +01:00
|
|
|
|
2022-01-23 16:49:23 +01:00
|
|
|
my $json = encode_json($compile_out);
|
|
|
|
|
2022-02-06 20:33:21 +01:00
|
|
|
print "Done compiling: $json\n";
|
|
|
|
|
2022-01-23 16:49:23 +01:00
|
|
|
print $output "result:$json\n";
|
|
|
|
print $output "result:end\n";
|
|
|
|
|
|
|
|
if ($compile_in->{'persist-key'}) {
|
|
|
|
system("id");
|
|
|
|
system("cp -R -p \"/home/$USERNAME/$compile_in->{'persist-key'}\" \"/root/factdata/$compile_in->{'persist-key'}\"");
|
|
|
|
system("umount /root/factdata");
|
|
|
|
system ("rm -rf \"/home/$USERNAME/$compile_in->{'persist-key'}\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
exit;
|
|
|
|
} else {
|
|
|
|
waitpid $pid, 0;
|
2022-02-06 20:33:21 +01:00
|
|
|
|
|
|
|
# kill any left-over processes started by $USERNAME
|
|
|
|
system("pkill -u $USERNAME");
|
2022-01-23 16:49:23 +01:00
|
|
|
}
|
|
|
|
|
2022-02-06 20:33:21 +01:00
|
|
|
if (not $USE_LOCAL) {
|
2022-01-23 16:49:23 +01:00
|
|
|
print "=" x 40, "\n";
|
|
|
|
next;
|
|
|
|
} else {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (1) {
|
|
|
|
print $heartbeat "\n";
|
|
|
|
sleep 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close $input;
|
|
|
|
close $output;
|
|
|
|
close $heartbeat;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub interpret {
|
|
|
|
my %h = @_;
|
|
|
|
|
|
|
|
$h{lang} = '_default' if not exists $languages{$h{lang}};
|
|
|
|
|
|
|
|
my $mod = $h{lang}->new(%h);
|
|
|
|
|
|
|
|
$mod->preprocess;
|
|
|
|
|
|
|
|
$mod->postprocess if not $mod->{error} and not $mod->{done};
|
|
|
|
|
|
|
|
if (exists $mod->{no_output} or not length $mod->{output}) {
|
|
|
|
if ($h{factoid}) {
|
|
|
|
$mod->{output} = "";
|
|
|
|
} else {
|
|
|
|
$mod->{output} .= "\n" if length $mod->{output};
|
|
|
|
$mod->{output} .= "Success (no output).\n" if not $mod->{error};
|
|
|
|
$mod->{output} .= "Success (exit code $mod->{error}).\n" if $mod->{error};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $mod->{output};
|
|
|
|
}
|
|
|
|
|
|
|
|
load_modules;
|
|
|
|
run_server;
|