2024-01-01 09:55:49 +00:00
|
|
|
package netns
|
|
|
|
|
|
|
|
// inspired from https://github.com/vishvananda/netns/blob/master/nshandle_linux.go#L11
|
|
|
|
|
|
|
|
import (
|
2024-01-01 22:49:26 +00:00
|
|
|
"fmt"
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
"os"
|
|
|
|
"path"
|
2024-01-01 09:55:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const bindMountPath = "/run/netns"
|
|
|
|
|
|
|
|
// NsHandle is a handle to a network namespace. It can be cast directly
|
|
|
|
// to an int and used as a file descriptor.
|
|
|
|
type NsHandle struct {
|
2024-01-01 22:49:26 +00:00
|
|
|
Fd int
|
|
|
|
Name string
|
2024-01-01 09:55:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// persist and bind mount net namespace to `/run/netns`
|
2024-01-01 22:49:26 +00:00
|
|
|
func (ns *NsHandle) persist() error {
|
2024-01-01 09:55:49 +00:00
|
|
|
if _, err := os.Stat(bindMountPath); os.IsNotExist(err) {
|
|
|
|
err = os.MkdirAll(bindMountPath, 0o755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-01 22:49:26 +00:00
|
|
|
namedPath := path.Join(bindMountPath, ns.Name)
|
2024-01-01 09:55:49 +00:00
|
|
|
|
|
|
|
f, err := os.OpenFile(namedPath, os.O_CREATE|os.O_EXCL, 0o444)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-03 19:33:29 +00:00
|
|
|
err = unix.Mount(GetPath(), namedPath, "bind", unix.MS_BIND, "")
|
2024-01-01 09:55:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equal determines if two network handles refer to the same network
|
|
|
|
// namespace. This is done by comparing the device and inode that the
|
|
|
|
// file descriptors point to.
|
2024-01-01 22:49:26 +00:00
|
|
|
func (ns *NsHandle) Equal(other NsHandle) bool {
|
|
|
|
if *ns == other {
|
2024-01-01 09:55:49 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
var s1, s2 unix.Stat_t
|
2024-01-01 22:49:26 +00:00
|
|
|
if err := unix.Fstat(ns.Fd, &s1); err != nil {
|
2024-01-01 09:55:49 +00:00
|
|
|
return false
|
|
|
|
}
|
2024-01-01 22:49:26 +00:00
|
|
|
if err := unix.Fstat(other.Fd, &s2); err != nil {
|
2024-01-01 09:55:49 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return (s1.Dev == s2.Dev) && (s1.Ino == s2.Ino)
|
|
|
|
}
|
|
|
|
|
|
|
|
// String shows the file descriptor number and its dev and inode.
|
2024-01-01 22:49:26 +00:00
|
|
|
func (ns *NsHandle) String() string {
|
|
|
|
if ns.Fd == -1 {
|
2024-01-01 09:55:49 +00:00
|
|
|
return "NS(none)"
|
|
|
|
}
|
|
|
|
var s unix.Stat_t
|
2024-01-01 22:49:26 +00:00
|
|
|
if err := unix.Fstat(ns.Fd, &s); err != nil {
|
2024-01-03 19:33:29 +00:00
|
|
|
return fmt.Sprintf("NS(%d: unknown)", ns.Fd)
|
2024-01-01 09:55:49 +00:00
|
|
|
}
|
2024-01-03 19:33:29 +00:00
|
|
|
return fmt.Sprintf("NS(%d: %d, %d)", ns.Fd, s.Dev, s.Ino)
|
2024-01-01 09:55:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsOpen returns true if Close() has not been called.
|
2024-01-01 22:49:26 +00:00
|
|
|
func (ns *NsHandle) IsOpen() bool {
|
|
|
|
return ns.Fd != -1
|
2024-01-01 09:55:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the NsHandle and resets its file descriptor to -1.
|
|
|
|
// It is not safe to use an NsHandle after Close() is called.
|
2024-01-01 22:49:26 +00:00
|
|
|
func (ns *NsHandle) Close() error {
|
|
|
|
if err := unix.Close(ns.Fd); err != nil {
|
2024-01-01 09:55:49 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-01-01 22:49:26 +00:00
|
|
|
ns.Fd = -1
|
2024-01-01 09:55:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// None gets an empty (closed) NsHandle.
|
|
|
|
func None() NsHandle {
|
|
|
|
return NsHandle{
|
2024-01-01 22:49:26 +00:00
|
|
|
Fd: -1,
|
2024-01-01 09:55:49 +00:00
|
|
|
}
|
|
|
|
}
|