如何解决我的epoll_wait可以被触发,但是总是返回fd 0
我正打算将OpenWrt的ubus移植到MTK的mips平台7620/7621。但似乎其中的epoll无法正常工作。
#include <stdio.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <fcntl.h>
#include <libuBox/usock.h>
#ifndef EPOLLRDHUP
#define EPOLLRDHUP 0x2000
#endif
static int poll_fd = -1;
static int usage(const char *progname)
{
fprintf(stderr,"Usage: %s [<options>]\n"
"Options: \n"
" -s <socket>: Set the unix domain socket to listen on\n"
"\n",progname);
return 1;
}
int main(int argc,char *argv[]) {
const char *ubus_socket = UBUS_UNIX_SOCKET;
int ret = 0;
int ch;
int fd,cfd;
struct epoll_event ev;
struct epoll_event evs = {0};
poll_fd = epoll_create(32);
if (poll_fd < 0) {
perror("epoll create");
ret = -1;
goto out;
}
//fcntl(poll_fd,F_SETFD,fcntl(poll_fd,F_GETFD) | FD_CLOEXEC);
while ((ch = getopt(argc,argv,"s:")) != -1) {
switch (ch) {
case 's':
ubus_socket = optarg;
break;
default:
return usage(argv[0]);
}
}
unlink(ubus_socket);
umask(0177);
fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK,ubus_socket,NULL);
if (fd < 0) {
perror("usock");
ret = -1;
goto out;
} else {
printf("sock fd %d\n",fd);
}
ev.data.fd = fd;
ev.events = EPOLLIN | EPOLLRDHUP | EPOLLET;
ret = epoll_ctl(poll_fd,EPOLL_CTL_ADD,fd,&ev);
if (ret < 0) {
perror("epoll ctl");
goto out;
}
while (1) {
ret = epoll_wait(poll_fd,&evs,1,-1);
if (ret < 0) {
perror("epoll wait");
goto out;
}
if (ret > 0) {
printf("wait for %d %x\n",ret,evs.events);
if (evs.events & EPOLLIN) {
printf("in fd %d\n",evs.data.fd);
if (evs.data.fd == fd) {
cfd = accept(evs.data.fd,NULL,0);
if (cfd < 0) {
perror("accept");
goto out;
}
printf("accept fd %d\n",cfd);
}
}
}
}
out:
return ret;
}
epoll_event中的fd每次都是0,这与我之前创建的ubus套接字不匹配。而且,即使我在epoll_ctl(...,EPOLL_CTL_ADD,..)之后添加了一些内容,epoll_event数据中的ptr也始终为NULL。
The struct epoll_event is defined as :
typedef union epoll_data {
void *ptr;
int fd;
uint32_t u32;
uint64_t u64;
} epoll_data_t;
struct epoll_event {
uint32_t events; /* Epoll events */
epoll_data_t data; /* User data variable */
};
我的内核版本是2.6.36.x。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。