RDPUser
Posts: 175
Joined: Tue Jan 30, 2018 12:18 pm

Reading from /dev/serial0 changes group from dialout to tty and read permisson is gone

Sun Nov 22, 2020 12:17 am

RPI4 latest packages, just upgraded

Code: Select all

 uname -r
5.4.72-v7l+
Boot RPi4

Code: Select all

ls -l /dev/serial0
lrwxrwxrwx 1 root root 5 Nov 22 00:07 /dev/serial0 -> ttyS0
pi@raspberrypi:~ $ ls -l /dev/ttyS0
crw-rw---- 1 root dialout 4, 64 Nov 22 00:07 /dev/ttyS0
Now I'm running the script from here https://pypi.org/project/mh-z19/, note as user pi

Code: Select all

pi@raspberrypi:~ $ python3 -m mh_z19
{"co2": 395}
Now group of serial port changed

Code: Select all

ls -l /dev/ttyS0
crw--w---- 1 root tty 4, 64 Nov 22 00:09 /dev/ttyS0
Subsquent calls of reading the sensor fails

Code: Select all

 python3 -m mh_z19
Traceback (most recent call last):
  File "/home/pi/.local/lib/python3.7/site-packages/serial/serialposix.py", line 265, in open
    self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
PermissionError: [Errno 13] Permission denied: '/dev/serial0'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/.local/lib/python3.7/site-packages/mh_z19/__init__.py", line 60, in mh_z19
    ser = connect_serial()
  File "/home/pi/.local/lib/python3.7/site-packages/mh_z19/__init__.py", line 56, in connect_serial
    timeout=1.0)
  File "/home/pi/.local/lib/python3.7/site-packages/serial/serialutil.py", line 240, in __init__
    self.open()
  File "/home/pi/.local/lib/python3.7/site-packages/serial/serialposix.py", line 268, in open
    raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg))
serial.serialutil.SerialException: [Errno 13] could not open port /dev/serial0: [Errno 13] Permission denied: '/dev/serial0'
null
Pleae note not only group changes, but also the read permission of group is gone.

Code: Select all

sudo chmod g+r  /dev/ttyS0
Works only once. After executing

Code: Select all

python3 -m mh_z19
I get a value. But then read permission is gone again.

Whats wrong here? Since I'm not root the script shouldn't be able to change group and permission of /dev/ttyS0
Trying it manually fails, as expected.

Code: Select all

pi@raspberrypi:~ $ chmod g-r  /dev/ttyS0
chmod: changing permissions of '/dev/ttyS0': Operation not permitted
Last edited by RDPUser on Sun Nov 22, 2020 11:27 am, edited 1 time in total.

cleverca22
Posts: 2501
Joined: Sat Aug 18, 2012 2:33 pm

Re: Reading from /dev/serial0 changes group from dialout to tty and read permisson is gone

Sun Nov 22, 2020 2:19 am

is there a login console running on that serial port?
check the output of "ps aux | grep getty" ?

RDPUser
Posts: 175
Joined: Tue Jan 30, 2018 12:18 pm

Re: Reading from /dev/serial0 changes group from dialout to tty and read permisson is gone

Sun Nov 22, 2020 9:02 am

Indeed it seems running.

Code: Select all

ps aux | grep getty
root       560  0.0  0.0   4308  1300 tty1     Ss+  00:19   0:00 /sbin/agetty -o -p -- \u --noclear tty1 linux
pi        6132  0.0  0.0   7348   564 pts/0    S+   08:59   0:00 grep --color=auto getty
However raspi-config tells me:

Code: Select all

The serial login shell is disabled                       │
 The serial interface is enabled    
Yesterday I've enabled serial port and I am 100% sure I've selected no at the question for serial login shell.

How can the login shell cause such a behaviour?
How can I deactivate it?

SurferTim
Posts: 1828
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Reading from /dev/serial0 changes group from dialout to tty and read permisson is gone

Sun Nov 22, 2020 9:16 am

I think this will do it:
Menu -> Preferences -> Raspberry Pi Configuration -> Interfaces.
enable serial port
disable serial console

RDPUser
Posts: 175
Joined: Tue Jan 30, 2018 12:18 pm

Re: Reading from /dev/serial0 changes group from dialout to tty and read permisson is gone

Sun Nov 22, 2020 9:48 am

I'm on buster lite only connected via SSH. I habe only raspi-config
vid.gif
vid.gif (157.07 KiB) Viewed 407 times
If I enable serial console, I have another agetty process

Code: Select all

ps aux | grep getty
root       563  0.0  0.0   6616  1552 ttyS0    Ss+  09:46   0:00 /sbin/agetty -o -p -- \u --keep-baud 115200,38400,9600 ttyS0 vt220
root       568  0.0  0.0   4308  1240 tty1     Ss+  09:46   0:00 /sbin/agetty -o -p -- \u --noclear tty1 linux
pi        1001  0.0  0.0   7348   544 pts/0    S+   09:46   0:00 grep --color=auto getty
When I disable again I have again

Code: Select all

pi@raspberrypi:~ $ ps aux | grep getty
root       590  0.0  0.0   4308  1304 tty1     Ss+  09:47   0:00 /sbin/agetty -o -p -- \u --noclear tty1 linux
pi        1010  0.0  0.0   7348   556 pts/0    S+   09:47   0:00 grep --color=auto getty


cleverca22
Posts: 2501
Joined: Sat Aug 18, 2012 2:33 pm

Re: Reading from /dev/serial0 changes group from dialout to tty and read permisson is gone

Sun Nov 22, 2020 9:35 pm

the one on tty1 is for login with a usb keyboard and the hdmi port, so that one is fine

hippy
Posts: 8952
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Reading from /dev/serial0 changes group from dialout to tty and read permisson is gone

Sun Nov 22, 2020 11:53 pm

Note the Python code does things with "getty". I didn't analyse it further but there could be something in that.

https://github.com/UedaTakeyuki/mh-z19/ ... /mh_z19.py

trejan
Posts: 3064
Joined: Tue Jul 02, 2019 2:28 pm

Re: Reading from /dev/serial0 changes group from dialout to tty and read permisson is gone

Mon Nov 23, 2020 12:33 am

hippy wrote:
Sun Nov 22, 2020 11:53 pm
Note the Python code does things with "getty". I didn't analyse it further but there could be something in that.
Eww. Why is code for reading a CO2 sensor messing about with this. getty is the process that handles the login prompt. If you don't give mh_z19.py the serial_console_untouched argument then it stops getty, does a reading and then starts getty even if it wasn't running before.

RDPUser: You must run mh_z19.py as root or pass that argument to stop it messing with getty.

[edit]removed comment about code[/edit]
Last edited by trejan on Mon Nov 23, 2020 4:45 pm, edited 1 time in total.

cleverca22
Posts: 2501
Joined: Sat Aug 18, 2012 2:33 pm

Re: Reading from /dev/serial0 changes group from dialout to tty and read permisson is gone

Mon Nov 23, 2020 2:31 am

trejan wrote:
Mon Nov 23, 2020 12:33 am
hippy wrote:
Sun Nov 22, 2020 11:53 pm
Note the Python code does things with "getty". I didn't analyse it further but there could be something in that.
Eww. Why is code for reading a CO2 sensor messing about with this. getty is the process that handles the login prompt. If you don't give mh_z19.py the serial_console_untouched argument then it stops getty, does a reading and then starts getty even if it wasn't running before.
ewww!, that explains everything!

hippy
Posts: 8952
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Reading from /dev/serial0 changes group from dialout to tty and read permisson is gone

Mon Nov 23, 2020 3:52 pm

trejan wrote:
Mon Nov 23, 2020 12:33 am
Eww. Why is code for reading a CO2 sensor messing about with this. getty is the process that handles the login prompt. If you don't give mh_z19.py the serial_console_untouched argument then it stops getty, does a reading and then starts getty even if it wasn't running before.
Thanks for the analysis. It appears the author of the code imagined people would be running a login on the serial port using a UART module and would be swapping that for the sensor, wrote the code to disable 'getty' and re-enable it once sensor reading had been taken.

Doing it the way the author did required running with 'sudo' and the 'serial_console_untouched' option was primarily added so 'sudo' wasn't required.

We should however not be too harsh on the author. This is the inevitable consequences of encouraging people to code, them doing that as best they can, then letting their code loose on the world.

trejan
Posts: 3064
Joined: Tue Jul 02, 2019 2:28 pm

Re: Reading from /dev/serial0 changes group from dialout to tty and read permisson is gone

Mon Nov 23, 2020 4:47 pm

hippy wrote:
Mon Nov 23, 2020 3:52 pm
We should however not be too harsh on the author. This is the inevitable consequences of encouraging people to code, them doing that as best they can, then letting their code loose on the world.
Yes. That is true. It is a very dubious feature IMO but they must have needed it for themselves. I've removed my comment regarding that as they were good enough to upload this for other people and also take requests for changes.

Return to “Troubleshooting”