]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-wait-online-link.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / network / networkd-wait-online-link.c
CommitLineData
7de12ae7
TG
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2014 Lennart Poettering
7 Copyright 2014 Tom Gundersen
8
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21***/
22
7de12ae7 23#include "sd-network.h"
7de12ae7
TG
24
25#include "networkd-wait-online-link.h"
07630cea 26#include "string-util.h"
7de12ae7
TG
27
28int link_new(Manager *m, Link **ret, int ifindex, const char *ifname) {
29 _cleanup_(link_freep) Link *l = NULL;
30 int r;
31
32 assert(m);
33 assert(ifindex > 0);
34
d5099efc 35 r = hashmap_ensure_allocated(&m->links, NULL);
7de12ae7
TG
36 if (r < 0)
37 return r;
38
d5099efc 39 r = hashmap_ensure_allocated(&m->links_by_name, &string_hash_ops);
7de12ae7
TG
40 if (r < 0)
41 return r;
42
43 l = new0(Link, 1);
44 if (!l)
45 return -ENOMEM;
46
47 l->manager = m;
48
49 l->ifname = strdup(ifname);
50 if (!l->ifname)
51 return -ENOMEM;
52
53 r = hashmap_put(m->links_by_name, l->ifname, l);
54 if (r < 0)
55 return r;
56
57 l->ifindex = ifindex;
58
59 r = hashmap_put(m->links, INT_TO_PTR(ifindex), l);
60 if (r < 0)
61 return r;
62
63 if (ret)
64 *ret = l;
65 l = NULL;
66
67 return 0;
68}
69
70Link *link_free(Link *l) {
71
72 if (!l)
73 return NULL;
74
75 if (l->manager) {
76 hashmap_remove(l->manager->links, INT_TO_PTR(l->ifindex));
77 hashmap_remove(l->manager->links_by_name, l->ifname);
78 }
79
80 free(l->ifname);
81 free(l);
82 return NULL;
83 }
84
1c4baffc 85int link_update_rtnl(Link *l, sd_netlink_message *m) {
d2437732 86 const char *ifname;
7de12ae7
TG
87 int r;
88
89 assert(l);
90 assert(l->manager);
91 assert(m);
92
93 r = sd_rtnl_message_link_get_flags(m, &l->flags);
94 if (r < 0)
95 return r;
96
1c4baffc 97 r = sd_netlink_message_read_string(m, IFLA_IFNAME, &ifname);
7de12ae7
TG
98 if (r < 0)
99 return r;
100
101 if (!streq(l->ifname, ifname)) {
102 char *new_ifname;
103
104 new_ifname = strdup(ifname);
105 if (!new_ifname)
106 return -ENOMEM;
107
108 hashmap_remove(l->manager->links_by_name, l->ifname);
109 free(l->ifname);
110 l->ifname = new_ifname;
111
112 r = hashmap_put(l->manager->links_by_name, l->ifname, l);
113 if (r < 0)
114 return r;
115 }
116
117 return 0;
118}
119
120int link_update_monitor(Link *l) {
121 assert(l);
122
a1e58e8e 123 l->operational_state = mfree(l->operational_state);
7de12ae7 124
d6731e4c 125 sd_network_link_get_operational_state(l->ifindex, &l->operational_state);
7de12ae7 126
a1e58e8e 127 l->state = mfree(l->state);
7de12ae7 128
438ca2bb 129 sd_network_link_get_setup_state(l->ifindex, &l->state);
7de12ae7
TG
130
131 return 0;
132}