I have a situation where I would like to elevate the permissions I have in a web environment so that I can access a serial device.
The specific case is where I have a web interface for configuring a modem that comes up on /dev/ttyUSB[0-9].
Zero or more modems will be plugged in by an end user. I am writing some software that is capable of discerning which is a USB Wireless Modem by reading /sys/devices and talking to the modem using some AT commands.
I would like to be able to open the device and do something like:
ser = serial.Serial(tty, baudrate=115200, timeout=10)
ser.write('AT+CGSN\r\n')
imei = ser.readline()
The problem is that pyserial does this: self.fd = os.open(self.portstr, os.O_RDWR|os.O_NOCTTY|os.O_NONBLOCK) to open the serial port, where portstr is /dev/ttyUSB0, but it does it as the nobody user, which is unprivileged.
Serial ports on this system are owned by root:uucp and are set as 0660 (i.e. rw-rw----).
What is the best way for a user such as nobody who should have as few permissions as possible to open a file in dev?
Ideas I will consider:
- Doing things in a subprocess using
sudo. - Changing permissions of the files in
/dev/(instructions on how to do this properly using udev are appreciated!) - Using another API or piece of software I have not considered.