chiark / gitweb /
add simple event loop
[elogind.git] / socket.c
index ed14db55f99c898e2851b0e569957be6c806a3a1..ac0ea124b4f081af0994cbd4d50205173ddd296b 100644 (file)
--- a/socket.c
+++ b/socket.c
@@ -5,6 +5,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <sys/poll.h>
 
 #include "name.h"
 #include "socket.h"
@@ -124,7 +125,9 @@ static void close_fds(Socket *s) {
                 if (p->fd < 0)
                         continue;
 
-                close_nointr(p->fd);
+                name_unwatch_fd(NAME(s), p->fd);
+                assert_se(close_nointr(p->fd) >= 0);
+
                 p->fd = -1;
         }
 }
@@ -185,6 +188,9 @@ static int socket_start(Name *n) {
                                 goto rollback;
                         }
                 }
+
+                if ((r = name_watch_fd(n, p->fd, POLLIN)) < 0)
+                        goto rollback;
         }
 
         socket_set_state(s, SOCKET_LISTENING);
@@ -231,6 +237,23 @@ static NameActiveState socket_active_state(Name *n) {
         return state_table[SOCKET(n)->state];
 }
 
+static void socket_fd_event(Name *n, int fd, uint32_t events) {
+        Socket *s = SOCKET(n);
+
+        assert(n);
+
+        if (events != POLLIN)
+                goto fail;
+
+        log_info("POLLIN on %s", name_id(n));
+
+        return;
+
+fail:
+        close_fds(s);
+        socket_set_state(s, SOCKET_MAINTAINANCE);
+}
+
 static void socket_free_hook(Name *n) {
         SocketExecCommand c;
         Socket *s = SOCKET(n);
@@ -268,5 +291,7 @@ const NameVTable socket_vtable = {
 
         .active_state = socket_active_state,
 
+        .fd_event = socket_fd_event,
+
         .free_hook = socket_free_hook
 };