Depending on your modem, USB serial port device may show up as /dev/ttyUSBn
or /dev/ttyACMn
, where n
is some number starting from 0
.
You can configure udev
rule to automatically react on device being inserted or removed.
If you want to do it on your own in C, you need to make use of netlink(7)
sockets. If you don't want to fiddle with them, probably easier approach is to simply use utility udevadm
provided by udev package (udevadm is using netlink
internally), something like that:
udevadm monitor --kernel
If you are going to use it in your C program, simply call it in pipe like this:
stdbuf -o0 udevadm monitor --kernel
and setup select
loop, so it can react immediately. Wrapping in stdbuf
is necessary to avoid buffering provided by udevadm.