github.com/containerd/Containerd@v1.4.13/mount/mountinfo_bsd.go (about)

     1  // +build freebsd openbsd
     2  
     3  /*
     4     Copyright The containerd Authors.
     5  
     6     Licensed under the Apache License, Version 2.0 (the "License");
     7     you may not use this file except in compliance with the License.
     8     You may obtain a copy of the License at
     9  
    10         http://www.apache.org/licenses/LICENSE-2.0
    11  
    12     Unless required by applicable law or agreed to in writing, software
    13     distributed under the License is distributed on an "AS IS" BASIS,
    14     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15     See the License for the specific language governing permissions and
    16     limitations under the License.
    17  */
    18  
    19  package mount
    20  
    21  /*
    22  #include <sys/param.h>
    23  #include <sys/ucred.h>
    24  #include <sys/mount.h>
    25  */
    26  import "C"
    27  
    28  import (
    29  	"fmt"
    30  	"reflect"
    31  	"unsafe"
    32  )
    33  
    34  // Self retrieves a list of mounts for the current running process.
    35  func Self() ([]Info, error) {
    36  	var rawEntries *C.struct_statfs
    37  
    38  	count := int(C.getmntinfo(&rawEntries, C.MNT_WAIT))
    39  	if count == 0 {
    40  		return nil, fmt.Errorf("Failed to call getmntinfo")
    41  	}
    42  
    43  	var entries []C.struct_statfs
    44  	header := (*reflect.SliceHeader)(unsafe.Pointer(&entries))
    45  	header.Cap = count
    46  	header.Len = count
    47  	header.Data = uintptr(unsafe.Pointer(rawEntries))
    48  
    49  	var out []Info
    50  	for _, entry := range entries {
    51  		var mountinfo Info
    52  		mountinfo.Mountpoint = C.GoString(&entry.f_mntonname[0])
    53  		mountinfo.Source = C.GoString(&entry.f_mntfromname[0])
    54  		mountinfo.FSType = C.GoString(&entry.f_fstypename[0])
    55  		out = append(out, mountinfo)
    56  	}
    57  	return out, nil
    58  }
    59  
    60  // PID collects the mounts for a specific process ID.
    61  func PID(pid int) ([]Info, error) {
    62  	return nil, fmt.Errorf("mountinfo.PID is not implemented on freebsd")
    63  }