chiark / gitweb /
bus: internalize a lot of protocol definitions
[elogind.git] / src / libsystemd-rtnl / sd-rtnl.c
index b375576835d6b84bcc1e35bea589dfe976d1fba3..8631ff05e0fed6c51127cbe7ac6c5431cc44d7e9 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "macro.h"
 #include "util.h"
+#include "hashmap.h"
 
 #include "sd-rtnl.h"
 #include "rtnl-internal.h"
@@ -118,6 +119,9 @@ sd_rtnl *sd_rtnl_unref(sd_rtnl *rtnl) {
                         sd_rtnl_message_unref(rtnl->wqueue[i]);
                 free(rtnl->wqueue);
 
+                hashmap_free_free(rtnl->reply_callbacks);
+                prioq_free(rtnl->reply_callbacks_prioq);
+
                 if (rtnl->fd >= 0)
                         close_nointr_nofail(rtnl->fd);
 
@@ -226,10 +230,65 @@ static int dispatch_wqueue(sd_rtnl *rtnl) {
         return ret;
 }
 
+static int process_timeout(sd_rtnl *rtnl) {
+        _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *m = NULL;
+        struct reply_callback *c;
+        usec_t n;
+        int r;
+
+        assert(rtnl);
+
+        c = prioq_peek(rtnl->reply_callbacks_prioq);
+        if (!c)
+                return 0;
+
+        n = now(CLOCK_MONOTONIC);
+        if (c->timeout > n)
+                return 0;
+
+        r = message_new_synthetic_error(-ETIMEDOUT, c->serial, &m);
+        if (r < 0)
+                return r;
+
+        assert_se(prioq_pop(rtnl->reply_callbacks_prioq) == c);
+        hashmap_remove(rtnl->reply_callbacks, &c->serial);
+
+        r = c->callback(rtnl, m, c->userdata);
+        free(c);
+
+        return r < 0 ? r : 1;
+}
+
+static int process_reply(sd_rtnl *rtnl, sd_rtnl_message *m) {
+        struct reply_callback *c;
+        uint64_t serial;
+        int r;
+
+        assert(rtnl);
+        assert(m);
+
+        serial = message_get_serial(m);
+        c = hashmap_remove(rtnl->reply_callbacks, &serial);
+        if (!c)
+                return 0;
+
+        if (c->timeout != 0)
+                prioq_remove(rtnl->reply_callbacks_prioq, c, &c->prioq_idx);
+
+        r = c->callback(rtnl, m, c->userdata);
+        free(c);
+
+        return r;
+}
+
 static int process_running(sd_rtnl *rtnl, sd_rtnl_message **ret) {
         _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *m = NULL;
         int r;
 
+        r = process_timeout(rtnl);
+        if (r != 0)
+                goto null_message;
+
         r = dispatch_wqueue(rtnl);
         if (r != 0)
                 goto null_message;
@@ -240,6 +299,10 @@ static int process_running(sd_rtnl *rtnl, sd_rtnl_message **ret) {
         if (!m)
                 goto null_message;
 
+        r = process_reply(rtnl, m);
+        if (r != 0)
+                goto null_message;
+
         if (ret) {
                 *ret = m;
                 m = NULL;
@@ -255,7 +318,9 @@ null_message:
 
         return r;
 }
+
 int sd_rtnl_process(sd_rtnl *rtnl, sd_rtnl_message **ret) {
+        RTNL_DONT_DESTROY(rtnl);
         int r;
 
         assert_return(rtnl, -EINVAL);
@@ -279,18 +344,44 @@ static usec_t calc_elapse(uint64_t usec) {
         return now(CLOCK_MONOTONIC) + usec;
 }
 
-static int rtnl_poll(sd_rtnl *nl, uint64_t timeout_usec) {
+static int rtnl_poll(sd_rtnl *rtnl, bool need_more, uint64_t timeout_usec) {
         struct pollfd p[1] = {};
         struct timespec ts;
-        int r;
+        usec_t m = (usec_t) -1;
+        int r, e;
+
+        assert(rtnl);
+
+        e = sd_rtnl_get_events(rtnl);
+        if (e < 0)
+                return e;
+
+        if (need_more)
+                /* Caller wants more data, and doesn't care about
+                 * what's been read or any other timeouts. */
+                return e |= POLLIN;
+        else {
+                usec_t until;
+                /* Caller wants to process if there is something to
+                 * process, but doesn't care otherwise */
 
-        assert(nl);
+                r = sd_rtnl_get_timeout(rtnl, &until);
+                if (r < 0)
+                        return r;
+                if (r > 0) {
+                        usec_t nw;
+                        nw = now(CLOCK_MONOTONIC);
+                        m = until > nw ? until - nw : 0;
+                }
+        }
 
-        p[0].fd = nl->fd;
-        p[0].events = POLLIN;
+        if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m))
+                m = timeout_usec;
 
-        r = ppoll(p, 1, timeout_usec == (uint64_t) -1 ? NULL :
-                        timespec_store(&ts, timeout_usec), NULL);
+        p[0].fd = rtnl->fd;
+        p[0].events = e;
+
+        r = ppoll(p, 1, m == (uint64_t) -1 ? NULL : timespec_store(&ts, m), NULL);
         if (r < 0)
                 return -errno;
 
@@ -304,7 +395,106 @@ int sd_rtnl_wait(sd_rtnl *nl, uint64_t timeout_usec) {
         if (nl->rqueue_size > 0)
                 return 0;
 
-        return rtnl_poll(nl, timeout_usec);
+        return rtnl_poll(nl, false, timeout_usec);
+}
+
+static int timeout_compare(const void *a, const void *b) {
+        const struct reply_callback *x = a, *y = b;
+
+        if (x->timeout != 0 && y->timeout == 0)
+                return -1;
+
+        if (x->timeout == 0 && y->timeout != 0)
+                return 1;
+
+        if (x->timeout < y->timeout)
+                return -1;
+
+        if (x->timeout > y->timeout)
+                return 1;
+
+        return 0;
+}
+
+int sd_rtnl_call_async(sd_rtnl *nl,
+                       sd_rtnl_message *m,
+                       sd_rtnl_message_handler_t callback,
+                       void *userdata,
+                       uint64_t usec,
+                       uint32_t *serial) {
+        struct reply_callback *c;
+        uint32_t s;
+        int r, k;
+
+        assert_return(nl, -EINVAL);
+        assert_return(m, -EINVAL);
+        assert_return(callback, -EINVAL);
+        assert_return(!rtnl_pid_changed(nl), -ECHILD);
+
+        r = hashmap_ensure_allocated(&nl->reply_callbacks, uint64_hash_func, uint64_compare_func);
+        if (r < 0)
+                return r;
+
+        if (usec != (uint64_t) -1) {
+                r = prioq_ensure_allocated(&nl->reply_callbacks_prioq, timeout_compare);
+                if (r < 0)
+                        return r;
+        }
+
+        c = new0(struct reply_callback, 1);
+        if (!c)
+                return -ENOMEM;
+
+        c->callback = callback;
+        c->userdata = userdata;
+        c->timeout = calc_elapse(usec);
+
+        k = sd_rtnl_send(nl, m, &s);
+        if (k < 0) {
+                free(c);
+                return k;
+        }
+
+        c->serial = s;
+
+        r = hashmap_put(nl->reply_callbacks, &c->serial, c);
+        if (r < 0) {
+                free(c);
+                return r;
+        }
+
+        if (c->timeout != 0) {
+                r = prioq_put(nl->reply_callbacks_prioq, c, &c->prioq_idx);
+                if (r > 0) {
+                        c->timeout = 0;
+                        sd_rtnl_call_async_cancel(nl, c->serial);
+                        return r;
+                }
+        }
+
+        if (serial)
+                *serial = s;
+
+        return k;
+}
+
+int sd_rtnl_call_async_cancel(sd_rtnl *nl, uint32_t serial) {
+        struct reply_callback *c;
+        uint64_t s = serial;
+
+        assert_return(nl, -EINVAL);
+        assert_return(serial != 0, -EINVAL);
+        assert_return(!rtnl_pid_changed(nl), -ECHILD);
+
+        c = hashmap_remove(nl->reply_callbacks, &s);
+        if (!c)
+                return 0;
+
+        if (c->timeout != 0)
+                prioq_remove(nl->reply_callbacks_prioq, c, &c->prioq_idx);
+
+        free(c);
+        return 1;
 }
 
 int sd_rtnl_call(sd_rtnl *nl,
@@ -354,7 +544,7 @@ int sd_rtnl_call(sd_rtnl *nl,
                         uint32_t received_serial = message_get_serial(incoming);
 
                         if (received_serial == serial) {
-                                r = message_get_errno(incoming);
+                                r = sd_rtnl_message_get_errno(incoming);
                                 if (r < 0)
                                         return r;
 
@@ -388,7 +578,7 @@ int sd_rtnl_call(sd_rtnl *nl,
                 } else
                         left = (uint64_t) -1;
 
-                r = rtnl_poll(nl, left);
+                r = rtnl_poll(nl, true, left);
                 if (r < 0)
                         return r;
 
@@ -397,3 +587,200 @@ int sd_rtnl_call(sd_rtnl *nl,
                         return r;
         }
 }
+
+int sd_rtnl_flush(sd_rtnl *rtnl) {
+        int r;
+
+        assert_return(rtnl, -EINVAL);
+        assert_return(!rtnl_pid_changed(rtnl), -ECHILD);
+
+        if (rtnl->wqueue_size <= 0)
+                return 0;
+
+        for (;;) {
+                r = dispatch_wqueue(rtnl);
+                if (r < 0)
+                        return r;
+
+                if (rtnl->wqueue_size <= 0)
+                        return 0;
+
+                r = rtnl_poll(rtnl, false, (uint64_t) -1);
+                if (r < 0)
+                        return r;
+        }
+}
+
+int sd_rtnl_get_events(sd_rtnl *rtnl) {
+        int flags = 0;
+
+        assert_return(rtnl, -EINVAL);
+        assert_return(!rtnl_pid_changed(rtnl), -ECHILD);
+
+        if (rtnl->rqueue_size <= 0)
+                flags |= POLLIN;
+        if (rtnl->wqueue_size > 0)
+                flags |= POLLOUT;
+
+        return flags;
+}
+
+int sd_rtnl_get_timeout(sd_rtnl *rtnl, uint64_t *timeout_usec) {
+        struct reply_callback *c;
+
+        assert_return(rtnl, -EINVAL);
+        assert_return(timeout_usec, -EINVAL);
+        assert_return(!rtnl_pid_changed(rtnl), -ECHILD);
+
+        if (rtnl->rqueue_size > 0) {
+                *timeout_usec = 0;
+                return 1;
+        }
+
+        c = prioq_peek(rtnl->reply_callbacks_prioq);
+        if (!c) {
+                *timeout_usec = (uint64_t) -1;
+                return 0;
+        }
+
+        *timeout_usec = c->timeout;
+
+        return 1;
+}
+
+static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
+        sd_rtnl *rtnl = userdata;
+        int r;
+
+        assert(rtnl);
+
+        r = sd_rtnl_process(rtnl, NULL);
+        if (r < 0)
+                return r;
+
+        return 1;
+}
+
+static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) {
+        sd_rtnl *rtnl = userdata;
+        int r;
+
+        assert(rtnl);
+
+        r = sd_rtnl_process(rtnl, NULL);
+        if (r < 0)
+                return r;
+
+        return 1;
+}
+
+static int prepare_callback(sd_event_source *s, void *userdata) {
+        sd_rtnl *rtnl = userdata;
+        int r, e;
+        usec_t until;
+
+        assert(s);
+        assert(rtnl);
+
+        e = sd_rtnl_get_events(rtnl);
+        if (e < 0)
+                return e;
+
+        r = sd_event_source_set_io_events(rtnl->io_event_source, e);
+        if (r < 0)
+                return r;
+
+        r = sd_rtnl_get_timeout(rtnl, &until);
+        if (r < 0)
+                return r;
+        if (r > 0) {
+                int j;
+
+                j = sd_event_source_set_time(rtnl->time_event_source, until);
+                if (j < 0)
+                        return j;
+        }
+
+        r = sd_event_source_set_enabled(rtnl->time_event_source, r > 0);
+        if (r < 0)
+                return r;
+
+        return 1;
+}
+
+static int quit_callback(sd_event_source *event, void *userdata) {
+        sd_rtnl *rtnl = userdata;
+
+        assert(event);
+
+        sd_rtnl_flush(rtnl);
+
+        return 1;
+}
+
+int sd_rtnl_attach_event(sd_rtnl *rtnl, sd_event *event, int priority) {
+        int r;
+
+        assert_return(rtnl, -EINVAL);
+        assert_return(!rtnl->event, -EBUSY);
+
+        assert(!rtnl->io_event_source);
+        assert(!rtnl->time_event_source);
+
+        if (event)
+                rtnl->event = sd_event_ref(event);
+        else {
+                r = sd_event_default(&rtnl->event);
+                if (r < 0)
+                        return r;
+        }
+
+        r = sd_event_add_io(rtnl->event, rtnl->fd, 0, io_callback, rtnl, &rtnl->io_event_source);
+        if (r < 0)
+                goto fail;
+
+        r = sd_event_source_set_priority(rtnl->io_event_source, priority);
+        if (r < 0)
+                goto fail;
+
+        r = sd_event_source_set_prepare(rtnl->io_event_source, prepare_callback);
+        if (r < 0)
+                goto fail;
+
+        r = sd_event_add_monotonic(rtnl->event, 0, 0, time_callback, rtnl, &rtnl->time_event_source);
+        if (r < 0)
+                goto fail;
+
+        r = sd_event_source_set_priority(rtnl->time_event_source, priority);
+        if (r < 0)
+                goto fail;
+
+        r = sd_event_add_quit(rtnl->event, quit_callback, rtnl, &rtnl->quit_event_source);
+        if (r < 0)
+                goto fail;
+
+        return 0;
+
+fail:
+        sd_rtnl_detach_event(rtnl);
+        return r;
+}
+
+int sd_rtnl_detach_event(sd_rtnl *rtnl) {
+        assert_return(rtnl, -EINVAL);
+        assert_return(rtnl->event, -ENXIO);
+
+        if (rtnl->io_event_source)
+                rtnl->io_event_source = sd_event_source_unref(rtnl->io_event_source);
+
+        if (rtnl->time_event_source)
+                rtnl->time_event_source = sd_event_source_unref(rtnl->time_event_source);
+
+        if (rtnl->quit_event_source)
+                rtnl->quit_event_source = sd_event_source_unref(rtnl->quit_event_source);
+
+        if (rtnl->event)
+                rtnl->event = sd_event_unref(rtnl->event);
+
+        return 0;
+}