From: Yu Watanabe Date: Fri, 12 Jul 2019 02:11:08 +0000 (+0900) Subject: network: introduce GetLinkByName and GetLinkByIndex bus methods X-Git-Tag: v243-rc1~100^2~9 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=071712b281a018a20f329ec46f9230e0f2624443;p=thirdparty%2Fsystemd.git network: introduce GetLinkByName and GetLinkByIndex bus methods --- diff --git a/src/network/networkd-manager-bus.c b/src/network/networkd-manager-bus.c index b52e3f7dd60..6174104f5eb 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 + #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 }; diff --git a/src/network/org.freedesktop.network1.conf b/src/network/org.freedesktop.network1.conf index 279da06a986..56a78ee7dca 100644 --- a/src/network/org.freedesktop.network1.conf +++ b/src/network/org.freedesktop.network1.conf @@ -40,6 +40,14 @@ send_interface="org.freedesktop.network1.Manager" send_member="ListLinks"/> + + + +