Movatterモバイル変換


[0]ホーム

URL:


man7.org > Linux >man-pages

Linux/UNIX system programming training


kcmp(2) — Linux manual page

NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |STANDARDS |HISTORY |NOTES |EXAMPLES |SEE ALSO |COLOPHON

kcmp(2)                    System Calls Manualkcmp(2)

NAME        top

       kcmp - compare two processes to determine if they share a kernel       resource

LIBRARY        top

       Standard C library (libc,-lc)

SYNOPSIS        top

#include <linux/kcmp.h>/* Definition ofKCMP_*constants */#include <sys/syscall.h>/* Definition ofSYS_*constants */#include <unistd.h>int syscall(SYS_kcmp, pid_tpid1, pid_tpid2, inttype,unsigned longidx1, unsigned longidx2);Note: glibc provides no wrapper forkcmp(), necessitating the use       ofsyscall(2).

DESCRIPTION        top

       Thekcmp() system call can be used to check whether the two       processes identified bypid1 andpid2 share a kernel resource such       as virtual memory, file descriptors, and so on.       Permission to employkcmp() is governed by ptrace access modePTRACE_MODE_READ_REALCREDSchecks against bothpid1 andpid2; seeptrace(2).       Thetype 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 descriptoridx1 in the processpid1              refers to the same open file description (seeopen(2)) as              file descriptoridx2 in 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)).KCMP_FILES              Check whether the processes share the same set of open file              descriptors.  The argumentsidx1 andidx2 are 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 argumentsidx1 andidx2 are ignored.  See the discussion of theCLONE_FSflag              inclone(2).KCMP_IO              Check whether the processes share I/O context.  The              argumentsidx1 andidx2 are ignored.  See the discussion of              theCLONE_IOflag inclone(2).KCMP_SIGHAND              Check whether the processes share the same table of signal              dispositions.  The argumentsidx1 andidx2 are 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 argumentsidx1 andidx2 are              ignored.  See the discussion of theCLONE_SYSVSEMflag inclone(2).KCMP_VM              Check whether the processes share the same address space.              The argumentsidx1 andidx2 are ignored.  See the              discussion of theCLONE_VMflag inclone(2).KCMP_EPOLL_TFD(since Linux 4.13)              Check whether the file descriptoridx1 of the processpid1              is present in theepoll(7) instance described byidx2 of              the processpid2.  The argumentidx2 is 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       fromepoll_create(2),tfd is a target file descriptor number, andtoff 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 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.

RETURN VALUE        top

       The return value of a successful call tokcmp() is simply the       result of arithmetic comparison of kernel pointers (when the       kernel compares resources, it uses their memory addresses).       The easiest way to explain is to consider an example.  Suppose       thatv1 andv2 are the addresses of appropriate resources, then       the return value is one of the following:0v1 is equal tov2; in other words, the two processes                     share the resource.1v1 is less thanv2.2v1 is greater thanv2.3v1 is not equal tov2, but ordering information is                     unavailable.       On error, -1 is returned, anderrno 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.

ERRORS        top

EBADFtype isKCMP_FILEandfd1 orfd2 is not an open file              descriptor.EFAULTThe epoll slot addressed byidx2 is outside of the user's              address space.EINVALtype is invalid.ENOENTThe target file is not present inepoll(7) instance.EPERMInsufficient 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_scope is 2, limitskcmp() to              child processes; seeptrace(2).ESRCHProcesspid1 orpid2 does not exist.

STANDARDS        top

       Linux.

HISTORY        top

       Linux 3.5.       Before Linux 5.12, this system call is available only if the       kernel is configured withCONFIG_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       theproc(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 withCONFIG_KCMP.

NOTES        top

       Seeclone(2) for some background information on the shared       resources referred to on this page.

EXAMPLES        top

       The program below useskcmp() 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) ==> sameProgram 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        top

clone(2),unshare(2)

COLOPHON        top

       This page is part of theman-pages (Linux kernel and C library       user-space interface documentation) project.  Information about       the project can be found at        ⟨https://www.kernel.org/doc/man-pages/⟩.  If you have a bug report       for this manual page, see       ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩.       This page was obtained from the tarball man-pages-6.15.tar.gz       fetched from       ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on       2025-08-11.  If you discover any rendering problems in this HTML       version of the page, or you believe there is a better or more up-       to-date source for the page, or you have corrections or       improvements to the information in this COLOPHON (which isnot       part of the original manual page), send a mail to       man-pages@man7.orgLinux man-pages 6.15            2025-05-17kcmp(2)

Pages that refer to this page:clone(2)open(2)pidfd_getfd(2)ptrace(2)syscalls(2)unshare(2)capabilities(7)epoll(7)



HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface.

For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere.

Hosting byjambit GmbH.

Cover of TLPI


[8]ページ先頭

©2009-2025 Movatter.jp