]>
git.proxmox.com Git - systemd.git/blob - src/network/wait-online/link.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "sd-network.h"
5 #include "alloc-util.h"
9 #include "string-util.h"
11 int link_new(Manager
*m
, Link
**ret
, int ifindex
, const char *ifname
) {
12 _cleanup_(link_freep
) Link
*l
= NULL
;
13 _cleanup_free_
char *n
= NULL
;
19 r
= hashmap_ensure_allocated(&m
->links
, NULL
);
23 r
= hashmap_ensure_allocated(&m
->links_by_name
, &string_hash_ops
);
37 .ifname
= TAKE_PTR(n
),
39 .required_operstate
= LINK_OPERSTATE_DEGRADED
,
42 r
= hashmap_put(m
->links_by_name
, l
->ifname
, l
);
46 r
= hashmap_put(m
->links
, INT_TO_PTR(ifindex
), l
);
57 Link
*link_free(Link
*l
) {
63 hashmap_remove(l
->manager
->links
, INT_TO_PTR(l
->ifindex
));
64 hashmap_remove(l
->manager
->links_by_name
, l
->ifname
);
72 int link_update_rtnl(Link
*l
, sd_netlink_message
*m
) {
80 r
= sd_rtnl_message_link_get_flags(m
, &l
->flags
);
84 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
88 if (!streq(l
->ifname
, ifname
)) {
91 new_ifname
= strdup(ifname
);
95 assert_se(hashmap_remove(l
->manager
->links_by_name
, l
->ifname
) == l
);
96 free_and_replace(l
->ifname
, new_ifname
);
98 r
= hashmap_put(l
->manager
->links_by_name
, l
->ifname
, l
);
106 int link_update_monitor(Link
*l
) {
107 _cleanup_free_
char *operstate
= NULL
, *required_operstate
= NULL
, *state
= NULL
;
108 LinkOperationalState s
;
114 r
= sd_network_link_get_required_for_online(l
->ifindex
);
116 ret
= log_link_debug_errno(l
, r
, "Failed to determine whether the link is required for online or not, "
119 l
->required_for_online
= r
> 0;
121 r
= sd_network_link_get_required_operstate_for_online(l
->ifindex
, &required_operstate
);
123 ret
= log_link_debug_errno(l
, r
, "Failed to get required operational state, ignoring: %m");
125 s
= link_operstate_from_string(required_operstate
);
127 ret
= log_link_debug_errno(l
, SYNTHETIC_ERRNO(EINVAL
),
128 "Failed to parse required operational state, ignoring: %m");
130 l
->required_operstate
= s
;
133 r
= sd_network_link_get_operational_state(l
->ifindex
, &operstate
);
135 ret
= log_link_debug_errno(l
, r
, "Failed to get operational state, ignoring: %m");
137 s
= link_operstate_from_string(operstate
);
139 ret
= log_link_debug_errno(l
, SYNTHETIC_ERRNO(EINVAL
),
140 "Failed to parse operational state, ignoring: %m");
142 l
->operational_state
= s
;
145 r
= sd_network_link_get_setup_state(l
->ifindex
, &state
);
147 ret
= log_link_debug_errno(l
, r
, "Failed to get setup state, ignoring: %m");
149 free_and_replace(l
->state
, state
);