Skip to content

Commit a0f6d01

Browse files
authored
fix(usbgadget): do not panic if a change isn't found (#481)
* fix(usbgadget): do not panic if a change isn't found * chore(usbgadget): rebind usb after updating config
1 parent b4dd496 commit a0f6d01

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

internal/usbgadget/changeset_resolver.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ func (c *ChangeSetResolver) doResolveChanges(initial bool) error {
4848

4949
for _, key := range c.orderedChanges {
5050
change := c.changesMap[key.(string)]
51+
if change == nil {
52+
c.l.Error().Str("key", key.(string)).Msg("fileChange not found")
53+
continue
54+
}
55+
5156
if !initial {
5257
change.ResetActionResolution()
5358
}
@@ -123,7 +128,11 @@ func (c *ChangeSetResolver) applyChanges() error {
123128

124129
err := c.changeset.applyChange(change)
125130
if err != nil {
126-
return err
131+
if change.IgnoreErrors {
132+
c.l.Warn().Str("change", change.String()).Err(err).Msg("ignoring error")
133+
} else {
134+
return err
135+
}
127136
}
128137
}
129138

internal/usbgadget/config.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,7 @@ func (u *UsbGadget) Init() error {
177177

178178
u.udc = udcs[0]
179179

180-
err := u.WithTransaction(func() error {
181-
u.tx.MountConfigFS()
182-
u.tx.CreateConfigPath()
183-
u.tx.WriteGadgetConfig()
184-
return nil
185-
})
180+
err := u.configureUsbGadget(false)
186181
if err != nil {
187182
return u.logError("unable to initialize USB stack", err)
188183
}
@@ -196,13 +191,22 @@ func (u *UsbGadget) UpdateGadgetConfig() error {
196191

197192
u.loadGadgetConfig()
198193

199-
err := u.WithTransaction(func() error {
200-
u.tx.WriteGadgetConfig()
201-
return nil
202-
})
194+
err := u.configureUsbGadget(true)
203195
if err != nil {
204196
return u.logError("unable to update gadget config", err)
205197
}
206198

207199
return nil
208200
}
201+
202+
func (u *UsbGadget) configureUsbGadget(resetUsb bool) error {
203+
return u.WithTransaction(func() error {
204+
u.tx.MountConfigFS()
205+
u.tx.CreateConfigPath()
206+
u.tx.WriteGadgetConfig()
207+
if resetUsb {
208+
u.tx.RebindUsb(true)
209+
}
210+
return nil
211+
})
212+
}

internal/usbgadget/config_tx.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ func (tx *UsbGadgetTransaction) WriteUDC() {
319319
// bound the gadget to a UDC (USB Device Controller)
320320
path := path.Join(tx.kvmGadgetPath, "UDC")
321321
tx.addFileChange("udc", RequestedFileChange{
322+
Key: "udc",
322323
Path: path,
323324
ExpectedState: FileStateFileContentMatch,
324325
ExpectedContent: []byte(tx.udc),
@@ -334,6 +335,8 @@ func (tx *UsbGadgetTransaction) RebindUsb(ignoreUnbindError bool) {
334335
ExpectedState: FileStateFileWrite,
335336
ExpectedContent: []byte(tx.udc),
336337
Description: "unbind UDC",
338+
DependsOn: []string{"udc"},
339+
IgnoreErrors: ignoreUnbindError,
337340
})
338341
// bind the gadget to the UDC
339342
tx.addFileChange("udc", RequestedFileChange{
@@ -342,6 +345,5 @@ func (tx *UsbGadgetTransaction) RebindUsb(ignoreUnbindError bool) {
342345
ExpectedContent: []byte(tx.udc),
343346
Description: "bind UDC",
344347
DependsOn: []string{path.Join(tx.dwc3Path, "unbind")},
345-
IgnoreErrors: ignoreUnbindError,
346348
})
347349
}

0 commit comments

Comments
 (0)