netns_linux.go 744 B

1234567891011121314151617181920212223242526272829
  1. // Copyright (c) 2016 Arista Networks, Inc.
  2. // Use of this source code is governed by the Apache License 2.0
  3. // that can be found in the COPYING file.
  4. package netns
  5. import "golang.org/x/sys/unix"
  6. // close closes the file descriptor mapped to a network namespace
  7. func (h nsHandle) close() error {
  8. return unix.Close(int(h))
  9. }
  10. // fd returns the handle as a uintptr
  11. func (h nsHandle) fd() int {
  12. return int(h)
  13. }
  14. // getNs returns a file descriptor mapping to the given network namespace
  15. var getNs = func(nsName string) (handle, error) {
  16. fd, err := unix.Open(nsName, unix.O_RDONLY, 0)
  17. return nsHandle(fd), err
  18. }
  19. // setNs sets the process's network namespace
  20. var setNs = func(h handle) error {
  21. return unix.Setns(h.fd(), unix.CLONE_NEWNET)
  22. }