go-judge/pkg/pool/environment.go
2020-04-16 20:15:59 -04:00

65 lines
1.6 KiB
Go

package pool
import (
"context"
"fmt"
"os"
"syscall"
"github.com/criyle/go-judge/pkg/envexec"
"github.com/criyle/go-sandbox/container"
)
var _ envexec.Environment = &Environment{}
// Environment defines interface to access container resources
type Environment struct {
container.Environment
cgPool CgroupPool
wd *os.File // container work dir
}
// Destory destories the environment
func (c *Environment) Destory() error {
return c.Environment.Destroy()
}
// Execve execute process inside the environment
func (c *Environment) Execve(ctx context.Context, param envexec.ExecveParam) (envexec.Process, error) {
cg, err := c.cgPool.Get()
if err != nil {
return nil, fmt.Errorf("execve: failed to get cgroup %v", err)
}
cg.SetMemoryLimit(param.Limit.Memory)
cg.SetProcLimit(param.Limit.Proc)
p := container.ExecveParam{
Args: param.Args,
Env: param.Env,
Files: param.Files,
ExecFile: param.ExecFile,
SyncFunc: cg.AddProc,
}
rt := c.Environment.Execve(ctx, p)
return newProcess(rt, cg, c.cgPool), nil
}
// WorkDir returns opened work directory, should not close after
func (c *Environment) WorkDir() *os.File {
c.wd.Seek(0, 0)
return c.wd
}
// Open opens file relative to work directory
func (c *Environment) Open(path string, flags int, perm os.FileMode) (*os.File, error) {
fd, err := syscall.Openat(int(c.wd.Fd()), path, flags|syscall.O_CLOEXEC, uint32(perm))
if err != nil {
return nil, fmt.Errorf("openAtWorkDir: %v", err)
}
f := os.NewFile(uintptr(fd), path)
if f == nil {
return nil, fmt.Errorf("openAtWorkDir: failed to NewFile")
}
return f, nil
}