1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
package device
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/lxc/incus/v6/shared/util"
)
// deviceJoinPath joins together prefix and text delimited by a "." for device path generation.
func deviceJoinPath(parts ...string) string {
return strings.Join(parts, ".")
}
// validatePCIDevice returns whether a configured PCI device exists under the given address.
// It also returns nil, if an empty address is supplied.
func validatePCIDevice(address string) error {
if address != "" && !util.PathExists(fmt.Sprintf("/sys/bus/pci/devices/%s", address)) {
return fmt.Errorf("Invalid PCI address (no device found): %s", address)
}
return nil
}
// checkAttachedRunningProcess checks if a device is tied to running processes.
func checkAttachedRunningProcesses(devicePath string) ([]string, error) {
var processes []string
procDir := "/proc"
files, err := os.ReadDir(procDir)
if err != nil {
return nil, fmt.Errorf("failed to read /proc directory: %w", err)
}
for _, file := range files {
// Check if the directory name is a number (i.e., a PID).
_, err := strconv.Atoi(file.Name())
if err != nil {
continue
}
mapsFile := filepath.Join(procDir, file.Name(), "maps")
f, err := os.Open(mapsFile)
if err != nil {
continue // If we can't read a process's maps file, skip it.
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
if strings.Contains(scanner.Text(), devicePath) {
processes = append(processes, file.Name())
break
}
}
}
return processes, nil
}
|