@@ -51,6 +51,10 @@ 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 kcmp function has been added, along with the <sys/kcmp.h>
+ header. It checks whether two processes share a kernel resource, such
+ as an open file description or the address space.
+
Deprecated and removed features, and other changes affecting compatibility:
* The posix_spawn_file_actions_addchdir and posix_spawn_file_actions_addfchdir
@@ -178,6 +178,7 @@ sysdep_headers += \
sys/fsuid.h \
sys/inotify.h \
sys/ioprio.h \
+ sys/kcmp.h \
sys/kd.h \
sys/klog.h \
sys/membarrier.h \
@@ -214,6 +215,7 @@ tests += \
tst-gettid \
tst-gettid-kill \
tst-ioprio \
+ tst-kcmp \
tst-linux-mremap1 \
tst-membarrier \
tst-memfd_create \
@@ -350,6 +350,7 @@ libc {
getxattrat;
ioprio_get;
ioprio_set;
+ kcmp;
listmount;
listxattrat;
membarrier;
@@ -2781,6 +2781,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -3128,6 +3128,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -2542,6 +2542,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -2834,6 +2834,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -2831,6 +2831,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -2818,6 +2818,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -2855,6 +2855,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -3038,6 +3038,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -2293,6 +2293,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -2302,6 +2302,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -2814,6 +2814,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -2981,6 +2981,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -2867,6 +2867,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -2864,6 +2864,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -2944,6 +2944,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -2942,6 +2942,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -2950,6 +2950,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -2852,6 +2852,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -2292,6 +2292,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -3171,6 +3171,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -3216,6 +3216,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -2925,6 +2925,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -3001,6 +3001,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -2545,6 +2545,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -2745,6 +2745,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -2962,6 +2962,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -2861,6 +2861,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -2858,6 +2858,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -3192,6 +3192,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -2828,6 +2828,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
new file mode 100644
@@ -0,0 +1,73 @@
+/* Compare kernel resources of processes. 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_KCMP_H
+#define _SYS_KCMP_H 1
+
+#include <features.h>
+#include <bits/types.h>
+
+#ifdef __has_include
+# if __has_include ("linux/kcmp.h")
+# include "linux/kcmp.h"
+# endif
+#endif
+
+#ifndef _LINUX_KCMP_H
+/* Types of kernel resources compared by kcmp. */
+enum kcmp_type
+{
+ KCMP_FILE,
+ KCMP_VM,
+ KCMP_FILES,
+ KCMP_FS,
+ KCMP_SIGHAND,
+ KCMP_IO,
+ KCMP_SYSVSEM,
+ KCMP_EPOLL_TFD,
+
+ KCMP_TYPES
+};
+
+/* Target file of an epoll set, for KCMP_EPOLL_TFD. */
+struct kcmp_epoll_slot
+{
+ __uint32_t efd; /* Epoll file descriptor. */
+ __uint32_t tfd; /* Target file descriptor number. */
+ __uint32_t toff; /* Target offset within the same numbered
+ sequence. */
+};
+#endif /* !_LINUX_KCMP_H */
+
+__BEGIN_DECLS
+
+/* Compare the kernel resource of type TYPE (one of the KCMP_* values) used by
+ the process PID1 with the one used by the process PID2. For KCMP_FILE,
+ IDX1 and IDX2 are file descriptors of the processes; for KCMP_EPOLL_TFD,
+ IDX1 is a file descriptor of PID1 and IDX2 points to a
+ struct kcmp_epoll_slot describing a target file registered in an epoll
+ set of PID2.
+ Return 0 if the resources are the same, 1 or 2 if the first one is
+ respectively less or greater than the second one (in an arbitrary
+ ordering), or -1 and set errno on failure. */
+extern int kcmp (__pid_t __pid1, __pid_t __pid2, int __type,
+ unsigned long int __idx1, unsigned long int __idx2) __THROW;
+
+__END_DECLS
+
+#endif /* sys/kcmp.h */
@@ -36,6 +36,7 @@ ioperm - ioperm i:UUi ioperm
iopl - iopl i:i iopl
ioprio_get EXTRA ioprio_get i:ii ioprio_get
ioprio_set EXTRA ioprio_set i:iii ioprio_set
+kcmp EXTRA kcmp i:iiiUU kcmp
klogctl EXTRA syslog i:isi klogctl
lchown - lchown i:sii __lchown lchown
membarrier EXTRA membarrier i:iUi membarrier
new file mode 100644
@@ -0,0 +1,105 @@
+/* Basic tests for Linux kcmp.
+ 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 <errno.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <sys/epoll.h>
+#include <sys/kcmp.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <support/check.h>
+#include <support/xunistd.h>
+
+static int
+do_test (void)
+{
+ pid_t pid = getpid ();
+ int fd1 = xopen ("/dev/null", O_RDONLY, 0);
+
+ int r = kcmp (pid, pid, KCMP_FILE, fd1, fd1);
+ if (r == -1 && errno == ENOSYS)
+ FAIL_UNSUPPORTED ("kcmp is not supported by the kernel");
+ TEST_COMPARE (r, 0);
+
+ /* A duplicated descriptor refers to the same open file description. */
+ int fd2 = xdup (fd1);
+ TEST_COMPARE (kcmp (pid, pid, KCMP_FILE, fd1, fd2), 0);
+
+ /* A new open creates a new one, and the result orders both. */
+ int fd3 = xopen ("/dev/null", O_RDONLY, 0);
+ r = kcmp (pid, pid, KCMP_FILE, fd1, fd3);
+ TEST_VERIFY (r == 1 || r == 2);
+ TEST_COMPARE (kcmp (pid, pid, KCMP_FILE, fd3, fd1), 3 - r);
+
+ TEST_COMPARE (kcmp (pid, pid, KCMP_VM, 0, 0), 0);
+ TEST_COMPARE (kcmp (pid, pid, KCMP_FILES, 0, 0), 0);
+
+ /* Check if kcmp and file descriptor child sharing. */
+ int pipefd[2];
+ xpipe (pipefd);
+ pid_t child = xfork ();
+ if (child == 0)
+ {
+ char c;
+ xclose (pipefd[1]);
+ /* Wait until the parent closes the write end of the pipe. */
+ TEST_COMPARE (read (pipefd[0], &c, 1), 0);
+ _exit (0);
+ }
+ TEST_COMPARE (kcmp (pid, child, KCMP_FILE, fd1, fd1), 0);
+ TEST_VERIFY (kcmp (pid, child, KCMP_VM, 0, 0) != 0);
+ TEST_VERIFY (kcmp (pid, child, KCMP_FILES, 0, 0) != 0);
+
+ /* Compare a file with the target file of an epoll set. */
+ int efd = epoll_create1 (EPOLL_CLOEXEC);
+ TEST_VERIFY_EXIT (efd != -1);
+ struct epoll_event ev = { .events = EPOLLIN };
+ TEST_COMPARE (epoll_ctl (efd, EPOLL_CTL_ADD, pipefd[0], &ev), 0);
+ struct kcmp_epoll_slot slot =
+ {
+ .efd = efd,
+ .tfd = pipefd[0],
+ };
+ TEST_COMPARE (kcmp (pid, pid, KCMP_EPOLL_TFD, pipefd[0],
+ (uintptr_t) &slot), 0);
+ TEST_VERIFY (kcmp (pid, pid, KCMP_EPOLL_TFD, fd1, (uintptr_t) &slot) != 0);
+
+ TEST_COMPARE (kcmp (pid, pid, KCMP_FILE, fd1, -1), -1);
+ TEST_COMPARE (errno, EBADF);
+ TEST_COMPARE (kcmp (pid, pid, KCMP_TYPES, 0, 0), -1);
+ TEST_COMPARE (errno, EINVAL);
+
+ xclose (pipefd[1]);
+ int status;
+ xwaitpid (child, &status, 0);
+ TEST_VERIFY (WIFEXITED (status) && WEXITSTATUS (status) == 0);
+
+ /* The child no longer exists. */
+ TEST_COMPARE (kcmp (pid, child, KCMP_VM, 0, 0), -1);
+ TEST_COMPARE (errno, ESRCH);
+
+ xclose (efd);
+ xclose (pipefd[0]);
+ xclose (fd3);
+ xclose (fd2);
+ xclose (fd1);
+ return 0;
+}
+
+#include <support/test-driver.c>
@@ -2777,6 +2777,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F
@@ -2796,6 +2796,7 @@ GLIBC_2.45 file_setattr F
GLIBC_2.45 getxattrat F
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 listxattrat F
GLIBC_2.45 membarrier F