I've seen so many things that can be poll()
'ed on that I'm wondering whether that would include the detection of another process binding and then listening to a port.
poll()
is most often used on a set of sockets to know when they gets new content (read) or have space in its output buffer (write).
Now we have:
signalfd()
to convert a Unix signal to a file descriptor andpoll()
on that.pipe()
to listen to inter-process pipes (very similar to sockets)timerfd_create()
to create a timer that will wake you up through yourpoll()
eventfd()
to create an event notification file descriptor (for example, to send events between threads)special socket to listen to the kernel creating new processes (fork()) and killing processes (exit())
struct sockaddr_nl sa_nl; int s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR); sa_nl.nl_family = AF_NETLINK; sa_nl.nl_groups = CN_IDX_PROC; sa_nl.nl_pid = getpid(); bind(nl_sock, (struct sockaddr *) &sa_nl, sizeof(sa_nl)); ...
Now... Is there a way to know when a process calls the listen()
function?
In other words, know as soon as a new bound address and port appear in the output of netstat
with the LISTEN keyword attached to it such as:
Proto Local Address Foreign Address State
tcp 0.0.0.0:25 0.0.0.0:* LISTEN
Obviously, I know that the connection will appear in /proc/net/tcp
which I can scan (just like netstat
does,) but that would require polling (opposed to sleeping with a call to poll()
) with an interval timer. I know how to do that, but I would prefer to be able to know that the port is open without having to read a file over and over again...