/
unix_hotplug.go
219 lines (177 loc) · 5.96 KB
/
unix_hotplug.go
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// +build linux,cgo
package device
import (
"fmt"
"strings"
udev "github.com/farjump/go-libudev"
deviceConfig "github.com/lxc/lxd/lxd/device/config"
"github.com/lxc/lxd/lxd/instance"
"github.com/lxc/lxd/lxd/instance/instancetype"
"github.com/lxc/lxd/shared"
)
// unixHotplugIsOurDevice indicates whether the unixHotplug device event qualifies as part of our device.
// This function is not defined against the unixHotplug struct type so that it can be used in event
// callbacks without needing to keep a reference to the unixHotplug device struct.
func unixHotplugIsOurDevice(config deviceConfig.Device, unixHotplug *UnixHotplugEvent) bool {
// Check if event matches criteria for this device, if not return.
if (config["vendorid"] != "" && config["vendorid"] != unixHotplug.Vendor) || (config["productid"] != "" && config["productid"] != unixHotplug.Product) {
return false
}
return true
}
type unixHotplug struct {
deviceCommon
}
// isRequired indicates whether the device config requires this device to start OK.
func (d *unixHotplug) isRequired() bool {
// Defaults to not required.
if shared.IsTrue(d.config["required"]) {
return true
}
return false
}
// validateConfig checks the supplied config for correctness.
func (d *unixHotplug) validateConfig(instConf instance.ConfigReader) error {
if !instanceSupported(instConf.Type(), instancetype.Container) {
return ErrUnsupportedDevType
}
rules := map[string]func(string) error{
"vendorid": shared.IsDeviceID,
"productid": shared.IsDeviceID,
"uid": unixValidUserID,
"gid": unixValidUserID,
"mode": unixValidOctalFileMode,
"required": shared.IsBool,
}
err := d.config.Validate(rules)
if err != nil {
return err
}
if d.config["vendorid"] == "" && d.config["productid"] == "" {
return fmt.Errorf("Unix hotplug devices require a vendorid or a productid")
}
return nil
}
// Register is run after the device is started or when LXD starts.
func (d *unixHotplug) Register() error {
// Extract variables needed to run the event hook so that the reference to this device
// struct is not needed to be kept in memory.
devicesPath := d.inst.DevicesPath()
devConfig := d.config
deviceName := d.name
state := d.state
// Handler for when a UnixHotplug event occurs.
f := func(e UnixHotplugEvent) (*deviceConfig.RunConfig, error) {
runConf := deviceConfig.RunConfig{}
if e.Action == "add" {
if !unixHotplugIsOurDevice(devConfig, &e) {
return nil, nil
}
if e.Subsystem == "block" {
err := unixDeviceSetupBlockNum(state, devicesPath, "unix", deviceName, devConfig, e.Major, e.Minor, e.Path, false, &runConf)
if err != nil {
return nil, err
}
} else {
err := unixDeviceSetupCharNum(state, devicesPath, "unix", deviceName, devConfig, e.Major, e.Minor, e.Path, false, &runConf)
if err != nil {
return nil, err
}
}
} else if e.Action == "remove" {
relativeTargetPath := strings.TrimPrefix(e.Path, "/")
err := unixDeviceRemove(devicesPath, "unix", deviceName, relativeTargetPath, &runConf)
if err != nil {
return nil, err
}
// Add a post hook function to remove the specific unix hotplug device file after unmount.
runConf.PostHooks = []func() error{func() error {
err := unixDeviceDeleteFiles(state, devicesPath, "unix", deviceName, relativeTargetPath)
if err != nil {
return fmt.Errorf("Failed to delete files for device '%s': %v", deviceName, err)
}
return nil
}}
}
runConf.Uevents = append(runConf.Uevents, e.UeventParts)
return &runConf, nil
}
unixHotplugRegisterHandler(d.inst, d.name, f)
return nil
}
// Start is run when the device is added to the instance
func (d *unixHotplug) Start() (*deviceConfig.RunConfig, error) {
runConf := deviceConfig.RunConfig{}
runConf.PostHooks = []func() error{d.Register}
device := d.loadUnixDevice()
if d.isRequired() && device == nil {
return nil, fmt.Errorf("Required Unix Hotplug device not found")
}
if device == nil {
return &runConf, nil
}
devnum := device.Devnum()
major := uint32(devnum.Major())
minor := uint32(devnum.Minor())
// setup device
var err error
if device.Subsystem() == "block" {
err = unixDeviceSetupBlockNum(d.state, d.inst.DevicesPath(), "unix", d.name, d.config, major, minor, device.Devnode(), false, &runConf)
} else {
err = unixDeviceSetupCharNum(d.state, d.inst.DevicesPath(), "unix", d.name, d.config, major, minor, device.Devnode(), false, &runConf)
}
if err != nil {
return nil, err
}
return &runConf, nil
}
// Stop is run when the device is removed from the instance
func (d *unixHotplug) Stop() (*deviceConfig.RunConfig, error) {
unixHotplugUnregisterHandler(d.inst, d.name)
runConf := deviceConfig.RunConfig{
PostHooks: []func() error{d.postStop},
}
err := unixDeviceRemove(d.inst.DevicesPath(), "unix", d.name, "", &runConf)
if err != nil {
return nil, err
}
return &runConf, nil
}
// postStop is run after the device is removed from the instance
func (d *unixHotplug) postStop() error {
err := unixDeviceDeleteFiles(d.state, d.inst.DevicesPath(), "unix", d.name, "")
if err != nil {
return fmt.Errorf("Failed to delete files for device '%s': %v", d.name, err)
}
return nil
}
// loadUnixDevice scans the host machine for unix devices with matching product/vendor ids
// and returns the first matching device with the subsystem type char or block
func (d *unixHotplug) loadUnixDevice() *udev.Device {
// Find device if exists
u := udev.Udev{}
e := u.NewEnumerate()
if d.config["vendorid"] != "" {
e.AddMatchProperty("ID_VENDOR_ID", d.config["vendorid"])
}
if d.config["productid"] != "" {
e.AddMatchProperty("ID_MODEL_ID", d.config["productid"])
}
e.AddMatchIsInitialized()
devices, _ := e.Devices()
var device *udev.Device
for i := range devices {
device = devices[i]
devnum := device.Devnum()
if devnum.Major() == 0 || devnum.Minor() == 0 {
continue
}
if device.Devnode() == "" {
continue
}
if !strings.HasPrefix(device.Subsystem(), "usb") {
return device
}
}
return nil
}