]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: use the overall online state in network_is_online()
authorAlvin Šipraga <alsi@bang-olufsen.dk>
Tue, 30 Mar 2021 21:18:08 +0000 (23:18 +0200)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 19 May 2021 01:34:06 +0000 (10:34 +0900)
Since networkd advertises a reliable online state, use it in
network_is_online(). If for some reason networkd does not know the
online state (e.g. it does not manage any of the network interfaces),
fall back to the original best-guess logic.

src/libsystemd/sd-network/network-util.c

index b01f88366693c425326879020d359ab0285d7977..4a648caa5daac129978cd994a0631e085f123e7d 100644 (file)
 #include "strv.h"
 
 bool network_is_online(void) {
-        _cleanup_free_ char *carrier_state = NULL, *addr_state = NULL;
+        _cleanup_free_ char *online_state = NULL;
+        LinkOnlineState state;
         int r;
 
-        r = sd_network_get_carrier_state(&carrier_state);
-        if (r < 0) /* if we don't know anything, we consider the system online */
-                return true;
+        r = sd_network_get_online_state(&online_state);
+        if (r < 0)
+                state = _LINK_ONLINE_STATE_INVALID;
+        else
+                state = link_online_state_from_string(online_state);
 
-        r = sd_network_get_address_state(&addr_state);
-        if (r < 0) /* if we don't know anything, we consider the system online */
+        if (state >= LINK_ONLINE_STATE_PARTIAL)
                 return true;
+        else if (state < 0) {
+                _cleanup_free_ char *carrier_state = NULL, *addr_state = NULL;
 
-        if (STR_IN_SET(carrier_state, "degraded-carrier", "carrier") &&
-            STR_IN_SET(addr_state, "routable", "degraded"))
-                return true;
+                r = sd_network_get_carrier_state(&carrier_state);
+                if (r < 0) /* if we don't know anything, we consider the system online */
+                        return true;
+
+                r = sd_network_get_address_state(&addr_state);
+                if (r < 0) /* if we don't know anything, we consider the system online */
+                        return true;
+
+                /* we don't know the online state for certain, so make an educated guess */
+                if (STR_IN_SET(carrier_state, "degraded-carrier", "carrier") &&
+                    STR_IN_SET(addr_state, "routable", "degraded"))
+                        return true;
+        }
 
         return false;
 }