Skip to content

Commit

Permalink
refactor: change 'constants' using syscall package to use unix package
Browse files Browse the repository at this point in the history
  • Loading branch information
nixpig committed Dec 28, 2024
1 parent 9626091 commit edd5bc3
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 147 deletions.
4 changes: 2 additions & 2 deletions container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions container/container_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strconv"
"strings"
"syscall"

"golang.org/x/sys/unix"
)

func (c *Container) Delete(force bool) error {
Expand All @@ -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 {
Expand All @@ -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)
}
}
Expand Down
3 changes: 2 additions & 1 deletion container/container_kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
11 changes: 6 additions & 5 deletions filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"syscall"

"github.com/opencontainers/runtime-spec/specs-go"
"golang.org/x/sys/unix"
)

func MountDevice(device Device) error {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
}

Expand Down
7 changes: 4 additions & 3 deletions filesystem/masked_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package filesystem

import (
"os"
"syscall"

"golang.org/x/sys/unix"
)

func MountMaskedPaths(paths []string) error {
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion filesystem/pivot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"path/filepath"
"syscall"

"golang.org/x/sys/unix"
)

const oldroot = ".oldroot"
Expand All @@ -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)
}

Expand Down
10 changes: 6 additions & 4 deletions filesystem/readonly_paths.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package filesystem

import "syscall"
import (
"golang.org/x/sys/unix"
)

func MountReadonlyPaths(paths []string) error {
for _, path := range paths {
if err := MountDevice(Device{
Source: path,
Target: path,
Fstype: "",
Flags: syscall.MS_REC | syscall.MS_BIND,
Flags: unix.MS_REC | unix.MS_BIND,
Data: "",
}); err != nil {
return err
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion filesystem/rootfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 9 additions & 9 deletions namespace/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}
Expand Down
Loading

0 comments on commit edd5bc3

Please sign in to comment.