gitee.com/mysnapcore/mysnapd@v0.1.0/interfaces/builtin/serial_port.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016-2017 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package builtin 21 22 import ( 23 "fmt" 24 "path/filepath" 25 "regexp" 26 "strconv" 27 "strings" 28 29 "gitee.com/mysnapcore/mysnapd/interfaces" 30 "gitee.com/mysnapcore/mysnapd/interfaces/apparmor" 31 "gitee.com/mysnapcore/mysnapd/interfaces/hotplug" 32 "gitee.com/mysnapcore/mysnapd/interfaces/udev" 33 "gitee.com/mysnapcore/mysnapd/snap" 34 ) 35 36 const serialPortSummary = `allows accessing a specific serial port` 37 38 const serialPortBaseDeclarationSlots = ` 39 serial-port: 40 allow-installation: 41 slot-snap-type: 42 - core 43 - gadget 44 deny-auto-connection: true 45 ` 46 47 // serialPortInterface is the type for serial port interfaces. 48 type serialPortInterface struct{} 49 50 // Name of the serial-port interface. 51 func (iface *serialPortInterface) Name() string { 52 return "serial-port" 53 } 54 55 func (iface *serialPortInterface) StaticInfo() interfaces.StaticInfo { 56 return interfaces.StaticInfo{ 57 Summary: serialPortSummary, 58 BaseDeclarationSlots: serialPortBaseDeclarationSlots, 59 } 60 } 61 62 func (iface *serialPortInterface) String() string { 63 return iface.Name() 64 } 65 66 // Pattern to match allowed serial device nodes, path attributes will be 67 // compared to this for validity when not using udev identification 68 // Known device node patterns we need to support 69 // - ttyUSBX (UART over USB devices) 70 // - ttyACMX (ACM modem devices ) 71 // - ttyXRUSBx (Exar Corp. USB UART devices) 72 // - ttySX (UART serial ports) 73 // - ttyOX (UART serial ports on ARM) 74 // - ttymxcX (serial ports on i.mx6UL) 75 // - ttySCX (NXP SC16IS7xx serial devices) 76 // - ttyMSMX (Qualcomm msm7x serial devices) 77 // - ttyHSX (Qualcomm GENI based QTI serial cores) 78 // - ttyGSX (USB gadget serial devices) 79 var serialDeviceNodePattern = regexp.MustCompile("^/dev/tty(mxc|USB|ACM|AMA|XRUSB|S|O|SC|MSM|HS|GS)[0-9]+$") 80 81 // Pattern that is considered valid for the udev symlink to the serial device, 82 // path attributes will be compared to this for validity when usb vid and pid 83 // are also specified 84 var serialUDevSymlinkPattern = regexp.MustCompile("^/dev/serial-port-[a-z0-9]+$") 85 86 // BeforePrepareSlot checks validity of the defined slot 87 func (iface *serialPortInterface) BeforePrepareSlot(slot *snap.SlotInfo) error { 88 // Check slot has a path attribute identify serial device 89 path, ok := slot.Attrs["path"].(string) 90 if !ok || path == "" { 91 return fmt.Errorf("serial-port slot must have a path attribute") 92 } 93 94 // XXX: this interface feeds the cleaned path into the regex and is 95 // left unchanged here for historical reasons. New interfaces (eg, 96 // like raw-volume) should instead use verifySlotPathAttribute() which 97 // performs additional verification. 98 path = filepath.Clean(path) 99 100 if iface.hasUsbAttrs(slot) { 101 // Must be path attribute where symlink will be placed and usb vendor and product identifiers 102 // Check the path attribute is in the allowable pattern 103 if !serialUDevSymlinkPattern.MatchString(path) { 104 return fmt.Errorf("serial-port path attribute specifies invalid symlink location") 105 } 106 107 usbVendor, vOk := slot.Attrs["usb-vendor"].(int64) 108 if !vOk { 109 return fmt.Errorf("serial-port slot failed to find usb-vendor attribute") 110 } 111 if (usbVendor < 0x1) || (usbVendor > 0xFFFF) { 112 return fmt.Errorf("serial-port usb-vendor attribute not valid: %d", usbVendor) 113 } 114 115 usbProduct, pOk := slot.Attrs["usb-product"].(int64) 116 if !pOk { 117 return fmt.Errorf("serial-port slot failed to find usb-product attribute") 118 } 119 if (usbProduct < 0x0) || (usbProduct > 0xFFFF) { 120 return fmt.Errorf("serial-port usb-product attribute not valid: %d", usbProduct) 121 } 122 123 usbInterfaceNumber, ok := slot.Attrs["usb-interface-number"].(int64) 124 if ok && (usbInterfaceNumber < 0 || usbInterfaceNumber >= UsbMaxInterfaces) { 125 return fmt.Errorf("serial-port usb-interface-number attribute cannot be negative or larger than %d", UsbMaxInterfaces-1) 126 } 127 } else { 128 // Just a path attribute - must be a valid usb device node 129 // Check the path attribute is in the allowable pattern 130 if !serialDeviceNodePattern.MatchString(path) { 131 return fmt.Errorf("serial-port path attribute must be a valid device node") 132 } 133 } 134 return nil 135 } 136 137 func (iface *serialPortInterface) UDevPermanentSlot(spec *udev.Specification, slot *snap.SlotInfo) error { 138 var usbVendor, usbProduct, usbInterfaceNumber int64 139 var path string 140 if err := slot.Attr("usb-vendor", &usbVendor); err != nil { 141 return nil 142 } 143 if err := slot.Attr("usb-product", &usbProduct); err != nil { 144 return nil 145 } 146 if err := slot.Attr("path", &path); err != nil || path == "" { 147 return nil 148 } 149 if err := slot.Attr("usb-interface-number", &usbInterfaceNumber); err == nil { 150 spec.AddSnippet(fmt.Sprintf(`# serial-port 151 IMPORT{builtin}="usb_id" 152 SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTRS{idVendor}=="%04x", ATTRS{idProduct}=="%04x", ENV{ID_USB_INTERFACE_NUM}=="%02x", SYMLINK+="%s"`, usbVendor, usbProduct, usbInterfaceNumber, strings.TrimPrefix(path, "/dev/"))) 153 } else { 154 spec.AddSnippet(fmt.Sprintf(`# serial-port 155 IMPORT{builtin}="usb_id" 156 SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTRS{idVendor}=="%04x", ATTRS{idProduct}=="%04x", SYMLINK+="%s"`, usbVendor, usbProduct, strings.TrimPrefix(path, "/dev/"))) 157 } 158 return nil 159 } 160 161 func (iface *serialPortInterface) AppArmorConnectedPlug(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 162 if iface.hasUsbAttrs(slot) { 163 // This apparmor rule is an approximation of serialDeviceNodePattern 164 // (AARE is different than regex, so we must approximate). 165 // UDev tagging and device cgroups will restrict down to the specific device 166 spec.AddSnippet("/dev/tty[A-Z]*[0-9] rwk,") 167 return nil 168 } 169 170 // Path to fixed device node 171 var path string 172 if err := slot.Attr("path", &path); err != nil { 173 return nil 174 } 175 cleanedPath := filepath.Clean(path) 176 spec.AddSnippet(fmt.Sprintf("%s rwk,", cleanedPath)) 177 return nil 178 } 179 180 func (iface *serialPortInterface) UDevConnectedPlug(spec *udev.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 181 // For connected plugs, we use vendor and product ids if available, 182 // otherwise add the kernel device 183 hasOnlyPath := !iface.hasUsbAttrs(slot) 184 var usbVendor, usbProduct int64 185 var path string 186 if err := slot.Attr("usb-vendor", &usbVendor); err != nil && !hasOnlyPath { 187 return nil 188 } 189 if err := slot.Attr("usb-product", &usbProduct); err != nil && !hasOnlyPath { 190 return nil 191 } 192 if err := slot.Attr("path", &path); err != nil && hasOnlyPath { 193 return nil 194 } 195 196 if hasOnlyPath { 197 spec.TagDevice(fmt.Sprintf(`SUBSYSTEM=="tty", KERNEL=="%s"`, strings.TrimPrefix(path, "/dev/"))) 198 } else { 199 var usbInterfaceNumber int64 200 if err := slot.Attr("usb-interface-number", &usbInterfaceNumber); err == nil { 201 spec.TagDevice(fmt.Sprintf(`IMPORT{builtin}="usb_id" 202 SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTRS{idVendor}=="%04x", ATTRS{idProduct}=="%04x", ENV{ID_USB_INTERFACE_NUM}=="%02x"`, usbVendor, usbProduct, usbInterfaceNumber)) 203 } else { 204 spec.TagDevice(fmt.Sprintf(`IMPORT{builtin}="usb_id" 205 SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTRS{idVendor}=="%04x", ATTRS{idProduct}=="%04x"`, usbVendor, usbProduct)) 206 } 207 } 208 return nil 209 } 210 211 func (iface *serialPortInterface) AutoConnect(*snap.PlugInfo, *snap.SlotInfo) bool { 212 // allow what declarations allowed 213 return true 214 } 215 216 func (iface *serialPortInterface) HotplugDeviceDetected(di *hotplug.HotplugDeviceInfo) (*hotplug.ProposedSlot, error) { 217 bus, _ := di.Attribute("ID_BUS") 218 if di.Subsystem() != "tty" || bus != "usb" || !serialDeviceNodePattern.MatchString(di.DeviceName()) { 219 return nil, nil 220 } 221 222 slot := hotplug.ProposedSlot{ 223 Attrs: map[string]interface{}{ 224 "path": di.DeviceName(), 225 }, 226 } 227 if vendor, ok := di.Attribute("ID_VENDOR_ID"); ok { 228 slot.Attrs["usb-vendor"] = vendor 229 } 230 if product, ok := di.Attribute("ID_MODEL_ID"); ok { 231 slot.Attrs["usb-product"] = product 232 } 233 return &slot, nil 234 } 235 236 func slotDeviceAttrEqual(di *hotplug.HotplugDeviceInfo, devinfoAttribute string, slotAttributeValue int64) bool { 237 var attr string 238 var ok bool 239 if attr, ok = di.Attribute(devinfoAttribute); !ok { 240 return false 241 } 242 val, err := strconv.ParseInt(attr, 16, 64) 243 return err == nil && val == slotAttributeValue 244 } 245 246 func (iface *serialPortInterface) HandledByGadget(di *hotplug.HotplugDeviceInfo, slot *snap.SlotInfo) bool { 247 // if the slot has vendor, product and interface number set, check if they match 248 var usbVendor, usbProduct, usbInterfaceNumber int64 249 if err := slot.Attr("usb-vendor", &usbVendor); err == nil { 250 if !slotDeviceAttrEqual(di, "ID_VENDOR_ID", usbVendor) { 251 return false 252 } 253 if err := slot.Attr("usb-product", &usbProduct); err != nil { 254 return false 255 } 256 if !slotDeviceAttrEqual(di, "ID_MODEL_ID", usbProduct) { 257 return false 258 } 259 if err := slot.Attr("usb-interface-number", &usbInterfaceNumber); err == nil { 260 if !slotDeviceAttrEqual(di, "ID_USB_INTERFACE_NUM", usbInterfaceNumber) { 261 return false 262 } 263 } 264 return true 265 } 266 267 var path string 268 if err := slot.Attr("path", &path); err != nil { 269 return false 270 } 271 return di.DeviceName() == path 272 } 273 274 func (iface *serialPortInterface) hasUsbAttrs(attrs interfaces.Attrer) bool { 275 var v int64 276 if err := attrs.Attr("usb-vendor", &v); err == nil { 277 return true 278 } 279 if err := attrs.Attr("usb-product", &v); err == nil { 280 return true 281 } 282 if err := attrs.Attr("usb-interface-number", &v); err == nil { 283 return true 284 } 285 return false 286 } 287 288 func init() { 289 registerIface(&serialPortInterface{}) 290 }