Skip to content

Commit

Permalink
Use golang.org/x/sys/unix instead of syscall
Browse files Browse the repository at this point in the history
The syscall package is deprecated and no longer updated as per
https://golang.org/pkg/syscall/. Use the golang.org/x/sys/unix package
instead, which also provides a wrapper for SYS_SETNS, so the syscall
number encoding depending on runtime.GOARCH can be dropped.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
  • Loading branch information
tklauser committed Feb 18, 2020
1 parent 0a2b9b5 commit 20c1755
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 34 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/vishvananda/netns

go 1.12

require golang.org/x/sys v0.0.0-20200217220822-9197077df867
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/sys v0.0.0-20200217220822-9197077df867 h1:JoRuNIf+rpHl+VhScRQQvzbHed86tKkqwPMV34T8myw=
golang.org/x/sys v0.0.0-20200217220822-9197077df867/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
19 changes: 10 additions & 9 deletions netns.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ package netns

import (
"fmt"
"syscall"

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

// NsHandle is a handle to a network namespace. It can be cast directly
Expand All @@ -24,23 +25,23 @@ func (ns NsHandle) Equal(other NsHandle) bool {
if ns == other {
return true
}
var s1, s2 syscall.Stat_t
if err := syscall.Fstat(int(ns), &s1); err != nil {
var s1, s2 unix.Stat_t
if err := unix.Fstat(int(ns), &s1); err != nil {
return false
}
if err := syscall.Fstat(int(other), &s2); err != nil {
if err := unix.Fstat(int(other), &s2); err != nil {
return false
}
return (s1.Dev == s2.Dev) && (s1.Ino == s2.Ino)
}

// String shows the file descriptor number and its dev and inode.
func (ns NsHandle) String() string {
var s syscall.Stat_t
if ns == -1 {
return "NS(None)"
}
if err := syscall.Fstat(int(ns), &s); err != nil {
var s unix.Stat_t
if err := unix.Fstat(int(ns), &s); err != nil {
return fmt.Sprintf("NS(%d: unknown)", ns)
}
return fmt.Sprintf("NS(%d: %d, %d)", ns, s.Dev, s.Ino)
Expand All @@ -49,11 +50,11 @@ func (ns NsHandle) String() string {
// UniqueId returns a string which uniquely identifies the namespace
// associated with the network handle.
func (ns NsHandle) UniqueId() string {
var s syscall.Stat_t
if ns == -1 {
return "NS(none)"
}
if err := syscall.Fstat(int(ns), &s); err != nil {
var s unix.Stat_t
if err := unix.Fstat(int(ns), &s); err != nil {
return "NS(unknown)"
}
return fmt.Sprintf("NS(%d:%d)", s.Dev, s.Ino)
Expand All @@ -67,7 +68,7 @@ func (ns NsHandle) IsOpen() bool {
// Close closes the NsHandle and resets its file descriptor to -1.
// It is not safe to use an NsHandle after Close() is called.
func (ns *NsHandle) Close() error {
if err := syscall.Close(int(*ns)); err != nil {
if err := unix.Close(int(*ns)); err != nil {
return err
}
(*ns) = -1
Expand Down
31 changes: 6 additions & 25 deletions netns_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,11 @@ import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
)

// SYS_SETNS syscall allows changing the namespace of the current process.
var SYS_SETNS = map[string]uintptr{
"386": 346,
"amd64": 308,
"arm64": 268,
"arm": 375,
"mips": 4344,
"mipsle": 4344,
"mips64le": 4344,
"ppc64": 350,
"ppc64le": 350,
"riscv64": 268,
"s390x": 339,
}[runtime.GOARCH]
"golang.org/x/sys/unix"
)

// Deprecated: use syscall pkg instead (go >= 1.5 needed).
const (
Expand All @@ -41,11 +26,7 @@ const (
// Setns sets namespace using syscall. Note that this should be a method
// in syscall but it has not been added.
func Setns(ns NsHandle, nstype int) (err error) {
_, _, e1 := syscall.Syscall(SYS_SETNS, uintptr(ns), uintptr(nstype), 0)
if e1 != 0 {
err = e1
}
return
return unix.Setns(int(ns), nstype)
}

// Set sets the current network namespace to the namespace represented
Expand All @@ -57,21 +38,21 @@ func Set(ns NsHandle) (err error) {
// New creates a new network namespace, sets it as current and returns
// a handle to it.
func New() (ns NsHandle, err error) {
if err := syscall.Unshare(CLONE_NEWNET); err != nil {
if err := unix.Unshare(CLONE_NEWNET); err != nil {
return -1, err
}
return Get()
}

// Get gets a handle to the current threads network namespace.
func Get() (NsHandle, error) {
return GetFromThread(os.Getpid(), syscall.Gettid())
return GetFromThread(os.Getpid(), unix.Gettid())
}

// GetFromPath gets a handle to a network namespace
// identified by the path
func GetFromPath(path string) (NsHandle, error) {
fd, err := syscall.Open(path, syscall.O_RDONLY, 0)
fd, err := unix.Open(path, unix.O_RDONLY, 0)
if err != nil {
return -1, err
}
Expand Down

0 comments on commit 20c1755

Please sign in to comment.