WIP: port-serial: Implement better serial port locking, using both flock(2) and tty_ioctl(4)
There are two ways to lock serial port devices on Linux:
-
Using the API described in
tty_ioctl(4)
, by callingioctl(fd, TIOCEXCL)
. This will prevent non-root processes from opening the device (returningEBUSY
), and give the ability to all processes to be informed that the device is already locked, by callingioctl(fd, TIOCGEXCL, &locking_status)
. -
Using the API described in
flock(2)
, by callingflock(fd, LOCK_EX | LOCK_NB)
. This gives the ability to be informed whether the device is already locked by looking at the return value.
Currently, only 0.5 way is actually implemented in ModemManager: the serial port device is locked using ioctl(fd, TIOCEXCL)
, but the status is not checked using ioctl(fd, TIOCGEXCL, &locking_status)
- and as ModemManager usually runs as root, let another program communicating with USB modems be written, ModemManager would not check and take in account that the port is already locked, making the mechanism less effective.
Also, it would be better to additionally implement the flock(2)
way, as it would guarantee wider compatibility with possibly existing implementations (as both mechanisms don't share the same information).
This pull request effectively fixes both issues: it checks the tty_ioctl(4)
locking status using ioctl(fd, TIOCGEXCL, &locking_status)
, and it also implements flock(2)
serial port locking.