-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve serial symlink (/dev/serial<n>) creation #76
Conversation
etc.armhf/udev/rules.d/99-com.rules
Outdated
fi \ | ||
KERNEL=="ttyAMA[0-9]*|ttyS[0-9]*", PROGRAM="/bin/sh -c '\ | ||
ALIASES=/proc/device-tree/aliases; \ | ||
TTYNODE=`readlink /sys/class/tty/%k/device/of_node | sed 's/base/:/' | cut -d: -f2`; \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about using modern command substitution syntax (same in other cases below)?
TTYNODE=`readlink /sys/class/tty/%k/device/of_node | sed 's/base/:/' | cut -d: -f2`; \ | |
TTYNODE=$$(readlink /sys/class/tty/%k/device/of_node | sed 's/base/:/' | cut -d: -f2); \ |
Rationale: https://www.shellcheck.net/wiki/SC2006
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you tried that in a udev rule?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah right, the dollar signs need to be escaped with $$
. Just tested on RPi Zero W:
root@Amizero:~# cat /etc/udev/rules.d/test.rules
KERNEL=="wlan0", ACTION=="add", RUN+="/bin/sh -c 'echo $$(echo success) > /root/test'"
root@Amizero:~# cat /root/test
success
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm guessing you haven't, otherwise you'd know it's a bad idea.
Rationale:
Jun 19 14:47:39 raspberrypi systemd-udevd[189]: /etc/udev/rules.d/99-com.rules:35 Invalid value "/bin/sh -c 'ALIASES=/proc/device-tree/aliases; TTYNODE=$(readlink /sys/class/tty/%k/device/of_node | sed 's/base/:/' | cut -d: -f2); if [ -e $$ALIASES/bluetooth ] && [ $$TTYNODE/bluetooth = `strings $$ALIASES/bluetooth` ]; then echo 1; elif [ -e $$ALIASES/console ]; then if [ $$TTYNODE = `strings $$ALIASES/console` ]; then echo 0;else exit 1; fi elif [ $$TTYNODE = `strings $$ALIASES/serial0` ]; then echo 0; elif [ $$TTYNODE = `strings $$ALIASES/serial1` ]; then echo 1; else exit 1; fi '" for PROGRAM (char 56: invalid substitution type), ignoring.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my last post: Double $$
needed just like for variable calls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better?
etc.armhf/udev/rules.d/99-com.rules
Outdated
KERNEL=="ttyAMA[0-9]*|ttyS[0-9]*", PROGRAM="/bin/sh -c '\ | ||
ALIASES=/proc/device-tree/aliases; \ | ||
TTYNODE=$$(readlink /sys/class/tty/%k/device/of_node | sed 's/base/:/' | cut -d: -f2); \ | ||
if [ -e $$ALIASES/bluetooth ] && [ $$TTYNODE/bluetooth = `strings $$ALIASES/bluetooth` ]; then \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last:
if [ -e $$ALIASES/bluetooth ] && [ $$TTYNODE/bluetooth = `strings $$ALIASES/bluetooth` ]; then \ | |
if [ -e $$ALIASES/bluetooth ] && [ $$TTYNODE/bluetooth = $$(strings $$ALIASES/bluetooth) ]; then \ |
A tiny thing, I know. I just can't hold myself when seeing backticks in shell code, aiming for best practice coding 🙂.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be done now.
The kernel change that sets UART indexes based on Device Tree aliases gives predictable UART /dev/tty* names but has the potential to break our downstream use for determining which UART is the console ("serial0") and which is for Blueooth ("serial1"). Instead we use the "console" and "bluetooth" aliases respectively. As a bonus, the improved console matching (based on /sys/class/tty entries) also allows us to handle all UARTs with one rule.
- Raspberry Pi | Update serial console udev rules: RPi-Distro/raspberrypi-sys-mods#76
The kernel change that sets UART indexes based on Device Tree aliases gives predictable UART /dev/tty* names but has the potential to break our downstream use for determining which UART is the console ("serial0") and which is for Blueooth ("serial1"). Instead we use the "console" and "bluetooth" aliases respectively.
As a bonus, the improved console matching (based on /sys/class/tty entries) also allows us to handle all UARTs with one rule.