]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/network/wait-online/manager.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / network / wait-online / manager.c
index d51b0a59d6e3bf9d10b2f156ad5df2c710d0da46..67218b6db3635d596fe49744a76ccb6dbe2c288b 100644 (file)
@@ -1,21 +1,4 @@
-/***
-  This file is part of systemd.
-
-  Copyright 2013 Tom Gundersen <teg@jklm.no>
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
+/* SPDX-License-Identifier: LGPL-2.1+ */
 
 #include <netinet/ether.h>
 #include <linux/if.h>
@@ -26,6 +9,7 @@
 #include "manager.h"
 #include "netlink-util.h"
 #include "network-internal.h"
+#include "strv.h"
 #include "time-util.h"
 #include "util.h"
 
@@ -41,6 +25,9 @@ bool manager_ignore_link(Manager *m, Link *link) {
         if (m->interfaces && !strv_contains(m->interfaces, link->ifname))
                 return true;
 
+        if (!link->required_for_online)
+                return true;
+
         /* ignore interfaces we explicitly are asked to ignore */
         return strv_fnmatch(m->ignore, link->ifname, 0);
 }
@@ -102,16 +89,25 @@ static int manager_process_link(sd_netlink *rtnl, sd_netlink_message *mm, void *
         assert(mm);
 
         r = sd_netlink_message_get_type(mm, &type);
-        if (r < 0)
-                goto fail;
+        if (r < 0) {
+                log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m");
+                return 0;
+        }
 
         r = sd_rtnl_message_link_get_ifindex(mm, &ifindex);
-        if (r < 0)
-                goto fail;
+        if (r < 0) {
+                log_warning_errno(r, "rtnl: Could not get ifindex from link, ignoring: %m");
+                return 0;
+        } else if (ifindex <= 0) {
+                log_warning("rtnl: received link message with invalid ifindex %d, ignoring", ifindex);
+                return 0;
+        }
 
         r = sd_netlink_message_read_string(mm, IFLA_IFNAME, &ifname);
-        if (r < 0)
-                goto fail;
+        if (r < 0) {
+                log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m");
+                return 0;
+        }
 
         l = hashmap_get(m->links, INT_TO_PTR(ifindex));
 
@@ -123,16 +119,16 @@ static int manager_process_link(sd_netlink *rtnl, sd_netlink_message *mm, void *
 
                         r = link_new(m, &l, ifindex, ifname);
                         if (r < 0)
-                                goto fail;
+                                return log_error_errno(r, "Failed to create link object: %m");
 
                         r = link_update_monitor(l);
                         if (r < 0)
-                                goto fail;
+                                return log_error_errno(r, "Failed to initialize link object: %m");
                 }
 
                 r = link_update_rtnl(l, mm);
                 if (r < 0)
-                        goto fail;
+                        return log_warning_errno(r, "Failed to process RTNL link message: %m");;
 
                 break;
 
@@ -146,10 +142,6 @@ static int manager_process_link(sd_netlink *rtnl, sd_netlink_message *mm, void *
         }
 
         return 0;
-
-fail:
-        log_warning_errno(r, "Failed to process RTNL link message: %m");
-        return 0;
 }
 
 static int on_rtnl_event(sd_netlink *rtnl, sd_netlink_message *mm, void *userdata) {
@@ -182,11 +174,11 @@ static int manager_rtnl_listen(Manager *m) {
         if (r < 0)
                 return r;
 
-        r = sd_netlink_add_match(m->rtnl, RTM_NEWLINK, on_rtnl_event, m);
+        r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWLINK, on_rtnl_event, NULL, m, "wait-online-on-NEWLINK");
         if (r < 0)
                 return r;
 
-        r = sd_netlink_add_match(m->rtnl, RTM_DELLINK, on_rtnl_event, m);
+        r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELLINK, on_rtnl_event, NULL, m, "wait-online-on-DELLINK");
         if (r < 0)
                 return r;
 
@@ -276,8 +268,8 @@ int manager_new(Manager **ret, char **interfaces, char **ignore, usec_t timeout)
         if (r < 0)
                 return r;
 
-        sd_event_add_signal(m->event, NULL, SIGTERM, NULL,  NULL);
-        sd_event_add_signal(m->event, NULL, SIGINT, 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) {
                 usec_t usec;
@@ -299,21 +291,16 @@ int manager_new(Manager **ret, char **interfaces, char **ignore, usec_t timeout)
         if (r < 0)
                 return r;
 
-        *ret = m;
-        m = NULL;
+        *ret = TAKE_PTR(m);
 
         return 0;
 }
 
 void manager_free(Manager *m) {
-        Link *l;
-
         if (!m)
                 return;
 
-        while ((l = hashmap_first(m->links)))
-               link_free(l);
-        hashmap_free(m->links);
+        hashmap_free_with_destructor(m->links, link_free);
         hashmap_free(m->links_by_name);
 
         sd_event_source_unref(m->network_monitor_event_source);