How do I get the descriptor type? I am using epoll to monitor lots of descriptors like sockets, timers, and signals. I saw it is possible using fstat, but the mode only says something about sockets and pipes. fstat manpage. Is there a special function to identity a descriptor?
Asked
Active
Viewed 1,002 times
3
-
Sorry, your question is not clear. What do you want to get? – kirelagin Jun 05 '13 at 08:33
-
2Oh, I see, you want to get a type (that is socket/file/etc.) of a descriptor, returned from epoll? – kirelagin Jun 05 '13 at 08:33
-
Try to examine the `st_dev` field you get from `fstat`. Get the major number, find it in `/proc/devices` and see what you get for different descriptors. – ugoren Jun 05 '13 at 10:41
-
I am using epoll, to which I add a signalfd, timerfd, tcp server sockets, tcp client sockets. When I use epoll_wait, I get a list of ready descriptors. But I don't know, if the descriptors are sockets, timerfd or signalfd. For tcp sockets, it is easy to find out, if they are clients or servers (getsockopt). The problem is that fstat doesn't mention timerfd or signalfd. – Kouros Jun 06 '13 at 08:10
-
See the example on the man page [fstat](http://linux.die.net/man/2/fstat) – Kouros Jun 06 '13 at 08:20
-
1It seems as if you won't get around storing the descriptor's `int` values along with their type at a time you still know which descriptor is of which type, then later you could compare against this storage. – alk Jun 06 '13 at 17:41
-
@alk yeah I thought about it. There probably no better way. Thanks – Kouros Jun 07 '13 at 11:12
1 Answers
1
I do not think there is any simple or uniform way to do what you are asking. The command lsof]1 manages to determine this information so you may want to take a look at that code to see what they are doing.
Off the top of my head to determine if a descriptor is a socket you can use getsockopt(2). If a call to getsockopt fails and errno == ENOTSOCK then your descriptor is not a socket. likewise you can use isatty(3) to determine if a descriptor belongs to a serial port or terminal.

LogicG8
- 1,767
- 16
- 26