man-pages/man2/kcmp.2.html
2021-03-31 01:06:50 +01:00

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">&nbsp;</A>
<H2>NAME</H2>
kcmp - compare two processes to determine if they share a kernel resource
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/linux/kcmp.h">linux/kcmp.h</A>&gt;</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">&nbsp;</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&nbsp;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>&nbsp;&nbsp;&nbsp;&nbsp;__u32&nbsp;efd;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u32&nbsp;tfd;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;__u64&nbsp;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">&nbsp;</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">&nbsp;</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">&nbsp;</A>
<H2>VERSIONS</H2>
The
<B>kcmp</B>()
system call first appeared in Linux 3.5.
<A NAME="lbAH">&nbsp;</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">&nbsp;</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">&nbsp;</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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT>Compare duplicate FDs from different processes:<BR>
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT>kcmp(1145, 1144, KCMP_FILE, 3, 3) ==&gt; same<BR>
Child opened file on FD 4
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT>Compare FDs from distinct open()s in same process:<BR>
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT>kcmp(1145, 1145, KCMP_FILE, 3, 4) ==&gt; different<BR>
Child duplicated FD 3 to create FD 5
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT>Compare duplicated FDs in same process:<BR>
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT>kcmp(1145, 1145, KCMP_FILE, 3, 5) ==&gt; same<BR>
<A NAME="lbAK">&nbsp;</A>
<H3>Program source</H3>
#define _GNU_SOURCE
#include &lt;<A HREF="file:///usr/include/sys/syscall.h">sys/syscall.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/sys/wait.h">sys/wait.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/sys/stat.h">sys/stat.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/fcntl.h">fcntl.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/linux/kcmp.h">linux/kcmp.h</A>&gt;
<P>
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;while&nbsp;(0)
<P>
static int
kcmp(pid_t pid1, pid_t pid2, int type,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;long&nbsp;idx1,&nbsp;unsigned&nbsp;long&nbsp;idx2)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;syscall(SYS_kcmp,&nbsp;pid1,&nbsp;pid2,&nbsp;type,&nbsp;idx1,&nbsp;idx2);
}
<P>
static void
test_kcmp(char *msg, id_t pid1, pid_t pid2, int fd_a, int fd_b)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\t%s\n&quot;,&nbsp;msg);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\t\tkcmp(%ld,&nbsp;%ld,&nbsp;KCMP_FILE,&nbsp;%d,&nbsp;%d)&nbsp;==&gt;&nbsp;%s\n&quot;,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(long)&nbsp;pid1,&nbsp;(long)&nbsp;pid2,&nbsp;fd_a,&nbsp;fd_b,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(kcmp(pid1,&nbsp;pid2,&nbsp;KCMP_FILE,&nbsp;fd_a,&nbsp;fd_b)&nbsp;==&nbsp;0)&nbsp;?
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;same&quot;&nbsp;:&nbsp;&quot;different&quot;);
}
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;fd1,&nbsp;fd2,&nbsp;fd3;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;pathname[]&nbsp;=&nbsp;&quot;/tmp/kcmp.test&quot;;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fd1&nbsp;=&nbsp;open(pathname,&nbsp;O_CREAT&nbsp;|&nbsp;O_RDWR,&nbsp;S_IRUSR&nbsp;|&nbsp;S_IWUSR);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(fd1&nbsp;==&nbsp;-1)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errExit(&quot;open&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Parent&nbsp;PID&nbsp;is&nbsp;%ld\n&quot;,&nbsp;(long)&nbsp;getpid());
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Parent&nbsp;opened&nbsp;file&nbsp;on&nbsp;FD&nbsp;%d\n\n&quot;,&nbsp;fd1);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;switch&nbsp;(fork())&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;-1:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errExit(&quot;fork&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;0:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;PID&nbsp;of&nbsp;child&nbsp;of&nbsp;fork()&nbsp;is&nbsp;%ld\n&quot;,&nbsp;(long)&nbsp;getpid());
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;test_kcmp(&quot;Compare&nbsp;duplicate&nbsp;FDs&nbsp;from&nbsp;different&nbsp;processes:&quot;,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getpid(),&nbsp;getppid(),&nbsp;fd1,&nbsp;fd1);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fd2&nbsp;=&nbsp;open(pathname,&nbsp;O_CREAT&nbsp;|&nbsp;O_RDWR,&nbsp;S_IRUSR&nbsp;|&nbsp;S_IWUSR);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(fd2&nbsp;==&nbsp;-1)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errExit(&quot;open&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Child&nbsp;opened&nbsp;file&nbsp;on&nbsp;FD&nbsp;%d\n&quot;,&nbsp;fd2);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;test_kcmp(&quot;Compare&nbsp;FDs&nbsp;from&nbsp;distinct&nbsp;open()s&nbsp;in&nbsp;same&nbsp;process:&quot;,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getpid(),&nbsp;getpid(),&nbsp;fd1,&nbsp;fd2);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fd3&nbsp;=&nbsp;dup(fd1);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(fd3&nbsp;==&nbsp;-1)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errExit(&quot;dup&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Child&nbsp;duplicated&nbsp;FD&nbsp;%d&nbsp;to&nbsp;create&nbsp;FD&nbsp;%d\n&quot;,&nbsp;fd1,&nbsp;fd3);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;test_kcmp(&quot;Compare&nbsp;duplicated&nbsp;FDs&nbsp;in&nbsp;same&nbsp;process:&quot;,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getpid(),&nbsp;getpid(),&nbsp;fd1,&nbsp;fd3);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;default:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wait(NULL);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAL">&nbsp;</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">&nbsp;</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">&nbsp;</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>