]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/network/wait-online/manager.c
tree-wide: define iterator inside of the macro
[thirdparty/systemd.git] / src / network / wait-online / manager.c
index eb551f116aedac50144a062987a45fa1d1633d7b..e84eed46ac8d0ba93c2eaf4e8e2b1384b5c91ac1 100644 (file)
@@ -29,10 +29,10 @@ static bool manager_ignore_link(Manager *m, Link *link) {
                 return true;
 
         /* ignore interfaces we explicitly are asked to ignore */
-        return strv_fnmatch(m->ignore, link->ifname, 0);
+        return strv_fnmatch(m->ignore, link->ifname);
 }
 
-static int manager_link_is_online(Manager *m, Link *l, LinkOperationalState s) {
+static int manager_link_is_online(Manager *m, Link *l, LinkOperationalStateRange s) {
         /* This returns the following:
          * -EAGAIN: not processed by udev or networkd
          *       0: operstate is not enough
@@ -46,55 +46,75 @@ static int manager_link_is_online(Manager *m, Link *l, LinkOperationalState s) {
                 return log_link_debug_errno(l, SYNTHETIC_ERRNO(EAGAIN),
                                             "link is being processed by networkd");
 
-        if (s < 0)
-                s = m->required_operstate >= 0 ? m->required_operstate : l->required_operstate;
+        if (s.min < 0)
+                s.min = m->required_operstate.min >= 0 ? m->required_operstate.min
+                                                       : l->required_operstate.min;
 
-        if (l->operational_state < s) {
-                log_link_debug(l, "Operational state '%s' is below '%s'",
+        if (s.max < 0)
+                s.max = m->required_operstate.max >= 0 ? m->required_operstate.max
+                                                       : l->required_operstate.max;
+
+        if (l->operational_state < s.min || l->operational_state > s.max) {
+                log_link_debug(l, "Operational state '%s' is not in range ['%s':'%s']",
                                link_operstate_to_string(l->operational_state),
-                               link_operstate_to_string(s));
+                               link_operstate_to_string(s.min), link_operstate_to_string(s.max));
                 return 0;
         }
 
         return 1;
 }
 
-bool manager_all_configured(Manager *m) {
+bool manager_configured(Manager *m) {
         bool one_ready = false;
-        Iterator i;
         const char *ifname;
         void *p;
         Link *l;
         int r;
 
-        /* wait for all the links given on the command line to appear */
-        HASHMAP_FOREACH_KEY(p, ifname, m->interfaces, i) {
-                LinkOperationalState s = PTR_TO_INT(p);
+        if (!hashmap_isempty(m->interfaces)) {
+                /* wait for all the links given on the command line to appear */
+                HASHMAP_FOREACH_KEY(p, ifname, m->interfaces) {
+                        LinkOperationalStateRange *range = p;
+
+                        l = hashmap_get(m->links_by_name, ifname);
+                        if (!l && range->min == LINK_OPERSTATE_MISSING) {
+                                one_ready = true;
+                                continue;
+                        }
+
+                        if (!l) {
+                                log_debug("still waiting for %s", ifname);
+                                if (!m->any)
+                                        return false;
+                                continue;
+                        }
+
+                        if (manager_link_is_online(m, l, *range) <= 0) {
+                                if (!m->any)
+                                        return false;
+                                continue;
+                        }
 
-                l = hashmap_get(m->links_by_name, ifname);
-                if (!l) {
-                        log_debug("still waiting for %s", ifname);
-                        return false;
+                        one_ready = true;
                 }
 
-                if (manager_link_is_online(m, l, s) <= 0)
-                        return false;
+                /* all interfaces given by the command line are online, or
+                 * one of the specified interfaces is online. */
+                return one_ready;
         }
 
-        if (!hashmap_isempty(m->interfaces))
-                /* all interfaces given by the command line are online. */
-                return true;
-
         /* wait for all links networkd manages to be in admin state 'configured'
          * and at least one link to gain a carrier */
-        HASHMAP_FOREACH(l, m->links, i) {
+        HASHMAP_FOREACH(l, m->links) {
                 if (manager_ignore_link(m, l)) {
-                        log_link_info(l, "link is ignored");
+                        log_link_debug(l, "link is ignored");
                         continue;
                 }
 
-                r = manager_link_is_online(m, l, _LINK_OPERSTATE_INVALID);
-                if (r < 0)
+                r = manager_link_is_online(m, l,
+                                           (LinkOperationalStateRange) { _LINK_OPERSTATE_INVALID,
+                                                                         _LINK_OPERSTATE_INVALID });
+                if (r < 0 && !m->any)
                         return false;
                 if (r > 0)
                         /* we wait for at least one link to be ready,
@@ -155,7 +175,7 @@ static int manager_process_link(sd_netlink *rtnl, sd_netlink_message *mm, void *
                         log_link_warning_errno(l, r, "Failed to process RTNL link message, ignoring: %m");
 
                 r = link_update_monitor(l);
-                if (r < 0)
+                if (r < 0 && r != -ENODATA)
                         log_link_warning_errno(l, r, "Failed to update link state, ignoring: %m");
 
                 break;
@@ -180,7 +200,7 @@ static int on_rtnl_event(sd_netlink *rtnl, sd_netlink_message *mm, void *userdat
         if (r < 0)
                 return r;
 
-        if (manager_all_configured(m))
+        if (manager_configured(m))
                 sd_event_exit(m->event, 0);
 
         return 1;
@@ -234,7 +254,6 @@ static int manager_rtnl_listen(Manager *m) {
 
 static int on_network_event(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
         Manager *m = userdata;
-        Iterator i;
         Link *l;
         int r;
 
@@ -242,13 +261,13 @@ static int on_network_event(sd_event_source *s, int fd, uint32_t revents, void *
 
         sd_network_monitor_flush(m->network_monitor);
 
-        HASHMAP_FOREACH(l, m->links, i) {
+        HASHMAP_FOREACH(l, m->links) {
                 r = link_update_monitor(l);
-                if (r < 0)
-                        log_link_warning_errno(l, r, "Failed to update monitor information: %m");
+                if (r < 0 && r != -ENODATA)
+                        log_link_warning_errno(l, r, "Failed to update link state, ignoring: %m");
         }
 
-        if (manager_all_configured(m))
+        if (manager_configured(m))
                 sd_event_exit(m->event, 0);
 
         return 0;
@@ -280,7 +299,8 @@ static int manager_network_monitor_listen(Manager *m) {
 }
 
 int manager_new(Manager **ret, Hashmap *interfaces, char **ignore,
-                LinkOperationalState required_operstate, usec_t timeout) {
+                LinkOperationalStateRange required_operstate,
+                bool any, usec_t timeout) {
         _cleanup_(manager_freep) Manager *m = NULL;
         int r;
 
@@ -294,13 +314,14 @@ int manager_new(Manager **ret, Hashmap *interfaces, char **ignore,
                 .interfaces = interfaces,
                 .ignore = ignore,
                 .required_operstate = required_operstate,
+                .any = any,
         };
 
         r = sd_event_default(&m->event);
         if (r < 0)
                 return r;
 
-        (void) sd_event_add_signal(m->event, NULL, SIGTERM, NULL,  NULL);
+        (void) sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
         (void) sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
 
         if (timeout > 0) {