From: Lennart Poettering Date: Fri, 12 Jan 2024 10:47:45 +0000 (+0100) Subject: networkd: expose the nsid in the GetNamespaceId() varlink call X-Git-Tag: v256-rc1~1170^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F30908%2Fhead;p=thirdparty%2Fsystemd.git networkd: expose the nsid in the GetNamespaceId() varlink call Let's return both ids in the GetNamespaceID(), since they are pretty much the same concept. --- diff --git a/src/network/networkd-manager-varlink.c b/src/network/networkd-manager-varlink.c index d9750d9b641..30aebb5d9e1 100644 --- a/src/network/networkd-manager-varlink.c +++ b/src/network/networkd-manager-varlink.c @@ -25,23 +25,35 @@ static int vl_method_get_states(Varlink *link, JsonVariant *parameters, VarlinkM } static int vl_method_get_namespace_id(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { - uint64_t id; + uint64_t inode = 0; + uint32_t nsid = UINT32_MAX; + int r; assert(link); if (json_variant_elements(parameters) > 0) return varlink_error_invalid_parameter(link, parameters); + /* Network namespaces have two identifiers: the inode number (which all namespace types have), and + * the "nsid" (aka the "cookie"), which only network namespaces know as a concept, and which is not + * assigned by default, but once it is, is fixed. Let's return both, to avoid any confusion which one + * this is. */ + struct stat st; - if (stat("/proc/self/ns/net", &st) < 0) { + if (stat("/proc/self/ns/net", &st) < 0) log_warning_errno(errno, "Failed to stat network namespace, ignoring: %m"); - id = 0; - } else - id = st.st_ino; + else + inode = st.st_ino; + + r = netns_get_nsid(/* netnsfd= */ -EBADF, &nsid); + if (r < 0) + log_warning_errno(r, "Failed to query network nsid, ignoring: %m"); return varlink_replyb(link, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_UNSIGNED("NamespaceId", id))); + JSON_BUILD_PAIR_UNSIGNED("NamespaceId", inode), + JSON_BUILD_PAIR_CONDITION(nsid == UINT32_MAX, "NamespaceNSID", JSON_BUILD_NULL), + JSON_BUILD_PAIR_CONDITION(nsid != UINT32_MAX, "NamespaceNSID", JSON_BUILD_UNSIGNED(nsid)))); } int manager_connect_varlink(Manager *m) { diff --git a/src/shared/varlink-io.systemd.Network.c b/src/shared/varlink-io.systemd.Network.c index ec25e26e295..e2da5810e34 100644 --- a/src/shared/varlink-io.systemd.Network.c +++ b/src/shared/varlink-io.systemd.Network.c @@ -11,7 +11,8 @@ static VARLINK_DEFINE_METHOD(GetStates, VARLINK_DEFINE_OUTPUT(OperationalState, VARLINK_STRING, 0)); static VARLINK_DEFINE_METHOD(GetNamespaceId, - VARLINK_DEFINE_OUTPUT(NamespaceId, VARLINK_INT, 0)); + VARLINK_DEFINE_OUTPUT(NamespaceId, VARLINK_INT, 0), + VARLINK_DEFINE_OUTPUT(NamespaceNSID, VARLINK_INT, VARLINK_NULLABLE)); VARLINK_DEFINE_INTERFACE( io_systemd_Network,