kcmp(2)
๐ kcmp(2) โ the compare-two-processes syscall ๐
okay bestie, welcome to the System Calls Manual page for kcmp(2). it's giving "am I the same as you or not" energy, and honestly? iconic. let's get into it โจ
๐ธ NAME ๐ธ
kcmp โ compare two processes to determine if they share a kernel resource
๐ LIBRARY ๐
Standard C library (libc, -lc)
โจ SYNOPSIS โจ
#include <linux/kcmp.h> /* Definition of KCMP_* constants */
#include <sys/syscall.h> /* Definition of SYS_* constants */
#include <unistd.h>
int syscall(SYS_kcmp, pid_t pid1, pid_t pid2, int type,
unsigned long idx1, unsigned long idx2);
lowkey important: glibc provides no wrapper forkcmp(), so you literally have to do it yourself viasyscall(2). no shortcut, bestie, we're going raw. ๐ซถ
๐ DESCRIPTION ๐
The kcmp() system call can be used to check whether the two processes identified by pid1 and pid2 share a kernel resource such as virtual memory, file descriptors, and so on. like, do these two girlies share a closet or not ๐
Permission to employ kcmp() is governed by ptrace access mode PTRACE_MODE_READ_REALCREDS checks against both pid1 and pid2; see ptrace(2). so both sides get the vibe check, no exceptions.
The type argument specifies which resource is to be compared in the two processes. It has one of the following values:
KCMP_FILEโ Check whether a file descriptoridx1in the processpid1refers to the same open file description (seeopen(2)) as file descriptoridx2in the processpid2. The existence of two file descriptors that refer to the same open file description can occur as a result ofdup(2)(and similar)fork(2), or passing file descriptors via a domain socket (seeunix(7)). besties sharing one open file description fr fr ๐KCMP_FILESโ Check whether the processes share the same set of open file descriptors. The argumentsidx1andidx2are ignored. See the discussion of theCLONE_FILESflag inclone(2).KCMP_FSโ Check whether the processes share the same filesystem information (i.e., file mode creation mask, working directory, and filesystem root). The argumentsidx1andidx2are ignored. See the discussion of theCLONE_FSflag inclone(2).KCMP_IOโ Check whether the processes share I/O context. The argumentsidx1andidx2are ignored. See the discussion of theCLONE_IOflag inclone(2).KCMP_SIGHANDโ Check whether the processes share the same table of signal dispositions. The argumentsidx1andidx2are ignored. See the discussion of theCLONE_SIGHANDflag inclone(2).KCMP_SYSVSEMโ Check whether the processes share the same list of System V semaphore undo operations. The argumentsidx1andidx2are ignored. See the discussion of theCLONE_SYSVSEMflag inclone(2).KCMP_VMโ Check whether the processes share the same address space. The argumentsidx1andidx2are ignored. See the discussion of theCLONE_VMflag inclone(2).KCMP_EPOLL_TFD(since Linux 4.13) โ Check whether the file descriptoridx1of the processpid1is present in theepoll(7)instance described byidx2of the processpid2. The argumentidx2is a pointer to a structure where the target file is described. This structure has the form:
struct kcmp_epoll_slot {
__u32 efd;
__u32 tfd;
__u64 toff;
};
Within this structure, efd is an epoll file descriptor returned from epoll_create(2), tfd is a target file descriptor number, and toff is a target file offset counted from zero. Several different targets may be registered with the same file descriptor number and setting a specific offset helps to investigate each of them. so like, toff is how you tell your twins apart ๐ง
๐จ bestie listen ๐จ Note thekcmp()is not protected against false positives, which may occur if the processes are currently running. One should stop the processes by sendingSIGSTOP(seesignal(7)) prior to inspection with this system call to obtain meaningful results. like, freeze them first or you're getting goofy answers, no cap.
๐ฅ RETURN VALUE ๐ฅ
The return value of a successful call to kcmp() is simply the result of arithmetic comparison of kernel pointers (when the kernel compares resources, it uses their memory addresses). so it's literally just pointer math, the kernel is not being deep about this ๐
The easiest way to explain is to consider an example. Suppose that v1 and v2 are the addresses of appropriate resources, then the return value is one of the following:
0 v1 is equal to v2; in other words, the two processes share
the resource.
1 v1 is less than v2.
2 v1 is greater than v2.
3 v1 is not equal to v2, but ordering information is
unavailable.
On error, -1 is returned, and errno is set to indicate the error.
kcmp() was designed to return values suitable for sorting. This is particularly handy if one needs to compare a large number of file descriptors. sorting girlies rise up, this was lowkey made for you โจ
๐ซถ ERRORS ๐ซถ
EBADFโtypeisKCMP_FILEandfd1orfd2is not an open file descriptor.EFAULTโ The epoll slot addressed byidx2is outside of the user's address space.EINVALโtypeis invalid.ENOENTโ The target file is not present inepoll(7)instance.EPERMโ Insufficient permission to inspect process resources. TheCAP_SYS_PTRACEcapability is required to inspect processes that you do not own. Other ptrace limitations may also apply, such asCONFIG_SECURITY_YAMA, which, when/proc/sys/kernel/yama/ptrace_scopeis2, limitskcmp()to child processes; seeptrace(2). so basically, you can only snoop on your own kids unless you've got the credentials ๐ESRCHโ Processpid1orpid2does not exist. ghosted, literally ๐ป
๐ STANDARDS ๐
Linux.
๐ HISTORY ๐
Linux 3.5.
Before Linux 5.12, this system call is available only if the kernel is configured with CONFIG_CHECKPOINT_RESTORE, since the original purpose of the system call was for the checkpoint/restore in user space (CRIU) feature. (The alternative to this system call would have been to expose suitable process information via the proc(5) filesystem; this was deemed to be unsuitable for security reasons.) Since Linux 5.12, this system call is also available if the kernel is configured with CONFIG_KCMP. glow-up era, honestly ๐
๐ฆ NOTES ๐ฆ
See clone(2) for some background information on the shared resources referred to on this page.
โญ EXAMPLES โญ
The program below uses kcmp() to test whether pairs of file descriptors refer to the same open file description. The program tests different cases for the file descriptor pairs, as described in the program output. An example run of the program is as follows:
$ ./a.out;
Parent PID is 1144
Parent opened file on FD 3
PID of child of fork() is 1145
Compare duplicate FDs from different processes:
kcmp(1145, 1144, KCMP_FILE, 3, 3) ==> same
Child opened file on FD 4
Compare FDs from distinct open()s in same process:
kcmp(1145, 1145, KCMP_FILE, 3, 4) ==> different
Child duplicated FD 3 to create FD 5
Compare duplicated FDs in same process:
kcmp(1145, 1145, KCMP_FILE, 3, 5) ==> same
๐ Program source ๐
#define _GNU_SOURCE
#include <err.h>
#include <fcntl.h>
#include <linux/kcmp.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
static int
kcmp(pid_t pid1, pid_t pid2, int type,
unsigned long idx1, unsigned long idx2)
{
return syscall(SYS_kcmp, pid1, pid2, type, idx1, idx2);
}
static void
test_kcmp(char *msg, pid_t pid1, pid_t pid2, int fd_a, int fd_b)
{
printf("\t%s\n", msg);
printf("\t\tkcmp(%jd, %jd, KCMP_FILE, %d, %d) ==> %s\n",
(intmax_t) pid1, (intmax_t) pid2, fd_a, fd_b,
(kcmp(pid1, pid2, KCMP_FILE, fd_a, fd_b) == 0) ?
"same" : "different");
}
int
main(void)
{
int fd1, fd2, fd3;
static const char pathname[] = "/tmp/kcmp.test";
fd1 = open(pathname, O_CREAT | O_RDWR, 0600);
if (fd1 == -1)
err(EXIT_FAILURE, "open");
printf("Parent PID is %jd\n", (intmax_t) getpid());
printf("Parent opened file on FD %d\n\n", fd1);
switch (fork()) {
case -1:
err(EXIT_FAILURE, "fork");
case 0:
printf("PID of child of fork() is %jd\n", (intmax_t) getpid());
test_kcmp("Compare duplicate FDs from different processes:",
getpid(), getppid(), fd1, fd1);
fd2 = open(pathname, O_CREAT | O_RDWR, 0600);
if (fd2 == -1)
err(EXIT_FAILURE, "open");
printf("Child opened file on FD %d\n", fd2);
test_kcmp("Compare FDs from distinct open()s in same process:",
getpid(), getpid(), fd1, fd2);
fd3 = dup(fd1);
if (fd3 == -1)
err(EXIT_FAILURE, "dup");
printf("Child duplicated FD %d to create FD %d\n", fd1, fd3);
test_kcmp("Compare duplicated FDs in same process:",
getpid(), getpid(), fd1, fd3);
break;
default:
wait(NULL);
}
exit(EXIT_SUCCESS);
}
๐ SEE ALSO ๐
and that's the whole page bestie, you ate that ๐ this was Linux man-pages 6.20, dated 2026-02-08 โ so you already know it's fresh, periodt. go forth and kcmp() responsibly, mwah ๐โจ๐
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 2012, Cyrill Gorcunov <gorcunov@openvz.org>
- Copyright 2012-2016, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms