prettify code, fix bugs, make it isolated compile with gcc

This commit is contained in:
root 2024-08-21 17:25:19 +05:00
parent 6c3b64ed39
commit bb8d9fa5ca
7 changed files with 116 additions and 36 deletions

1
.gitignore vendored
View file

@ -4,3 +4,4 @@
/starter/starter /starter/starter
/starter/minrootfs /starter/minrootfs
/pkg/go/gen /pkg/go/gen
/runs

View file

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

View file

@ -14,22 +14,57 @@ import (
) )
const runIdLength = 20 const runIdLength = 20
var coresIsolated []int
type Limits struct { type Limits struct {
Core int Processes int32
Memory int Core int32
Time int Memory int32
Time int32
} }
func extractNumbers(s string) (result []int) { const Mib = 1024*1024;
const BuildMemory = 1024*Mib;
const BuildTime = 5000;
type RunnerService struct {
CoresIsolated []int32
CoreFreed chan int32
}
func genRunId() (runId string) {
for i := 0; i < runIdLength; i++ {
runId += string('a' + byte(rand.Int31n(26)))
}
return
}
func int32ToString(n int32) string {
return fmt.Sprintf("%d", n)
}
func copyFile(src string,dst string) error {
log.Println(src,dst)
srcFile,err := os.Open(src)
if(err!=nil) {return err}
defer srcFile.Close()
dstFile,err := os.Create(dst)
if(err!=nil) {return err}
defer dstFile.Close()
_,err = io.Copy(dstFile,srcFile)
if(err!=nil) {return err}
err = dstFile.Sync()
if(err!=nil) {return err}
return nil
}
func extractNumbers(s string) (result []int32) {
lastNumber, isNumber := false,false lastNumber, isNumber := false,false
var curNumber = 0 var curNumber int32 = 0
for _, char := range s { for _, char := range s {
isNumber = (char >= '0' && char <= '9') isNumber = (char >= '0' && char <= '9')
if(isNumber) { if(isNumber) {
curNumber *= 10 curNumber *= 10
curNumber += int(char - '0') curNumber += int32(char - '0')
} }
if(!isNumber && lastNumber) { if(!isNumber && lastNumber) {
result = append(result, curNumber) result = append(result, curNumber)
@ -43,13 +78,14 @@ func extractNumbers(s string) (result []int) {
return return
} }
func Init() error { func NewRunnerService() (*RunnerService, error) {
runnerService := RunnerService{make([]int32,0),make(chan int32)}
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
//croup initialisation: //croup initialisation:
if cgroups.Mode() == cgroups.Unified { if cgroups.Mode() == cgroups.Unified {
log.Println("cgroups v2 usage approved") log.Println("cgroups v2 usage approved")
} else { } else {
return fmt.Errorf("cgroups v2 are not enabled")//TODO: trouble description return nil,fmt.Errorf("cgroups v2 are not enabled")//TODO: trouble description
} }
//isolated cores initialisation: //isolated cores initialisation:
cmdlineBytes := make([]byte, 400) cmdlineBytes := make([]byte, 400)
@ -59,29 +95,55 @@ func Init() error {
kernelParams := strings.Split(cmdline," ") kernelParams := strings.Split(cmdline," ")
for _, param := range kernelParams{ for _, param := range kernelParams{
if(len(param) >= 9 && param[:9] == "isolcpus=") { if(len(param) >= 9 && param[:9] == "isolcpus=") {
coresIsolated = append(coresIsolated, extractNumbers(param[9:])...) runnerService.CoresIsolated = append(runnerService.CoresIsolated, extractNumbers(param[9:])...)
} }
} }
if(len(coresIsolated) == 0) { if(len(runnerService.CoresIsolated) == 0) {
return fmt.Errorf("no free cores available")//TODO: trouble description return nil,fmt.Errorf("no free cores available")//TODO: trouble description
} }
log.Println("running on cores:", coresIsolated) go func() {
for _,core := range runnerService.CoresIsolated {
runnerService.CoreFreed <- core
}
}()
log.Println("running on cores:", runnerService.CoresIsolated)
//runs directory
os.Mkdir("runs",0777)
os.Chmod("runs",0777)
//complete
log.Println("Runner initialisation successful!") log.Println("Runner initialisation successful!")
return nil return &runnerService,nil
} }
func IsolatedRun(command []string, limits Limits) { func (runnerService RunnerService) Build(buildCmd []string, filename string) {
var runId string core := <-runnerService.CoreFreed
for i := 0; i < runIdLength; i++ {runId += string('a' + byte(rand.Int31n(26)))} runId := genRunId()
os.Mkdir("starter/" + runId, 0777) err := os.Mkdir("runs/" + runId, 0777)
args := []string{string(limits.Core), string(limits.Memory), string(limits.Time), runId} 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
os.RemoveAll("runs/" + runId)
go func() {
runnerService.CoreFreed <- core
}()
}
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...) args = append(args, command...)
log.Println(limits.Processes)
log.Println(string(limits.Processes))
log.Println(args)
cmd := exec.Command("starter/starter", args...) cmd := exec.Command("starter/starter", args...)
var stdBuffer bytes.Buffer var stdBuffer bytes.Buffer
mw := io.MultiWriter(os.Stdout, &stdBuffer) mw := io.MultiWriter(os.Stdout, &stdBuffer)
cmd.Stdout = mw cmd.Stdout = mw
err := cmd.Run(); err := cmd.Run();
if err != nil { log.Fatal(err) } if err != nil {
os.RemoveAll("starter/" + runId) log.Println(123)
cmd.Wait() log.Println(err)
}
//cmd.Wait()
} }

View file

@ -25,7 +25,10 @@ void prepare_cgroup(struct limits* limits) {
asprintf(&cpus_string,"%d\n",limits->core); asprintf(&cpus_string,"%d\n",limits->core);
write_file("cpuset.cpus",cpus_string); write_file("cpuset.cpus",cpus_string);
free(cpus_string); free(cpus_string);
write_file("pids.max","1\n"); char* processes_string=NULL;
asprintf(&processes_string,"%d\n",limits->processes);
write_file("pids.max",processes_string);
free(processes_string);
//write_file("cpuset.cpus","3\n"); //write_file("cpuset.cpus","3\n");
chdir(cwd); chdir(cwd);
} }

View file

@ -10,7 +10,7 @@ NAME=minrootfs
SCRIPT_DIR=$(dirname -- "$(readlink -f "${BASH_SOURCE}")") SCRIPT_DIR=$(dirname -- "$(readlink -f "${BASH_SOURCE}")")
rm -rf $SCRIPT_DIR/$NAME rm -rf $SCRIPT_DIR/$NAME
sudo $SCRIPT_DIR/alpine-make-rootfs/alpine-make-rootfs $SCRIPT_DIR/$NAME $SCRIPT_DIR/alpine-make-rootfs/alpine-make-rootfs --packages 'python3 gcc libc-dev' $SCRIPT_DIR/$NAME
#chown -R nobody:nogroup $SCRIPT_DIR/$NAME #chown -R nobody:nogroup $SCRIPT_DIR/$NAME
#rm -r ../$NAME #rm -r ../$NAME
#mv $NAME ../$NAME #mv $NAME ../$NAME

View file

@ -39,7 +39,7 @@ void ro_fs(char* shared_mountpoint) {
strcpy(shared_folder,"../"); strcpy(shared_folder,"../");
strcat(shared_folder,shared_mountpoint); strcat(shared_folder,shared_mountpoint);
if (mount("/","/","ext4",MS_REMOUNT | MS_RDONLY| MS_BIND,"")) die("failed to mount: %m"); if (mount("/","/","ext4",MS_REMOUNT | MS_RDONLY| MS_BIND,"")) die("failed to mount: %m");
if (mount(shared_mountpoint,"shared","ext4",MS_REMOUNT | MS_RDONLY | MS_BIND,"")) die("failed to mount: %m"); if (mount(shared_mountpoint,"shared","ext4",MS_REMOUNT | MS_BIND,"")) die("failed to mount: %m");
free(shared_folder); free(shared_folder);
} }
//#undef shared_mountpoint //#undef shared_mountpoint
@ -56,11 +56,19 @@ static int nsrun(void* arg) {
if (prctl(PR_SET_PDEATHSIG, SIGKILL)) die("cannot PR_SET_PDEATHSIG for child process: %m\n"); if (prctl(PR_SET_PDEATHSIG, SIGKILL)) die("cannot PR_SET_PDEATHSIG for child process: %m\n");
struct params *params = (struct params*) arg; struct params *params = (struct params*) arg;
await_setup(params->fd[0]); await_setup(params->fd[0]);
if(chdir("shared")) die("failed to chdir: %m"); char cwd[PATH_MAX];
//int out_fd=open("out",O_WRONLY|O_CREAT); if(getcwd(cwd,sizeof(cwd))==NULL) die("getcwd error: %m");
//if(out_fd==-1) die("unable to open out file:%m"); if(chdir(params->shared_folder)) die("failed to chdir: %m");
//else dup2(out_fd,STDOUT_FILENO); int out_fd=open("out",O_WRONLY|O_CREAT,0666);
if(chdir("..")) die("failed to chdir: %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");
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");
else dup2(err_fd,STDERR_FILENO);
if(chdir(cwd)) die("failed to chdir: %m");
mnt_prepare("minrootfs",params->shared_folder); mnt_prepare("minrootfs",params->shared_folder);
sethostname(hostname,sizeof(hostname)); sethostname(hostname,sizeof(hostname));
ro_fs(params->shared_folder); ro_fs(params->shared_folder);

View file

@ -28,6 +28,7 @@ struct limits {
size_t memory; size_t memory;
int core; int core;
int time; int time;
int processes;
}; };
struct killparams{ struct killparams{
@ -65,11 +66,13 @@ static char nmstack[STACK_SIZE];
static char killstack[STACK_SIZE]; static char killstack[STACK_SIZE];
static void parse_args(int argc, char **argv, struct params *params,struct limits *limits){ static void parse_args(int argc, char **argv, struct params *params,struct limits *limits){
if (argc < 6) { if (argc < 7) {
puts("usage:\n starter <core_id> <memory_amount> <time limit in ms> <shared folder> <command, arg1,arg2,...>"); puts("usage:\n starter <max processes number> <core_id> <memory_amount> <time limit in ms> <shared folder> <command, arg1,arg2,...>");
exit(0); exit(0);
} }
argc--; argv++; argc--; argv++;
limits->processes = atoi(argv[0]);
argc--; argv++;
limits->core = atoi(argv[0]); limits->core = atoi(argv[0]);
argc--; argv++; argc--; argv++;
limits->memory = atoi(argv[0]); limits->memory = atoi(argv[0]);