cec39eb00c
Collapse ServerService and ClientService to one definition each by hiding Linux vs Darwin behind ExportHost, ImportHost, and Export seams. Along the way, extract three state machines that previously lived as scattered fields on the service structs: - LeaseManager owns its own mutex and closes the availability-vs-insert TOCTOU by checking export busy inside Issue under the same lock as the insert. - DataSession gives the three per-import data-plane implementations (Linux kernel handoff, Darwin server data session, Darwin virtual controller) a uniform Done/Err/Close interface. - clientAssignment encapsulates the matched/import-all target state and exposes ApplyMatched/ApplyAll diffs to ClientService, which keeps worker goroutine lifecycle. Service busy tracking moves off the per-platform serverExport struct onto ServerService.busy, since it follows the lease/import lifecycle rather than physical claim/release. linux_test.go is migrated to construct ServerService and ClientService through the new host interfaces.
81 lines
1.7 KiB
Go
81 lines
1.7 KiB
Go
//go:build linux
|
|
|
|
package usbip
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/sagernet/sing-box/log"
|
|
"github.com/sagernet/sing-box/option"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
func newPlatformExportHost(logger log.ContextLogger, matches []option.USBIPDeviceMatch) (ExportHost, error) {
|
|
return newLinuxExportHost(logger, matches, systemUSBIPOps), nil
|
|
}
|
|
|
|
func newPlatformImportHost(logger log.ContextLogger) (ImportHost, error) {
|
|
return newLinuxImportHost(logger, systemUSBIPOps), nil
|
|
}
|
|
|
|
func linuxStableID(d sysfsDevice) string {
|
|
if d.Serial != "" {
|
|
return fmt.Sprintf("usb:%04x:%04x:%s", d.VendorID, d.ProductID, d.Serial)
|
|
}
|
|
return "linux-busid:" + d.BusID
|
|
}
|
|
|
|
func linuxUSBIPStatusState(status int) string {
|
|
switch status {
|
|
case usbipStatusAvailable:
|
|
return deviceStateAvailable
|
|
case usbipStatusUsed:
|
|
return deviceStateBusy
|
|
default:
|
|
return deviceStateUnavailable
|
|
}
|
|
}
|
|
|
|
func linuxUSBIPStatusReason(status int) string {
|
|
switch status {
|
|
case usbipStatusAvailable:
|
|
return "available"
|
|
case usbipStatusUsed:
|
|
return "used"
|
|
case usbipStatusError:
|
|
return "error"
|
|
default:
|
|
return fmt.Sprintf("status=0x%08x", uint32(status))
|
|
}
|
|
}
|
|
|
|
func sysBusDevicePath(busid string) string {
|
|
return sysBusUSBDevices + "/" + busid
|
|
}
|
|
|
|
func isVHCIImportedDevice(path string) bool {
|
|
if strings.Contains(path, "vhci_hcd") {
|
|
return true
|
|
}
|
|
realPath, err := filepath.EvalSymlinks(path)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return strings.Contains(realPath, "vhci_hcd")
|
|
}
|
|
|
|
func isMissingUSBDeviceError(err error) bool {
|
|
return errors.Is(err, unix.ENOENT) || errors.Is(err, unix.ENODEV)
|
|
}
|
|
|
|
func driverOrNone(d string) string {
|
|
if d == "" {
|
|
return "(no driver)"
|
|
}
|
|
return d
|
|
}
|