From edd5bc38bf89af2be905b60dd50565b758145c48 Mon Sep 17 00:00:00 2001 From: nixpig <143995476+nixpig@users.noreply.github.com> Date: Sat, 28 Dec 2024 06:14:00 +0000 Subject: [PATCH] refactor: change 'constants' using syscall package to use unix package --- container/container.go | 4 +- container/container_delete.go | 6 +- container/container_kill.go | 3 +- filesystem/filesystem.go | 11 +-- filesystem/masked_paths.go | 7 +- filesystem/pivot.go | 4 +- filesystem/readonly_paths.go | 10 ++- filesystem/rootfs.go | 3 +- namespace/namespace.go | 18 ++--- signal/signal.go | 135 +++++++++------------------------- signal/signal_test.go | 20 ++--- terminal/terminal.go | 4 +- 12 files changed, 78 insertions(+), 147 deletions(-) diff --git a/container/container.go b/container/container.go index 16b279a..7005226 100644 --- a/container/container.go +++ b/container/container.go @@ -8,11 +8,11 @@ import ( "path/filepath" "strconv" "strings" - "syscall" "github.com/nixpig/brownie/lifecycle" "github.com/opencontainers/runtime-spec/specs-go" "golang.org/x/mod/semver" + "golang.org/x/sys/unix" ) const ( @@ -140,7 +140,7 @@ func (c *Container) RefreshState() error { return fmt.Errorf("find refresh container process (%d): %w", c.State.PID, err) } - if err := process.Signal(syscall.Signal(0)); err != nil { + if err := process.Signal(unix.Signal(0)); err != nil { c.SetStatus(specs.StateStopped) if err := c.Save(); err != nil { return fmt.Errorf("save refresh container state: %w", err) diff --git a/container/container_delete.go b/container/container_delete.go index 4e66031..a132d7c 100644 --- a/container/container_delete.go +++ b/container/container_delete.go @@ -7,6 +7,8 @@ import ( "strconv" "strings" "syscall" + + "golang.org/x/sys/unix" ) func (c *Container) Delete(force bool) error { @@ -19,7 +21,7 @@ func (c *Container) Delete(force bool) error { return fmt.Errorf("find container process (%d): %w", c.PID(), err) } if process != nil { - process.Signal(syscall.Signal(9)) + process.Signal(unix.Signal(9)) } if err := os.RemoveAll(filepath.Join(containerRootDir, c.ID())); err != nil { @@ -40,7 +42,7 @@ func killAllChildren(pid int) error { } for _, p := range childPIDs { - if err := syscall.Kill(p, syscall.Signal(9)); err != nil { + if err := syscall.Kill(p, unix.Signal(9)); err != nil { return fmt.Errorf("kill child pid: %w", err) } } diff --git a/container/container_kill.go b/container/container_kill.go index 2dc2fb9..68561bc 100644 --- a/container/container_kill.go +++ b/container/container_kill.go @@ -5,9 +5,10 @@ import ( "syscall" "github.com/opencontainers/runtime-spec/specs-go" + "golang.org/x/sys/unix" ) -func (c *Container) Kill(sig syscall.Signal) error { +func (c *Container) Kill(sig unix.Signal) error { if !c.CanBeKilled() { return fmt.Errorf("container cannot be killed in current state (%s)", c.Status()) } diff --git a/filesystem/filesystem.go b/filesystem/filesystem.go index 1398a5a..df6c38e 100644 --- a/filesystem/filesystem.go +++ b/filesystem/filesystem.go @@ -9,6 +9,7 @@ import ( "syscall" "github.com/opencontainers/runtime-spec/specs-go" + "golang.org/x/sys/unix" ) func MountDevice(device Device) error { @@ -46,7 +47,7 @@ func mountRootfs(containerRootfs string) error { Source: "", Target: "/", Fstype: "", - Flags: syscall.MS_PRIVATE | syscall.MS_REC, + Flags: unix.MS_PRIVATE | unix.MS_REC, Data: "", }); err != nil { return err @@ -56,7 +57,7 @@ func mountRootfs(containerRootfs string) error { Source: containerRootfs, Target: containerRootfs, Fstype: "", - Flags: syscall.MS_BIND | syscall.MS_REC, + Flags: unix.MS_BIND | unix.MS_REC, Data: "", }); err != nil { return err @@ -108,7 +109,7 @@ func mountDevices(devices []specs.LinuxDevice, rootfs string) error { Source: dev.Path, Target: absPath, Fstype: "bind", - Flags: syscall.MS_BIND, + Flags: unix.MS_BIND, Data: "", }); err != nil { return fmt.Errorf("mount device: %w", err) @@ -134,14 +135,14 @@ func mountSpecMounts(mounts []specs.Mount, rootfs string) error { var flags uintptr if mount.Type == "bind" { - flags |= syscall.MS_BIND + flags |= unix.MS_BIND } var dataOptions []string for _, opt := range mount.Options { if opt == "bind" || opt == "rbind" { mount.Type = "bind" - flags |= syscall.MS_BIND + flags |= unix.MS_BIND } } diff --git a/filesystem/masked_paths.go b/filesystem/masked_paths.go index cca488c..adfb321 100644 --- a/filesystem/masked_paths.go +++ b/filesystem/masked_paths.go @@ -2,7 +2,8 @@ package filesystem import ( "os" - "syscall" + + "golang.org/x/sys/unix" ) func MountMaskedPaths(paths []string) error { @@ -17,7 +18,7 @@ func MountMaskedPaths(paths []string) error { Source: "tmpfs", Target: path, Fstype: "tmpfs", - Flags: syscall.MS_RDONLY, + Flags: unix.MS_RDONLY, Data: "", }); err != nil { return err @@ -27,7 +28,7 @@ func MountMaskedPaths(paths []string) error { Source: "/dev/null", Target: path, Fstype: "bind", - Flags: syscall.MS_BIND, + Flags: unix.MS_BIND, Data: "", }); err != nil { return err diff --git a/filesystem/pivot.go b/filesystem/pivot.go index 50355c7..ab1124a 100644 --- a/filesystem/pivot.go +++ b/filesystem/pivot.go @@ -5,6 +5,8 @@ import ( "os" "path/filepath" "syscall" + + "golang.org/x/sys/unix" ) const oldroot = ".oldroot" @@ -28,7 +30,7 @@ func pivotRootfs(containerRootfs string) error { return fmt.Errorf("chdir to new root: %w", err) } - if err := syscall.Unmount(oldroot, syscall.MNT_DETACH); err != nil { + if err := syscall.Unmount(oldroot, unix.MNT_DETACH); err != nil { return fmt.Errorf("unmount old root: %w", err) } diff --git a/filesystem/readonly_paths.go b/filesystem/readonly_paths.go index a809035..2bfa7bc 100644 --- a/filesystem/readonly_paths.go +++ b/filesystem/readonly_paths.go @@ -1,6 +1,8 @@ package filesystem -import "syscall" +import ( + "golang.org/x/sys/unix" +) func MountReadonlyPaths(paths []string) error { for _, path := range paths { @@ -8,7 +10,7 @@ func MountReadonlyPaths(paths []string) error { Source: path, Target: path, Fstype: "", - Flags: syscall.MS_REC | syscall.MS_BIND, + Flags: unix.MS_REC | unix.MS_BIND, Data: "", }); err != nil { return err @@ -18,8 +20,8 @@ func MountReadonlyPaths(paths []string) error { Source: path, Target: path, Fstype: "", - Flags: syscall.MS_NOSUID | syscall.MS_NODEV | syscall.MS_NOEXEC | - syscall.MS_BIND | syscall.MS_REMOUNT | syscall.MS_RDONLY, + Flags: unix.MS_NOSUID | unix.MS_NODEV | unix.MS_NOEXEC | + unix.MS_BIND | unix.MS_REMOUNT | unix.MS_RDONLY, Data: "", }); err != nil { return err diff --git a/filesystem/rootfs.go b/filesystem/rootfs.go index 5a35868..e6828de 100644 --- a/filesystem/rootfs.go +++ b/filesystem/rootfs.go @@ -5,6 +5,7 @@ import ( "syscall" "github.com/opencontainers/runtime-spec/specs-go" + "golang.org/x/sys/unix" ) func SetupRootfs(rootfs string, spec *specs.Spec) error { @@ -70,7 +71,7 @@ func MountRootReadonly(ro bool) error { "", "/", "", - syscall.MS_BIND|syscall.MS_REMOUNT|syscall.MS_RDONLY, + unix.MS_BIND|unix.MS_REMOUNT|unix.MS_RDONLY, "", ); err != nil { return fmt.Errorf("remount root as readonly: %w", err) diff --git a/namespace/namespace.go b/namespace/namespace.go index 3e75074..d861353 100644 --- a/namespace/namespace.go +++ b/namespace/namespace.go @@ -37,21 +37,21 @@ func (ns *LinuxNamespace) ToEnv() string { func (ns *LinuxNamespace) ToFlag() uintptr { switch ns.Type { case specs.PIDNamespace: - return syscall.CLONE_NEWPID + return unix.CLONE_NEWPID case specs.NetworkNamespace: - return syscall.CLONE_NEWNET + return unix.CLONE_NEWNET case specs.MountNamespace: - return syscall.CLONE_NEWNS + return unix.CLONE_NEWNS case specs.IPCNamespace: - return syscall.CLONE_NEWIPC + return unix.CLONE_NEWIPC case specs.UTSNamespace: - return syscall.CLONE_NEWUTS + return unix.CLONE_NEWUTS case specs.UserNamespace: - return syscall.CLONE_NEWUSER + return unix.CLONE_NEWUSER case specs.CgroupNamespace: - return syscall.CLONE_NEWCGROUP + return unix.CLONE_NEWCGROUP case specs.TimeNamespace: - return syscall.CLONE_NEWTIME + return unix.CLONE_NEWTIME default: return 0 } @@ -64,7 +64,7 @@ func (ns *LinuxNamespace) Enter() error { } defer syscall.Close(fd) - _, _, errno := syscall.RawSyscall(unix.SYS_SETNS, uintptr(fd), 0, 0) + _, _, errno := syscall.Syscall(unix.SYS_SETNS, uintptr(fd), 0, 0) if errno != 0 { return fmt.Errorf("errno: %w", errno) } diff --git a/signal/signal.go b/signal/signal.go index 917117a..9151c7e 100644 --- a/signal/signal.go +++ b/signal/signal.go @@ -1,139 +1,70 @@ package signal -import ( - "syscall" -) +import "golang.org/x/sys/unix" -func FromInt(s int) syscall.Signal { - switch s { - case 1: - return syscall.SIGHUP - case 2: - return syscall.SIGINT - case 3: - return syscall.SIGQUIT - case 4: - return syscall.SIGILL - case 5: - return syscall.SIGTRAP - case 6: - return syscall.SIGIOT - case 7: - return syscall.SIGBUS - case 8: - return syscall.SIGFPE - case 9: - return syscall.SIGKILL - case 10: - return syscall.SIGUSR1 - case 11: - return syscall.SIGSEGV - case 12: - return syscall.SIGUSR2 - case 13: - return syscall.SIGPIPE - case 14: - return syscall.SIGALRM - case 15: - return syscall.SIGTERM - case 16: - return syscall.SIGSTKFLT - case 17: - return syscall.SIGCHLD - case 18: - return syscall.SIGCONT - case 19: - return syscall.SIGSTOP - case 20: - return syscall.SIGTSTP - case 21: - return syscall.SIGTTIN - case 22: - return syscall.SIGTTOU - case 23: - return syscall.SIGURG - case 24: - return syscall.SIGXCPU - case 25: - return syscall.SIGXFSZ - case 26: - return syscall.SIGVTALRM - case 27: - return syscall.SIGPROF - case 28: - return syscall.SIGWINCH - case 29: - return syscall.SIGIO - case 30: - return syscall.SIGPWR - } - - return 0 -} - -func FromString(s string) syscall.Signal { +func FromString(s string) unix.Signal { switch s { case "SIGHUP", "HUP", "1": - return syscall.SIGHUP + return unix.SIGHUP case "SIGINT", "INT", "2": - return syscall.SIGINT + return unix.SIGINT case "SIGQUIT", "QUIT", "3": - return syscall.SIGQUIT + return unix.SIGQUIT case "SIGILL", "ILL", "4": - return syscall.SIGILL + return unix.SIGILL case "SIGTRAP", "TRAP", "5": - return syscall.SIGTRAP + return unix.SIGTRAP case "SIGIOT", "IOT", "6": - return syscall.SIGIOT + return unix.SIGIOT case "SIGBUS", "BUS", "7": - return syscall.SIGBUS + return unix.SIGBUS case "SIGFPE", "FPE", "8": - return syscall.SIGFPE + return unix.SIGFPE case "SIGKILL", "KILL", "9": - return syscall.SIGKILL + return unix.SIGKILL case "SIGUSR1", "USR1", "10": - return syscall.SIGUSR1 + return unix.SIGUSR1 case "SIGSEGV", "SEGV", "11": - return syscall.SIGSEGV + return unix.SIGSEGV case "SIGUSR2", "USR2", "12": - return syscall.SIGUSR2 + return unix.SIGUSR2 case "SIGPIPE", "PIPE", "13": - return syscall.SIGPIPE + return unix.SIGPIPE case "SIGALRM", "ALRM", "14": - return syscall.SIGALRM + return unix.SIGALRM case "SIGTERM", "TERM", "15": - return syscall.SIGTERM + return unix.SIGTERM case "SIGSTKFLT", "STKFLT", "16": - return syscall.SIGSTKFLT + return unix.SIGSTKFLT case "SIGCHLD", "CHLD", "17": - return syscall.SIGCHLD + return unix.SIGCHLD case "SIGCONT", "CONT", "18": - return syscall.SIGCONT + return unix.SIGCONT case "SIGSTOP", "STOP", "19": - return syscall.SIGSTOP + return unix.SIGSTOP case "SIGTSTP", "TSTP", "20": - return syscall.SIGTSTP + return unix.SIGTSTP case "SIGTTIN", "TTIN", "21": - return syscall.SIGTTIN + return unix.SIGTTIN case "SIGTTOU", "TTOU", "22": - return syscall.SIGTTOU + return unix.SIGTTOU case "SIGURG", "URG", "23": - return syscall.SIGURG + return unix.SIGURG case "SIGXCPU", "XCPU", "24": - return syscall.SIGXCPU + return unix.SIGXCPU case "SIGXFSZ", "XFSZ", "25": - return syscall.SIGXFSZ + return unix.SIGXFSZ case "SIGVTALRM", "VTALRM", "26": - return syscall.SIGVTALRM + return unix.SIGVTALRM case "SIGPROF", "PROF", "27": - return syscall.SIGPROF + return unix.SIGPROF case "SIGWINCH", "WINCH", "28": - return syscall.SIGWINCH + return unix.SIGWINCH case "SIGIO", "IO", "29": - return syscall.SIGIO + return unix.SIGIO case "SIGPWR", "PWR", "30": - return syscall.SIGPWR + return unix.SIGPWR } - return syscall.Signal(0) + return unix.Signal(0) } diff --git a/signal/signal_test.go b/signal/signal_test.go index 0db496d..771fe4a 100644 --- a/signal/signal_test.go +++ b/signal/signal_test.go @@ -1,39 +1,29 @@ package signal_test import ( - "syscall" "testing" "github.com/nixpig/brownie/signal" "github.com/stretchr/testify/assert" + "golang.org/x/sys/unix" ) -func TestFromInt(t *testing.T) { - sig := signal.FromInt(9) - assert.Equal(t, syscall.SIGKILL, sig) -} - -func TestFromIntInvalid(t *testing.T) { - sig := signal.FromInt(99) - assert.Equal(t, syscall.Signal(0), sig) -} - func TestFromStringNumber(t *testing.T) { sig := signal.FromString("10") - assert.Equal(t, syscall.SIGUSR1, sig) + assert.Equal(t, unix.SIGUSR1, sig) } func TestFromStringShort(t *testing.T) { sig := signal.FromString("CHLD") - assert.Equal(t, syscall.SIGCHLD, sig) + assert.Equal(t, unix.SIGCHLD, sig) } func TestFromStringLong(t *testing.T) { sig := signal.FromString("SIGQUIT") - assert.Equal(t, syscall.SIGQUIT, sig) + assert.Equal(t, unix.SIGQUIT, sig) } func TestFromStringInvalid(t *testing.T) { sig := signal.FromString("something invalid") - assert.Equal(t, syscall.Signal(0), sig) + assert.Equal(t, unix.Signal(0), sig) } diff --git a/terminal/terminal.go b/terminal/terminal.go index 556ae24..4f3bfc5 100644 --- a/terminal/terminal.go +++ b/terminal/terminal.go @@ -58,8 +58,8 @@ type PtySocket struct { func NewPtySocket(consoleSocketPath string) (*PtySocket, error) { fd, err := syscall.Socket( - syscall.AF_UNIX, - syscall.SOCK_STREAM, + unix.AF_UNIX, + unix.SOCK_STREAM, 0, ) if err != nil {