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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2018 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package hotplug
import (
"fmt"
"path/filepath"
"github.com/snapcore/snapd/dirs"
)
type hotplugDeviceInfoData struct {
// map of all attributes returned for given uevent.
Data map[string]string `json:"data"`
}
// HotplugDeviceInfo carries information about added/removed device detected at runtime.
type HotplugDeviceInfo struct {
hotplugDeviceInfoData
}
// NewHotplugDeviceInfo creates HotplugDeviceInfo structure related to udev add or remove event.
func NewHotplugDeviceInfo(env map[string]string) (*HotplugDeviceInfo, error) {
if _, ok := env["DEVPATH"]; !ok {
return nil, fmt.Errorf("missing device path attribute")
}
return &HotplugDeviceInfo{
hotplugDeviceInfoData: hotplugDeviceInfoData{Data: env},
}, nil
}
// Returns the value of "SUBSYSTEM" attribute of the udev event associated with the device, e.g. "usb".
// Subsystem value is always present.
func (h *HotplugDeviceInfo) Subsystem() string {
return h.Data["SUBSYSTEM"]
}
// Returns full device path under /sysfs, e.g /sys/devices/pci0000:00/0000:00:14.0/usb1/1-2.
// The path is derived from DEVPATH attribute of the udev event.
func (h *HotplugDeviceInfo) DevicePath() string {
// DEVPATH is guaranteed to exist (checked in the ctor).
path, _ := h.Attribute("DEVPATH")
return filepath.Join(dirs.SysfsDir, path)
}
// Returns the value of "MINOR" attribute of the udev event associated with the device.
// The Minor value may be empty.
func (h *HotplugDeviceInfo) Minor() string {
return h.Data["MINOR"]
}
// Returns the value of "MAJOR" attribute of the udev event associated with the device.
// The Major value may be empty.
func (h *HotplugDeviceInfo) Major() string {
return h.Data["MAJOR"]
}
// Returns the value of "DEVNAME" attribute of the udev event associated with the device, e.g. "/dev/ttyUSB0".
// The DeviceName value may be empty.
func (h *HotplugDeviceInfo) DeviceName() string {
return h.Data["DEVNAME"]
}
// Returns the value of "DEVTYPE" attribute of the udev event associated with the device, e.g. "usb_device".
// The DeviceType value may be empty.
func (h *HotplugDeviceInfo) DeviceType() string {
return h.Data["DEVTYPE"]
}
// Generic method for getting arbitrary attribute from the uevent data.
func (h *HotplugDeviceInfo) Attribute(name string) (string, bool) {
val, ok := h.Data[name]
return val, ok
}
func (h *HotplugDeviceInfo) firstAttrValueOf(tryAttrs ...string) string {
for _, attr := range tryAttrs {
if val, _ := h.Attribute(attr); val != "" {
return val
}
}
return ""
}
func (h *HotplugDeviceInfo) String() string {
return h.str(70)
}
// ShortString returns a string representation of the device with more aggressive truncating of model/vendor name.
func (h *HotplugDeviceInfo) ShortString() string {
return h.str(16)
}
func (h *HotplugDeviceInfo) str(maxModelOrVendorLen int) string {
var nameOrPath string
// devname is the name of the device under /dev, eg. /dev/ttyS0;
// prefer devname over devpath as this is the one used to talk to the device.
if nameOrPath = h.DeviceName(); nameOrPath == "" {
// devpath is the path of the device under /sys, eg. /sys/devices/pnp0/00:04/tty/ttyS0.
nameOrPath = h.DevicePath()
}
modelOrVendor := h.firstAttrValueOf("ID_MODEL_FROM_DATABASE", "ID_MODEL", "ID_MODEL_ID", "ID_VENDOR_FROM_DATABASE", "ID_VENDOR", "ID_VENDOR_ID")
if len(modelOrVendor) > maxModelOrVendorLen {
modelOrVendor = modelOrVendor[0:maxModelOrVendorLen] + "…"
}
var serial string
if modelOrVendor != "" {
serial = h.firstAttrValueOf("ID_SERIAL_SHORT", "ID_SERIAL")
} else {
serial = h.firstAttrValueOf("ID_SERIAL", "ID_SERIAL_SHORT")
}
hasSerial := (serial != "" && serial != "noserial")
s := nameOrPath
if modelOrVendor != "" || hasSerial {
s += " ("
if modelOrVendor != "" {
s += modelOrVendor
if hasSerial {
s += "; "
}
}
if hasSerial {
s += "serial: " + serial
}
s += ")"
}
return s
}
|