2012-08-06 4 views

답변

14

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 메쏘드를 정의 할 수는 있지만, 나는 그렇게 할 것입니다.

+0

'open (path, O_RDONLY | O_DIRECTORY);에 비해'dirfd (opendir ("/ tmp"))가 선호됩니까? 그냥 스타일 질문. 'opendir'을 사용하면 마술처럼 fs 지원 폴링을하지 않습니다. – schmichael

+0

'dirfd (opendir ("..."))'이식성이 높기 때문에 일반적으로 선호됩니다. 필자는 리눅스 커널 해커입니다. 개인적으로는 가장 적절하다고 생각되는 경우에도 개인적으로 시스템 호출 인터페이스를 사용하는 경향이 있습니다. 분명히 여기에서는 'epoll'이 리눅스에만 한정되어 있기 때문에 별 상관이 없습니다. – nelhage