xref: /openbsd-src/lib/libkvm/kvm_getprocs.3 (revision d4db6826f2c70001edcfae4c864482298cec7d85)
1*d4db6826Sderaadt.\"	$OpenBSD: kvm_getprocs.3,v 1.21 2019/08/11 15:48:08 deraadt Exp $
23c298e91Smillert.\"	$NetBSD: kvm_getprocs.3,v 1.13 2003/08/07 16:44:37 agc Exp $
315f0ebb0Sderaadt.\"
4df930be7Sderaadt.\" Copyright (c) 1992, 1993
5df930be7Sderaadt.\"	The Regents of the University of California.  All rights reserved.
6df930be7Sderaadt.\"
7df930be7Sderaadt.\" This code is derived from software developed by the Computer Systems
8df930be7Sderaadt.\" Engineering group at Lawrence Berkeley Laboratory under DARPA contract
9df930be7Sderaadt.\" BG 91-66 and contributed to Berkeley.
10df930be7Sderaadt.\"
11df930be7Sderaadt.\" Redistribution and use in source and binary forms, with or without
12df930be7Sderaadt.\" modification, are permitted provided that the following conditions
13df930be7Sderaadt.\" are met:
14df930be7Sderaadt.\" 1. Redistributions of source code must retain the above copyright
15df930be7Sderaadt.\"    notice, this list of conditions and the following disclaimer.
16df930be7Sderaadt.\" 2. Redistributions in binary form must reproduce the above copyright
17df930be7Sderaadt.\"    notice, this list of conditions and the following disclaimer in the
18df930be7Sderaadt.\"    documentation and/or other materials provided with the distribution.
196580fee3Smillert.\" 3. Neither the name of the University nor the names of its contributors
20df930be7Sderaadt.\"    may be used to endorse or promote products derived from this software
21df930be7Sderaadt.\"    without specific prior written permission.
22df930be7Sderaadt.\"
23df930be7Sderaadt.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24df930be7Sderaadt.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25df930be7Sderaadt.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26df930be7Sderaadt.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27df930be7Sderaadt.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28df930be7Sderaadt.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29df930be7Sderaadt.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30df930be7Sderaadt.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31df930be7Sderaadt.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32df930be7Sderaadt.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33df930be7Sderaadt.\" SUCH DAMAGE.
34df930be7Sderaadt.\"
35df930be7Sderaadt.\"     @(#)kvm_getprocs.3	8.1 (Berkeley) 6/4/93
36df930be7Sderaadt.\"
37*d4db6826Sderaadt.Dd $Mdocdate: August 11 2019 $
38df930be7Sderaadt.Dt KVM_GETPROCS 3
39df930be7Sderaadt.Os
40df930be7Sderaadt.Sh NAME
41df930be7Sderaadt.Nm kvm_getprocs ,
42df930be7Sderaadt.Nm kvm_getargv ,
43a60854cdSguenther.Nm kvm_getenvv
44df930be7Sderaadt.Nd access user process state
45df930be7Sderaadt.Sh SYNOPSIS
4686f9d4cdStedu.In sys/param.h
4786f9d4cdStedu.In sys/sysctl.h
4886f9d4cdStedu.In kvm.h
49df930be7Sderaadt.Ft struct kinfo_proc *
5009431e5cSguenther.Fn kvm_getprocs "kvm_t *kd" "int op" "int arg" "size_t elemsize" "int *cnt"
51df930be7Sderaadt.Ft char **
52df930be7Sderaadt.Fn kvm_getargv "kvm_t *kd" "const struct kinfo_proc *p" "int nchr"
53df930be7Sderaadt.Ft char **
54df930be7Sderaadt.Fn kvm_getenvv "kvm_t *kd" "const struct kinfo_proc *p" "int nchr"
55df930be7Sderaadt.Sh DESCRIPTION
56df930be7Sderaadt.Fn kvm_getprocs
57df930be7Sderaadtreturns a (sub-)set of active processes in the kernel indicated by
58df930be7Sderaadt.Fa kd .
59df930be7SderaadtThe
60df930be7Sderaadt.Fa op
61df930be7Sderaadtand
62df930be7Sderaadt.Fa arg
633c298e91Smillertarguments constitute a predicate which limits the set of processes returned.
642bb07095SaaronThe value of
65df930be7Sderaadt.Fa op
66df930be7Sderaadtdescribes the filtering predicate as follows:
67df930be7Sderaadt.Pp
68df930be7Sderaadt.Bl -tag -width 20n -offset indent -compact
69fbd5ac03Sangelos.It Dv KERN_PROC_KTHREAD
70fbd5ac03Sangelosall processes (user-level plus kernel threads)
712bb07095Saaron.It Dv KERN_PROC_ALL
72fbd5ac03Sangelosall user-level processes
732bb07095Saaron.It Dv KERN_PROC_PID
74096e5ce2Saaronprocesses with process ID
75df930be7Sderaadt.Fa arg
762bb07095Saaron.It Dv KERN_PROC_PGRP
77df930be7Sderaadtprocesses with process group
78df930be7Sderaadt.Fa arg
792bb07095Saaron.It Dv KERN_PROC_SESSION
80df930be7Sderaadtprocesses with session
81df930be7Sderaadt.Fa arg
822bb07095Saaron.It Dv KERN_PROC_TTY
83ce8628bcSjaredyprocesses with
84ce8628bcSjaredy.Xr tty 4
85df930be7Sderaadt.Fa arg
862bb07095Saaron.It Dv KERN_PROC_UID
87096e5ce2Saaronprocesses with effective user ID
88df930be7Sderaadt.Fa arg
892bb07095Saaron.It Dv KERN_PROC_RUID
90096e5ce2Saaronprocesses with real user ID
91df930be7Sderaadt.Fa arg
92df930be7Sderaadt.El
93df930be7Sderaadt.Pp
9409431e5cSguentherOnly the first
9509431e5cSguenther.Fa elemsize
9609431e5cSguentherbytes of each array entry are returned.
9709431e5cSguentherIf the size of the
9809431e5cSguenther.Vt kinfo_proc
9909431e5cSguentherstructure increases in size in a future release of
10009431e5cSguenther.Ox ,
10109431e5cSguentherthe library will only return the requested amount of data for
10209431e5cSguenthereach array entry and programs that use
10309431e5cSguenther.Fn kvm_getprocs
10409431e5cSguentherwill continue to function without the need for recompilation.
105df930be7SderaadtThe number of processes found is returned in the reference parameter
106df930be7Sderaadt.Fa cnt .
1072bb07095SaaronThe processes are returned as a contiguous array of
108ce8628bcSjaredy.Vt kinfo_proc
109ce8628bcSjaredystructures, the definition for which is available in
110369bef3aSschwarze.In sys/sysctl.h .
111df930be7SderaadtThis memory is locally allocated, and subsequent calls to
112df930be7Sderaadt.Fn kvm_getprocs
113df930be7Sderaadtand
114df930be7Sderaadt.Fn kvm_close
115df930be7Sderaadtwill overwrite this storage.
116df930be7Sderaadt.Pp
117243c13beSpirofti.Fn kvm_getprocs
118243c13beSpiroftisets the thread ID field accordingly for each thread except for the
119243c13beSpiroftiprocess (main thread) which has it set to \-1.
120243c13beSpirofti.Pp
121df930be7Sderaadt.Fn kvm_getargv
122df930be7Sderaadtreturns a null-terminated argument vector that corresponds to the
123df930be7Sderaadtcommand line arguments passed to process indicated by
124df930be7Sderaadt.Fa p .
125df930be7SderaadtMost likely, these arguments correspond to the values passed to
126*d4db6826Sderaadt.Xr execve 2
1272bb07095Saaronon process creation.
1282bb07095SaaronThis information is, however,
129df930be7Sderaadtdeliberately under control of the process itself.
130df930be7SderaadtNote that the original command name can be found, unaltered,
131096e5ce2Saaronin the
132ce8628bcSjaredy.Va p_comm
133096e5ce2Saaronfield of the process structure returned by
134df930be7Sderaadt.Fn kvm_getprocs .
135df930be7Sderaadt.Pp
136df930be7SderaadtThe
137df930be7Sderaadt.Fa nchr
138df930be7Sderaadtargument indicates the maximum number of characters, including null bytes,
1392bb07095Saaronto use in building the strings.
1402bb07095SaaronIf this amount is exceeded, the string
141df930be7Sderaadtcausing the overflow is truncated and the partial result is returned.
142df930be7SderaadtThis is handy for programs like
143df930be7Sderaadt.Xr ps 1
144df930be7Sderaadtand
145df930be7Sderaadt.Xr w 1
146df930be7Sderaadtthat print only a one line summary of a command and should not copy
147df930be7Sderaadtout large amounts of text only to ignore it.
148df930be7SderaadtIf
149df930be7Sderaadt.Fa nchr
150df930be7Sderaadtis zero, no limit is imposed and all argument strings are returned in
151df930be7Sderaadttheir entirety.
152df930be7Sderaadt.Pp
153ce8628bcSjaredyThe memory allocated to the
154ce8628bcSjaredy.Li argv
155ce8628bcSjaredypointers and string storage is owned by the
156ce8628bcSjaredy.Xr kvm 3
157ce8628bcSjaredylibrary.
1582bb07095SaaronSubsequent
159df930be7Sderaadt.Fn kvm_getprocs
160df930be7Sderaadtand
161df930be7Sderaadt.Xr kvm_close 3
162df930be7Sderaadtcalls will clobber this storage.
163df930be7Sderaadt.Pp
164df930be7SderaadtThe
165df930be7Sderaadt.Fn kvm_getenvv
166df930be7Sderaadtfunction is similar to
167df930be7Sderaadt.Fn kvm_getargv
1682bb07095Saaronbut returns the vector of environment strings.
1692bb07095SaaronThis data is also alterable by the process.
170a60854cdSguenther.Sh RETURN VALUES
17109431e5cSguenther.Fn kvm_getprocs ,
17209431e5cSguenther.Fn kvm_getargv ,
1733c298e91Smillertand
1743c298e91Smillert.Fn kvm_getenvv
175df930be7Sderaadtall return
176df930be7Sderaadt.Dv NULL
177df930be7Sderaadton failure.
178df930be7Sderaadt.Sh SEE ALSO
179df930be7Sderaadt.Xr kvm 3 ,
180df930be7Sderaadt.Xr kvm_geterr 3 ,
181df930be7Sderaadt.Xr kvm_nlist 3 ,
182df930be7Sderaadt.Xr kvm_open 3 ,
183376bc393Sjmc.Xr kvm_read 3
184cdafad35Sjmc.Sh BUGS
185ce8628bcSjaredyThese routines do not belong in the
186ce8628bcSjaredy.Xr kvm 3
187ce8628bcSjaredyinterface.
188