Skip to content

Commit 1436b8f

Browse files
authored
python3/usb_scan: Skip empty lines in usb-policy.conf, add more comments (#6616)
Allow empty lines in usb-policy.conf (it's standard practice in similar configuration files to allow both comments and empty lines, and it can help readability quite a bit). Previously, the script would fail in a rather unhelpful manner: Traceback (most recent call last): File "/opt/xensource/libexec/usb_scan.py", line 681, in <module> pusbs = make_pusbs_list(devices, interfaces) File "/opt/xensource/libexec/usb_scan.py", line 660, in make_pusbs_list policy = Policy() File "/opt/xensource/libexec/usb_scan.py", line 384, in __init__ self.parse_line(line) File "/opt/xensource/libexec/usb_scan.py", line 444, in parse_line if action.lower() == "allow": UnboundLocalError: local variable 'action' referenced before assignment See this forum thread for a user figuring this out on their own: https://xcp-ng.org/forum/topic/11091/usb-passthrough-has-stopped-working-after-update-and-updating-usb-policy.conf/ Add some more comments to usb-policy.conf to help debug cases like the above.
2 parents 0d281ff + c7986ad commit 1436b8f

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

python3/libexec/usb_scan.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,10 @@ def parse_line(self, line):
421421
:param line: (str) single line of policy file
422422
:return: None
423423
"""
424+
# 0. skip empty lines
425+
if line.strip() == '':
426+
return
427+
424428
# 1. remove comments
425429
# ^([^#]*)(#.*)?$
426430
i = line.find("#")

python3/tests/test_usb_scan.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,11 @@ def test_usb_config_error_missing_colon(self):
372372
ALLOW # Otherwise allow everything else
373373
"""
374374
self.verify_usb_config_error_common(content, "to unpack")
375+
376+
def test_usb_config_empty_line(self):
377+
content = """# empty line
378+
ALLOW:vid=056a pid=0314 class=03 # Wacom Intuos tablet
379+
380+
ALLOW # Otherwise allow everything else
381+
"""
382+
self.verify_usb_config_error_common(content, "")

scripts/usb-policy.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
# When you change this file, run 'xe pusb-scan' to confirm
22
# the file can be parsed correctly.
3+
# You can also run '/opt/xensource/libexec/usb_scan.py -d' to see
4+
# debug output from the script parsing this configuration file.
35
#
46
# Syntax is an ordered list of case insensitive rules where # is line comment
57
# and each rule is (ALLOW | DENY) : ( match )*
68
# and each match is (class|subclass|prot|vid|pid|rel) = hex-number
79
# Maximum hex value for class/subclass/prot is FF, and for vid/pid/rel is FFFF
810
#
11+
# Rules are ordered so that the first matching rule will override
12+
# any other rules for the device below it
13+
#
914
# USB Hubs (class 09) are always denied, independently of the rules in this file
1015
DENY: vid=17e9 # All DisplayLink USB displays
1116
DENY: class=02 # Communications and CDC-Control

0 commit comments

Comments
 (0)