pySerial in a daemon process

P

paul

I am writing a daemon process that reads data from the serial port /
dev/ttyS0. I am using pyserial & the method for setting up a daemon
described in "Chris' Python Page" (http://homepage.hispeed.ch/py430/
python/) on an Ubuntu linux pc.

Everything works great EXCEPT...

in the daemon script, there are two lines to change the uid & gid that
the script runs as:
os.setegid(10)
os.seteuid(1000)

If I comment these out, so that the daemon runs as root, then
everything works fine. I can also manually run the script that the
daemon process kicks off from my own id just fine.

If I put these back in and try to run the daemon, the script fails
when I try to connect to the serial port, with this error:
serial.serialutil.SerialException: Could not open port: [Errno 13]
Permission denied: '/dev/ttyS0'

In my debugging I have tried setting the uid to my own uid, and the
gid to a variety of different groups that I belong to. None of them
work.

I could certainly run the daemon as root, but I would rather not.
What really confuses me is that if I manually run the script as myself
(without using the daemon script), it connects fine, but when root
uses os.seteuid() to my uid, it fails.

What do I need to do to get this to work?

Many thanks,

Paul
 
B

Bjoern Schliessmann

paul said:
If I put these back in and try to run the daemon, the script fails
when I try to connect to the serial port, with this error:
serial.serialutil.SerialException: Could not open port: [Errno 13]
Permission denied: '/dev/ttyS0'

Did you check the permissions on this file? Often you have to be
member of a "dialout" group or similar to get access.

Regards,


Björn
 
P

paul

paul said:
If I put these back in and try to run the daemon, the script fails
when I try to connect to the serial port, with this error:
serial.serialutil.SerialException: Could not open port: [Errno 13]
Permission denied: '/dev/ttyS0'

Did you check the permissions on this file? Often you have to be
member of a "dialout" group or similar to get access.

Regards,

Björn


THANKS! That did it, but perhaps can you explain to me why...

Before I had posted this question first thing I did was to look at /
dev/ttyS0, and indeed the group is dialout (gid=20), so I tried a
couple of things:

1. checked my id to see if I was a member of dialout, and indeed I
am. So, I edited the daemon script to use my id and set the gid to
dialout (ie: os.setegid(20) & os.seteuid(1000)) and it still failed.

2. made the id I really want to use (pydaemon, uid=110) a member of
dialout, and set the daemon script to use those (ie: os.setegid(20) &
os.seteuid(110)) and it still failed...

After reading your comment, I went and did a chgrp to set the various
script file groups to dialout, and indeed it now works. What confuses
me is that if the script needed that group, why doesn't the setegid or
seteuid fail? Why does the script keep running okay until the
connect, and fail then?

Thanks!

Paul
 
L

Lawrence D'Oliveiro

paul said:
Before I had posted this question first thing I did was to look at /
dev/ttyS0, and indeed the group is dialout (gid=20)...

What are the full protections on the serial port? That is, can you post the
output of

ls -l /dev/ttyS0

please?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top