2014-01-30 48 views

回答

6

man 2 stat

int fstat(int filedes, struct stat *buf);

...以下POSIX的宏定义使用st_mode字段来检查文件类型:

  S_ISFIFO(m) FIFO (named pipe)? 

所以struct stat st; ... !fstat(fileno(pFile, &st) && S_ISFIFO(st.st_mode)应该工作。

+1

还有['S_ISFIFO(st.st_mode)'](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_stat.h.html#tag_13_62),它是 测试管道的宏或FIFO专用文件。目前尚不清楚您是否可以将管道与FIFO区分开来。 –

+0

谢谢! S_ISFIFO(st.st_mode)是我正在寻找的.. – crypton480

0

停止执行后可能有点太晚了fopen()。这是因为open()系统调用会阻塞,直到有人打开FIFO进行写入。相反,在fopen()之前使用stat()系统调用(在Unix/Linux上)找出。

+2

使用'stat'然后'fopen'可能导致TOCTTOU漏洞。如果这是一个安全问题,最好的解决方案是“打开”非阻塞,然后是“fstat”和“fdopen”。 – Brian

+0

我认为一个非阻塞的open()意味着后续的read()调用是非阻塞的。但是查看手册页确认open()本身也是非阻塞的。 – SzG

相关问题