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