]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
Don't mess descr->ev
authorClement Calmels <clement.calmels@fr.ibm.com>
Wed, 13 Jan 2010 17:51:16 +0000 (18:51 +0100)
committerDaniel Lezcano <dlezcano@fr.ibm.com>
Wed, 13 Jan 2010 17:51:16 +0000 (18:51 +0100)
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

src/lxc/mainloop.c

index 03d178f9e72bf3d1882ddf4e2864486daff20d39..6b656d13f8acc762ae21a5fb1c2eaa8ca9965d7b 100644 (file)
@@ -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)