github.com/psiphon-inc/goarista@v0.0.0-20160825065156-d002785f4c67/netns/netns_linux.go (about)

     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  
     5  package netns
     6  
     7  import "golang.org/x/sys/unix"
     8  
     9  // close closes the file descriptor mapped to a network namespace
    10  func (h nsHandle) close() error {
    11  	return unix.Close(int(h))
    12  }
    13  
    14  // fd returns the handle as a uintptr
    15  func (h nsHandle) fd() int {
    16  	return int(h)
    17  }
    18  
    19  // getNs returns a file descriptor mapping to the given network namespace
    20  var getNs = func(nsName string) (handle, error) {
    21  	fd, err := unix.Open(nsName, unix.O_RDONLY, 0)
    22  	return nsHandle(fd), err
    23  }
    24  
    25  // setNs sets the process's network namespace
    26  var setNs = func(h handle) error {
    27  	return unix.Setns(h.fd(), unix.CLONE_NEWNET)
    28  }