github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/interfaces/builtin/i2c.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016 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 "strings" 27 28 "github.com/snapcore/snapd/interfaces" 29 "github.com/snapcore/snapd/interfaces/apparmor" 30 "github.com/snapcore/snapd/interfaces/udev" 31 "github.com/snapcore/snapd/snap" 32 ) 33 34 const i2cSummary = `allows access to specific I2C controller` 35 36 const i2cBaseDeclarationSlots = ` 37 i2c: 38 allow-installation: 39 slot-snap-type: 40 - gadget 41 - core 42 deny-auto-connection: true 43 ` 44 45 const i2cConnectedPlugAppArmorPath = ` 46 # Description: Can access I2C controller 47 48 %s rw, 49 ` 50 51 const i2cConnectedPlugAppArmorSysfsName = ` 52 # Description: Can access I2C sysfs name 53 54 /sys/bus/i2c/devices/%s/** rw, 55 ` 56 57 // The type for i2c interface 58 type i2cInterface struct{} 59 60 // Getter for the name of the i2c interface 61 func (iface *i2cInterface) Name() string { 62 return "i2c" 63 } 64 65 func (iface *i2cInterface) StaticInfo() interfaces.StaticInfo { 66 return interfaces.StaticInfo{ 67 Summary: i2cSummary, 68 BaseDeclarationSlots: i2cBaseDeclarationSlots, 69 } 70 } 71 72 func (iface *i2cInterface) String() string { 73 return iface.Name() 74 } 75 76 // Pattern to match allowed i2c device nodes. It is gonna be used to check the 77 // validity of the path attributes in case the udev is not used for 78 // identification 79 var i2cControlDeviceNodePattern = regexp.MustCompile("^/dev/i2c-[0-9]+$") 80 81 // Pattern to match allowed i2c sysfs names. 82 var i2cValidSysfsName = regexp.MustCompile("^[a-zA-Z0-9_-]+$") 83 84 // Check validity of the defined slot 85 func (iface *i2cInterface) BeforePrepareSlot(slot *snap.SlotInfo) error { 86 sysfsName, ok := slot.Attrs["sysfs-name"].(string) 87 if ok { 88 if !i2cValidSysfsName.MatchString(sysfsName) { 89 return fmt.Errorf("%s sysfs-name attribute must be a valid sysfs-name", iface.Name()) 90 } 91 if _, ok := slot.Attrs["path"].(string); ok { 92 return fmt.Errorf("%s slot can only use path or sysfs-name", iface.Name()) 93 } 94 return nil 95 } 96 97 // Validate the path 98 path, ok := slot.Attrs["path"].(string) 99 if !ok || path == "" { 100 return fmt.Errorf("%s slot must have a path or sysfs-name attribute", iface.Name()) 101 } 102 // XXX: this interface feeds the cleaned path into the regex and is 103 // left unchanged here for historical reasons. New interfaces (eg, 104 // like raw-volume) should instead use verifySlotPathAttribute() which 105 // performs additional verification. 106 path = filepath.Clean(path) 107 108 if !i2cControlDeviceNodePattern.MatchString(path) { 109 return fmt.Errorf("%s path attribute must be a valid device node", iface.Name()) 110 } 111 112 return nil 113 } 114 115 func (iface *i2cInterface) AppArmorConnectedPlug(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 116 117 // check if sysfsName is set and if so stop after that 118 var sysfsName string 119 if err := slot.Attr("sysfs-name", &sysfsName); err == nil { 120 spec.AddSnippet(fmt.Sprintf(i2cConnectedPlugAppArmorSysfsName, sysfsName)) 121 return nil 122 } 123 124 // do path if sysfsName is not set (they can't be set both) 125 var path string 126 if err := slot.Attr("path", &path); err != nil { 127 return nil 128 } 129 130 cleanedPath := filepath.Clean(path) 131 spec.AddSnippet(fmt.Sprintf(i2cConnectedPlugAppArmorPath, cleanedPath)) 132 // Use parametric snippets to avoid no-expr-simplify side-effects. 133 spec.AddParametricSnippet([]string{ 134 "/sys/devices/platform/{*,**.i2c}/i2c-" /* ###PARAM### */, "/** rw, # Add any condensed parametric rules", 135 }, strings.TrimPrefix(path, "/dev/i2c-")) 136 return nil 137 } 138 139 func (iface *i2cInterface) UDevConnectedPlug(spec *udev.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 140 var path string 141 if err := slot.Attr("path", &path); err != nil { 142 return nil 143 } 144 spec.TagDevice(fmt.Sprintf(`KERNEL=="%s"`, strings.TrimPrefix(path, "/dev/"))) 145 return nil 146 } 147 148 func (iface *i2cInterface) AutoConnect(*snap.PlugInfo, *snap.SlotInfo) bool { 149 // Allow what is allowed in the declarations 150 return true 151 } 152 153 func init() { 154 registerIface(&i2cInterface{}) 155 }