Skip to content
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

Merged
merged 1 commit into from
Jun 19, 2023

Conversation

pelwell
Copy link
Member

@pelwell pelwell commented Jun 19, 2023

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.

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`; \
Copy link
Contributor

@MichaIng MichaIng Jun 19, 2023

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)?

Suggested change
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

Copy link
Member Author

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?

Copy link
Contributor

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

Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better?

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 \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last:

Suggested change
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 🙂.

Copy link
Member Author

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.
@pelwell pelwell merged commit 8f1815e into RPi-Distro:master Jun 19, 2023
MichaIng added a commit to MichaIng/DietPi that referenced this pull request Jun 21, 2023
- Raspberry Pi | Update serial console udev rules: RPi-Distro/raspberrypi-sys-mods#76
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants