Merge lp:~c-lobrano/snappy/hw-assign-fix-overwrite-udev-rule into lp:~snappy-dev/snappy/snappy-moved-to-github

Proposed by Carlo Lobrano
Status: Merged
Approved by: John Lenton
Approved revision: 719
Merged at revision: 718
Proposed branch: lp:~c-lobrano/snappy/hw-assign-fix-overwrite-udev-rule
Merge into: lp:~snappy-dev/snappy/snappy-moved-to-github
Diff against target: 159 lines (+87/-9)
3 files modified
.bzrignore (+3/-0)
snappy/hwaccess.go (+71/-9)
snappy/hwaccess_test.go (+13/-0)
To merge this branch: bzr merge lp:~c-lobrano/snappy/hw-assign-fix-overwrite-udev-rule
Reviewer Review Type Date Requested Status
John Lenton (community) Approve
Review via email: mp+272000@code.launchpad.net

Commit message

Description of the changes:

- hw-assign now appends each new udev rule for snap to the existing file, if any, in place of overwriting the same file
- hw-unassign now removes only the single udev rule related to the device unassigned, in place of just deleting the file

Description of the change

Proposal fix for Bug #1497299

Please let me know if this fix can be improved somehow or if I misinterpreted something.

Best regards,
Carlo Lobrano

To post a comment you must log in.
Revision history for this message
John Lenton (chipaca) wrote :

Thank you for this!

There's one thing that needs addressing, inline below. Also some stylistic nits for you to consider.

review: Needs Fixing
Revision history for this message
Carlo Lobrano (c-lobrano) :
Revision history for this message
John Lenton (chipaca) wrote :

Better!

Still two problems in the code. One in a comment below. The other is from golint:

snappy/hwaccess.go:119:35: don't use underscores in Go names; func parameter new_rule should be newRule
snappy/hwaccess.go:135:2: don't use underscores in Go names; var updated_rules should be updatedRules
snappy/hwaccess.go:238:3: don't use underscores in Go names; var device_pattern should be devicePattern

look at .precommit in the project root for help in having golint run automatically for you.

Revision history for this message
Carlo Lobrano (c-lobrano) :
Revision history for this message
John Lenton (chipaca) wrote :

Good. Thank you!

review: Approve
Revision history for this message
John Lenton (chipaca) :
Revision history for this message
Carlo Lobrano (c-lobrano) :
Revision history for this message
Snappy Tarmac (snappydevtarmac) wrote :

There are additional revisions which have not been approved in review. Please seek review and approval of these new revisions.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file '.bzrignore'
2--- .bzrignore 2015-08-25 16:36:01 +0000
3+++ .bzrignore 2015-09-25 15:00:57 +0000
4@@ -1,2 +1,5 @@
5 _integration-tests/bin/
6 _integration-tests/data/output/
7+./share
8+./snappy/hello_1.0.1_all.snap
9+./tags
10
11=== modified file 'snappy/hwaccess.go'
12--- snappy/hwaccess.go 2015-09-15 12:55:09 +0000
13+++ snappy/hwaccess.go 2015-09-25 15:00:57 +0000
14@@ -20,6 +20,7 @@
15 package snappy
16
17 import (
18+ "bufio"
19 "encoding/json"
20 "fmt"
21 "io/ioutil"
22@@ -115,6 +116,26 @@
23 return filepath.Join(snapUdevRulesDir, fmt.Sprintf("70-snappy_hwassign_%s.rules", partid))
24 }
25
26+func addUdevRuleForSnap(snapname, newRule string) error {
27+ udevRulesFile := udevRulesPathForPart(snapname)
28+
29+ rules, err := ioutil.ReadFile(udevRulesFile)
30+ if nil != err && !os.IsNotExist(err) {
31+ return err
32+ }
33+
34+ // At this point either rules variable contains some rules if the
35+ // file exists, or it is nil if the file does not exist yet.
36+ // In both cases, updatedRules will have the right content.
37+ updatedRules := append(rules, newRule...)
38+
39+ if err := helpers.AtomicWriteFile(udevRulesFile, updatedRules, 0644); nil != err {
40+ return err
41+ }
42+
43+ return nil
44+}
45+
46 func writeUdevRuleForDeviceCgroup(snapname, device string) error {
47 os.MkdirAll(snapUdevRulesDir, 0755)
48
49@@ -130,7 +151,7 @@
50 KERNEL=="%v", TAG:="snappy-assign", ENV{SNAPPY_APP}:="%s"
51 `, filepath.Base(device), snapname)
52
53- if err := ioutil.WriteFile(udevRulesPathForPart(snapname), []byte(acl), 0644); err != nil {
54+ if err := addUdevRuleForSnap(snapname, acl); err != nil {
55 return err
56 }
57
58@@ -197,6 +218,49 @@
59 return appArmorAdditional.WritePath, nil
60 }
61
62+func removeUdevRuleForSnap(snapname, device string) error {
63+ udevRulesFile := udevRulesPathForPart(snapname)
64+
65+ file, err := os.Open(udevRulesFile)
66+ if nil != err && !os.IsNotExist(err) {
67+ return err
68+ }
69+
70+ // Get the full list of rules to keep
71+ var rulesToKeep []string
72+ scanner := bufio.NewScanner(file)
73+ devicePattern := "\"" + filepath.Base(device) + "\""
74+
75+ for scanner.Scan() {
76+ rule := scanner.Text()
77+ if "" != rule && !strings.Contains(rule, devicePattern) {
78+ rulesToKeep = append(rulesToKeep, rule)
79+ }
80+ }
81+ file.Close()
82+
83+ // Update the file with the remaining rules or delete it
84+ // if there is not any rule left.
85+ if 0 < len(rulesToKeep) {
86+ // Appending the []string list of rules in a single
87+ // string to convert it later in []byte
88+ var out string
89+ for _, rule := range rulesToKeep {
90+ out = out + rule + "\n"
91+ }
92+
93+ if err := helpers.AtomicWriteFile(udevRulesFile, []byte(out), 0644); nil != err {
94+ return err
95+ }
96+ } else {
97+ if err := os.Remove(udevRulesFile); nil != err {
98+ return err
99+ }
100+ }
101+
102+ return nil
103+}
104+
105 // RemoveHWAccess allows the given snap package to access the given hardware
106 // device
107 func RemoveHWAccess(snapname, device string) error {
108@@ -227,14 +291,12 @@
109 return err
110 }
111
112- udevRulesFile := udevRulesPathForPart(snapname)
113- if helpers.FileExists(udevRulesFile) {
114- if err := os.Remove(udevRulesFile); err != nil {
115- return err
116- }
117- if err := activateOemHardwareUdevRules(); err != nil {
118- return err
119- }
120+ if err = removeUdevRuleForSnap(snapname, device); nil != err {
121+ return err
122+ }
123+
124+ if err := activateOemHardwareUdevRules(); err != nil {
125+ return err
126 }
127
128 // re-generate apparmor rules
129
130=== modified file 'snappy/hwaccess_test.go'
131--- snappy/hwaccess_test.go 2015-09-15 12:55:09 +0000
132+++ snappy/hwaccess_test.go 2015-09-25 15:00:57 +0000
133@@ -209,6 +209,14 @@
134 }
135 `)
136
137+ // check the udev rule file contains all the rules
138+ content, err = ioutil.ReadFile(filepath.Join(snapUdevRulesDir, "70-snappy_hwassign_hello-app.rules"))
139+ c.Assert(err, IsNil)
140+ c.Assert(string(content), Equals, `
141+KERNEL=="bar", TAG:="snappy-assign", ENV{SNAPPY_APP}:="hello-app"
142+
143+KERNEL=="bar*", TAG:="snappy-assign", ENV{SNAPPY_APP}:="hello-app"
144+`)
145 // remove
146 err = RemoveHWAccess("hello-app", "/dev/bar")
147 c.Assert(err, IsNil)
148@@ -229,6 +237,11 @@
149 ]
150 }
151 `)
152+ // check the udevReadGlob Udev rule is still there
153+ content, err = ioutil.ReadFile(filepath.Join(snapUdevRulesDir, "70-snappy_hwassign_hello-app.rules"))
154+ c.Assert(err, IsNil)
155+ c.Assert(string(content), Equals, `KERNEL=="bar*", TAG:="snappy-assign", ENV{SNAPPY_APP}:="hello-app"
156+`)
157 }
158
159 func makeRunUdevAdmMock(a *[][]string) func(args ...string) error {

Subscribers

People subscribed via source and target branches