chiark / gitweb /
resolved: add daemon to manage resolv.conf
[elogind.git] / src / network / networkd-wait-online.c
index 6530659c10e9a1e2972b1be66f54101a19643c80..3ea7a8384ebe15674a98e7c819c08e65b3331dc1 100644 (file)
@@ -116,6 +116,7 @@ static bool all_configured(Manager *m) {
         if (n <= 0)
                 return false;
 
+        /* wait for networkd to be aware of all the links given on the commandline */
         STRV_FOREACH(ifname, arg_interfaces) {
                 _cleanup_rtnl_message_unref_ sd_rtnl_message *message = NULL, *reply = NULL;
                 bool found = false;
@@ -164,56 +165,28 @@ static bool all_configured(Manager *m) {
                 if (!found) {
                         /* link exists, but networkd is not yet aware of it */
                         return false;
+                }
         }
 
+        /* wait for all links networkd manages to be in admin state 'configured'
+           and at least one link to gain a carrier */
         for (i = 0; i < n; i++) {
-                _cleanup_free_ char *state = NULL;
-
-                r = sd_network_get_link_state(indices[i], &state);
-                if (r == -EUNATCH) {
-                        _cleanup_rtnl_message_unref_ sd_rtnl_message *message = NULL, *reply = NULL;
-                        unsigned flags;
-                        uint8_t operstate;
-
-                        r = sd_rtnl_message_new_link(m->rtnl, &message, RTM_GETLINK, indices[i]);
-                        if (r < 0) {
-                                log_warning("could not create GETLINK message: %s", strerror(-r));
-                                return false;
-                        }
-
-                        r = sd_rtnl_call(m->rtnl, message, 0, &reply);
-                        if (r < 0) {
-                                log_debug("could not get link %u: %s", indices[i], strerror(-r));
-                                continue;
-                        }
-
-                        r = sd_rtnl_message_link_get_flags(reply, &flags);
-                        if (r < 0) {
-                                log_warning("could not get link flags: %s", strerror(-r));
-                                return false;
-                        }
-
-                        r = sd_rtnl_message_read_u8(reply, IFLA_OPERSTATE, &operstate);
-                        if (r < 0) {
-                                log_debug("could not get link operational state: %s", strerror(-r));
-                                operstate = IF_OPER_UNKNOWN;
-                        }
-                        
-                        if (!(flags & IFF_LOOPBACK) &&
-                            link_has_carrier(flags, operstate)) {
-                                /* this link is not managed by us,
-                                   but something else may have
-                                   made it ready, so don't block */
-                                one_ready = true;
-                        }
+                _cleanup_free_ char *state = NULL, *oper_state = NULL;
 
+                if (sd_network_link_is_loopback(indices[i]))
+                        /* ignore loopback devices */
                         continue;
-                } else if (r < 0 || !streq(state, "configured"))
-                        /* managed by networkd, but not yet configured */
+
+                r = sd_network_get_link_state(indices[i], &state);
+                if (r == -EBUSY || (r >= 0 && !streq(state, "configured")))
+                        /* not yet processed by udev, or managed by networkd, but not yet configured */
                         return false;
 
-                /* we wait for at least one link to appear */
-                one_ready = true;
+                r = sd_network_get_link_operational_state(indices[i], &oper_state);
+                if (r >= 0 && streq(oper_state, "carrier"))
+                        /* we wait for at least one link to be ready,
+                           regardless of who manages it */
+                        one_ready = true;
         }
 
         return one_ready;
@@ -229,17 +202,7 @@ static int monitor_event_handler(sd_event_source *s, int fd, uint32_t revents,
         if (all_configured(m))
                 sd_event_exit(m->event, 0);
 
-        return 1;
-}
-
-static int newlink_event_handler(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) {
-        Manager *m = userdata;
-
-        assert(m);
-        assert(m->event);
-
-        if (all_configured(m))
-                sd_event_exit(m->event, 0);
+        sd_network_monitor_flush(m->monitor);
 
         return 1;
 }
@@ -257,7 +220,6 @@ void manager_free(Manager *m) {
 int main(int argc, char *argv[]) {
         _cleanup_manager_free_ Manager *m = NULL;
         _cleanup_event_source_unref_ sd_event_source *event_source = NULL;
-        _cleanup_network_monitor_unref_ sd_network_monitor *monitor = NULL;
         int r, fd, events;
 
         umask(0022);
@@ -276,25 +238,31 @@ int main(int argc, char *argv[]) {
         if (!m)
                 return log_oom();
 
-        r = sd_network_monitor_new(NULL, &monitor);
+        r = sd_event_new(&m->event);
         if (r < 0) {
-                log_error("Could not create monitor: %s", strerror(-r));
+                log_error("Could not create event: %s", strerror(-r));
                 goto out;
         }
 
-        r = sd_event_new(&m->event);
+        r = sd_rtnl_open(&m->rtnl, 0);
         if (r < 0) {
-                log_error("Could not create event: %s", strerror(-r));
+                log_error("Could not create rtnl: %s", strerror(-r));
+                goto out;
+        }
+
+        r = sd_network_monitor_new(NULL, &m->monitor);
+        if (r < 0) {
+                log_error("Could not create monitor: %s", strerror(-r));
                 goto out;
         }
 
-        fd = sd_network_monitor_get_fd(monitor);
+        fd = sd_network_monitor_get_fd(m->monitor);
         if (fd < 0) {
                 log_error("Could not get monitor fd: %s", strerror(-r));
                 goto out;
         }
 
-        events = sd_network_monitor_get_events(monitor);
+        events = sd_network_monitor_get_events(m->monitor);
         if (events < 0) {
                 log_error("Could not get monitor events: %s", strerror(-r));
                 goto out;
@@ -307,22 +275,6 @@ int main(int argc, char *argv[]) {
                 goto out;
         }
 
-        r = sd_rtnl_open(&m->rtnl, RTMGRP_LINK);
-        if (r < 0) {
-                log_error("Could not create rtnl: %s", strerror(-r));
-                goto out;
-        }
-
-        r = sd_rtnl_attach_event(m->rtnl, m->event, 0);
-        if (r < 0) {
-                log_error("Could not attach event to rtnl: %s", strerror(-r));
-                return r;
-        }
-
-        r = sd_rtnl_add_match(m->rtnl, RTM_NEWLINK, newlink_event_handler, m);
-        if (r < 0)
-                return r;
-
         if (all_configured(m)) {
                 r = 0;
                 goto out;