]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
e331e246 | 2 | |
071712b2 YW |
3 | #include <net/if.h> |
4 | ||
b5efdb8a | 5 | #include "alloc-util.h" |
071712b2 | 6 | #include "bus-common-errors.h" |
e331e246 | 7 | #include "bus-util.h" |
7f3c07ad | 8 | #include "networkd-link-bus.h" |
37d577c8 | 9 | #include "networkd-link.h" |
79a59fa5 | 10 | #include "networkd-manager-bus.h" |
23f53b99 | 11 | #include "networkd-manager.h" |
37d577c8 | 12 | #include "path-util.h" |
51517f9e | 13 | #include "strv.h" |
e331e246 | 14 | |
37d577c8 YW |
15 | static int method_list_links(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
16 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
17 | Manager *manager = userdata; | |
18 | Iterator i; | |
19 | Link *link; | |
20 | int r; | |
21 | ||
22 | r = sd_bus_message_new_method_return(message, &reply); | |
23 | if (r < 0) | |
24 | return r; | |
25 | ||
26 | r = sd_bus_message_open_container(reply, 'a', "(iso)"); | |
27 | if (r < 0) | |
28 | return r; | |
29 | ||
30 | HASHMAP_FOREACH(link, manager->links, i) { | |
31 | _cleanup_free_ char *path = NULL; | |
32 | ||
33 | path = link_bus_path(link); | |
34 | if (!path) | |
35 | return -ENOMEM; | |
36 | ||
37 | r = sd_bus_message_append( | |
38 | reply, "(iso)", | |
39 | link->ifindex, | |
40 | link->ifname, | |
41 | empty_to_root(path)); | |
42 | if (r < 0) | |
43 | return r; | |
44 | } | |
45 | ||
46 | r = sd_bus_message_close_container(reply); | |
47 | if (r < 0) | |
48 | return r; | |
49 | ||
50 | return sd_bus_send(NULL, reply, NULL); | |
51 | } | |
52 | ||
071712b2 YW |
53 | static int method_get_link_by_name(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
54 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
55 | _cleanup_free_ char *path = NULL; | |
56 | Manager *manager = userdata; | |
57 | const char *name; | |
58 | int index, r; | |
59 | Link *link; | |
60 | ||
61 | r = sd_bus_message_read(message, "s", &name); | |
62 | if (r < 0) | |
63 | return r; | |
64 | ||
65 | index = if_nametoindex(name); | |
66 | if (index <= 0) | |
67 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %s not known", name); | |
68 | ||
69 | link = hashmap_get(manager->links, INT_TO_PTR(index)); | |
70 | if (!link) | |
71 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %s not known", name); | |
72 | ||
73 | r = sd_bus_message_new_method_return(message, &reply); | |
74 | if (r < 0) | |
75 | return r; | |
76 | ||
77 | path = link_bus_path(link); | |
78 | if (!path) | |
79 | return -ENOMEM; | |
80 | ||
81 | r = sd_bus_message_append(reply, "io", link->ifindex, empty_to_root(path)); | |
82 | if (r < 0) | |
83 | return r; | |
84 | ||
85 | return sd_bus_send(NULL, reply, NULL); | |
86 | } | |
87 | ||
88 | static int method_get_link_by_index(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
89 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
90 | _cleanup_free_ char *path = NULL; | |
91 | Manager *manager = userdata; | |
92 | int32_t index; | |
93 | Link *link; | |
94 | int r; | |
95 | ||
96 | r = sd_bus_message_read(message, "i", &index); | |
97 | if (r < 0) | |
98 | return r; | |
99 | ||
100 | link = hashmap_get(manager->links, INT_TO_PTR((int) index)); | |
101 | if (!link) | |
102 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %" PRIi32 " not known", index); | |
103 | ||
104 | r = sd_bus_message_new_method_return(message, &reply); | |
105 | if (r < 0) | |
106 | return r; | |
107 | ||
108 | path = link_bus_path(link); | |
109 | if (!path) | |
110 | return -ENOMEM; | |
111 | ||
112 | r = sd_bus_message_append(reply, "so", link->ifname, empty_to_root(path)); | |
113 | if (r < 0) | |
114 | return r; | |
115 | ||
116 | return sd_bus_send(NULL, reply, NULL); | |
117 | } | |
118 | ||
e331e246 TG |
119 | const sd_bus_vtable manager_vtable[] = { |
120 | SD_BUS_VTABLE_START(0), | |
121 | ||
122 | SD_BUS_PROPERTY("OperationalState", "s", property_get_operational_state, offsetof(Manager, operational_state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
7f3c07ad YW |
123 | SD_BUS_PROPERTY("CarrierState", "s", property_get_carrier_state, offsetof(Manager, carrier_state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
124 | SD_BUS_PROPERTY("AddressState", "s", property_get_address_state, offsetof(Manager, address_state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
e331e246 | 125 | |
37d577c8 | 126 | SD_BUS_METHOD("ListLinks", NULL, "a(iso)", method_list_links, SD_BUS_VTABLE_UNPRIVILEGED), |
071712b2 YW |
127 | SD_BUS_METHOD("GetLinkByName", "s", "io", method_get_link_by_name, SD_BUS_VTABLE_UNPRIVILEGED), |
128 | SD_BUS_METHOD("GetLinkByIndex", "i", "so", method_get_link_by_index, SD_BUS_VTABLE_UNPRIVILEGED), | |
37d577c8 | 129 | |
e331e246 TG |
130 | SD_BUS_VTABLE_END |
131 | }; | |
132 | ||
46606fdd | 133 | int manager_send_changed_strv(Manager *manager, char **properties) { |
e331e246 | 134 | assert(manager); |
46606fdd | 135 | assert(properties); |
e331e246 | 136 | |
4852e159 | 137 | if (!manager->bus) |
46606fdd | 138 | return 0; |
e331e246 TG |
139 | |
140 | return sd_bus_emit_properties_changed_strv( | |
141 | manager->bus, | |
142 | "/org/freedesktop/network1", | |
143 | "org.freedesktop.network1.Manager", | |
46606fdd YW |
144 | properties); |
145 | } |