namespace.go 998 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package libnetwork
  2. import "syscall"
  3. type networkNamespace struct {
  4. path string
  5. interfaces []*Interface
  6. }
  7. // Create a new network namespace mounted on the provided path.
  8. func NewNamespace(path string) (Namespace, error) {
  9. if err := reexec(reexecCreateNamespace, path); err != nil {
  10. return nil, err
  11. }
  12. return &networkNamespace{path: path}, nil
  13. }
  14. func (n *networkNamespace) AddInterface(i *Interface) error {
  15. // TODO Open pipe, pass fd to child and write serialized Interface on it.
  16. if err := reexec(reexecMoveInterface, i.SrcName, i.DstName); err != nil {
  17. return err
  18. }
  19. n.interfaces = append(n.interfaces, i)
  20. return nil
  21. }
  22. func (n *networkNamespace) Interfaces() []*Interface {
  23. return n.interfaces
  24. }
  25. func (n *networkNamespace) Path() string {
  26. return n.path
  27. }
  28. func (n *networkNamespace) Destroy() error {
  29. // Assuming no running process is executing in this network namespace,
  30. // unmounting is sufficient to destroy it.
  31. return syscall.Unmount(n.path, syscall.MNT_DETACH)
  32. }