go.ligato.io/vpp-agent/v3@v3.5.0/plugins/linux/nsplugin/linuxcalls/system_linuxcalls.go (about) 1 // Copyright (c) 2018 Cisco and/or its affiliates. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at: 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package linuxcalls 16 17 import ( 18 "os" 19 "syscall" 20 21 "github.com/pkg/errors" 22 "github.com/vishvananda/netns" 23 ) 24 25 /* File system */ 26 27 // FileExists checks whether the file exists. 28 func (sh *systemHandler) FileExists(name string) (bool, error) { 29 if _, err := os.Stat(name); err != nil { 30 if os.IsNotExist(err) { 31 return false, nil 32 } 33 return false, errors.Errorf("failed to stat file %s: %v", name, err) 34 } 35 return true, nil 36 } 37 38 // OpenFile opens a file. 39 func (sh *systemHandler) OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) { 40 return os.OpenFile(name, flag, perm) 41 } 42 43 // MkDirAll creates a directory with all parent directories. 44 func (sh *systemHandler) MkDirAll(path string, perm os.FileMode) error { 45 return os.MkdirAll(path, perm) 46 } 47 48 // Remove removes named file or directory. 49 func (sh *systemHandler) Remove(name string) error { 50 return os.Remove(name) 51 } 52 53 // Mount makes resources available. 54 func (sh *systemHandler) Mount(source string, target string, fsType string, flags uintptr, data string) error { 55 return syscall.Mount(source, target, fsType, flags, data) 56 } 57 58 // Unmount resources. 59 func (sh *systemHandler) Unmount(target string, flags int) error { 60 return syscall.Unmount(target, flags) 61 } 62 63 /* Network Namespace */ 64 65 // NewNetworkNamespace creates a new namespace and returns a handle to manage it further. 66 func (sh *systemHandler) NewNetworkNamespace() (ns netns.NsHandle, err error) { 67 return netns.New() 68 } 69 70 // DuplicateNamespaceHandle duplicates network namespace handle. 71 func (sh *systemHandler) DuplicateNamespaceHandle(ns netns.NsHandle) (netns.NsHandle, error) { 72 dup, err := syscall.Dup(int(ns)) 73 return netns.NsHandle(dup), err 74 } 75 76 // GetCurrentNamespace gets a handle to the current threads network namespace. 77 func (sh *systemHandler) GetCurrentNamespace() (ns netns.NsHandle, err error) { 78 return netns.Get() 79 } 80 81 // GetNamespaceFromPath gets a handle to a network namespace identified 82 // by the path. 83 func (sh *systemHandler) GetNamespaceFromPath(path string) (ns netns.NsHandle, err error) { 84 return netns.GetFromPath(path) 85 } 86 87 // GetNamespaceFromPid gets a handle to the network namespace of a given pid. 88 func (sh *systemHandler) GetNamespaceFromPid(pid int) (ns netns.NsHandle, err error) { 89 return netns.GetFromPid(pid) 90 } 91 92 // GetNamespaceFromName gets a handle to a named network namespace such as one 93 // created by `ip netns add`. 94 func (sh *systemHandler) GetNamespaceFromName(name string) (ns netns.NsHandle, err error) { 95 return netns.GetFromName(name) 96 } 97 98 // SetNamespace sets the current namespace to the namespace represented by the handle. 99 func (sh *systemHandler) SetNamespace(ns netns.NsHandle) (err error) { 100 return netns.Set(ns) 101 }