[PATCH 22/23] linux: Add listns
Adhemerval Zanella
adhemerval.zanella@linaro.org
Tue Nov 3 14:32:27 GMT 2026
The listns system call (commit 76b6f5dfb3fda76fce1f9990d6fa58adc711122b,
Linux 6.19) lists the IDs of the active namespaces the caller is allowed
to see, in ascending order, optionally selecting namespace types or the
namespaces owned by a user namespace. The IDs are the ones returned by
the NS_GET_ID ioctl on a namespace file descriptor.
ssize_t listns (const struct ns_id_req *req, uint64_t *ns_ids,
size_t nr_ns_ids, unsigned int flags);
The new <sys/nsfs.h> header, named after the kernel <linux/nsfs.h>,
includes it if it is available. The kernel header defines
NS_ID_REQ_SIZE_VER0 along with the structure, so it is used to avoid
redefining it. The namespace types are the CLONE_NEW* values from
<sched.h>.
No widely used project calls it through syscall yet.
Checked on x86_64-linux-gnu and aarch64-linux-gnu.
---
NEWS | 5 +
sysdeps/unix/sysv/linux/Makefile | 2 +
sysdeps/unix/sysv/linux/Versions | 1 +
sysdeps/unix/sysv/linux/aarch64/libc.abilist | 1 +
sysdeps/unix/sysv/linux/alpha/libc.abilist | 1 +
sysdeps/unix/sysv/linux/arc/libc.abilist | 1 +
sysdeps/unix/sysv/linux/arm/be/libc.abilist | 1 +
sysdeps/unix/sysv/linux/arm/le/libc.abilist | 1 +
sysdeps/unix/sysv/linux/csky/libc.abilist | 1 +
sysdeps/unix/sysv/linux/hppa/libc.abilist | 1 +
sysdeps/unix/sysv/linux/i386/libc.abilist | 1 +
.../sysv/linux/loongarch/ilp32/libc.abilist | 1 +
.../sysv/linux/loongarch/lp64/libc.abilist | 1 +
.../sysv/linux/m68k/coldfire/libc.abilist | 1 +
.../unix/sysv/linux/m68k/m680x0/libc.abilist | 1 +
.../sysv/linux/microblaze/be/libc.abilist | 1 +
.../sysv/linux/microblaze/le/libc.abilist | 1 +
.../sysv/linux/mips/mips32/fpu/libc.abilist | 1 +
.../sysv/linux/mips/mips32/nofpu/libc.abilist | 1 +
.../sysv/linux/mips/mips64/n32/libc.abilist | 1 +
.../sysv/linux/mips/mips64/n64/libc.abilist | 1 +
sysdeps/unix/sysv/linux/or1k/libc.abilist | 1 +
.../linux/powerpc/powerpc32/fpu/libc.abilist | 1 +
.../powerpc/powerpc32/nofpu/libc.abilist | 1 +
.../linux/powerpc/powerpc64/be/libc.abilist | 1 +
.../linux/powerpc/powerpc64/le/libc.abilist | 1 +
.../unix/sysv/linux/riscv/rv32/libc.abilist | 1 +
.../unix/sysv/linux/riscv/rv64/libc.abilist | 1 +
sysdeps/unix/sysv/linux/s390/libc.abilist | 1 +
sysdeps/unix/sysv/linux/sh/be/libc.abilist | 1 +
sysdeps/unix/sysv/linux/sh/le/libc.abilist | 1 +
.../sysv/linux/sparc/sparc32/libc.abilist | 1 +
.../sysv/linux/sparc/sparc64/libc.abilist | 1 +
sysdeps/unix/sysv/linux/sys/nsfs.h | 74 ++++++
sysdeps/unix/sysv/linux/syscalls.list | 1 +
sysdeps/unix/sysv/linux/tst-listns.c | 240 ++++++++++++++++++
.../unix/sysv/linux/x86_64/64/libc.abilist | 1 +
.../unix/sysv/linux/x86_64/x32/libc.abilist | 1 +
38 files changed, 355 insertions(+)
create mode 100644 sysdeps/unix/sysv/linux/sys/nsfs.h
create mode 100644 sysdeps/unix/sysv/linux/tst-listns.c
diff --git a/NEWS b/NEWS
index 4d9b78b7586..fba37064b38 100644
--- a/NEWS
+++ b/NEWS
@@ -55,6 +55,11 @@ Major new features:
header. It checks whether two processes share a kernel resource, such
as an open file description or the address space.
+* On Linux, the listns function has been added, along with the
+ <sys/nsfs.h> header. It lists the IDs of the namespaces the caller is
+ allowed to see, optionally only those of some types or owned by a user
+ namespace.
+
Deprecated and removed features, and other changes affecting compatibility:
* The posix_spawn_file_actions_addchdir and posix_spawn_file_actions_addfchdir
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index ff57fee384c..0a01dbbc8dd 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -183,6 +183,7 @@ sysdep_headers += \
sys/klog.h \
sys/membarrier.h \
sys/mount.h \
+ sys/nsfs.h \
sys/pci.h \
sys/personality.h \
sys/pidfd.h \
@@ -217,6 +218,7 @@ tests += \
tst-ioprio \
tst-kcmp \
tst-linux-mremap1 \
+ tst-listns \
tst-membarrier \
tst-memfd_create \
tst-memfd_secret \
diff --git a/sysdeps/unix/sysv/linux/Versions b/sysdeps/unix/sysv/linux/Versions
index 6b3c413a6d4..f895742ccb3 100644
--- a/sysdeps/unix/sysv/linux/Versions
+++ b/sysdeps/unix/sysv/linux/Versions
@@ -352,6 +352,7 @@ libc {
ioprio_set;
kcmp;
listmount;
+ listns;
listxattrat;
membarrier;
memfd_secret;
diff --git a/sysdeps/unix/sysv/linux/aarch64/libc.abilist b/sysdeps/unix/sysv/linux/aarch64/libc.abilist
index 7b0a2397704..820f7dd3af9 100644
--- a/sysdeps/unix/sysv/linux/aarch64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/aarch64/libc.abilist
@@ -2783,6 +2783,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/alpha/libc.abilist b/sysdeps/unix/sysv/linux/alpha/libc.abilist
index 7440d4ea5c2..efa75065b83 100644
--- a/sysdeps/unix/sysv/linux/alpha/libc.abilist
+++ b/sysdeps/unix/sysv/linux/alpha/libc.abilist
@@ -3130,6 +3130,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/arc/libc.abilist b/sysdeps/unix/sysv/linux/arc/libc.abilist
index 6cd569feea3..bba3450c471 100644
--- a/sysdeps/unix/sysv/linux/arc/libc.abilist
+++ b/sysdeps/unix/sysv/linux/arc/libc.abilist
@@ -2544,6 +2544,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/arm/be/libc.abilist b/sysdeps/unix/sysv/linux/arm/be/libc.abilist
index 220925115c8..7cc35a77ce0 100644
--- a/sysdeps/unix/sysv/linux/arm/be/libc.abilist
+++ b/sysdeps/unix/sysv/linux/arm/be/libc.abilist
@@ -2836,6 +2836,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/arm/le/libc.abilist b/sysdeps/unix/sysv/linux/arm/le/libc.abilist
index fcf578cff82..27043e24e95 100644
--- a/sysdeps/unix/sysv/linux/arm/le/libc.abilist
+++ b/sysdeps/unix/sysv/linux/arm/le/libc.abilist
@@ -2833,6 +2833,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/csky/libc.abilist b/sysdeps/unix/sysv/linux/csky/libc.abilist
index 4d4a86f5a4d..c3337d446f9 100644
--- a/sysdeps/unix/sysv/linux/csky/libc.abilist
+++ b/sysdeps/unix/sysv/linux/csky/libc.abilist
@@ -2820,6 +2820,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/hppa/libc.abilist b/sysdeps/unix/sysv/linux/hppa/libc.abilist
index 8d12d802c55..850a617b062 100644
--- a/sysdeps/unix/sysv/linux/hppa/libc.abilist
+++ b/sysdeps/unix/sysv/linux/hppa/libc.abilist
@@ -2857,6 +2857,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/i386/libc.abilist b/sysdeps/unix/sysv/linux/i386/libc.abilist
index 6e652795dea..c43e1909095 100644
--- a/sysdeps/unix/sysv/linux/i386/libc.abilist
+++ b/sysdeps/unix/sysv/linux/i386/libc.abilist
@@ -3040,6 +3040,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/loongarch/ilp32/libc.abilist b/sysdeps/unix/sysv/linux/loongarch/ilp32/libc.abilist
index 6f7bd790652..f1fe8833e63 100644
--- a/sysdeps/unix/sysv/linux/loongarch/ilp32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/loongarch/ilp32/libc.abilist
@@ -2295,6 +2295,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist b/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist
index 275eaed7ad1..ef94f919cf9 100644
--- a/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist
@@ -2304,6 +2304,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist
index 483ddbbaf44..df9abdee9e4 100644
--- a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist
+++ b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist
@@ -2816,6 +2816,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist b/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist
index ee151404ebe..db713f2d8e7 100644
--- a/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist
+++ b/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist
@@ -2983,6 +2983,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist b/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist
index b25d96d35ea..25969831b41 100644
--- a/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist
+++ b/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist
@@ -2869,6 +2869,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist b/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist
index ec896a3c5f5..7eed5d1ce88 100644
--- a/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist
+++ b/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist
@@ -2866,6 +2866,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist
index d83b83fd820..19b28086025 100644
--- a/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist
+++ b/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist
@@ -2946,6 +2946,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist
index f481286283e..07933e17c45 100644
--- a/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist
+++ b/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist
@@ -2944,6 +2944,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist
index 23747992b1f..4febd2367c0 100644
--- a/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist
@@ -2952,6 +2952,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist b/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist
index 61dc7c9b01f..6ec1e694447 100644
--- a/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist
@@ -2854,6 +2854,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/or1k/libc.abilist b/sysdeps/unix/sysv/linux/or1k/libc.abilist
index 9f9741283a4..c58edc69ed3 100644
--- a/sysdeps/unix/sysv/linux/or1k/libc.abilist
+++ b/sysdeps/unix/sysv/linux/or1k/libc.abilist
@@ -2294,6 +2294,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
index 1c7889e9e6d..151ff28ddf7 100644
--- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
@@ -3173,6 +3173,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist
index f0da189de41..f277b58eb15 100644
--- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist
@@ -3218,6 +3218,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist
index 90b64a92269..5fdcb443c07 100644
--- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist
@@ -2927,6 +2927,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist b/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist
index b0529357fc5..1341073d330 100644
--- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist
@@ -3003,6 +3003,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist b/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist
index 0c23eb13589..ea76179456e 100644
--- a/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist
@@ -2547,6 +2547,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist b/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist
index 2cc2b986e70..0ac55bf57f2 100644
--- a/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist
@@ -2747,6 +2747,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/s390/libc.abilist b/sysdeps/unix/sysv/linux/s390/libc.abilist
index 71e6abd28da..7e669cd77b5 100644
--- a/sysdeps/unix/sysv/linux/s390/libc.abilist
+++ b/sysdeps/unix/sysv/linux/s390/libc.abilist
@@ -2964,6 +2964,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/sh/be/libc.abilist b/sysdeps/unix/sysv/linux/sh/be/libc.abilist
index ae173346f86..3d3c9004fbd 100644
--- a/sysdeps/unix/sysv/linux/sh/be/libc.abilist
+++ b/sysdeps/unix/sysv/linux/sh/be/libc.abilist
@@ -2863,6 +2863,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/sh/le/libc.abilist b/sysdeps/unix/sysv/linux/sh/le/libc.abilist
index 12d2d7bfb4c..677a7a9d7b5 100644
--- a/sysdeps/unix/sysv/linux/sh/le/libc.abilist
+++ b/sysdeps/unix/sysv/linux/sh/le/libc.abilist
@@ -2860,6 +2860,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist b/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist
index 829f47c3212..ff5a306551f 100644
--- a/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist
@@ -3194,6 +3194,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist b/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist
index 48322991ded..d1b5b2f4c36 100644
--- a/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist
@@ -2830,6 +2830,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/sys/nsfs.h b/sysdeps/unix/sysv/linux/sys/nsfs.h
new file mode 100644
index 00000000000..6c8c65b7c49
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/sys/nsfs.h
@@ -0,0 +1,74 @@
+/* Namespace file system interfaces. Linux version.
+ Copyright (C) 2026 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#ifndef _SYS_NSFS_H
+#define _SYS_NSFS_H 1
+
+#include <features.h>
+#include <bits/types.h>
+#define __need_size_t
+#include <stddef.h>
+
+#ifdef __has_include
+# if __has_include ("linux/nsfs.h")
+# include "linux/nsfs.h"
+# endif
+#endif
+
+#ifndef NS_ID_REQ_SIZE_VER0
+/* Namespace and parameters for listns. NS_ID is the last listed namespace ID
+ (or zero), NS_TYPE is a mask of CLONE_NEW* values to select the namespace
+ types (or zero for all of them), and USER_NS_ID selects only the namespaces
+ owned by that user namespace (or zero for any owner). */
+struct ns_id_req
+{
+ __uint32_t size;
+ __uint32_t spare;
+ __uint64_t ns_id;
+ __extension__ struct
+ {
+ __uint32_t ns_type;
+ __uint32_t spare2;
+ __uint64_t user_ns_id;
+ };
+};
+
+/* Special USER_NS_ID value for listns: the user namespace of the
+ caller. */
+# define LISTNS_CURRENT_USER 0xffffffffffffffff
+
+# define NS_ID_REQ_SIZE_VER0 32 /* sizeof first published struct */
+#endif
+
+#ifndef __ssize_t_defined
+typedef __ssize_t ssize_t;
+# define __ssize_t_defined
+#endif
+
+__BEGIN_DECLS
+
+/* Store in NS_IDS the IDs of up to NR_NS_IDS namespaces selected by REQ, in
+ ascending order and starting after REQ->ns_id. Only active namespaces the
+ caller is allowed to see are listed. FLAGS must be zero.
+ Return the number of IDs stored, or -1 and set errno on failure. */
+extern ssize_t listns (const struct ns_id_req *__req, __uint64_t *__ns_ids,
+ size_t __nr_ns_ids, unsigned int __flags) __THROW;
+
+__END_DECLS
+
+#endif /* sys/nsfs.h */
diff --git a/sysdeps/unix/sysv/linux/syscalls.list b/sysdeps/unix/sysv/linux/syscalls.list
index 6fc1a0db80c..7d71492ab0d 100644
--- a/sysdeps/unix/sysv/linux/syscalls.list
+++ b/sysdeps/unix/sysv/linux/syscalls.list
@@ -54,6 +54,7 @@ open_tree EXTRA open_tree i:isU open_tree
open_tree_attr EXTRA open_tree_attr i:isUPU open_tree_attr
statmount EXTRA statmount i:ppUU statmount
listmount EXTRA listmount i:ppUU listmount
+listns EXTRA listns i:ppUU listns
pipe2 - pipe2 i:fi __pipe2 pipe2
pidfd_open EXTRA pidfd_open i:iU pidfd_open
pidfd_getfd EXTRA pidfd_getfd i:iiU pidfd_getfd
diff --git a/sysdeps/unix/sysv/linux/tst-listns.c b/sysdeps/unix/sysv/linux/tst-listns.c
new file mode 100644
index 00000000000..20ad9a715dc
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/tst-listns.c
@@ -0,0 +1,240 @@
+/* Basic tests for Linux listns.
+ Copyright (C) 2026 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <array_length.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <sched.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/ioctl.h>
+#include <sys/nsfs.h>
+#include <support/check.h>
+#include <support/namespace.h>
+#include <support/support.h>
+#include <support/xunistd.h>
+
+/* Added in Linux 6.18, so it is available whenever listns is. */
+#ifndef NS_GET_ID
+# define NS_GET_ID _IOR (0xb7, 13, uint64_t)
+#endif
+
+static const struct
+{
+ const char *name;
+ uint32_t type;
+} ns_types[] =
+{
+ { "cgroup", CLONE_NEWCGROUP },
+ { "ipc", CLONE_NEWIPC },
+ { "mnt", CLONE_NEWNS },
+ { "net", CLONE_NEWNET },
+ { "pid", CLONE_NEWPID },
+ { "time", CLONE_NEWTIME },
+ { "user", CLONE_NEWUSER },
+ { "uts", CLONE_NEWUTS },
+};
+
+/* Return the ID of the namespace NAME of the calling process, or 0 if the
+ kernel does not support this namespace type. */
+static uint64_t
+get_ns_id (const char *name)
+{
+ char *path = xasprintf ("/proc/self/ns/%s", name);
+ int fd = open (path, O_RDONLY | O_CLOEXEC);
+ if (fd == -1)
+ {
+ if (errno != ENOENT)
+ FAIL_EXIT1 ("open (\"%s\"): %m", path);
+ free (path);
+ return 0;
+ }
+ uint64_t id;
+ if (ioctl (fd, NS_GET_ID, &id) != 0)
+ FAIL_EXIT1 ("ioctl (\"%s\", NS_GET_ID): %m", path);
+ xclose (fd);
+ free (path);
+ return id;
+}
+
+struct id_list
+{
+ uint64_t *ids;
+ size_t count;
+};
+
+/* List all the namespaces of types TYPE owned by USER_NS_ID, with calls
+ that return at most PAGE IDs. listns cannot report how many namespaces
+ there are (a zero NR_NS_IDS only returns zero), and namespaces can be
+ created or destroyed between calls, so the IDs are retrieved in pages:
+ each call continues after the last listed ID, set in REQ.NS_ID. */
+static struct id_list
+list_all (uint32_t type, uint64_t user_ns_id, size_t page)
+{
+ struct ns_id_req req =
+ {
+ .size = NS_ID_REQ_SIZE_VER0,
+ .ns_type = type,
+ .user_ns_id = user_ns_id,
+ };
+ struct id_list l = { NULL, 0 };
+ while (true)
+ {
+ l.ids = xrealloc (l.ids, (l.count + page) * sizeof (uint64_t));
+ ssize_t r = listns (&req, l.ids + l.count, page, 0);
+ /* If the previous page ended with the last namespace, there is no
+ namespace after REQ.NS_ID and listns fails with ENOENT instead of
+ returning zero. */
+ if (r == -1 && errno == ENOENT && req.ns_id != 0)
+ break;
+ if (r == -1)
+ FAIL_EXIT1 ("listns (ns_type=%#" PRIx32 ", user_ns_id=%#" PRIx64
+ "): %m", type, user_ns_id);
+ TEST_VERIFY_EXIT (r <= page);
+
+ /* The IDs are listed in ascending order, after REQ.NS_ID. */
+ for (size_t i = 0; i < r; i++)
+ {
+ TEST_VERIFY (l.ids[l.count + i] > req.ns_id);
+ req.ns_id = l.ids[l.count + i];
+ }
+ l.count += r;
+ /* A partial page is the last one. */
+ if (r < page)
+ break;
+ }
+ return l;
+}
+
+static bool
+contains (const struct id_list *l, uint64_t id)
+{
+ for (size_t i = 0; i < l->count; i++)
+ if (l->ids[i] == id)
+ return true;
+ return false;
+}
+
+/* Check the listing of the namespaces owned by a new user namespace,
+ which are only the ones created along with it. */
+static void
+check_owned (void *closure)
+{
+ if (unshare (CLONE_NEWUSER | CLONE_NEWNS | CLONE_NEWUTS) != 0)
+ {
+ printf ("info: cannot create user namespace: %m\n");
+ return;
+ }
+
+ uint64_t mntns = get_ns_id ("mnt");
+ uint64_t utsns = get_ns_id ("uts");
+ uint64_t expected[] =
+ {
+ mntns < utsns ? mntns : utsns,
+ mntns < utsns ? utsns : mntns,
+ };
+
+ const uint64_t users[] = { LISTNS_CURRENT_USER, get_ns_id ("user") };
+ for (int i = 0; i < array_length (users); i++)
+ {
+ /* Also check that each call continues after the last listed ID.
+ With the two expected namespaces, pages of 1 and 2 IDs end the
+ listing with ENOENT, and a page of 3 IDs with a partial page. */
+ for (size_t page = 1; page <= 3; page++)
+ {
+ struct id_list l = list_all (0, users[i], page);
+ TEST_COMPARE (l.count, array_length (expected));
+ if (l.count == array_length (expected))
+ TEST_COMPARE_BLOB (l.ids, l.count * sizeof (uint64_t),
+ expected, sizeof (expected));
+ free (l.ids);
+ }
+
+ struct id_list l = list_all (CLONE_NEWUTS, users[i], 4);
+ TEST_COMPARE (l.count, 1);
+ if (l.count == 1)
+ TEST_COMPARE (l.ids[0], utsns);
+ free (l.ids);
+ }
+}
+
+static int
+do_test (void)
+{
+ support_need_proc ("needs /proc/self/ns");
+
+ struct ns_id_req req = { .size = NS_ID_REQ_SIZE_VER0 };
+ uint64_t ids[1];
+ if (listns (&req, ids, array_length (ids), 0) == -1 && errno == ENOSYS)
+ FAIL_UNSUPPORTED ("listns is not supported by the kernel");
+
+ uint64_t own_ids[array_length (ns_types)];
+ for (size_t i = 0; i < array_length (ns_types); i++)
+ own_ids[i] = get_ns_id (ns_types[i].name);
+
+ /* The namespaces of the calling process are always listed, and only
+ they are listed when selecting their type. */
+ struct id_list all = list_all (0, 0, 256);
+ for (size_t i = 0; i < array_length (ns_types); i++)
+ {
+ if (own_ids[i] == 0)
+ continue;
+ if (!contains (&all, own_ids[i]))
+ FAIL ("%s namespace %" PRIu64 " is not listed", ns_types[i].name,
+ own_ids[i]);
+
+ struct id_list l = list_all (ns_types[i].type, 0, 256);
+ for (size_t j = 0; j < array_length (ns_types); j++)
+ if (own_ids[j] != 0 && contains (&l, own_ids[j]) != (i == j))
+ FAIL ("%s namespace %" PRIu64 " %s listed for type %s",
+ ns_types[j].name, own_ids[j], i == j ? "is not" : "is",
+ ns_types[i].name);
+ free (l.ids);
+ }
+ free (all.ids);
+
+ /* FLAGS must be zero. */
+ TEST_COMPARE (listns (&req, ids, array_length (ids), 1), -1);
+ TEST_COMPARE (errno, EINVAL);
+
+ /* The first published structure is the smallest supported one. */
+ req.size = NS_ID_REQ_SIZE_VER0 - 1;
+ TEST_COMPARE (listns (&req, ids, array_length (ids), 0), -1);
+ TEST_COMPARE (errno, EINVAL);
+ req.size = NS_ID_REQ_SIZE_VER0;
+
+ /* The reserved field must be zero. */
+ req.spare = 1;
+ TEST_COMPARE (listns (&req, ids, array_length (ids), 0), -1);
+ TEST_COMPARE (errno, EINVAL);
+ req.spare = 0;
+
+ /* Only namespace types can be selected. */
+ req.ns_type = CLONE_NEWUTS | CLONE_VM;
+ TEST_COMPARE (listns (&req, ids, array_length (ids), 0), -1);
+ TEST_COMPARE (errno, EOPNOTSUPP);
+
+ support_isolate_in_subprocess (check_owned, NULL);
+
+ return 0;
+}
+
+#include <support/test-driver.c>
diff --git a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist
index c2e320d4e03..34e8bfbe9dd 100644
--- a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist
@@ -2779,6 +2779,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
diff --git a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist
index 91650b87f68..951e75322c3 100644
--- a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist
@@ -2798,6 +2798,7 @@ GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set F
GLIBC_2.45 kcmp F
GLIBC_2.45 listmount F
+GLIBC_2.45 listns F
GLIBC_2.45 listxattrat F
GLIBC_2.45 membarrier F
GLIBC_2.45 memfd_secret F
--
2.53.0
More information about the Libc-alpha
mailing list