$RodHat_/search
Syscall reference · Processes

kcmp(2)

Compare whether two resources in different processes refer to the same kernel object.

Since
Linux 3.5check your kernel →
glibc
none (use syscall(2))
System
Linux
Man page
kcmp(2)

Synopsis

#include <linux/kcmp.h>
#include <sys/syscall.h>
#include <unistd.h>

int syscall(SYS_kcmp, pid_t pid1, pid_t pid2, int type,
            unsigned long idx1, unsigned long idx2);

Rod's take

No glibc wrapper; use syscall(SYS_kcmp). Return value is strcmp-like: zero means same object, non-zero means different but ordered by kernel address (useful for CRIU sorting, meaningless otherwise). Requires ptrace access to both processes. KCMP_FILE is the common use case: it distinguishes a dup'd fd from an independently opened one, which /proc inode comparison cannot do without a race.

Flags and arguments that matter

NameWhat it does
KCMP_FILEidx1 and idx2 are fd numbers in pid1 and pid2. Returns zero if they share the same open file description (same struct file). Duplicated fds and SCM_RIGHTS-passed fds share a description; independently opened fds do not.
KCMP_VMidx1/idx2 ignored. Returns zero if pid1 and pid2 share the same virtual memory space (CLONE_VM threads, or before exec).
KCMP_FSReturns zero if pid1 and pid2 share the same filesystem context: root directory, cwd, and umask. Threads created with CLONE_FS share this.
KCMP_FILESReturns zero if pid1 and pid2 share the same open file descriptor table entirely. Threads created with CLONE_FILES share the table.
KCMP_SIGHANDReturns zero if pid1 and pid2 share the same signal handler table. Threads created with CLONE_SIGHAND share it.
KCMP_IOReturns zero if pid1 and pid2 share the same I/O context (block-layer accounting).
KCMP_SYSVSEMReturns zero if pid1 and pid2 share the same System V semaphore undo list.
KCMP_EPOLL_TFDidx1 is an epoll fd in pid1, idx2 is a target fd in pid2. Returns zero if the target fd is registered with that epoll instance. Used by CRIU to reconstruct epoll state.

Errors you will actually hit

errnoMeaning
EPERMCaller lacks ptrace access to both target processes. Requires same uid or CAP_SYS_PTRACE; also blocked by Yama ptrace_scope >= 1 for non-descendant processes.
ESRCHpid1 or pid2 does not exist or is a zombie.
EINVALtype is not a valid KCMP_* value.
EBADFidx1 or idx2 is not an open fd in the respective process (KCMP_FILE / KCMP_EPOLL_TFD).

Example

#define _GNU_SOURCE
#include <linux/kcmp.h>
#include <sys/syscall.h>
#include <unistd.h>

/* are fd 3 in pid1 and fd 7 in pid2 the same open file description? */
int same = (syscall(SYS_kcmp, pid1, pid2, KCMP_FILE, 3, 7) == 0);

Trimmed from the full walkthrough in kcmp(2): comparing kernel objects across processes.

BSD equivalent

No equivalent. FreeBSD and OpenBSD have no kcmp. Closest approach is parsing /proc/<pid>/fd symlinks, which is racy.

The long version, from the console

Sources

  1. kcmp(2), man7.org

Version numbers are read off the cited man pages, not memory. Spot one that's wrong? Tell Rod.