597 lines
15 KiB
HTML
597 lines
15 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of KCMP</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>KCMP</H1>
|
|
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-10-10<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
kcmp - compare two processes to determine if they share a kernel resource
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/linux/kcmp.h">linux/kcmp.h</A>></B>
|
|
|
|
<B>int kcmp(pid_t </B><I>pid1</I><B>, pid_t </B><I>pid2</I><B>, int </B><I>type</I><B>,</B>
|
|
<B> unsigned long </B><I>idx1</I><B>, unsigned long </B><I>idx2</I><B>);</B>
|
|
</PRE>
|
|
|
|
<P>
|
|
|
|
<I>Note</I>:
|
|
|
|
There is no glibc wrapper for this system call; see NOTES.
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>kcmp</B>()
|
|
|
|
system call can be used to check whether the two processes identified by
|
|
<I>pid1</I>
|
|
|
|
and
|
|
<I>pid2</I>
|
|
|
|
share a kernel resource such as virtual memory, file descriptors,
|
|
and so on.
|
|
<P>
|
|
|
|
Permission to employ
|
|
<B>kcmp</B>()
|
|
|
|
is governed by ptrace access mode
|
|
<B>PTRACE_MODE_READ_REALCREDS</B>
|
|
|
|
checks against both
|
|
<I>pid1</I>
|
|
|
|
and
|
|
<I>pid2</I>;
|
|
|
|
see
|
|
<B><A HREF="/cgi-bin/man/man2html?2+ptrace">ptrace</A></B>(2).
|
|
|
|
<P>
|
|
|
|
The
|
|
<I>type</I>
|
|
|
|
argument specifies which resource is to be compared in the two processes.
|
|
It has one of the following values:
|
|
<DL COMPACT>
|
|
<DT id="1"><B>KCMP_FILE</B>
|
|
|
|
<DD>
|
|
Check whether a file descriptor
|
|
<I>idx1</I>
|
|
|
|
in the process
|
|
<I>pid1</I>
|
|
|
|
refers to the same open file description (see
|
|
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2))
|
|
|
|
as file descriptor
|
|
<I>idx2</I>
|
|
|
|
in the process
|
|
<I>pid2</I>.
|
|
|
|
The existence of two file descriptors that refer to the same
|
|
open file description can occur as a result of
|
|
<B><A HREF="/cgi-bin/man/man2html?2+dup">dup</A></B>(2)
|
|
|
|
(and similar)
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2),
|
|
|
|
or passing file descriptors via a domain socket (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+unix">unix</A></B>(7)).
|
|
|
|
<DT id="2"><B>KCMP_FILES</B>
|
|
|
|
<DD>
|
|
Check whether the processes share the same set of open file descriptors.
|
|
The arguments
|
|
<I>idx1</I>
|
|
|
|
and
|
|
<I>idx2</I>
|
|
|
|
are ignored.
|
|
See the discussion of the
|
|
<B>CLONE_FILES</B>
|
|
|
|
flag in
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2).
|
|
|
|
<DT id="3"><B>KCMP_FS</B>
|
|
|
|
<DD>
|
|
Check whether the processes share the same filesystem information
|
|
(i.e., file mode creation mask, working directory, and filesystem root).
|
|
The arguments
|
|
<I>idx1</I>
|
|
|
|
and
|
|
<I>idx2</I>
|
|
|
|
are ignored.
|
|
See the discussion of the
|
|
<B>CLONE_FS</B>
|
|
|
|
flag in
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2).
|
|
|
|
<DT id="4"><B>KCMP_IO</B>
|
|
|
|
<DD>
|
|
Check whether the processes share I/O context.
|
|
The arguments
|
|
<I>idx1</I>
|
|
|
|
and
|
|
<I>idx2</I>
|
|
|
|
are ignored.
|
|
See the discussion of the
|
|
<B>CLONE_IO</B>
|
|
|
|
flag in
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2).
|
|
|
|
<DT id="5"><B>KCMP_SIGHAND</B>
|
|
|
|
<DD>
|
|
Check whether the processes share the same table of signal dispositions.
|
|
The arguments
|
|
<I>idx1</I>
|
|
|
|
and
|
|
<I>idx2</I>
|
|
|
|
are ignored.
|
|
See the discussion of the
|
|
<B>CLONE_SIGHAND</B>
|
|
|
|
flag in
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2).
|
|
|
|
<DT id="6"><B>KCMP_SYSVSEM</B>
|
|
|
|
<DD>
|
|
Check whether the processes share the same
|
|
list of System V semaphore undo operations.
|
|
The arguments
|
|
<I>idx1</I>
|
|
|
|
and
|
|
<I>idx2</I>
|
|
|
|
are ignored.
|
|
See the discussion of the
|
|
<B>CLONE_SYSVSEM</B>
|
|
|
|
flag in
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2).
|
|
|
|
<DT id="7"><B>KCMP_VM</B>
|
|
|
|
<DD>
|
|
Check whether the processes share the same address space.
|
|
The arguments
|
|
<I>idx1</I>
|
|
|
|
and
|
|
<I>idx2</I>
|
|
|
|
are ignored.
|
|
See the discussion of the
|
|
<B>CLONE_VM</B>
|
|
|
|
flag in
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2).
|
|
|
|
<DT id="8"><B>KCMP_EPOLL_TFD</B> (since Linux 4.13)
|
|
|
|
<DD>
|
|
|
|
Check whether the file descriptor
|
|
<I>idx1</I>
|
|
|
|
of the process
|
|
<I>pid1</I>
|
|
|
|
is present in the
|
|
<B><A HREF="/cgi-bin/man/man2html?7+epoll">epoll</A></B>(7)
|
|
|
|
instance described by
|
|
<I>idx2</I>
|
|
|
|
of the process
|
|
<I>pid2</I>.
|
|
|
|
The argument
|
|
<I>idx2</I>
|
|
|
|
is a pointer to a structure where the target file is described.
|
|
This structure has the form:
|
|
</DL>
|
|
<P>
|
|
|
|
|
|
|
|
struct kcmp_epoll_slot {
|
|
<BR> __u32 efd;
|
|
<BR> __u32 tfd;
|
|
<BR> __u64 toff;
|
|
};
|
|
|
|
|
|
<P>
|
|
|
|
Within this structure,
|
|
<I>efd</I>
|
|
|
|
is an epoll file descriptor returned from
|
|
<B><A HREF="/cgi-bin/man/man2html?2+epoll_create">epoll_create</A></B>(2),
|
|
|
|
<I>tfd</I>
|
|
|
|
is a target file descriptor number, and
|
|
<I>toff</I>
|
|
|
|
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.
|
|
<P>
|
|
|
|
Note the
|
|
<B>kcmp</B>()
|
|
|
|
is not protected against false positives which may occur if
|
|
the processes are currently running.
|
|
One should stop the processes by sending
|
|
<B>SIGSTOP</B>
|
|
|
|
(see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+signal">signal</A></B>(7))
|
|
|
|
prior to inspection with this system call to obtain meaningful results.
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
The return value of a successful call to
|
|
<B>kcmp</B>()
|
|
|
|
is simply the result of arithmetic comparison
|
|
of kernel pointers (when the kernel compares resources, it uses their
|
|
memory addresses).
|
|
<P>
|
|
|
|
The easiest way to explain is to consider an example.
|
|
Suppose that
|
|
<I>v1</I>
|
|
|
|
and
|
|
<I>v2</I>
|
|
|
|
are the addresses of appropriate resources, then the return value
|
|
is one of the following:
|
|
<DL COMPACT><DT id="9"><DD>
|
|
<DL COMPACT>
|
|
<DT id="10">0<DD>
|
|
<I>v1</I>
|
|
|
|
is equal to
|
|
<I>v2</I>;
|
|
|
|
in other words, the two processes share the resource.
|
|
<DT id="11">1<DD>
|
|
<I>v1</I>
|
|
|
|
is less than
|
|
<I>v2</I>.
|
|
|
|
<DT id="12">2<DD>
|
|
<I>v1</I>
|
|
|
|
is greater than
|
|
<I>v2</I>.
|
|
|
|
<DT id="13">3<DD>
|
|
<I>v1</I>
|
|
|
|
is not equal to
|
|
<I>v2</I>,
|
|
|
|
but ordering information is unavailable.
|
|
</DL>
|
|
</DL>
|
|
|
|
<P>
|
|
|
|
On error, -1 is returned, and
|
|
<I>errno</I>
|
|
|
|
is set appropriately.
|
|
<P>
|
|
|
|
<B>kcmp</B>()
|
|
|
|
was designed to return values suitable for sorting.
|
|
This is particularly handy if one needs to compare
|
|
a large number of file descriptors.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
<DL COMPACT>
|
|
<DT id="14"><B>EBADF</B>
|
|
|
|
<DD>
|
|
<I>type</I>
|
|
|
|
is
|
|
<B>KCMP_FILE</B>
|
|
|
|
and
|
|
<I>fd1</I>
|
|
|
|
or
|
|
<I>fd2</I>
|
|
|
|
is not an open file descriptor.
|
|
<DT id="15"><B>EFAULT</B>
|
|
|
|
<DD>
|
|
The epoll slot addressed by
|
|
<I>idx2</I>
|
|
|
|
is outside of the user's address space.
|
|
<DT id="16"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
<I>type</I>
|
|
|
|
is invalid.
|
|
<DT id="17"><B>ENOENT</B>
|
|
|
|
<DD>
|
|
The target file is not present in
|
|
<B><A HREF="/cgi-bin/man/man2html?7+epoll">epoll</A></B>(7)
|
|
|
|
instance.
|
|
<DT id="18"><B>EPERM</B>
|
|
|
|
<DD>
|
|
Insufficient permission to inspect process resources.
|
|
The
|
|
<B>CAP_SYS_PTRACE</B>
|
|
|
|
capability is required to inspect processes that you do not own.
|
|
Other ptrace limitations may also apply, such as
|
|
<B>CONFIG_SECURITY_YAMA</B>,
|
|
|
|
which, when
|
|
<I>/proc/sys/kernel/yama/ptrace_scope</I>
|
|
|
|
is 2, limits
|
|
<B>kcmp</B>()
|
|
|
|
to child processes;
|
|
see
|
|
<B><A HREF="/cgi-bin/man/man2html?2+ptrace">ptrace</A></B>(2).
|
|
|
|
<DT id="19"><B>ESRCH</B>
|
|
|
|
<DD>
|
|
Process
|
|
<I>pid1</I>
|
|
|
|
or
|
|
<I>pid2</I>
|
|
|
|
does not exist.
|
|
</DL>
|
|
<A NAME="lbAG"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
The
|
|
<B>kcmp</B>()
|
|
|
|
system call first appeared in Linux 3.5.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
<B>kcmp</B>()
|
|
|
|
is Linux-specific and should not be used in programs intended to be portable.
|
|
<A NAME="lbAI"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
Glibc does not provide a wrapper for this system call; call it using
|
|
<B><A HREF="/cgi-bin/man/man2html?2+syscall">syscall</A></B>(2).
|
|
|
|
<P>
|
|
|
|
This system call is available only if the kernel was configured with
|
|
<B>CONFIG_CHECKPOINT_RESTORE</B>.
|
|
|
|
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
|
|
<B><A HREF="/cgi-bin/man/man2html?5+proc">proc</A></B>(5)
|
|
|
|
filesystem; this was deemed to be unsuitable for security reasons.
|
|
<P>
|
|
|
|
See
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2)
|
|
|
|
for some background information on the shared resources
|
|
referred to on this page.
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The program below uses
|
|
<B>kcmp</B>()
|
|
|
|
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:
|
|
<P>
|
|
|
|
|
|
|
|
$ <B>./a.out</B>
|
|
Parent PID is 1144
|
|
Parent opened file on FD 3
|
|
<P>
|
|
PID of child of fork() is 1145
|
|
<TT> </TT>Compare duplicate FDs from different processes:<BR>
|
|
<TT> </TT><TT> </TT>kcmp(1145, 1144, KCMP_FILE, 3, 3) ==> same<BR>
|
|
Child opened file on FD 4
|
|
<TT> </TT>Compare FDs from distinct open()s in same process:<BR>
|
|
<TT> </TT><TT> </TT>kcmp(1145, 1145, KCMP_FILE, 3, 4) ==> different<BR>
|
|
Child duplicated FD 3 to create FD 5
|
|
<TT> </TT>Compare duplicated FDs in same process:<BR>
|
|
<TT> </TT><TT> </TT>kcmp(1145, 1145, KCMP_FILE, 3, 5) ==> same<BR>
|
|
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
#include <<A HREF="file:///usr/include/sys/syscall.h">sys/syscall.h</A>>
|
|
#include <<A HREF="file:///usr/include/sys/wait.h">sys/wait.h</A>>
|
|
#include <<A HREF="file:///usr/include/sys/stat.h">sys/stat.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>>
|
|
#include <<A HREF="file:///usr/include/fcntl.h">fcntl.h</A>>
|
|
#include <<A HREF="file:///usr/include/linux/kcmp.h">linux/kcmp.h</A>>
|
|
<P>
|
|
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
|
|
<BR> } while (0)
|
|
<P>
|
|
static int
|
|
kcmp(pid_t pid1, pid_t pid2, int type,
|
|
<BR> unsigned long idx1, unsigned long idx2)
|
|
{
|
|
<BR> return syscall(SYS_kcmp, pid1, pid2, type, idx1, idx2);
|
|
}
|
|
<P>
|
|
static void
|
|
test_kcmp(char *msg, id_t pid1, pid_t pid2, int fd_a, int fd_b)
|
|
{
|
|
<BR> printf("\t%s\n", msg);
|
|
<BR> printf("\t\tkcmp(%ld, %ld, KCMP_FILE, %d, %d) ==> %s\n",
|
|
<BR> (long) pid1, (long) pid2, fd_a, fd_b,
|
|
<BR> (kcmp(pid1, pid2, KCMP_FILE, fd_a, fd_b) == 0) ?
|
|
<BR> "same" : "different");
|
|
}
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> int fd1, fd2, fd3;
|
|
<BR> char pathname[] = "/tmp/kcmp.test";
|
|
<P>
|
|
<BR> fd1 = open(pathname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
|
|
<BR> if (fd1 == -1)
|
|
<BR> errExit("open");
|
|
<P>
|
|
<BR> printf("Parent PID is %ld\n", (long) getpid());
|
|
<BR> printf("Parent opened file on FD %d\n\n", fd1);
|
|
<P>
|
|
<BR> switch (fork()) {
|
|
<BR> case -1:
|
|
<BR> errExit("fork");
|
|
<P>
|
|
<BR> case 0:
|
|
<BR> printf("PID of child of fork() is %ld\n", (long) getpid());
|
|
<P>
|
|
<BR> test_kcmp("Compare duplicate FDs from different processes:",
|
|
<BR> getpid(), getppid(), fd1, fd1);
|
|
<P>
|
|
<BR> fd2 = open(pathname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
|
|
<BR> if (fd2 == -1)
|
|
<BR> errExit("open");
|
|
<BR> printf("Child opened file on FD %d\n", fd2);
|
|
<P>
|
|
<BR> test_kcmp("Compare FDs from distinct open()s in same process:",
|
|
<BR> getpid(), getpid(), fd1, fd2);
|
|
<P>
|
|
<BR> fd3 = dup(fd1);
|
|
<BR> if (fd3 == -1)
|
|
<BR> errExit("dup");
|
|
<BR> printf("Child duplicated FD %d to create FD %d\n", fd1, fd3);
|
|
<P>
|
|
<BR> test_kcmp("Compare duplicated FDs in same process:",
|
|
<BR> getpid(), getpid(), fd1, fd3);
|
|
<BR> break;
|
|
<P>
|
|
<BR> default:
|
|
<BR> wait(NULL);
|
|
<BR> }
|
|
<P>
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+unshare">unshare</A></B>(2)
|
|
|
|
<A NAME="lbAM"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="20"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="21"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="22"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="23"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="24"><A HREF="#lbAF">ERRORS</A><DD>
|
|
<DT id="25"><A HREF="#lbAG">VERSIONS</A><DD>
|
|
<DT id="26"><A HREF="#lbAH">CONFORMING TO</A><DD>
|
|
<DT id="27"><A HREF="#lbAI">NOTES</A><DD>
|
|
<DT id="28"><A HREF="#lbAJ">EXAMPLE</A><DD>
|
|
<DL>
|
|
<DT id="29"><A HREF="#lbAK">Program source</A><DD>
|
|
</DL>
|
|
<DT id="30"><A HREF="#lbAL">SEE ALSO</A><DD>
|
|
<DT id="31"><A HREF="#lbAM">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:33 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|