2015-01-15 06:51:17 +01:00
|
|
|
#!/usr/bin/env perl
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# 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/.
|
|
|
|
|
2011-01-26 02:59:19 +01:00
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
2019-04-27 09:23:23 +02:00
|
|
|
use English;
|
2015-01-15 06:51:17 +01:00
|
|
|
use File::Basename;
|
2017-09-15 04:10:21 +02:00
|
|
|
use JSON;
|
2015-01-15 06:51:17 +01:00
|
|
|
|
2017-09-10 03:21:20 +02:00
|
|
|
my $USERNAME = 'compiler';
|
2019-06-26 18:34:19 +02:00
|
|
|
my $USE_LOCAL = defined $ENV{'CC_LOCAL'};
|
2011-02-01 01:41:51 +01:00
|
|
|
|
2017-09-10 03:21:20 +02:00
|
|
|
use constant MOD_DIR => '/usr/local/share/compiler_vm/languages';
|
2015-01-15 06:51:17 +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 {
|
2014-02-23 01:56:51 +01:00
|
|
|
my ($input, $output, $heartbeat);
|
2011-02-01 01:41:51 +01:00
|
|
|
|
|
|
|
if(not defined $USE_LOCAL or $USE_LOCAL == 0) {
|
|
|
|
open($input, '<', "/dev/ttyS0") or die $!;
|
|
|
|
open($output, '>', "/dev/ttyS0") or die $!;
|
2014-02-23 01:56:51 +01:00
|
|
|
open($heartbeat, '>', "/dev/ttyS1") or die $!;
|
2011-02-01 01:41:51 +01:00
|
|
|
} else {
|
|
|
|
open($input, '<', "/dev/stdin") or die $!;
|
|
|
|
open($output, '>', "/dev/stdout") or die $!;
|
|
|
|
}
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2012-10-24 14:26:18 +02:00
|
|
|
my $date;
|
2011-01-26 02:59:19 +01:00
|
|
|
my $lang;
|
2015-01-15 06:51:17 +01:00
|
|
|
my $sourcefile;
|
|
|
|
my $execfile;
|
2011-01-26 02:59:19 +01:00
|
|
|
my $code;
|
2015-01-15 06:51:17 +01:00
|
|
|
my $cmdline;
|
2011-01-26 02:59:19 +01:00
|
|
|
my $user_input;
|
|
|
|
|
2014-02-23 01:56:51 +01:00
|
|
|
my $pid = fork;
|
|
|
|
die "Fork failed: $!" if not defined $pid;
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2014-02-23 01:56:51 +01:00
|
|
|
if($pid == 0) {
|
2017-12-02 20:37:51 +01:00
|
|
|
my $buffer = "";
|
|
|
|
my $length = 4096;
|
|
|
|
my $line;
|
|
|
|
my $total_read = 0;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
print "Waiting for input...\n";
|
|
|
|
my $ret = sysread($input, my $buf, $length);
|
|
|
|
|
|
|
|
if (not defined $ret) {
|
|
|
|
print "Error reading: $!\n";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
$total_read += $ret;
|
|
|
|
|
|
|
|
if ($ret == 0) {
|
|
|
|
print "input ded?\n";
|
|
|
|
print "got buffer [$buffer]\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
chomp $buf;
|
|
|
|
print "read $ret bytes [$total_read so far] [$buf]\n";
|
|
|
|
$buffer.= $buf;
|
|
|
|
|
|
|
|
if ($buffer =~ s/\s*:end:\s*$//m) {
|
|
|
|
$line = $buffer;
|
|
|
|
$buffer = "";
|
|
|
|
$total_read = 0;
|
|
|
|
} else {
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
2014-02-23 01:56:51 +01:00
|
|
|
chomp $line;
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2017-09-15 04:10:21 +02:00
|
|
|
print "-" x 40, "\n";
|
2014-02-23 01:56:51 +01:00
|
|
|
print "Got [$line]\n";
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2017-09-15 04:10:21 +02:00
|
|
|
my $compile_in = decode_json($line);
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2019-06-15 03:49:33 +02:00
|
|
|
$compile_in->{arguments} //= '';
|
|
|
|
$compile_in->{input} //= '';
|
|
|
|
|
2017-09-15 04:10:21 +02:00
|
|
|
print "Attempting compile [$compile_in->{lang}] ...\n";
|
2012-01-30 00:50:33 +01:00
|
|
|
|
2019-06-15 03:49:33 +02:00
|
|
|
use Data::Dumper;
|
|
|
|
print Dumper $compile_in;
|
|
|
|
|
2017-09-15 04:10:21 +02:00
|
|
|
my $pid = fork;
|
2014-02-23 01:56:51 +01:00
|
|
|
|
2017-09-15 04:10:21 +02:00
|
|
|
if (not defined $pid) {
|
|
|
|
print "fork failed: $!\n";
|
|
|
|
next;
|
|
|
|
}
|
2014-02-23 01:56:51 +01:00
|
|
|
|
2017-09-15 04:10:21 +02:00
|
|
|
if ($pid == 0) {
|
2019-04-27 09:23:23 +02:00
|
|
|
my ($uid, $gid, $home) = (getpwnam $USERNAME)[2, 3, 7];
|
2017-09-15 04:10:21 +02:00
|
|
|
if (not $uid and not $gid) {
|
|
|
|
print "Could not find user $USERNAME: $!\n";
|
2014-02-23 01:56:51 +01:00
|
|
|
exit;
|
|
|
|
}
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2017-09-19 06:36:40 +02:00
|
|
|
if ($compile_in->{'persist-key'}) {
|
2019-04-27 09:23:23 +02:00
|
|
|
system ("rm -rf \"/home/compiler/$compile_in->{'persist-key'}\"");
|
2017-09-19 06:36:40 +02:00
|
|
|
system("mount /dev/vdb1 /root/factdata");
|
2019-04-27 09:23:23 +02:00
|
|
|
system("mkdir -p \"/root/factdata/$compile_in->{'persist-key'}\"");
|
|
|
|
system("cp -R -p \"/root/factdata/$compile_in->{'persist-key'}\" \"/home/compiler/$compile_in->{'persist-key'}\"");
|
2017-09-19 06:36:40 +02:00
|
|
|
}
|
|
|
|
|
2017-12-02 20:37:51 +01:00
|
|
|
system("chmod -R 755 /home/compiler");
|
2019-04-27 09:23:23 +02:00
|
|
|
system("chown -R compiler /home/compiler");
|
|
|
|
system("chgrp -R compiler /home/compiler");
|
2017-09-19 06:36:40 +02:00
|
|
|
system("rm -rf /home/compiler/prog*");
|
2019-04-27 09:23:23 +02:00
|
|
|
system("pkill -u compiler");
|
|
|
|
|
|
|
|
$ENV{USER} = $USERNAME;
|
|
|
|
$ENV{LOGNAME} = $USERNAME;
|
|
|
|
$ENV{HOME} = $home;
|
2017-09-19 06:36:40 +02:00
|
|
|
|
2019-04-27 09:23:23 +02:00
|
|
|
$GID = $gid;
|
|
|
|
$EGID = "$gid $gid";
|
|
|
|
$EUID = $UID = $uid;
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2017-09-15 04:10:21 +02:00
|
|
|
my $result = interpret(%$compile_in);
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2017-09-15 04:10:21 +02:00
|
|
|
my $compile_out = { result => $result };
|
|
|
|
my $json = encode_json($compile_out);
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2017-09-15 04:10:21 +02:00
|
|
|
print "Done compiling; result: [$result] [$json]\n";
|
|
|
|
print $output "result:$json\n";
|
|
|
|
print $output "result:end\n";
|
2017-09-19 06:36:40 +02:00
|
|
|
|
|
|
|
$( = 0;
|
|
|
|
$< = 0;
|
|
|
|
|
|
|
|
if ($compile_in->{'persist-key'}) {
|
|
|
|
system("id");
|
2019-04-27 09:23:23 +02:00
|
|
|
system("cp -R -p \"/home/compiler/$compile_in->{'persist-key'}\" \"/root/factdata/$compile_in->{'persist-key'}\"");
|
2017-09-19 06:36:40 +02:00
|
|
|
system("umount /root/factdata");
|
2019-04-27 09:23:23 +02:00
|
|
|
system ("rm -rf \"/home/compiler/$compile_in->{'persist-key'}\"");
|
2017-09-19 06:36:40 +02:00
|
|
|
}
|
|
|
|
|
2017-09-15 04:10:21 +02:00
|
|
|
exit;
|
|
|
|
} else {
|
|
|
|
waitpid $pid, 0;
|
|
|
|
}
|
2015-09-04 06:32:44 +02:00
|
|
|
|
2017-09-15 04:10:21 +02:00
|
|
|
if(not defined $USE_LOCAL or $USE_LOCAL == 0) {
|
2017-12-02 20:37:51 +01:00
|
|
|
print "=" x 40, "\n";
|
2017-09-15 04:10:21 +02:00
|
|
|
print "input: ";
|
2014-02-23 01:56:51 +01:00
|
|
|
next;
|
2017-09-15 04:10:21 +02:00
|
|
|
} else {
|
|
|
|
exit;
|
2014-02-23 01:56:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while(1) {
|
|
|
|
print $heartbeat "\n";
|
|
|
|
sleep 1;
|
|
|
|
}
|
2011-01-26 02:59:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
close $input;
|
|
|
|
close $output;
|
2014-02-23 01:56:51 +01:00
|
|
|
close $heartbeat;
|
2011-01-26 02:59:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub interpret {
|
2017-09-15 04:10:21 +02:00
|
|
|
my %h = @_;
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2017-09-15 04:10:21 +02:00
|
|
|
$h{lang} = '_default' if not exists $languages{$h{lang}};
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2017-09-19 06:36:40 +02:00
|
|
|
chdir("/home/compiler");
|
2014-02-23 01:56:51 +01:00
|
|
|
|
2017-09-15 04:10:21 +02:00
|
|
|
my $mod = $h{lang}->new(%h);
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2015-01-15 06:51:17 +01:00
|
|
|
$mod->preprocess;
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2015-05-19 05:48:15 +02:00
|
|
|
$mod->postprocess if not $mod->{error} and not $mod->{done};
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2015-01-17 02:28:38 +01:00
|
|
|
if (exists $mod->{no_output} or not length $mod->{output}) {
|
2017-09-19 06:36:40 +02:00
|
|
|
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};
|
|
|
|
}
|
2011-01-26 02:59:19 +01:00
|
|
|
}
|
|
|
|
|
2015-01-15 06:51:17 +01:00
|
|
|
return $mod->{output};
|
2011-01-26 02:59:19 +01:00
|
|
|
}
|
|
|
|
|
2015-01-15 06:51:17 +01:00
|
|
|
load_modules;
|
|
|
|
run_server;
|