kcmp (2) - Linux Manuals
kcmp: compare two processes to determine if they share a kernel resource
NAME
kcmp - compare two processes to determine if they share a kernel resource
SYNOPSIS
#include <linux/kcmp.h> int kcmp(pid_t pid1, pid_t pid2, int type, unsigned long idx1, unsigned long idx2);
Note: There is no glibc wrapper for this system call; see NOTES.
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.Permission to employ kcmp() is governed by ptrace access mode PTRACE_MODE_READ_REALCREDS checks against both pid1 and pid2; see ptrace(2).
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 descriptor idx1 in the process pid1 refers to the same open file description (see open(2)) as file descriptor idx2 in the process pid2. The existence of two file descriptors that refer to the same open file description can occur as a result of dup(2) (and similar) fork(2), or passing file descriptors via a domain socket (see unix(7)).
- KCMP_FILES
- Check whether the processes share the same set of open file descriptors. The arguments idx1 and idx2 are ignored. See the discussion of the CLONE_FILES flag in clone(2).
- KCMP_FS
- Check whether the processes share the same filesystem information (i.e., file mode creation mask, working directory, and filesystem root). The arguments idx1 and idx2 are ignored. See the discussion of the CLONE_FS flag in clone(2).
- KCMP_IO
- Check whether the processes share I/O context. The arguments idx1 and idx2 are ignored. See the discussion of the CLONE_IO flag in clone(2).
- KCMP_SIGHAND
- Check whether the processes share the same table of signal dispositions. The arguments idx1 and idx2 are ignored. See the discussion of the CLONE_SIGHAND flag in clone(2).
- KCMP_SYSVSEM
- Check whether the processes share the same list of System V semaphore undo operations. The arguments idx1 and idx2 are ignored. See the discussion of the CLONE_SYSVSEM flag in clone(2).
- KCMP_VM
- Check whether the processes share the same address space. The arguments idx1 and idx2 are ignored. See the discussion of the CLONE_VM flag in clone(2).
- KCMP_EPOLL_TFD (since Linux 4.13)
- Check whether the file descriptor idx1 of the process pid1 is present in the epoll(7) instance described by idx2 of the process pid2. The argument idx2 is a pointer to a structure where the target file is described. This structure has the form:
struct kcmp_epoll_slot {
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.
Note the
kcmp()
is not protected against false positives which may occur if
the processes are currently running.
One should stop the processes by sending
SIGSTOP
(see
signal(7))
prior to inspection with this system call to obtain meaningful results.
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:
On error, -1 is returned, and
errno
is set appropriately.
kcmp()
was designed to return values suitable for sorting.
This is particularly handy if one needs to compare
a large number of file descriptors.
This system call is available only if the kernel was configured with
CONFIG_CHECKPOINT_RESTORE.
The main use of the system call is 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.
See
clone(2)
for some background information on the shared resources
referred to on this page.
$ ./a.out
Parent PID is 1144
Parent opened file on FD 3
PID of child of fork() is 1145
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
static int
kcmp(pid_t pid1, pid_t pid2, int type,
static void
test_kcmp(char *msg, pid_t pid1, pid_t pid2, int fd_a, int fd_b)
{
int
main(int argc, char *argv[])
{
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).
ERRORS
VERSIONS
The
kcmp()
system call first appeared in Linux 3.5.
CONFORMING TO
kcmp()
is Linux-specific and should not be used in programs intended to be portable.
NOTES
Glibc does not provide a wrapper for this system call; call it using
syscall(2).
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:
Child opened file on FD 4
Child duplicated FD 3 to create FD 5
Program source
#define _GNU_SOURCE
#include <sys/syscall.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/kcmp.h>