#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;
}