xref: /freebsd/lib/libsys/kcmp.2 (revision 38b3683592d4c20a74f52a6e8e29368e6fa61858)
1.\" SPDX-License-Identifier: BSD-2-Clause
2.\"
3.\" Copyright (c) 2024 The FreeBSD Foundation
4.\"
5.\" This documentation was written by
6.\" Mark Johnston <markj@FreeBSD.org> under sponsorship
7.\" from the FreeBSD Foundation.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.Dd January 23, 2024
31.Dt KCMP 2
32.Os
33.Sh NAME
34.Nm kcmp
35.Nd compare two kernel objects
36.Sh LIBRARY
37.Lb libc
38.Sh SYNOPSIS
39.In unistd.h
40.Ft int
41.Fn kcmp "pid_t pid1" "pid_t pid2" "int type" "uintptr_t idx1" "uintptr_t idx2"
42.Sh DESCRIPTION
43The
44.Nm
45system call allows the caller to determine whether the two processes with
46PIDs
47.Fa pid1
48and
49.Fa pid2
50reference the same kernel object.
51The
52.Fa type
53parameter specifies the type of object, and
54.Fa idx1
55and
56.Fa idx2
57are identifiers which refer to some object in the context of process
58.Fa pid1
59and
60.Fa pid2 ,
61respectively.
62.Pp
63The following values for
64.Fa type
65may be specified:
66.Bl -tag -width KCMP_FILE
67.It Dv KCMP_FILE
68Compare two file descriptions referred to by file descriptors
69.Fa idx1
70and
71.Fa idx2 .
72They may be equivalent if, for example, one of the descriptors was
73created by applying
74.Xr dup 2
75to the other descriptor.
76.It Dv KCMP_FILEOBJ
77Perform a
78.Dq deep comparison
79of the file descriptions referred to by file descriptors
80.Fa idx1
81and
82.Fa idx2 .
83This tests whether the underlying object referred to by the file descriptions
84is the same.
85For example, if the same filesystem path is opened twice, the kernel will create
86two separate file descriptions to back the two file descriptors, but they will
87refer to the same underlying object, a
88.Xr vnode 9 .
89When compared using the
90.Dv KCMP_FILE
91type, these descriptors will be different, but using the
92.Dv KCMP_FILEOBJ
93type, they will be equal (assuming that the path was not unlinked in between
94the two opens).
95.It Dv KCMP_FILES
96Determine whether the two processes share the same file descriptor table.
97This will be the case if one of the processes was created by
98.Xr rfork 2
99without specifying the
100.Dv RFFDG
101flag.
102The
103.Fa idx1
104and
105.Fa idx2
106parameters are ignored.
107.It Dv KCMP_SIGHAND
108Determine whether the two processes share the same signal handler table.
109This will be the case if one of the processes was created using the
110.Dv RFSIGSHARE
111flag to
112.Xr rfork 2 .
113The
114.Fa idx1
115and
116.Fa idx2
117parameters are ignored.
118.It Dv KCMP_VM
119Determine whether the two processes share a virtual memory address space.
120This may be the case if one of the processes created the other using
121.Xr vfork 2
122or
123.Xr rfork 2
124with the
125.Dv RFMEM
126flag.
127The
128.Fa idx1
129and
130.Fa idx2
131parameters are ignored.
132.El
133The caller of
134.Nm
135must have permission to debug both processes, otherwise the system call
136will fail.
137.Sh RETURN VALUES
138If
139.Fa idx1
140and
141.Fa idx2
142refer to the same object,
143.Nm
144returns 0.
145If the object referred to by
146.Fa pid1
147and
148.Fa idx1
149is less or greater than the object referred to by
150.Fa pid2
151and
152.Fa idx2 ,
153.Nm
154returns the values 1 and 2, respectively.
155The order is defined internally by the kernel and is stable until the system
156reboots.
157If the two objects cannot be compared for some reason,
158.Nm
159returns 3.
160For example, if
161.Fa type
162is
163.Dv KCMP_FILEOBJ
164and
165.Fa idx1
166and
167.Fa idx2
168are different descriptor types, e.g., a socket and a file, then
169.Nm
170will return 3.
171.Pp
172If an error occurs, the value -1 is returned and the global variable
173.Va errno
174is set to indicate the error.
175.Sh ERRORS
176.Nm
177may fail with the following errors:
178.Bl -tag -width Er
179.It Bq Er ENODEV
180.Dv KCMP_FILEOBJ
181was specified and
182.Fa idx1
183refers to a file descriptor which does not implement a comparison operator.
184.It Bq Er EINVAL
185The value of
186.Fa type
187is invalid.
188.It Bq Er EBADF
189One of the file descriptors referred to by
190.Fa idx1
191or
192.Fa idx2
193is not valid.
194.It Bq Er ESRCH
195One of the processes referred to by
196.Fa pid1
197or
198.Fa pid2
199does not exist or is not visible (e.g., due to jail restrictions).
200.It Bq Er EPERM
201The caller does not have permission to access one of the processes referred to
202by
203.Fa pid1
204or
205.Fa pid2 .
206.El
207.Sh SEE ALSO
208.Xr dup 2 ,
209.Xr fcntl 2 ,
210.Xr fork 2 ,
211.Xr rfork 2 ,
212.Xr vfork 2
213.Sh STANDARDS
214The
215.Nm
216system call originated in Linux.
217This implementation aims to be source-compatible with the Linux implementation.
218.Fx
219implements only a subset of the possible values for
220.Fa type
221supported in Linux.
222More values may be added in the future.
223The
224.Dv KCMP_FILEOBJ
225type is a FreeBSD extension.
226.Sh HISTORY
227The
228.Nm
229function was introduced in
230.Fx 14.1 .
231