github.com/cilium/cilium@v1.16.2/pkg/mountinfo/mountinfo_linux.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package mountinfo 5 6 import ( 7 "errors" 8 "os" 9 "path/filepath" 10 11 "golang.org/x/sys/unix" 12 ) 13 14 const ( 15 // FilesystemType superblock magic numbers for filesystems, 16 // to be used for IsMountFS. 17 FilesystemTypeBPFFS = unix.BPF_FS_MAGIC 18 FilesystemTypeCgroup2 = unix.CGROUP2_SUPER_MAGIC 19 ) 20 21 // IsMountFS returns two boolean values, checking 22 // - whether the path is a mount point; 23 // - if yes, whether its filesystem type is mntType. 24 // 25 // Note that this function can not detect bind mounts, 26 // and is not working properly when path="/". 27 func IsMountFS(mntType int64, path string) (bool, bool, error) { 28 var st, pst unix.Stat_t 29 30 err := unix.Lstat(path, &st) 31 if err != nil { 32 if errors.Is(err, unix.ENOENT) { 33 // non-existent path can't be a mount point 34 return false, false, nil 35 } 36 return false, false, &os.PathError{Op: "lstat", Path: path, Err: err} 37 } 38 39 parent := filepath.Dir(path) 40 err = unix.Lstat(parent, &pst) 41 if err != nil { 42 return false, false, &os.PathError{Op: "lstat", Path: parent, Err: err} 43 } 44 if st.Dev == pst.Dev { 45 // parent has the same dev -- not a mount point 46 return false, false, nil 47 } 48 49 // Check the fstype 50 fst := unix.Statfs_t{} 51 err = unix.Statfs(path, &fst) 52 if err != nil { 53 return true, false, &os.PathError{Op: "statfs", Path: path, Err: err} 54 } 55 56 return true, int64(fst.Type) == mntType, nil 57 58 }