mirror of
git://github.com/lxc/lxc
synced 2025-08-31 15:09:28 +00:00
Don't mess descr->ev
A simple test program to show up the issue: -8<--- #include <stdio.h> #include <unistd.h> #include "mainloop.h" struct lxc_epoll_descr loop; int cb1(int fd, void *data, struct lxc_epoll_descr *descr) { fprintf(stderr, "cb1\n"); return 1; } int cb2(int fd, void *data, struct lxc_epoll_descr *descr) { fprintf(stderr, "cb2\n"); return 1; } int main(int argc, char *argv[]) { int ret; int fds[2]; ret = pipe(fds); if (ret) { perror("pipe:"); return -1; } ret = lxc_mainloop_open(&loop); if (ret) { fprintf(stderr, "lxc_mainloop_open: %d\n", ret); return -1; } ret = lxc_mainloop_add_handler(&loop, fds[1], cb1, NULL); if (ret) { fprintf(stderr, "lxc_mainloop_add_handler(fds[1]): %d\n", ret); return -1; } ret = lxc_mainloop_add_handler(&loop, fds[0], cb2, NULL); if (ret) { fprintf(stderr, "lxc_mainloop_add_handler(fds[0]): %d\n", ret); return -1; } write(fds[1], &ret, sizeof(ret)); ret = lxc_mainloop(&loop); if (ret) { fprintf(stderr, "lxc_mainloop: %d\n", ret); return -1; } ret = lxc_mainloop_close(&loop); if (ret) { fprintf(stderr, "lxc_mainloop_close: %d\n", ret); return -1; } return 0; } Compile and run: $ gcc test.c -o test -I ./src/lxc/ ./src/lxc/liblxc_so-mainloop.o && ./test cb2
This commit is contained in:
committed by
Daniel Lezcano
parent
3ce45e6458
commit
5df6b18f0e
@@ -35,40 +35,32 @@ struct mainloop_handler {
|
||||
void *data;
|
||||
};
|
||||
|
||||
#define MAX_EVENTS 10
|
||||
|
||||
int lxc_mainloop(struct lxc_epoll_descr *descr)
|
||||
{
|
||||
int i, nfds, triggered;
|
||||
int i, nfds;
|
||||
struct mainloop_handler *handler;
|
||||
struct epoll_event events[MAX_EVENTS];
|
||||
|
||||
for (;;) {
|
||||
|
||||
triggered = 0;
|
||||
|
||||
nfds = epoll_wait(descr->epfd, descr->ev, descr->nfds, -1);
|
||||
nfds = epoll_wait(descr->epfd, events, MAX_EVENTS, -1);
|
||||
if (nfds < 0) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0; i < descr->nfds; i++) {
|
||||
|
||||
if (!(descr->ev[i].events & EPOLLIN) &&
|
||||
!(descr->ev[i].events & EPOLLHUP))
|
||||
continue;
|
||||
|
||||
triggered++;
|
||||
for (i = 0; i < nfds; i++) {
|
||||
handler =
|
||||
(struct mainloop_handler *) descr->ev[i].data.ptr;
|
||||
(struct mainloop_handler *) events[i].data.ptr;
|
||||
|
||||
/* If the handler returns a positive value, exit
|
||||
the mainloop */
|
||||
if (handler->callback(handler->fd, handler->data,
|
||||
descr) > 0)
|
||||
return 0;
|
||||
|
||||
if (triggered == nfds)
|
||||
break;
|
||||
}
|
||||
|
||||
if (!descr->nfds)
|
||||
|
Reference in New Issue
Block a user