通过Epoll 能监听普通文件吗?

epoll 是 Linux 系统中常用的多路复用 I/O 组件,一般用于监听 socket 是否能够进行 I/O 操作。…

epoll 是 Linux 系统中常用的多路复用 I/O 组件,一般用于监听 socket 是否能够进行 I/O 操作。那么,epoll 能监听普通文件吗?

我们先通过下面的例子来验证一下,epoll 能不能监听普通文件:

#include  
#include  
#include  
int main()
{
int epfd, fd;
struct epoll_event ev, events[2];
int result;
epfd = epoll_create(10);
if (epfd "epoll_create()");
return -1;
}


fd = open("./test.txt", O_RDONLY | O_CREAT);

if (fd "open()");
return -1;
}

ev.events = EPOLLIN;
result = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);

if (result "epoll_ctl()");
return -1;
}

epoll_wait(epfd, events, 2, -1);

return 0;
}

编译并且运行,结果如下:

[vagrant@localhost epoll]$ gcc epoll.c -o epoll
[vagrant@localhost epoll]$ ./epoll
epoll_ctl(): Operation not permitted

可以看到上面的运行结果报 Operation not permitted 的错误,这说明 epoll 是不能监听普通文件的,为什么呢?

寻根究底

我们应该对追寻真相抱着热衷的态度,所以必须找出 epoll 不能监听普通文件的原因。

因为在上面的例子中,是 epoll_ctl 函数报的错,所以我们首先应该从 epoll_ctl 的源码入手,如下:

SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd,
struct epoll_event __user *, event)
{
int error;
struct file *file, *tfile;

...


error = -EBADF;
file = fget(epfd);  // epoll 句柄对应的文件对象
if (!file)
goto error_return;


tfile = fget(fd);   // 被监听的文件句柄对应的文件对象
if (!tfile)
goto error_fput;


error = -EPERM; // Operation not permitted 错误号
if (!tfile->f_op || !tfile->f_op->poll)
goto error_tgt_fput;

...

error_tgt_fput:
fput(tfile);
error_fput:
fput(file);
error_return:

return error;

}

从上面代码可以看出,当被监听的文件没有提供 poll 接口时,就会返回 EPERM 的错误,这个错误就是 Operation not permitted 的错误号。

所以,出现 Operation not permitted 的原因就是:被监听的文件没有提供 poll 接口。

由于我们的文件系统是 ext4,所以我们来看看 ext4 文件系统中的文件是否提供了 poll 接口(位于文件 /fs/ext4/file.c 中):

const struct file_operations ext4_file_operations = {
.llseek         = generic_file_llseek,
.read           = do_sync_read,
.write          = do_sync_write,
.aio_read       = generic_file_aio_read,
.aio_write      = ext4_file_write,
.unlocked_ioctl = ext4_ioctl,
.mmap           = ext4_file_mmap,
.open           = ext4_file_open,
.release        = ext4_release_file,
.fsync          = ext4_sync_file,
.splice_read    = generic_file_splice_read,
.splice_write   = generic_file_splice_write,
;

ext4 文件的文件操作函数集被设置为 ext4_file_operations(也说就是:file->f_op = ext4_file_operations),从上面代码可以看出,ext4_file_operations 并没有提供 poll 接口。所以,当调用 epoll_ctl 把文件添加到 epoll 中进行监听时,就会返回 Operation not permitted 的错误。

从上面的分析可知,当文件系统提供 poll 接口时,就可以把文件添加到 epoll 中进行监听。

为您推荐

ssh远程超时中断的解决办法

有时我们网络正常的,但SSH连接经常出现中断的情况,以及在SSH远程时很慢的问题。 这是由于OpenSSL服务默认启用了...

Linux系统防火墙放行端口

如果您服务器内安装了宝塔面板,请直接登陆宝塔面板,安全,里面添加放行端口。如果添加后不生效,把防火墙开关一下即可。本教程...

Linux主机简单判断被CC攻击的网站命令-比较直接有效

CC攻击很容易发起,并且几乎不需要成本,导致现在的CC攻击越来越多。 大部分搞CC攻击的人,都是用在网上下载的工具,这些...

linux环境下测试get和post请求

Linux环境下测试get和post请求 ?get,post,curl   get请求 curl: curl ...

umount卸载磁盘提示target is busy

umount卸载磁盘提示target is busy. (目标忙) 的问题解决方案   umount卸载磁盘提...
返回顶部