mirror of
https://github.com/criyle/go-sandbox.git
synced 2025-11-04 14:49:53 +08:00
refactor: normalize error message & replace path with filepath
This commit is contained in:
parent
c63c27d3c2
commit
7aa1c5a28b
@ -117,7 +117,7 @@ func start() (*runner.Result, error) {
|
|||||||
if profilePath != "" {
|
if profilePath != "" {
|
||||||
c, err := os.ReadFile(profilePath)
|
c, err := os.ReadFile(profilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("profile: %v", err)
|
return nil, fmt.Errorf("profile: %w", err)
|
||||||
}
|
}
|
||||||
profile = string(c)
|
profile = string(c)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -237,11 +237,11 @@ func start() (*runner.Result, error) {
|
|||||||
if memfile {
|
if memfile {
|
||||||
fin, err := os.Open(args[0])
|
fin, err := os.Open(args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to open args[0]: %v", err)
|
return nil, fmt.Errorf("failed to open args[0]: %w", err)
|
||||||
}
|
}
|
||||||
execf, err := memfd.DupToMemfd("run_program", fin)
|
execf, err := memfd.DupToMemfd("run_program", fin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("dup to memfd failed: %v", err)
|
return nil, fmt.Errorf("dup to memfd failed: %w", err)
|
||||||
}
|
}
|
||||||
fin.Close()
|
fin.Close()
|
||||||
defer execf.Close()
|
defer execf.Close()
|
||||||
@ -252,7 +252,7 @@ func start() (*runner.Result, error) {
|
|||||||
// open input / output / err files
|
// open input / output / err files
|
||||||
files, err := prepareFiles(inputFileName, outputFileName, errorFileName)
|
files, err := prepareFiles(inputFileName, outputFileName, errorFileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to prepare files: %v", err)
|
return nil, fmt.Errorf("failed to prepare files: %w", err)
|
||||||
}
|
}
|
||||||
defer closeFiles(files)
|
defer closeFiles(files)
|
||||||
|
|
||||||
@ -295,7 +295,7 @@ func start() (*runner.Result, error) {
|
|||||||
if !unsafe || runt != "container" {
|
if !unsafe || runt != "container" {
|
||||||
filter, err = builder.Build()
|
filter, err = builder.Build()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to create seccomp filter %v", err)
|
return nil, fmt.Errorf("failed to create seccomp filter: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -329,12 +329,12 @@ func start() (*runner.Result, error) {
|
|||||||
|
|
||||||
m, err := b.Build()
|
m, err := b.Build()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to new container: %v", err)
|
return nil, fmt.Errorf("failed to new container: %w", err)
|
||||||
}
|
}
|
||||||
defer m.Destroy()
|
defer m.Destroy()
|
||||||
err = m.Ping()
|
err = m.Ping()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to ping container: %v", err)
|
return nil, fmt.Errorf("failed to ping container: %w", err)
|
||||||
}
|
}
|
||||||
if unsafe {
|
if unsafe {
|
||||||
filter = nil
|
filter = nil
|
||||||
|
|||||||
@ -47,6 +47,9 @@ func (c *containerServer) handleOpen(open []OpenCmd) error {
|
|||||||
for _, o := range open {
|
for _, o := range open {
|
||||||
outFile, err := os.OpenFile(o.Path, o.Flag, o.Perm)
|
outFile, err := os.OpenFile(o.Path, o.Flag, o.Perm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
for _, f := range fileToClose {
|
||||||
|
f.Close()
|
||||||
|
}
|
||||||
return c.sendErrorReply("open: %v", err)
|
return c.sendErrorReply("open: %v", err)
|
||||||
}
|
}
|
||||||
fileToClose = append(fileToClose, outFile)
|
fileToClose = append(fileToClose, outFile)
|
||||||
@ -85,6 +88,7 @@ func readDotEnv() ([]string, error) {
|
|||||||
if errors.Is(err, os.ErrNotExist) {
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
return nil, fmt.Errorf("dotenv: open /.env: %w", err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
@ -96,7 +100,7 @@ func readDotEnv() ([]string, error) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if !strings.Contains(line, "=") {
|
if !strings.Contains(line, "=") {
|
||||||
return nil, fmt.Errorf("dotenv: invalid line %s", line)
|
return nil, fmt.Errorf("dotenv: invalid line: %s", line)
|
||||||
}
|
}
|
||||||
ret = append(ret, line)
|
ret = append(ret, line)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -66,14 +66,14 @@ func (c *containerServer) handleExecve(cmd *execCmd, msg unixsocket.Msg) error {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
if err := c.sendReply(reply{}, msg); err != nil {
|
if err := c.sendReply(reply{}, msg); err != nil {
|
||||||
return fmt.Errorf("syncFunc: sendReply %v", err)
|
return fmt.Errorf("sync func: send reply: %w", err)
|
||||||
}
|
}
|
||||||
cmd, _, err := c.recvCmd()
|
cmd, _, err := c.recvCmd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("syncFunc: recvCmd %v", err)
|
return fmt.Errorf("sync func: recv cmd: %w", err)
|
||||||
}
|
}
|
||||||
if cmd.Cmd == cmdKill {
|
if cmd.Cmd == cmdKill {
|
||||||
return fmt.Errorf("syncFunc: received kill")
|
return fmt.Errorf("sync func: received kill")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -179,7 +179,7 @@ func convertReply(ret waitPidResult) reply {
|
|||||||
if ret.Err != nil {
|
if ret.Err != nil {
|
||||||
return reply{
|
return reply{
|
||||||
Error: &errorReply{
|
Error: &errorReply{
|
||||||
Msg: fmt.Sprintf("execve: wait4 %v", ret.Err),
|
Msg: fmt.Sprintf("execve: wait4: %v", ret.Err),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -229,7 +229,7 @@ func convertReply(ret waitPidResult) reply {
|
|||||||
default:
|
default:
|
||||||
return reply{
|
return reply{
|
||||||
Error: &errorReply{
|
Error: &errorReply{
|
||||||
Msg: fmt.Sprintf("execve: unknown status %v", waitStatus),
|
Msg: fmt.Sprintf("execve: unknown status: %v", waitStatus),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -88,14 +88,14 @@ func Init() (err error) {
|
|||||||
|
|
||||||
// ensure there's no fd leak to child process (e.g. VSCode leaks ptmx fd)
|
// ensure there's no fd leak to child process (e.g. VSCode leaks ptmx fd)
|
||||||
if err := closeOnExecAllFds(); err != nil {
|
if err := closeOnExecAllFds(); err != nil {
|
||||||
return fmt.Errorf("container_init: failed to close_on_exec all fd %v", err)
|
return fmt.Errorf("container_init: failed to close on exec all fds: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// new_container environment shared the socket at fd 3 (marked close_exec)
|
// new_container environment shared the socket at fd 3 (marked close_exec)
|
||||||
const defaultFd = 3
|
const defaultFd = 3
|
||||||
soc, err := unixsocket.NewSocket(defaultFd)
|
soc, err := unixsocket.NewSocket(defaultFd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("container_init: failed to new socket %v", err)
|
return fmt.Errorf("container_init: failed to create new socket: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// serve forever
|
// serve forever
|
||||||
@ -197,10 +197,10 @@ func (c *containerServer) serve() error {
|
|||||||
for {
|
for {
|
||||||
cmd, msg, err := c.recvCmd()
|
cmd, msg, err := c.recvCmd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("serve: recvCmd %v", err)
|
return fmt.Errorf("serve: recvCmd: %w", err)
|
||||||
}
|
}
|
||||||
if err := c.handleCmd(cmd, msg); err != nil {
|
if err := c.handleCmd(cmd, msg); err != nil {
|
||||||
return fmt.Errorf("serve: failed to execute cmd %v", err)
|
return fmt.Errorf("serve: failed to execute cmd: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -255,53 +255,53 @@ func initFileSystem(c containerConfig) error {
|
|||||||
// mount tmpfs as root
|
// mount tmpfs as root
|
||||||
const tmpfs = "tmpfs"
|
const tmpfs = "tmpfs"
|
||||||
if err := syscall.Mount(tmpfs, c.ContainerRoot, tmpfs, 0, ""); err != nil {
|
if err := syscall.Mount(tmpfs, c.ContainerRoot, tmpfs, 0, ""); err != nil {
|
||||||
return fmt.Errorf("init_fs: mount / %v", err)
|
return fmt.Errorf("init_fs: mount /: %w", err)
|
||||||
}
|
}
|
||||||
// change dir to container root
|
// change dir to container root
|
||||||
if err := syscall.Chdir(c.ContainerRoot); err != nil {
|
if err := syscall.Chdir(c.ContainerRoot); err != nil {
|
||||||
return fmt.Errorf("init_fs: chdir %v", err)
|
return fmt.Errorf("init_fs: chdir: %w", err)
|
||||||
}
|
}
|
||||||
// performing mounts
|
// performing mounts
|
||||||
for _, m := range c.Mounts {
|
for _, m := range c.Mounts {
|
||||||
if err := m.Mount(); err != nil {
|
if err := m.Mount(); err != nil {
|
||||||
return fmt.Errorf("init_fs: mount %v %v", m, err)
|
return fmt.Errorf("init_fs: mount %v: %w", m, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// pivot root
|
// pivot root
|
||||||
const oldRoot = "old_root"
|
const oldRoot = "old_root"
|
||||||
if err := os.Mkdir(oldRoot, 0755); err != nil {
|
if err := os.Mkdir(oldRoot, 0755); err != nil {
|
||||||
return fmt.Errorf("init_fs: mkdir(old_root) %v", err)
|
return fmt.Errorf("init_fs: mkdir old_root: %w", err)
|
||||||
}
|
}
|
||||||
if err := syscall.PivotRoot(c.ContainerRoot, oldRoot); err != nil {
|
if err := syscall.PivotRoot(c.ContainerRoot, oldRoot); err != nil {
|
||||||
return fmt.Errorf("init_fs: pivot_root(%s, %s) %v", c.ContainerRoot, oldRoot, err)
|
return fmt.Errorf("init_fs: pivot_root(%s, %s): %w", c.ContainerRoot, oldRoot, err)
|
||||||
}
|
}
|
||||||
if err := syscall.Unmount(oldRoot, syscall.MNT_DETACH); err != nil {
|
if err := syscall.Unmount(oldRoot, syscall.MNT_DETACH); err != nil {
|
||||||
return fmt.Errorf("init_fs: unmount(old_root) %v", err)
|
return fmt.Errorf("init_fs: unmount old_root: %w", err)
|
||||||
}
|
}
|
||||||
if err := os.Remove(oldRoot); err != nil {
|
if err := os.Remove(oldRoot); err != nil {
|
||||||
return fmt.Errorf("init_fs: unlink(old_root) %v", err)
|
return fmt.Errorf("init_fs: unlink old_root: %w", err)
|
||||||
}
|
}
|
||||||
// create symlinks
|
// create symlinks
|
||||||
for _, l := range c.SymbolicLinks {
|
for _, l := range c.SymbolicLinks {
|
||||||
// ensure dir exists
|
// ensure dir exists
|
||||||
dir := filepath.Dir(l.LinkPath)
|
dir := filepath.Dir(l.LinkPath)
|
||||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||||
return fmt.Errorf("init_fs: mkdir_all(%s) %v", dir, err)
|
return fmt.Errorf("init_fs: mkdir_all(%s): %w", dir, err)
|
||||||
}
|
}
|
||||||
if err := os.Symlink(l.Target, l.LinkPath); err != nil {
|
if err := os.Symlink(l.Target, l.LinkPath); err != nil {
|
||||||
return fmt.Errorf("init_fs: symlink %v", err)
|
return fmt.Errorf("init_fs: symlink: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// mask paths
|
// mask paths
|
||||||
for _, p := range c.MaskPaths {
|
for _, p := range c.MaskPaths {
|
||||||
if err := maskPath(p); err != nil {
|
if err := maskPath(p); err != nil {
|
||||||
return fmt.Errorf("init_fs: mask path %v", err)
|
return fmt.Errorf("init_fs: mask path: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// readonly root
|
// readonly root
|
||||||
const remountFlag = syscall.MS_BIND | syscall.MS_REMOUNT | syscall.MS_RDONLY | syscall.MS_NOATIME | syscall.MS_NOSUID
|
const remountFlag = syscall.MS_BIND | syscall.MS_REMOUNT | syscall.MS_RDONLY | syscall.MS_NOATIME | syscall.MS_NOSUID
|
||||||
if err := syscall.Mount(tmpfs, "/", tmpfs, remountFlag, ""); err != nil {
|
if err := syscall.Mount(tmpfs, "/", tmpfs, remountFlag, ""); err != nil {
|
||||||
return fmt.Errorf("init_fs: readonly remount / %v", err)
|
return fmt.Errorf("init_fs: readonly remount /: %w", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -368,7 +368,7 @@ func maskPath(path string) error {
|
|||||||
// otherwise, mount tmpfs to mask it
|
// otherwise, mount tmpfs to mask it
|
||||||
return syscall.Mount("tmpfs", path, "tmpfs", syscall.MS_RDONLY, "")
|
return syscall.Mount("tmpfs", path, "tmpfs", syscall.MS_RDONLY, "")
|
||||||
}
|
}
|
||||||
return err
|
return fmt.Errorf("mask path: %w", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -127,7 +127,7 @@ func (b *Builder) Build() (Environment, error) {
|
|||||||
// avoid non cinit enabled executable running as container init process
|
// avoid non cinit enabled executable running as container init process
|
||||||
if err = c.Ping(); err != nil {
|
if err = c.Ping(); err != nil {
|
||||||
c.Destroy()
|
c.Destroy()
|
||||||
return nil, fmt.Errorf("container: container init not responding to ping %v", err)
|
return nil, fmt.Errorf("container: init not responding to ping: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// container mount points
|
// container mount points
|
||||||
@ -154,13 +154,13 @@ func (b *Builder) Build() (Environment, error) {
|
|||||||
root := b.Root
|
root := b.Root
|
||||||
if b.TmpRoot != "" {
|
if b.TmpRoot != "" {
|
||||||
if root, err = os.MkdirTemp(b.Root, b.TmpRoot); err != nil {
|
if root, err = os.MkdirTemp(b.Root, b.TmpRoot); err != nil {
|
||||||
return nil, fmt.Errorf("container: failed to make tmp container root at %s %v", b.Root, err)
|
return nil, fmt.Errorf("container: failed to make tmp container root at %s: %w", b.Root, err)
|
||||||
}
|
}
|
||||||
defer os.Remove(root)
|
defer os.Remove(root)
|
||||||
}
|
}
|
||||||
if root == "" {
|
if root == "" {
|
||||||
if root, err = os.Getwd(); err != nil {
|
if root, err = os.Getwd(); err != nil {
|
||||||
return nil, fmt.Errorf("container: failed to get work directory %v", err)
|
return nil, fmt.Errorf("container: failed to get work directory: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
workDir := containerWD
|
workDir := containerWD
|
||||||
@ -206,14 +206,14 @@ func (b *Builder) startContainer() (*container, error) {
|
|||||||
// prepare host <-> container unix socket
|
// prepare host <-> container unix socket
|
||||||
ins, outs, err := newPassCredSocketPair()
|
ins, outs, err := newPassCredSocketPair()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("container: failed to create socket: %v", err)
|
return nil, fmt.Errorf("container: failed to create socket: %w", err)
|
||||||
}
|
}
|
||||||
defer outs.Close()
|
defer outs.Close()
|
||||||
|
|
||||||
outf, err := outs.File()
|
outf, err := outs.File()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ins.Close()
|
ins.Close()
|
||||||
return nil, fmt.Errorf("container: failed to dup container socket fd %v", err)
|
return nil, fmt.Errorf("container: failed to dup container socket fd: %w", err)
|
||||||
}
|
}
|
||||||
defer outf.Close()
|
defer outf.Close()
|
||||||
|
|
||||||
@ -258,7 +258,7 @@ func (b *Builder) startContainer() (*container, error) {
|
|||||||
}
|
}
|
||||||
if err = r.Start(); err != nil {
|
if err = r.Start(); err != nil {
|
||||||
ins.Close()
|
ins.Close()
|
||||||
return nil, fmt.Errorf("container: failed to start container %v", err)
|
return nil, fmt.Errorf("container: failed to start container: %w", err)
|
||||||
}
|
}
|
||||||
c := &container{
|
c := &container{
|
||||||
process: r.Process,
|
process: r.Process,
|
||||||
@ -386,10 +386,10 @@ func (b *Builder) getIDMapping(cred *syscall.Credential) ([]syscall.SysProcIDMap
|
|||||||
func (c *container) recvAckReply(name string) error {
|
func (c *container) recvAckReply(name string) error {
|
||||||
reply, _, err := c.recvReply()
|
reply, _, err := c.recvReply()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("%v: recvAck %v", name, err)
|
return fmt.Errorf("%s: recv ack: %w", name, err)
|
||||||
}
|
}
|
||||||
if reply.Error != nil {
|
if reply.Error != nil {
|
||||||
return fmt.Errorf("%v: container error %v", name, reply.Error)
|
return fmt.Errorf("%s: container error: %v", name, reply.Error)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,7 +24,7 @@ func (c *container) Ping() error {
|
|||||||
Cmd: cmdPing,
|
Cmd: cmdPing,
|
||||||
}
|
}
|
||||||
if err := c.sendCmd(cmd, unixsocket.Msg{}); err != nil {
|
if err := c.sendCmd(cmd, unixsocket.Msg{}); err != nil {
|
||||||
return fmt.Errorf("ping: %v", err)
|
return fmt.Errorf("ping: %w", err)
|
||||||
}
|
}
|
||||||
// receive no error
|
// receive no error
|
||||||
return c.recvAckReply("ping")
|
return c.recvAckReply("ping")
|
||||||
@ -40,7 +40,7 @@ func (c *container) conf(conf *containerConfig) error {
|
|||||||
ConfCmd: &confCmd{Conf: *conf},
|
ConfCmd: &confCmd{Conf: *conf},
|
||||||
}
|
}
|
||||||
if err := c.sendCmd(cmd, unixsocket.Msg{}); err != nil {
|
if err := c.sendCmd(cmd, unixsocket.Msg{}); err != nil {
|
||||||
return fmt.Errorf("conf: %v", err)
|
return fmt.Errorf("conf: %w", err)
|
||||||
}
|
}
|
||||||
return c.recvAckReply("conf")
|
return c.recvAckReply("conf")
|
||||||
}
|
}
|
||||||
@ -59,18 +59,18 @@ func (c *container) Open(p []OpenCmd) ([]*os.File, error) {
|
|||||||
OpenCmd: p,
|
OpenCmd: p,
|
||||||
}
|
}
|
||||||
if err := c.sendCmd(cmd, unixsocket.Msg{}); err != nil {
|
if err := c.sendCmd(cmd, unixsocket.Msg{}); err != nil {
|
||||||
return nil, fmt.Errorf("open: %v", err)
|
return nil, fmt.Errorf("open: %w", err)
|
||||||
}
|
}
|
||||||
reply, msg, err := c.recvReply()
|
reply, msg, err := c.recvReply()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("open: %v", err)
|
return nil, fmt.Errorf("open: %w", err)
|
||||||
}
|
}
|
||||||
if reply.Error != nil {
|
if reply.Error != nil {
|
||||||
return nil, fmt.Errorf("open: %v", reply.Error)
|
return nil, fmt.Errorf("open: %v", reply.Error)
|
||||||
}
|
}
|
||||||
if len(msg.Fds) != len(p) {
|
if len(msg.Fds) != len(p) {
|
||||||
closeFds(msg.Fds)
|
closeFds(msg.Fds)
|
||||||
return nil, fmt.Errorf("open: unexpected number of fd %v / %v", len(msg.Fds), len(p))
|
return nil, fmt.Errorf("open: unexpected number of fds: got %d, want %d", len(msg.Fds), len(p))
|
||||||
}
|
}
|
||||||
|
|
||||||
ret := make([]*os.File, 0, len(p))
|
ret := make([]*os.File, 0, len(p))
|
||||||
@ -79,7 +79,7 @@ func (c *container) Open(p []OpenCmd) ([]*os.File, error) {
|
|||||||
f := os.NewFile(uintptr(fd), p[i].Path)
|
f := os.NewFile(uintptr(fd), p[i].Path)
|
||||||
if f == nil {
|
if f == nil {
|
||||||
closeFds(msg.Fds)
|
closeFds(msg.Fds)
|
||||||
return nil, fmt.Errorf("open: failed NewFile %v", fd)
|
return nil, fmt.Errorf("open: failed to create file for fd: %d", fd)
|
||||||
}
|
}
|
||||||
ret = append(ret, f)
|
ret = append(ret, f)
|
||||||
}
|
}
|
||||||
@ -96,7 +96,7 @@ func (c *container) Delete(p string) error {
|
|||||||
DeleteCmd: &deleteCmd{Path: p},
|
DeleteCmd: &deleteCmd{Path: p},
|
||||||
}
|
}
|
||||||
if err := c.sendCmd(cmd, unixsocket.Msg{}); err != nil {
|
if err := c.sendCmd(cmd, unixsocket.Msg{}); err != nil {
|
||||||
return fmt.Errorf("delete: %v", err)
|
return fmt.Errorf("delete: %w", err)
|
||||||
}
|
}
|
||||||
return c.recvAckReply("delete")
|
return c.recvAckReply("delete")
|
||||||
}
|
}
|
||||||
@ -110,7 +110,7 @@ func (c *container) Reset() error {
|
|||||||
Cmd: cmdReset,
|
Cmd: cmdReset,
|
||||||
}
|
}
|
||||||
if err := c.sendCmd(cmd, unixsocket.Msg{}); err != nil {
|
if err := c.sendCmd(cmd, unixsocket.Msg{}); err != nil {
|
||||||
return fmt.Errorf("reset: %v", err)
|
return fmt.Errorf("reset: %w", err)
|
||||||
}
|
}
|
||||||
return c.recvAckReply("reset")
|
return c.recvAckReply("reset")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -43,27 +43,27 @@ func newSocket(s *unixsocket.Socket) *socket {
|
|||||||
return &soc
|
return &soc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *socket) RecvMsg(e interface{}) (msg unixsocket.Msg, err error) {
|
func (s *socket) RecvMsg(e any) (msg unixsocket.Msg, err error) {
|
||||||
n, msg, err := s.Socket.RecvMsg(s.buff)
|
n, msg, err := s.Socket.RecvMsg(s.buff)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return msg, fmt.Errorf("RecvMsg: %v", err)
|
return msg, fmt.Errorf("recv msg: %w", err)
|
||||||
}
|
}
|
||||||
s.recvBuff.Rotate(bytes.NewBuffer(s.buff[:n]))
|
s.recvBuff.Rotate(bytes.NewBuffer(s.buff[:n]))
|
||||||
|
|
||||||
if err := s.decoder.Decode(e); err != nil {
|
if err := s.decoder.Decode(e); err != nil {
|
||||||
return msg, fmt.Errorf("RecvMsg: failed to decode %v", err)
|
return msg, fmt.Errorf("recv msg: decode: %w", err)
|
||||||
}
|
}
|
||||||
return msg, nil
|
return msg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *socket) SendMsg(e interface{}, msg unixsocket.Msg) error {
|
func (s *socket) SendMsg(e any, msg unixsocket.Msg) error {
|
||||||
s.sendBuff.Reset()
|
s.sendBuff.Reset()
|
||||||
if err := s.encoder.Encode(e); err != nil {
|
if err := s.encoder.Encode(e); err != nil {
|
||||||
return fmt.Errorf("SendMsg: failed to encode %v", err)
|
return fmt.Errorf("send msg: encode: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.Socket.SendMsg(s.sendBuff.Bytes(), msg); err != nil {
|
if err := s.Socket.SendMsg(s.sendBuff.Bytes(), msg); err != nil {
|
||||||
return fmt.Errorf("SendMsg: failed to SendMsg %v", err)
|
return fmt.Errorf("send msg: %w", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,7 @@ package container
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path/filepath"
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -54,10 +54,10 @@ func removeContents(dir string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
err = os.RemoveAll(path.Join(dir, name))
|
err1 := os.RemoveAll(filepath.Join(dir, name))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
err = err1
|
||||||
|
}
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -178,7 +178,7 @@ func GetAvailableControllerV2() (*Controllers, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getAvailableControllerV2(prefix string) (*Controllers, error) {
|
func getAvailableControllerV2(prefix string) (*Controllers, error) {
|
||||||
return getAvailableControllerV2path(path.Join(basePath, prefix, cgroupControllers))
|
return getAvailableControllerV2path(filepath.Join(basePath, prefix, cgroupControllers))
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAvailableControllerV2path(p string) (*Controllers, error) {
|
func getAvailableControllerV2path(p string) (*Controllers, error) {
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -136,7 +136,7 @@ func newV1(prefix string, ct *Controllers) (cg Cgroup, err error) {
|
|||||||
|
|
||||||
func newV2(prefix string, ct *Controllers) (cg Cgroup, err error) {
|
func newV2(prefix string, ct *Controllers) (cg Cgroup, err error) {
|
||||||
v2 := &V2{
|
v2 := &V2{
|
||||||
path: path.Join(basePath, prefix),
|
path: filepath.Join(basePath, prefix),
|
||||||
control: ct,
|
control: ct,
|
||||||
}
|
}
|
||||||
if _, err := os.Stat(v2.path); err == nil {
|
if _, err := os.Stat(v2.path); err == nil {
|
||||||
@ -159,8 +159,8 @@ func newV2(prefix string, ct *Controllers) (cg Cgroup, err error) {
|
|||||||
parent := current
|
parent := current
|
||||||
current = current + "/" + e
|
current = current + "/" + e
|
||||||
// try mkdir if not exists
|
// try mkdir if not exists
|
||||||
if _, err := os.Stat(path.Join(basePath, current)); os.IsNotExist(err) {
|
if _, err := os.Stat(filepath.Join(basePath, current)); os.IsNotExist(err) {
|
||||||
if err := os.Mkdir(path.Join(basePath, current), dirPerm); err != nil {
|
if err := os.Mkdir(filepath.Join(basePath, current), dirPerm); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
@ -175,7 +175,7 @@ func newV2(prefix string, ct *Controllers) (cg Cgroup, err error) {
|
|||||||
if ect.Contains(ct) {
|
if ect.Contains(ct) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err := writeFile(path.Join(basePath, parent, cgroupSubtreeControl), controlMsg, filePerm); err != nil {
|
if err := writeFile(filepath.Join(basePath, parent, cgroupSubtreeControl), controlMsg, filePerm); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -197,7 +197,7 @@ func openExistingV1(prefix string, ct *Controllers) (cg Cgroup, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err = loopV1Controllers(ct, v1, func(name string, cg **v1controller) error {
|
if err = loopV1Controllers(ct, v1, func(name string, cg **v1controller) error {
|
||||||
p := path.Join(basePath, name, prefix)
|
p := filepath.Join(basePath, name, prefix)
|
||||||
*cg = newV1Controller(p)
|
*cg = newV1Controller(p)
|
||||||
// os.IsNotExist
|
// os.IsNotExist
|
||||||
if _, err := os.Stat(p); err != nil {
|
if _, err := os.Stat(p); err != nil {
|
||||||
@ -224,10 +224,10 @@ func openExistingV2(prefix string, ct *Controllers) (cg Cgroup, err error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if !ect.Contains(ct) {
|
if !ect.Contains(ct) {
|
||||||
return nil, fmt.Errorf("openCgroupV2: requesting %v controllers but %v found", ct, ect)
|
return nil, fmt.Errorf("open cgroup v2: requesting %v controllers but %v found", ct, ect)
|
||||||
}
|
}
|
||||||
return &V2{
|
return &V2{
|
||||||
path: path.Join(basePath, prefix),
|
path: filepath.Join(basePath, prefix),
|
||||||
control: ect,
|
control: ect,
|
||||||
existing: true,
|
existing: true,
|
||||||
}, nil
|
}, nil
|
||||||
|
|||||||
@ -6,7 +6,7 @@ import (
|
|||||||
"io/fs"
|
"io/fs"
|
||||||
"math/rand/v2"
|
"math/rand/v2"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
@ -24,7 +24,7 @@ func EnsureDirExists(path string) error {
|
|||||||
|
|
||||||
// CreateV1ControllerPath create path for controller with given group, prefix
|
// CreateV1ControllerPath create path for controller with given group, prefix
|
||||||
func CreateV1ControllerPath(controller, prefix string) (string, error) {
|
func CreateV1ControllerPath(controller, prefix string) (string, error) {
|
||||||
p := path.Join(basePath, controller, prefix)
|
p := filepath.Join(basePath, controller, prefix)
|
||||||
return p, EnsureDirExists(p)
|
return p, EnsureDirExists(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,7 +37,7 @@ func EnableV2Nesting() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
p, err := readFile(path.Join(basePath, cgroupProcs))
|
p, err := readFile(filepath.Join(basePath, cgroupProcs))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -47,11 +47,11 @@ func EnableV2Nesting() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// mkdir init
|
// mkdir init
|
||||||
if err := os.Mkdir(path.Join(basePath, initPath), dirPerm); err != nil && !errors.Is(err, os.ErrExist) {
|
if err := os.Mkdir(filepath.Join(basePath, initPath), dirPerm); err != nil && !errors.Is(err, os.ErrExist) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// move all process into init cgroup
|
// move all process into init cgroup
|
||||||
procFile, err := os.OpenFile(path.Join(basePath, initPath, cgroupProcs), os.O_RDWR, filePerm)
|
procFile, err := os.OpenFile(filepath.Join(basePath, initPath, cgroupProcs), os.O_RDWR, filePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -165,7 +165,7 @@ func nextRandom() string {
|
|||||||
func randomBuild(pattern string, build func(string) (Cgroup, error)) (Cgroup, error) {
|
func randomBuild(pattern string, build func(string) (Cgroup, error)) (Cgroup, error) {
|
||||||
prefix, suffix, err := prefixAndSuffix(pattern)
|
prefix, suffix, err := prefixAndSuffix(pattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cgroup.builder: random %v", err)
|
return nil, fmt.Errorf("cgroup.builder: random %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
try := 0
|
try := 0
|
||||||
@ -181,6 +181,6 @@ func randomBuild(pattern string, build func(string) (Cgroup, error)) (Cgroup, er
|
|||||||
}
|
}
|
||||||
return nil, fmt.Errorf("cgroup.builder: tried 10000 times but failed")
|
return nil, fmt.Errorf("cgroup.builder: tried 10000 times but failed")
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("cgroup.builder: random %v", err)
|
return nil, fmt.Errorf("cgroup.builder: random %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,7 +4,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -65,13 +64,13 @@ func (c *V1) Processes() ([]int, error) {
|
|||||||
if len(c.all) == 0 {
|
if len(c.all) == 0 {
|
||||||
return nil, os.ErrInvalid
|
return nil, os.ErrInvalid
|
||||||
}
|
}
|
||||||
return ReadProcesses(path.Join(c.all[0].path, cgroupProcs))
|
return ReadProcesses(filepath.Join(c.all[0].path, cgroupProcs))
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a sub-cgroup based on the existing one
|
// New creates a sub-cgroup based on the existing one
|
||||||
func (c *V1) New(name string) (cg Cgroup, err error) {
|
func (c *V1) New(name string) (cg Cgroup, err error) {
|
||||||
v1 := &V1{
|
v1 := &V1{
|
||||||
prefix: path.Join(c.prefix, name),
|
prefix: filepath.Join(c.prefix, name),
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -93,7 +92,7 @@ func (c *V1) New(name string) (cg Cgroup, err error) {
|
|||||||
if v.now == nil {
|
if v.now == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
p := path.Join(v.now.path, name)
|
p := filepath.Join(v.now.path, name)
|
||||||
*v.new = &v1controller{path: p}
|
*v.new = &v1controller{path: p}
|
||||||
err = EnsureDirExists(p)
|
err = EnsureDirExists(p)
|
||||||
if os.IsExist(err) {
|
if os.IsExist(err) {
|
||||||
|
|||||||
@ -2,7 +2,7 @@ package cgroup
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"path"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -50,7 +50,7 @@ func (c *v1controller) WriteFile(name string, content []byte) error {
|
|||||||
if c == nil || c.path == "" {
|
if c == nil || c.path == "" {
|
||||||
return ErrNotInitialized
|
return ErrNotInitialized
|
||||||
}
|
}
|
||||||
p := path.Join(c.path, name)
|
p := filepath.Join(c.path, name)
|
||||||
return writeFile(p, content, filePerm)
|
return writeFile(p, content, filePerm)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,10 +60,10 @@ func (c *v1controller) ReadFile(name string) ([]byte, error) {
|
|||||||
if c == nil || c.path == "" {
|
if c == nil || c.path == "" {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
p := path.Join(c.path, name)
|
p := filepath.Join(c.path, name)
|
||||||
return readFile(p)
|
return readFile(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *v1controller) AddProc(pids ...int) error {
|
func (c *v1controller) AddProc(pids ...int) error {
|
||||||
return AddProcesses(path.Join(c.path, cgroupProcs), pids)
|
return AddProcesses(filepath.Join(c.path, cgroupProcs), pids)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -26,18 +26,18 @@ func (c *V2) Open() (*os.File, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *V2) String() string {
|
func (c *V2) String() string {
|
||||||
ct, _ := getAvailableControllerV2path(path.Join(c.path, cgroupControllers))
|
ct, _ := getAvailableControllerV2path(filepath.Join(c.path, cgroupControllers))
|
||||||
return "v2(" + c.path + ")" + ct.String()
|
return "v2(" + c.path + ")" + ct.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddProc adds processes into the cgroup
|
// AddProc adds processes into the cgroup
|
||||||
func (c *V2) AddProc(pids ...int) error {
|
func (c *V2) AddProc(pids ...int) error {
|
||||||
return AddProcesses(path.Join(c.path, cgroupProcs), pids)
|
return AddProcesses(filepath.Join(c.path, cgroupProcs), pids)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Processes returns all processes within the cgroup
|
// Processes returns all processes within the cgroup
|
||||||
func (c *V2) Processes() ([]int, error) {
|
func (c *V2) Processes() ([]int, error) {
|
||||||
return ReadProcesses(path.Join(c.path, cgroupProcs))
|
return ReadProcesses(filepath.Join(c.path, cgroupProcs))
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a sub-cgroup based on the existing one
|
// New creates a sub-cgroup based on the existing one
|
||||||
@ -46,7 +46,7 @@ func (c *V2) New(name string) (Cgroup, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
v2 := &V2{
|
v2 := &V2{
|
||||||
path: path.Join(c.path, name),
|
path: filepath.Join(c.path, name),
|
||||||
control: c.control,
|
control: c.control,
|
||||||
}
|
}
|
||||||
if err := os.Mkdir(v2.path, dirPerm); err != nil {
|
if err := os.Mkdir(v2.path, dirPerm); err != nil {
|
||||||
@ -61,7 +61,7 @@ func (c *V2) New(name string) (Cgroup, error) {
|
|||||||
// Nest creates a sub-cgroup, moves current process into that cgroup
|
// Nest creates a sub-cgroup, moves current process into that cgroup
|
||||||
func (c *V2) Nest(name string) (Cgroup, error) {
|
func (c *V2) Nest(name string) (Cgroup, error) {
|
||||||
v2 := &V2{
|
v2 := &V2{
|
||||||
path: path.Join(c.path, name),
|
path: filepath.Join(c.path, name),
|
||||||
control: c.control,
|
control: c.control,
|
||||||
}
|
}
|
||||||
if err := os.Mkdir(v2.path, dirPerm); err != nil {
|
if err := os.Mkdir(v2.path, dirPerm); err != nil {
|
||||||
@ -85,12 +85,12 @@ func (c *V2) Nest(name string) (Cgroup, error) {
|
|||||||
|
|
||||||
func (c *V2) enableSubtreeControl() error {
|
func (c *V2) enableSubtreeControl() error {
|
||||||
c.subtreeOnce.Do(func() {
|
c.subtreeOnce.Do(func() {
|
||||||
ct, err := getAvailableControllerV2path(path.Join(c.path, cgroupControllers))
|
ct, err := getAvailableControllerV2path(filepath.Join(c.path, cgroupControllers))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.subtreeErr = err
|
c.subtreeErr = err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ect, err := getAvailableControllerV2path(path.Join(c.path, cgroupSubtreeControl))
|
ect, err := getAvailableControllerV2path(filepath.Join(c.path, cgroupSubtreeControl))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.subtreeErr = err
|
c.subtreeErr = err
|
||||||
return
|
return
|
||||||
@ -100,7 +100,7 @@ func (c *V2) enableSubtreeControl() error {
|
|||||||
}
|
}
|
||||||
s := ct.Names()
|
s := ct.Names()
|
||||||
controlMsg := []byte("+" + strings.Join(s, " +"))
|
controlMsg := []byte("+" + strings.Join(s, " +"))
|
||||||
c.subtreeErr = writeFile(path.Join(c.path, cgroupSubtreeControl), controlMsg, filePerm)
|
c.subtreeErr = writeFile(filepath.Join(c.path, cgroupSubtreeControl), controlMsg, filePerm)
|
||||||
})
|
})
|
||||||
return c.subtreeErr
|
return c.subtreeErr
|
||||||
}
|
}
|
||||||
@ -221,13 +221,13 @@ func (c *V2) ReadUint(filename string) (uint64, error) {
|
|||||||
// WriteFile writes cgroup file and handles potential EINTR error while writes to
|
// WriteFile writes cgroup file and handles potential EINTR error while writes to
|
||||||
// the slow device (cgroup)
|
// the slow device (cgroup)
|
||||||
func (c *V2) WriteFile(name string, content []byte) error {
|
func (c *V2) WriteFile(name string, content []byte) error {
|
||||||
p := path.Join(c.path, name)
|
p := filepath.Join(c.path, name)
|
||||||
return writeFile(p, content, filePerm)
|
return writeFile(p, content, filePerm)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadFile reads cgroup file and handles potential EINTR error while read to
|
// ReadFile reads cgroup file and handles potential EINTR error while read to
|
||||||
// the slow device (cgroup)
|
// the slow device (cgroup)
|
||||||
func (c *V2) ReadFile(name string) ([]byte, error) {
|
func (c *V2) ReadFile(name string) ([]byte, error) {
|
||||||
p := path.Join(c.path, name)
|
p := filepath.Join(c.path, name)
|
||||||
return readFile(p)
|
return readFile(p)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
var errNotImplemented = fmt.Errorf("memfd: unsupported on platform %s", runtime.GOOS)
|
var errNotImplemented = fmt.Errorf("memfd: unsupported on platform: %s", runtime.GOOS)
|
||||||
|
|
||||||
func New(name string) (*os.File, error) {
|
func New(name string) (*os.File, error) {
|
||||||
return nil, errNotImplemented
|
return nil, errNotImplemented
|
||||||
|
|||||||
@ -15,7 +15,7 @@ func ToSyscallName(sysno uint) (string, error) {
|
|||||||
}
|
}
|
||||||
n, ok := info.SyscallNumbers[int(sysno)]
|
n, ok := info.SyscallNumbers[int(sysno)]
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", fmt.Errorf("syscall no %d does not exits", sysno)
|
return "", fmt.Errorf("syscall number does not exist: %d", sysno)
|
||||||
}
|
}
|
||||||
return n, nil
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -45,19 +45,19 @@ func NewSocket(fd int) (*Socket, error) {
|
|||||||
|
|
||||||
file := os.NewFile(uintptr(fd), "unix-socket")
|
file := os.NewFile(uintptr(fd), "unix-socket")
|
||||||
if file == nil {
|
if file == nil {
|
||||||
return nil, fmt.Errorf("NewSocket: %d is not a valid fd", fd)
|
return nil, fmt.Errorf("new socket: %d is not a valid fd", fd)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
conn, err := net.FileConn(file)
|
conn, err := net.FileConn(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("new socket: fileconn: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
unixConn, ok := conn.(*net.UnixConn)
|
unixConn, ok := conn.(*net.UnixConn)
|
||||||
if !ok {
|
if !ok {
|
||||||
conn.Close()
|
conn.Close()
|
||||||
return nil, fmt.Errorf("NewSocket: %d is not a valid unix socket connection", fd)
|
return nil, fmt.Errorf("new socket: %d is not a valid unix socket connection", fd)
|
||||||
}
|
}
|
||||||
return newSocket(unixConn), nil
|
return newSocket(unixConn), nil
|
||||||
}
|
}
|
||||||
@ -66,21 +66,21 @@ func NewSocket(fd int) (*Socket, error) {
|
|||||||
func NewSocketPair() (*Socket, *Socket, error) {
|
func NewSocketPair() (*Socket, *Socket, error) {
|
||||||
fd, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_SEQPACKET|syscall.SOCK_CLOEXEC, 0)
|
fd, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_SEQPACKET|syscall.SOCK_CLOEXEC, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("NewSocketPair: failed to call socketpair %v", err)
|
return nil, nil, fmt.Errorf("new socket pair: socketpair: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ins, err := NewSocket(fd[0])
|
ins, err := NewSocket(fd[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
syscall.Close(fd[0])
|
syscall.Close(fd[0])
|
||||||
syscall.Close(fd[1])
|
syscall.Close(fd[1])
|
||||||
return nil, nil, fmt.Errorf("NewSocketPair: failed to call NewSocket on sender %v", err)
|
return nil, nil, fmt.Errorf("new socket pair: sender: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
outs, err := NewSocket(fd[1])
|
outs, err := NewSocket(fd[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ins.Close()
|
ins.Close()
|
||||||
syscall.Close(fd[1])
|
syscall.Close(fd[1])
|
||||||
return nil, nil, fmt.Errorf("NewSocketPair: failed to call NewSocket receiver %v", err)
|
return nil, nil, fmt.Errorf("new socket pair: receiver: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ins, outs, nil
|
return ins, outs, nil
|
||||||
|
|||||||
@ -3,7 +3,7 @@ package ptrace
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path/filepath"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/criyle/go-sandbox/pkg/seccomp/libseccomp"
|
"github.com/criyle/go-sandbox/pkg/seccomp/libseccomp"
|
||||||
@ -156,8 +156,8 @@ func getProcCwd(pid int) string {
|
|||||||
// built-in function did the dirty works to resolve relative paths
|
// built-in function did the dirty works to resolve relative paths
|
||||||
func absPath(pid int, p string) string {
|
func absPath(pid int, p string) string {
|
||||||
// if relative path
|
// if relative path
|
||||||
if !path.IsAbs(p) {
|
if !filepath.IsAbs(p) {
|
||||||
return path.Join(getProcCwd(pid), p)
|
return filepath.Join(getProcCwd(pid), p)
|
||||||
}
|
}
|
||||||
return path.Clean(p)
|
return filepath.Clean(p)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user