]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Requeue events when using epoll.
authorRoy Marples <roy@marples.name>
Thu, 5 Mar 2015 15:07:21 +0000 (15:07 +0000)
committerRoy Marples <roy@marples.name>
Thu, 5 Mar 2015 15:07:21 +0000 (15:07 +0000)
eloop.c
eloop.h

diff --git a/eloop.c b/eloop.c
index 6c9411384cfdb84e692e02228a2a047560820339..4d51c077e055999edd2f1e0796cc870d63733309 100644 (file)
--- a/eloop.c
+++ b/eloop.c
@@ -359,14 +359,13 @@ eloop_exit(struct eloop_ctx *ctx, int code)
        ctx->exitnow = 1;
 }
 
-#ifdef HAVE_KQUEUE
+#if defined(HAVE_KQUEUE) || defined(HAVE_EPOLL)
 static int
-eloop_kqueue_open(struct eloop_ctx *ctx)
+eloop_open(struct eloop_ctx *ctx)
 {
-#ifdef HAVE_KQUEUE1
-       if ((ctx->poll_fd = kqueue1(O_CLOEXEC)) == -1)
-               return -1;
-#else
+#if defined(HAVE_KQUEUE1)
+       return (ctx->poll_fd = kqueue1(O_CLOEXEC));
+#elif defined(HAVE_KQUEUE)
        int i;
 
        if ((ctx->poll_fd = kqueue()) == -1)
@@ -378,23 +377,30 @@ eloop_kqueue_open(struct eloop_ctx *ctx)
                ctx->poll_fd = -1;
                return -1;
        }
-#endif
 
        return ctx->poll_fd;
+#elif defined (HAVE_EPOLL)
+       return (ctx->poll_fd = epoll_create1(EPOLL_CLOEXEC));
+#endif
 }
 
 int
 eloop_requeue(struct eloop_ctx *ctx)
 {
-       size_t i;
        struct eloop_event *e;
-       struct kevent *ke;
        int error;
+#if defined(HAVE_KQUEUE)
+       size_t i;
+       struct kevent *ke;
+#elif defined(HAVE_EPOLL)
+       struct epoll_event epe;
+#endif
 
-       close(ctx->poll_fd);
-       if (eloop_kqueue_open(ctx) == -1)
+       if (ctx->poll_fd != -1)
+               close(ctx->poll_fd);
+       if (eloop_open(ctx) == -1)
                return -1;
-
+#if defined (HAVE_KQUEUE)
        i = 0;
        while (dhcpcd_handlesigs[i])
                i++;
@@ -424,6 +430,22 @@ eloop_requeue(struct eloop_ctx *ctx)
 
        error =  kevent(ctx->poll_fd, ke, LENC(i), NULL, 0, NULL);
        free(ke);
+
+#elif defined(HAVE_EPOLL)
+
+       error = 0;
+       TAILQ_FOREACH(e, &ctx->events, next) {
+               memset(&epe, 0, sizeof(epe));
+               epe.data.fd = e->fd;
+               epe.events = EPOLLIN;
+               if (e->write_cb)
+                       epe.events |= EPOLLOUT;
+               epe.data.ptr = e;
+               if (epoll_ctl(ctx->poll_fd, EPOLL_CTL_ADD, e->fd, &epe) == -1)
+                       error = -1;
+       }
+#endif
+
        return error;
 }
 #endif
@@ -445,21 +467,13 @@ eloop_init(void)
                TAILQ_INIT(&ctx->timeouts);
                TAILQ_INIT(&ctx->free_timeouts);
                ctx->exitcode = EXIT_FAILURE;
-#if defined(HAVE_KQUEUE)
-               /* requeue will put our signals in place */
-               if (eloop_kqueue_open(ctx) == -1 ||
-                   eloop_requeue(ctx) == -1)
-               {
-                       free(ctx);
-                       return NULL;
-               }
-#elif defined(HAVE_EPOLL)
-               if ((ctx->poll_fd = epoll_create1(EPOLL_CLOEXEC)) == -1) {
+#if defined(HAVE_KQUEUE) || defined(HAVE_EPOLL)
+               ctx->poll_fd = -1;
+#endif
+               if (eloop_requeue(ctx) == -1) {
                        free(ctx);
                        return NULL;
                }
-#endif
-
        }
 
        return ctx;
diff --git a/eloop.h b/eloop.h
index b7d5564030bc023c39c7de51c49840ccc41f5231..1f5ff7b4948d5360be1a524b4b5f95fb0dbc721d 100644 (file)
--- a/eloop.h
+++ b/eloop.h
@@ -100,7 +100,7 @@ int eloop_q_timeout_add_tv(struct eloop_ctx *, int queue,
 int eloop_timeout_add_now(struct eloop_ctx *, void (*)(void *), void *);
 void eloop_q_timeout_delete(struct eloop_ctx *, int, void (*)(void *), void *);
 struct eloop_ctx * eloop_init(void);
-#ifdef HAVE_KQUEUE
+#if defined(HAVE_KQUEUE) || defined(HAVE_EPOLL)
 int eloop_requeue(struct eloop_ctx *);
 #else
 #define eloop_requeue(a) (0)