1 .\" Copyright (C) 2012, Cyrill Gorcunov <gorcunov@openvz.org>
2 .\" and Copyright (C) 2012, 2016, Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of
10 .\" this manual under the conditions for verbatim copying, provided that
11 .\" the entire resulting derived work is distributed under the terms of
12 .\" a permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume
16 .\" no responsibility for errors or omissions, or for damages resulting
17 .\" from the use of the information contained herein. The author(s) may
18 .\" not have taken the same level of care in the production of this
19 .\" manual, which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" Kernel commit d97b46a64674a267bc41c9e16132ee2a98c3347d
28 .TH KCMP 2 2020-11-01 "Linux" "Linux Programmer's Manual"
30 kcmp \- compare two processes to determine if they share a kernel resource
33 .B #include <linux/kcmp.h>
35 .BI "int kcmp(pid_t " pid1 ", pid_t " pid2 ", int " type ,
36 .BI " unsigned long " idx1 ", unsigned long " idx2 );
40 There is no glibc wrapper for this system call; see NOTES.
44 system call can be used to check whether the two processes identified by
48 share a kernel resource such as virtual memory, file descriptors,
53 is governed by ptrace access mode
54 .B PTRACE_MODE_READ_REALCREDS
64 argument specifies which resource is to be compared in the two processes.
65 It has one of the following values:
68 Check whether a file descriptor
72 refers to the same open file description (see
78 The existence of two file descriptors that refer to the same
79 open file description can occur as a result of
83 or passing file descriptors via a domain socket (see
87 Check whether the processes share the same set of open file descriptors.
93 See the discussion of the
99 Check whether the processes share the same filesystem information
100 (i.e., file mode creation mask, working directory, and filesystem root).
106 See the discussion of the
112 Check whether the processes share I/O context.
118 See the discussion of the
124 Check whether the processes share the same table of signal dispositions.
130 See the discussion of the
136 Check whether the processes share the same
137 list of System\ V semaphore undo operations.
143 See the discussion of the
149 Check whether the processes share the same address space.
155 See the discussion of the
160 .BR KCMP_EPOLL_TFD " (since Linux 4.13)"
161 .\" commit 0791e3644e5ef21646fe565b9061788d05ec71d4
162 Check whether the file descriptor
168 instance described by
174 is a pointer to a structure where the target file is described.
175 This structure has the form:
179 struct kcmp_epoll_slot {
187 Within this structure,
189 is an epoll file descriptor returned from
190 .BR epoll_create (2),
192 is a target file descriptor number, and
194 is a target file offset counted from zero.
195 Several different targets may be registered with
196 the same file descriptor number and setting a specific
197 offset helps to investigate each of them.
201 is not protected against false positives which may occur if
202 the processes are currently running.
203 One should stop the processes by sending
207 prior to inspection with this system call to obtain meaningful results.
209 The return value of a successful call to
211 is simply the result of arithmetic comparison
212 of kernel pointers (when the kernel compares resources, it uses their
215 The easiest way to explain is to consider an example.
220 are the addresses of appropriate resources, then the return value
221 is one of the following:
227 in other words, the two processes share the resource.
240 but ordering information is unavailable.
243 On error, \-1 is returned, and
245 is set to indicate the error.
248 was designed to return values suitable for sorting.
249 This is particularly handy if one needs to compare
250 a large number of file descriptors.
261 is not an open file descriptor.
264 The epoll slot addressed by
266 is outside of the user's address space.
273 The target file is not present in
278 Insufficient permission to inspect process resources.
281 capability is required to inspect processes that you do not own.
282 Other ptrace limitations may also apply, such as
283 .BR CONFIG_SECURITY_YAMA ,
285 .I /proc/sys/kernel/yama/ptrace_scope
301 system call first appeared in Linux 3.5.
304 is Linux-specific and should not be used in programs intended to be portable.
306 Glibc does not provide a wrapper for this system call; call it using
310 this system call is available only if the kernel is configured with
311 .BR CONFIG_CHECKPOINT_RESTORE ,
312 since the original pupose the system call was for the
313 checkpoint/restore in user space (CRIU) feature.
314 (The alternative to this system call would have been to expose suitable
315 process information via the
317 filesystem; this was deemed to be unsuitable for security reasons.)
318 Since Linux 5.12, this system call is made available unconditionally.
322 for some background information on the shared resources
323 referred to on this page.
325 The program below uses
327 to test whether pairs of file descriptors refer to
328 the same open file description.
329 The program tests different cases for the file descriptor pairs,
330 as described in the program output.
331 An example run of the program is as follows:
337 Parent opened file on FD 3
339 PID of child of fork() is 1145
340 Compare duplicate FDs from different processes:
341 kcmp(1145, 1144, KCMP_FILE, 3, 3) ==> same
342 Child opened file on FD 4
343 Compare FDs from distinct open()s in same process:
344 kcmp(1145, 1145, KCMP_FILE, 3, 4) ==> different
345 Child duplicated FD 3 to create FD 5
346 Compare duplicated FDs in same process:
347 kcmp(1145, 1145, KCMP_FILE, 3, 5) ==> same
354 #include <sys/syscall.h>
355 #include <sys/wait.h>
356 #include <sys/stat.h>
362 #include <linux/kcmp.h>
364 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
368 kcmp(pid_t pid1, pid_t pid2, int type,
369 unsigned long idx1, unsigned long idx2)
371 return syscall(SYS_kcmp, pid1, pid2, type, idx1, idx2);
375 test_kcmp(char *msg, pid_t pid1, pid_t pid2, int fd_a, int fd_b)
377 printf("\et%s\en", msg);
378 printf("\et\etkcmp(%jd, %jd, KCMP_FILE, %d, %d) ==> %s\en",
379 (intmax_t) pid1, (intmax_t) pid2, fd_a, fd_b,
380 (kcmp(pid1, pid2, KCMP_FILE, fd_a, fd_b) == 0) ?
381 "same" : "different");
385 main(int argc, char *argv[])
388 char pathname[] = "/tmp/kcmp.test";
390 fd1 = open(pathname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
394 printf("Parent PID is %jd\en", (intmax_t) getpid());
395 printf("Parent opened file on FD %d\en\en", fd1);
402 printf("PID of child of fork() is %jd\en", (intmax_t) getpid());
404 test_kcmp("Compare duplicate FDs from different processes:",
405 getpid(), getppid(), fd1, fd1);
407 fd2 = open(pathname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
410 printf("Child opened file on FD %d\en", fd2);
412 test_kcmp("Compare FDs from distinct open()s in same process:",
413 getpid(), getpid(), fd1, fd2);
418 printf("Child duplicated FD %d to create FD %d\en", fd1, fd3);
420 test_kcmp("Compare duplicated FDs in same process:",
421 getpid(), getpid(), fd1, fd3);