[PATCH v3 21/25] linux: Add listns
Adhemerval Zanella
adhemerval.zanella@linaro.org
Mon Nov 16 19:42:34 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.
As for listmount, the namespace IDs are returned in a uint64_t array, so
programs which pass a kernel __u64 array need a cast on the 64-bit
architectures where __u64 is unsigned long long.
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 | 69 +++++
sysdeps/unix/sysv/linux/syscalls.list | 1 +
sysdeps/unix/sysv/linux/tst-listns.c | 243 ++++++++++++++++++
.../unix/sysv/linux/x86_64/64/libc.abilist | 1 +
.../unix/sysv/linux/x86_64/x32/libc.abilist | 1 +
38 files changed, 353 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 0994a7d78c3..54bc8c5a01b 100644
--- a/NEWS
+++ b/NEWS
@@ -52,6 +52,11 @@ Major new features:
with the <sys/ioprio.h> header. They retrieve and set the I/O
scheduling class and priority of processes.
+* 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 0a4a48b3c7b..db48ffdb76d 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -184,6 +184,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-gettid-kill \
tst-ioprio \
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 73c35e3ef5b..0e1fd4d16c7 100644
--- a/sysdeps/unix/sysv/linux/Versions
+++ b/sysdeps/unix/sysv/linux/Versions
@@ -351,6 +351,7 @@ libc {
ioprio_get;
ioprio_set;
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 ed6e15dad45..b24d0613ef7 100644
--- a/sysdeps/unix/sysv/linux/aarch64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/aarch64/libc.abilist
@@ -2782,6 +2782,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 06ad094d7d3..d89e3870bbf 100644
--- a/sysdeps/unix/sysv/linux/alpha/libc.abilist
+++ b/sysdeps/unix/sysv/linux/alpha/libc.abilist
@@ -3129,6 +3129,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 665e5882f0e..1868d4a7cbc 100644
--- a/sysdeps/unix/sysv/linux/arc/libc.abilist
+++ b/sysdeps/unix/sysv/linux/arc/libc.abilist
@@ -2543,6 +2543,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 893a5225cbe..0fbc2f5947f 100644
--- a/sysdeps/unix/sysv/linux/arm/be/libc.abilist
+++ b/sysdeps/unix/sysv/linux/arm/be/libc.abilist
@@ -2835,6 +2835,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 054cdb1098a..12d9e1ae9ae 100644
--- a/sysdeps/unix/sysv/linux/arm/le/libc.abilist
+++ b/sysdeps/unix/sysv/linux/arm/le/libc.abilist
@@ -2832,6 +2832,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 93a0d3b0658..8ca9a0f1132 100644
--- a/sysdeps/unix/sysv/linux/csky/libc.abilist
+++ b/sysdeps/unix/sysv/linux/csky/libc.abilist
@@ -2819,6 +2819,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 1102a7555cd..d8457291774 100644
--- a/sysdeps/unix/sysv/linux/hppa/libc.abilist
+++ b/sysdeps/unix/sysv/linux/hppa/libc.abilist
@@ -2856,6 +2856,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 f31848dc100..3c85600cd5a 100644
--- a/sysdeps/unix/sysv/linux/i386/libc.abilist
+++ b/sysdeps/unix/sysv/linux/i386/libc.abilist
@@ -3039,6 +3039,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 fc6268c80b5..3bd72753155 100644
--- a/sysdeps/unix/sysv/linux/loongarch/ilp32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/loongarch/ilp32/libc.abilist
@@ -2294,6 +2294,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 c3d473048ef..8f425dc776c 100644
--- a/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/loongarch/lp64/libc.abilist
@@ -2303,6 +2303,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 4e01878808e..548aaf05fac 100644
--- a/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist
+++ b/sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist
@@ -2815,6 +2815,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 cfe4501e8d9..b01516c3172 100644
--- a/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist
+++ b/sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist
@@ -2982,6 +2982,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 754439a8d6b..26c306f74e3 100644
--- a/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist
+++ b/sysdeps/unix/sysv/linux/microblaze/be/libc.abilist
@@ -2868,6 +2868,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 a461625b47c..eb9ad7c22cc 100644
--- a/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist
+++ b/sysdeps/unix/sysv/linux/microblaze/le/libc.abilist
@@ -2865,6 +2865,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 41f2c65dfd4..6510e0ec3c8 100644
--- a/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist
+++ b/sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist
@@ -2945,6 +2945,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 7690c7f5ef8..8f694cff7e8 100644
--- a/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist
+++ b/sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist
@@ -2943,6 +2943,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 808e98d3c2b..4d15b429237 100644
--- a/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist
@@ -2951,6 +2951,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 7440bcf0106..7309f1b3b82 100644
--- a/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist
@@ -2853,6 +2853,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 3b1d40cd8fb..cb44a37e1af 100644
--- a/sysdeps/unix/sysv/linux/or1k/libc.abilist
+++ b/sysdeps/unix/sysv/linux/or1k/libc.abilist
@@ -2293,6 +2293,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 e80833183cf..87fd6d55098 100644
--- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
@@ -3172,6 +3172,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 d435a698639..bf7443d41b9 100644
--- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist
@@ -3217,6 +3217,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 92545f60903..cbc540442aa 100644
--- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist
@@ -2926,6 +2926,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 35cf64bca41..6d9e4ef7249 100644
--- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist
@@ -3002,6 +3002,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 ad4b8e47e3e..ec6455b0942 100644
--- a/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist
@@ -2546,6 +2546,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 09d17b8bacb..64275f17de5 100644
--- a/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist
@@ -2746,6 +2746,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 529c7f8aab5..edc843e3211 100644
--- a/sysdeps/unix/sysv/linux/s390/libc.abilist
+++ b/sysdeps/unix/sysv/linux/s390/libc.abilist
@@ -2963,6 +2963,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 6d67cbc02c8..21d387470a7 100644
--- a/sysdeps/unix/sysv/linux/sh/be/libc.abilist
+++ b/sysdeps/unix/sysv/linux/sh/be/libc.abilist
@@ -2862,6 +2862,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 d01f775bba2..0055e4fedba 100644
--- a/sysdeps/unix/sysv/linux/sh/le/libc.abilist
+++ b/sysdeps/unix/sysv/linux/sh/le/libc.abilist
@@ -2859,6 +2859,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 5ff26b9299d..7a9fc0289d5 100644
--- a/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist
@@ -3193,6 +3193,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 f6493956e0e..af2203dbf84 100644
--- a/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist
@@ -2829,6 +2829,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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..23f2216e304
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/sys/nsfs.h
@@ -0,0 +1,69 @@
+/* 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 <sys/types.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
+
+__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. REQ->size must be the
+ size of the structure, and its spare fields must be zero. 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 (ENOENT
+ if no namespace follows REQ->ns_id). */
+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 81eca2c7702..0fd789efec7 100644
--- a/sysdeps/unix/sysv/linux/syscalls.list
+++ b/sysdeps/unix/sysv/linux/syscalls.list
@@ -39,6 +39,7 @@ ioprio_set EXTRA ioprio_set i:iii ioprio_set
klogctl EXTRA syslog i:isi klogctl
lchown - lchown i:sii __lchown lchown
listmount EXTRA listmount i:ppUU listmount
+listns EXTRA listns i:ppUU listns
membarrier EXTRA membarrier i:iUi membarrier
mincore - mincore i:aUV mincore
mlock - mlock i:bU mlock
diff --git a/sysdeps/unix/sysv/linux/tst-listns.c b/sysdeps/unix/sysv/linux/tst-listns.c
new file mode 100644
index 00000000000..6485785a03f
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/tst-listns.c
@@ -0,0 +1,243 @@
+/* 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. */
+#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];
+ /* The kernel does not fail with EPERM for this request, so it can only
+ come from a seccomp filter. */
+ if (listns (&req, ids, array_length (ids), 0) == -1
+ && (errno == ENOSYS || errno == EPERM))
+ FAIL_UNSUPPORTED ("listns is not available: %m");
+
+ 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 7295fc43e43..b44e5aefed3 100644
--- a/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist
+++ b/sysdeps/unix/sysv/linux/x86_64/64/libc.abilist
@@ -2778,6 +2778,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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 7cd21364349..047be7f401a 100644
--- a/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist
+++ b/sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist
@@ -2797,6 +2797,7 @@ GLIBC_2.45 getxattrat F
GLIBC_2.45 ioprio_get F
GLIBC_2.45 ioprio_set 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