diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2019-07-12 04:11:08 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2019-07-16 23:59:12 +0200 |
commit | 071712b281a018a20f329ec46f9230e0f2624443 (patch) | |
tree | cbaa913951feff07cd7f8dfde8b405e1773f5ce8 /src/network/networkd-manager-bus.c | |
parent | network: implement ListLinks DBus method (diff) | |
download | systemd-071712b281a018a20f329ec46f9230e0f2624443.tar.xz systemd-071712b281a018a20f329ec46f9230e0f2624443.zip |
network: introduce GetLinkByName and GetLinkByIndex bus methods
Diffstat (limited to 'src/network/networkd-manager-bus.c')
-rw-r--r-- | src/network/networkd-manager-bus.c | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/network/networkd-manager-bus.c b/src/network/networkd-manager-bus.c index b52e3f7dd6..6174104f5e 100644 --- a/src/network/networkd-manager-bus.c +++ b/src/network/networkd-manager-bus.c @@ -1,6 +1,9 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ +#include <net/if.h> + #include "alloc-util.h" +#include "bus-common-errors.h" #include "bus-util.h" #include "networkd-link-bus.h" #include "networkd-link.h" @@ -47,6 +50,72 @@ static int method_list_links(sd_bus_message *message, void *userdata, sd_bus_err return sd_bus_send(NULL, reply, NULL); } +static int method_get_link_by_name(sd_bus_message *message, void *userdata, sd_bus_error *error) { + _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; + _cleanup_free_ char *path = NULL; + Manager *manager = userdata; + const char *name; + int index, r; + Link *link; + + r = sd_bus_message_read(message, "s", &name); + if (r < 0) + return r; + + index = if_nametoindex(name); + if (index <= 0) + return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %s not known", name); + + link = hashmap_get(manager->links, INT_TO_PTR(index)); + if (!link) + return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %s not known", name); + + r = sd_bus_message_new_method_return(message, &reply); + if (r < 0) + return r; + + path = link_bus_path(link); + if (!path) + return -ENOMEM; + + r = sd_bus_message_append(reply, "io", link->ifindex, empty_to_root(path)); + if (r < 0) + return r; + + return sd_bus_send(NULL, reply, NULL); +} + +static int method_get_link_by_index(sd_bus_message *message, void *userdata, sd_bus_error *error) { + _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; + _cleanup_free_ char *path = NULL; + Manager *manager = userdata; + int32_t index; + Link *link; + int r; + + r = sd_bus_message_read(message, "i", &index); + if (r < 0) + return r; + + link = hashmap_get(manager->links, INT_TO_PTR((int) index)); + if (!link) + return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %" PRIi32 " not known", index); + + r = sd_bus_message_new_method_return(message, &reply); + if (r < 0) + return r; + + path = link_bus_path(link); + if (!path) + return -ENOMEM; + + r = sd_bus_message_append(reply, "so", link->ifname, empty_to_root(path)); + if (r < 0) + return r; + + return sd_bus_send(NULL, reply, NULL); +} + const sd_bus_vtable manager_vtable[] = { SD_BUS_VTABLE_START(0), @@ -55,6 +124,8 @@ const sd_bus_vtable manager_vtable[] = { SD_BUS_PROPERTY("AddressState", "s", property_get_address_state, offsetof(Manager, address_state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), SD_BUS_METHOD("ListLinks", NULL, "a(iso)", method_list_links, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("GetLinkByName", "s", "io", method_get_link_by_name, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("GetLinkByIndex", "i", "so", method_get_link_by_index, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_VTABLE_END }; |