From 155d84c555d65803dad8d96969b5d5481aa8d9c6 Mon Sep 17 00:00:00 2001 From: Alban Crequy Date: Wed, 24 May 2023 20:21:44 +0200 Subject: [PATCH] New functions GetFrom{Pid,Thread}WithAltProcfs Existing functions GetFrom{Pid,Thread}() need the pid/tid argument to be numbered as per the pid namespace of the /proc mount. In some container configurations, this is not suitable: a container with unshared pid namespace and the host's /proc mounted in /host/proc needs to lookup pids numbered as per the host pid namespace in /host/proc. GetFrom{Pid,Thread}WithAltProcfs() provide a way for such container setups to use an alternative /proc path. --- netns_linux.go | 12 ++++++++++++ netns_others.go | 8 ++++++++ 2 files changed, 20 insertions(+) diff --git a/netns_linux.go b/netns_linux.go index 8c9b177..a18b89b 100644 --- a/netns_linux.go +++ b/netns_linux.go @@ -122,6 +122,18 @@ func GetFromThread(pid, tid int) (NsHandle, error) { return GetFromPath(fmt.Sprintf("/proc/%d/task/%d/ns/net", pid, tid)) } +// GetFromPidWithAltProcfs gets a handle to the network namespace of a given +// pid using the specified procfs path. +func GetFromPidWithAltProcfs(pid int, procfs string) (NsHandle, error) { + return GetFromPath(fmt.Sprintf("%s/%d/ns/net", procfs, pid)) +} + +// GetFromThreadWithAltProcfs gets a handle to the network namespace of a given +// pid and tid using the specified procfs path. +func GetFromThreadWithAltProcfs(pid, tid int, procfs string) (NsHandle, error) { + return GetFromPath(fmt.Sprintf("%s/%d/task/%d/ns/net", procfs, pid, tid)) +} + // GetFromDocker gets a handle to the network namespace of a docker container. // Id is prefixed matched against the running docker containers, so a short // identifier can be used as long as it isn't ambiguous. diff --git a/netns_others.go b/netns_others.go index f444f6e..00c205e 100644 --- a/netns_others.go +++ b/netns_others.go @@ -51,6 +51,14 @@ func GetFromThread(pid int, tid int) (NsHandle, error) { return -1, ErrNotImplemented } +func GetFromPidWithAltProcfs(pid int, procfs string) (NsHandle, error) { + return -1, ErrNotImplemented +} + +func GetFromThreadWithAltProcfs(pid, tid int, procfs string) (NsHandle, error) { + return -1, ErrNotImplemented +} + func GetFromDocker(id string) (NsHandle, error) { return -1, ErrNotImplemented }