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