제목과 마찬가지로 epoll이있는 디렉토리 인 파일 설명자를 등록합니다. 어떻게합니까?epoll은 디렉토리를 참조하는 파일 설명자로 무엇을합니까?
답변
Nothing - fd는 (적어도 일반 Linux 파일 시스템의 경우) 등록하라는 호출은 EPERM
과 함께 실패합니다. 다음과 같은 결과로
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main(void) {
int ep = epoll_create1(0);
int fd = open("/tmp", O_RDONLY|O_DIRECTORY);
struct epoll_event evt = {
.events = EPOLLIN
};
if (ep < 0 || fd < 0) {
printf("Error opening fds.\n");
return -1;
}
if (epoll_ctl(ep, EPOLL_CTL_ADD, fd, &evt) < 0) {
perror("epoll_ctl");
return -1;
}
return 0;
}
:
[[email protected]:/tmp]$ make epoll
cc epoll.c -o epoll
[[email protected]:/tmp]$ ./epoll
epoll_ctl: Operation not permitted
여기에 무슨 일이 있었는지 알아 내기 위해, 나는 소스에 갔다
나는이 사용하여 다음 데모 프로그램을 테스트했다. I happen to know의 경우 대부분의 동작이epoll
이고 대상 파일에 해당하는
struct file_operations
의
->poll
함수에 의해 결정됩니다. 대상 파일은 해당 파일 시스템에 따라 다릅니다. 나는 전형적인 예로서
ext4
을 포착하고,
defines
ext4_dir_operations
으로는 다음과하는
fs/ext4/dir.c
보았다 :
const struct file_operations ext4_dir_operations = {
.llseek = ext4_dir_llseek,
.read = generic_read_dir,
.readdir = ext4_readdir,
.unlocked_ioctl = ext4_ioctl,
#ifdef CONFIG_COMPAT
.compat_ioctl = ext4_compat_ioctl,
#endif
.fsync = ext4_sync_file,
.release = ext4_release_dir,
};
참고가 NULL
에 초기화됩니다 의미 .poll
정의의 부족. 그래서, 다시 fs/eventpoll.c
에 정의는 epoll에 스윙, 우리는 poll
되는 NULL에 대한 점검을 찾아, 우리는 epoll_ctl
콜 정의에서 하나 early on 찾을 : 우리의 테스트 바와 같이 경우 대상 파일 아무튼,
/* The target file descriptor must support poll */
error = -EPERM;
if (!tfile->f_op || !tfile->f_op->poll)
goto error_tgt_fput;
을 poll
을 지원하면 삽입 시도는 EPERM
으로 실패합니다.
다른 파일 시스템이 자신의 디렉토리 파일 객체에 .poll
메쏘드를 정의 할 수는 있지만, 나는 그렇게 할 것입니다.
'open (path, O_RDONLY | O_DIRECTORY);에 비해'dirfd (opendir ("/ tmp"))가 선호됩니까? 그냥 스타일 질문. 'opendir'을 사용하면 마술처럼 fs 지원 폴링을하지 않습니다. – schmichael
'dirfd (opendir ("..."))'이식성이 높기 때문에 일반적으로 선호됩니다. 필자는 리눅스 커널 해커입니다. 개인적으로는 가장 적절하다고 생각되는 경우에도 개인적으로 시스템 호출 인터페이스를 사용하는 경향이 있습니다. 분명히 여기에서는 'epoll'이 리눅스에만 한정되어 있기 때문에 별 상관이 없습니다. – nelhage
Linux에서 파일 시스템 이벤트를 모니터링하려면 [inotify'] (http://linux.die.net/man/7/inotify)를 사용하십시오. – jxh