Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if a file handle is a socket?

I need to log socket usage, and I wrote a LD_PRELOAD library.

The problem is when I override read() and write() than ordinary file operations are get logged too (of course).

So how can I tell ordinary file descriptors and socket descriptors apart?

like image 557
netom Avatar asked Oct 17 '10 10:10

netom


1 Answers

Call fstat on the descriptor and use the S_ISSOCK macro on the result.

struct stat statbuf;
fstat(fd, &statbuf);
S_ISSOCK(statbuf.st_mode);
like image 101
Fred Foo Avatar answered Oct 03 '22 21:10

Fred Foo