This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Tjeerd.Pinkert
Recipients Tjeerd.Pinkert
Date 2010-10-05.20:48:01
SpamBayes Score 7.0220885e-11
Marked as misclassified No
Message-id <1286311684.44.0.770931367073.issue10032@psf.upfronthosting.co.za>
In-reply-to
Content
If I use os.setgid and os.setuid to switch to an other user in some daemon code, I cannot open the serial port anymore. If I run the same code directly from the user I can open the serial port. Since the serial module is using the open() call to open the serial device I wonder if the mistake is in the serial module or in the os module.

see also:
https://sourceforge.net/tracker/?func=detail&aid=3081643&group_id=46487&atid=446302

Sample code showing the behaviour using the daemon module from:
http://hathawaymix.org/Software/Sketches/daemon.py
(and no it's not this module, also my own crappy code did the same thing and gives the same erroneous behaviour)

------------------------
#!/usr/bin/python

"""Test daemon"""

import daemon
import logging
import time
import serial
import os

class HelloDaemon(daemon.Daemon):
    default_conf = 'test.conf'
    section = 'test'

    def setup_user(self):
        print os.getuid(), os.getgid()
        ser=serial.Serial(0)
        print ser.portstr
        ser.close()

    def run(self):
        while True:
            logging.info('The daemon says hello')
            time.sleep(1)

if __name__ == '__main__':
    HelloDaemon().main()
-----------------------------------------

now make the config file:
--------------------------------
[test]
uid = 
gid = 
pidfile = ./hellodaemon.pid
logfile = ./hellodaemon.log
loglevel = info
----------------------

when I run it as my own user it works fine, e.g.:
tjp@machine$ ./test.py
1000 1000
/dev/ttyS0

it nicely opens the port.

if I fill in tjp for uid and gid in the configfile and run it as:
tjp@machine$ sudo ./test.py
1000 1000
Traceback (most recent call last):
  File "./test.py", line 26, in <module>
    HelloDaemon().main()
  File "/home/tjp/tmp/pydaemon/daemon.py", line 121, in main
    self.start()
  File "/home/tjp/tmp/pydaemon/daemon.py", line 196, in start
    self.setup_user()
  File "./test.py", line 17, in setup_user
    ser=serial.Serial(0)
  File "/usr/lib/python2.6/dist-packages/serial/serialutil.py", line 166, in __init__
    self.open()
  File "/usr/lib/python2.6/dist-packages/serial/serialposix.py", line 175, in open
    raise SerialException("could not open port %s: %s" % (self._port, msg))
serial.serialutil.SerialException: could not open port 0: [Errno 13] Permission denied: '/dev/ttyS0'



I hope someone with more experience can either help me out, or confirm if this should be regarded a bug, and then in which module, os or serial

Yours, Tjeerd
History
Date User Action Args
2010-10-05 20:48:04Tjeerd.Pinkertsetrecipients: + Tjeerd.Pinkert
2010-10-05 20:48:04Tjeerd.Pinkertsetmessageid: <1286311684.44.0.770931367073.issue10032@psf.upfronthosting.co.za>
2010-10-05 20:48:03Tjeerd.Pinkertlinkissue10032 messages
2010-10-05 20:48:01Tjeerd.Pinkertcreate