如何判断文件句柄是否是套接字?

时间:2021-03-07 22:22:36

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

我需要记录套接字使用情况,并编写了一个LD_PRELOAD库。

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

问题是当我覆盖read()和write()时,普通文件操作也被记录(当然)。

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

那么如何区分普通文件描述符和套接字描述符呢?

1 个解决方案

#1


27  

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

在描述符上调用fstat并在结果上使用S_ISSOCK宏。

struct stat statbuf;
fstat(fd, &statbuf);
S_ISSOCK(statbuf.st_mode);

#1


27  

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

在描述符上调用fstat并在结果上使用S_ISSOCK宏。

struct stat statbuf;
fstat(fd, &statbuf);
S_ISSOCK(statbuf.st_mode);