]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-link-bus.c
Merge pull request #2085 from fbuihuu/more-use-of-check-load-state
[thirdparty/systemd.git] / src / network / networkd-link-bus.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2015 Tom Gundersen
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "alloc-util.h"
23 #include "bus-util.h"
24 #include "networkd-link.h"
25 #include "networkd.h"
26 #include "parse-util.h"
27 #include "strv.h"
28
29 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_operational_state, link_operstate, LinkOperationalState);
30 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_administrative_state, link_state, LinkState);
31
32 const sd_bus_vtable link_vtable[] = {
33 SD_BUS_VTABLE_START(0),
34
35 SD_BUS_PROPERTY("OperationalState", "s", property_get_operational_state, offsetof(Link, operstate), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
36 SD_BUS_PROPERTY("AdministrativeState", "s", property_get_administrative_state, offsetof(Link, state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
37
38 SD_BUS_VTABLE_END
39 };
40
41 static char *link_bus_path(Link *link) {
42 _cleanup_free_ char *ifindex = NULL;
43 char *p;
44 int r;
45
46 assert(link);
47 assert(link->ifindex > 0);
48
49 if (asprintf(&ifindex, "%d", link->ifindex) < 0)
50 return NULL;
51
52 r = sd_bus_path_encode("/org/freedesktop/network1/link", ifindex, &p);
53 if (r < 0)
54 return NULL;
55
56 return p;
57 }
58
59 int link_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
60 _cleanup_strv_free_ char **l = NULL;
61 Manager *m = userdata;
62 unsigned c = 0;
63 Link *link;
64 Iterator i;
65
66 assert(bus);
67 assert(path);
68 assert(m);
69 assert(nodes);
70
71 l = new0(char*, hashmap_size(m->links) + 1);
72 if (!l)
73 return -ENOMEM;
74
75 HASHMAP_FOREACH(link, m->links, i) {
76 char *p;
77
78 p = link_bus_path(link);
79 if (!p)
80 return -ENOMEM;
81
82 l[c++] = p;
83 }
84
85 l[c] = NULL;
86 *nodes = l;
87 l = NULL;
88
89 return 1;
90 }
91
92 int link_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
93 _cleanup_free_ char *identifier = NULL;
94 Manager *m = userdata;
95 Link *link;
96 int ifindex, r;
97
98 assert(bus);
99 assert(path);
100 assert(interface);
101 assert(m);
102 assert(found);
103
104 r = sd_bus_path_decode(path, "/org/freedesktop/network1/link", &identifier);
105 if (r <= 0)
106 return 0;
107
108 r = parse_ifindex(identifier, &ifindex);
109 if (r < 0)
110 return 0;
111
112 r = link_get(m, ifindex, &link);
113 if (r < 0)
114 return 0;
115
116 *found = link;
117
118 return 1;
119 }
120
121 int link_send_changed(Link *link, const char *property, ...) {
122 _cleanup_free_ char *p = NULL;
123 char **l;
124
125 assert(link);
126 assert(link->manager);
127
128 if (!link->manager->bus)
129 return 0; /* replace with assert when we have kdbus */
130
131 l = strv_from_stdarg_alloca(property);
132
133 p = link_bus_path(link);
134 if (!p)
135 return -ENOMEM;
136
137 return sd_bus_emit_properties_changed_strv(
138 link->manager->bus,
139 p,
140 "org.freedesktop.network1.Link",
141 l);
142 }