可以 `epoll_ctl` 修改传递给它的 `epoll_event` 结构吗?

May `epoll_ctl` modify the `epoll_event` structure passed to it?

Linux kernel manpages声明epoll_ctl程序如下:

int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);

很明显,event 参数被声明为指向 epoll_event struct.

的指针

在这个问题的上下文中,这一点的意义在于指针类型声明之前没有 const,因此,该过程似乎被允许修改传递的结构的内容。

这是某种遗漏,还是程序设计成那样,我们不得不假设传递的结构确实可能在程序内部被修改?

我知道这里的声明是明确的,但是有理由相信这是一个遗漏吗?

我也看过relevant source code in kernel 4.6 tree,我没有看到太多证据表明该程序甚至打算修改结构,所以那里。

找到一个比较conclusive answer on the Linux mailing list。在这里引用 Davide Libenzi,epoll 的主要或唯一作者:

From: Davide Libenzi <davidel <at> xmailserver.org>
Subject: Re: epoll_ctl and const correctness
Newsgroups: gmane.linux.kernel
Date: 2009-03-25 16:23:21 GMT (7 years, 17 weeks, 1 day, 9 hours and 4 minutes ago)

On Wed, 25 Mar 2009, nicolas sitbon wrote:

Currently, the prototype of epoll_ctl is :

int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);

I searched in the man of epoll_ctl and google, and it seems that the structure pointed to by event isn't modify, valgrind confirms this behaviour, so am I wrong? or the good prototype is

int epoll_ctl(int epfd, int op, int fd, struct epoll_event const *event);

根据目前的ctl操作来看,是的。但这样做会阻止 稍后添加其他非常量操作。

  • 大卫

要点是,尽管事实上的行为不是修改结构,但接口省略了 const 修饰符,因为将来可能会通过相同的系统调用添加其他控制操作,这需要一个event 参数指向的潜在可修改结构。

我应该先访问内核邮件列表,对于关于 SO 的另一个可能多余的信息表示歉意。为后代留下问题和答案。