[New-bugs-announce] [issue10032] os.setuid and os.setgid have unexpected influence on serial module

Tjeerd Pinkert report at bugs.python.org
Tue Oct 5 22:48:04 CEST 2010

New submission from Tjeerd Pinkert <t.j.pinkert at alumnus.utwente.nl>:

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:

Sample code showing the behaviour using the daemon module from:
(and no it's not this module, also my own crappy code did the same thing and gives the same erroneous behaviour)


"""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()
        print ser.portstr

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

if __name__ == '__main__':

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

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

it nicely opens the port.

if I fill in tjp for uid and gid in the configfile and run it as:
tjp at machine$ sudo ./test.py
1000 1000
Traceback (most recent call last):
  File "./test.py", line 26, in <module>
  File "/home/tjp/tmp/pydaemon/daemon.py", line 121, in main
  File "/home/tjp/tmp/pydaemon/daemon.py", line 196, in start
  File "./test.py", line 17, in setup_user
  File "/usr/lib/python2.6/dist-packages/serial/serialutil.py", line 166, in __init__
  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

components: Extension Modules
messages: 118033
nosy: Tjeerd.Pinkert
priority: normal
severity: normal
status: open
title: os.setuid and os.setgid have unexpected influence on serial module
versions: Python 2.6

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list