feat: add language selection for build;feat: file and line number for debug messages;fix:add error handling for chdir call

This commit is contained in:
dragonmuffin 2024-08-25 20:26:45 +05:00
parent 157eafc58b
commit 858b2e7fd7
7 changed files with 18 additions and 31 deletions

3
.gitmodules vendored
View file

@ -1,3 +1,6 @@
[submodule "proto"]
path = proto
url = https://git.sch9.ru/new_gate/contracts
[submodule "languages"]
path = languages
url = https://git.sch9.ru/new_gate/languages

1
languages Submodule

@ -0,0 +1 @@
Subproject commit 8a7e22c121766e865752f492af978f7225b2a95a

View file

@ -2,7 +2,6 @@ package main
import (
runner "git.sch9.ru/new_gate/ms-runner/runner"
"fmt"
)
func main() {
@ -10,8 +9,4 @@ func main() {
if(err != nil) {
panic(err)
}
fmt.Println(runnerService)
runnerService.Build([]string{},"");
//limits := runner.Limits{Core: 3,Memory: 10000000, Time: 1000}
//runner.IsolatedRun([]string{"/bin/echo", "123"}, limits)
}

View file

@ -1,7 +1,6 @@
package runner
import (
"bytes"
"io"
"fmt"
"log"
@ -11,6 +10,7 @@ import (
cgroups "github.com/containerd/cgroups"
rand "math/rand"
"time"
"git.sch9.ru/new_gate/ms-runner/languages"
)
const runIdLength = 20
@ -43,7 +43,6 @@ func int32ToString(n int32) string {
}
func copyFile(src string,dst string) error {
log.Println(src,dst)
srcFile,err := os.Open(src)
if(err!=nil) {return err}
defer srcFile.Close()
@ -115,15 +114,15 @@ func NewRunnerService() (*RunnerService, error) {
return &runnerService,nil
}
func (runnerService RunnerService) Build(buildCmd []string, filename string) {
func (runnerService RunnerService) Build(languageId int32, filename string) {
core := <-runnerService.CoreFreed
runId := genRunId()
err := os.Mkdir("runs/" + runId, 0777)
if(err!=nil) { log.Fatal(err) }
os.Chmod("runs/" + runId, 0777)
copyFile("starter/shared/main.c","runs/"+runId+"/main.c")//FIXME add source and file extension
runnerService.IsolatedRun([]string{"/usr/bin/gcc","/shared/main.c","-o","/shared/executable","-save-temps=obj"},"../runs/"+runId,Limits{Processes: 10,Core:core,Memory:BuildMemory,Time:BuildTime});
copyFile("runs/"+runId+"/executable","destination")//FIXME add destination
copyFile(filename,"runs/"+runId+"/src")
runnerService.IsolatedRun(languages.Languages[languageId].CompileCmd,"../runs/"+runId,Limits{Processes: 10,Core:core,Memory:BuildMemory,Time:BuildTime});
copyFile("runs/"+runId+"/executable","executable")
os.RemoveAll("runs/" + runId)
go func() {
runnerService.CoreFreed <- core
@ -133,17 +132,10 @@ func (runnerService RunnerService) Build(buildCmd []string, filename string) {
func (runnerService RunnerService) IsolatedRun(command []string, sharedFolder string, limits Limits) {
args := []string{int32ToString(limits.Processes),int32ToString(limits.Core), int32ToString(limits.Memory), int32ToString(limits.Time), sharedFolder}
args = append(args, command...)
log.Println(limits.Processes)
log.Println(string(limits.Processes))
log.Println(args)
log.Println("running isolated process with command:",args)
cmd := exec.Command("starter/starter", args...)
var stdBuffer bytes.Buffer
mw := io.MultiWriter(os.Stdout, &stdBuffer)
cmd.Stdout = mw
err := cmd.Run();
if err != nil {
log.Println(123)
log.Println(err)
}
//cmd.Wait()
}

View file

@ -11,16 +11,10 @@ static void procfs_prepare()
#define hostname "runner"
#define put_old "oldfs"
//#define shared_folder "../shared"
//#define shared_mountpoint "shared"
void mnt_prepare(char* rootfs, char* shared_mountpoint) {
//char* shared_folder = malloc(strlen(shared_mountpoint)+4);
//strcpy(shared_folder,"../");
//strcat(shared_folder,shared_mountpoint);
char* shared_folder=NULL;
asprintf(&shared_folder,"../%s",shared_mountpoint);
if (mount(rootfs,rootfs,"ext4",MS_BIND,"")) die("failed to mount %s: %m", rootfs);
//if (mount(shared_mountpoint,shared_mountpoint,"ext4",MS_BIND,"")) die("failed to mount %s: %m",shared_mountpoint);
if (chdir(rootfs)) die("falied to cd:%m");
//if (mount("/sys","sys","sysfs",0,"")) die("failed to mount sysfs: %m");
//if (mount("/dev","dev","udev",0,"")) die("failed to mount: %m");
@ -58,15 +52,15 @@ static int nsrun(void* arg) {
await_setup(params->fd[0]);
char cwd[PATH_MAX];
if(getcwd(cwd,sizeof(cwd))==NULL) die("getcwd error: %m");
if(chdir(params->shared_folder)) die("failed to chdir: %m");
if(chdir(params->shared_folder)) die("failed to chdir to shared folder: %m")
int out_fd=open("out",O_WRONLY|O_CREAT,0666);
if(out_fd==-1) die("unable to open output file:%m");
if(out_fd==-1) die("unable to open output file:%m")
else dup2(out_fd,STDOUT_FILENO);
int in_fd=open("in",O_RDONLY|O_CREAT,0666);
if(in_fd==-1) die("unable to open input file:%m");
if(in_fd==-1) die("unable to open input file:%m")
else dup2(in_fd,STDIN_FILENO);
int err_fd=open("err",O_WRONLY|O_CREAT,0666);
if(err_fd==-1) die("unable to open error file:%m");
if(err_fd==-1) die("unable to open error file:%m")
else dup2(err_fd,STDERR_FILENO);
if(chdir(cwd)) die("failed to chdir: %m");
mnt_prepare("minrootfs",params->shared_folder);

View file

@ -141,7 +141,7 @@ int main(int argc,char** argv) {
if (waitpid(nsrun_pid, NULL, 0) == -1) die("Failed to wait pid %d: %m\n", nsrun_pid);
kill(kill_pid,SIGKILL);//kill killer
int time = getmstime()-time_start;
chdir(params.shared_folder);
if(chdir(params.shared_folder)) die("Failed to chdir to shared folder:%m");
if(time>=limits.time) {
write_file("time", "-1");
}

View file

@ -1,10 +1,12 @@
#define die(...) {fprintf(stderr,"file %s,line %d\n",__FILE__,__LINE__);die_func(__VA_ARGS__);}
void remove_cgroup();
static void die(const char *fmt, ...)
static void die_func(const char *fmt, ...)
{
va_list params;
va_start(params, fmt);
vfprintf(stderr, fmt, params);
puts("\n");
va_end(params);
remove_cgroup();
exit(1);