xref: /openbsd-src/lib/libkvm/kvm_getprocs.3 (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1.\"	$OpenBSD: kvm_getprocs.3,v 1.13 2007/05/31 19:19:35 jmc Exp $
2.\"	$NetBSD: kvm_getprocs.3,v 1.13 2003/08/07 16:44:37 agc Exp $
3.\"
4.\" Copyright (c) 1992, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" This code is derived from software developed by the Computer Systems
8.\" Engineering group at Lawrence Berkeley Laboratory under DARPA contract
9.\" BG 91-66 and contributed to Berkeley.
10.\"
11.\" Redistribution and use in source and binary forms, with or without
12.\" modification, are permitted provided that the following conditions
13.\" are met:
14.\" 1. Redistributions of source code must retain the above copyright
15.\"    notice, this list of conditions and the following disclaimer.
16.\" 2. Redistributions in binary form must reproduce the above copyright
17.\"    notice, this list of conditions and the following disclaimer in the
18.\"    documentation and/or other materials provided with the distribution.
19.\" 3. Neither the name of the University nor the names of its contributors
20.\"    may be used to endorse or promote products derived from this software
21.\"    without specific prior written permission.
22.\"
23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE.
34.\"
35.\"     @(#)kvm_getprocs.3	8.1 (Berkeley) 6/4/93
36.\"
37.Dd $Mdocdate: May 31 2007 $
38.Dt KVM_GETPROCS 3
39.Os
40.Sh NAME
41.Nm kvm_getprocs ,
42.Nm kvm_getargv ,
43.Nm kvm_getenvv ,
44.Nm kvm_getproc2 ,
45.Nm kvm_getargv2 ,
46.Nm kvm_getenvv2
47.Nd access user process state
48.Sh SYNOPSIS
49.Fd #include <sys/param.h>
50.Fd #include <sys/sysctl.h>
51.Fd #include <kvm.h>
52.Ft struct kinfo_proc *
53.Fn kvm_getprocs "kvm_t *kd" "int op" "int arg" "int *cnt"
54.Ft char **
55.Fn kvm_getargv "kvm_t *kd" "const struct kinfo_proc *p" "int nchr"
56.Ft char **
57.Fn kvm_getenvv "kvm_t *kd" "const struct kinfo_proc *p" "int nchr"
58.Ft struct kinfo_proc2 *
59.Fn kvm_getproc2 "kvm_t *kd" "int op" "int arg" "size_t elemsize" "int *cnt"
60.Ft char **
61.Fn kvm_getargv2 "kvm_t *kd" "const struct kinfo_proc2 *p" "int nchr"
62.Ft char **
63.Fn kvm_getenvv2 "kvm_t *kd" "const struct kinfo_proc2 *p" "int nchr"
64.Sh DESCRIPTION
65.Fn kvm_getprocs
66returns a (sub-)set of active processes in the kernel indicated by
67.Fa kd .
68The
69.Fa op
70and
71.Fa arg
72arguments constitute a predicate which limits the set of processes returned.
73The value of
74.Fa op
75describes the filtering predicate as follows:
76.Pp
77.Bl -tag -width 20n -offset indent -compact
78.It Dv KERN_PROC_KTHREAD
79all processes (user-level plus kernel threads)
80.It Dv KERN_PROC_ALL
81all user-level processes
82.It Dv KERN_PROC_PID
83processes with process ID
84.Fa arg
85.It Dv KERN_PROC_PGRP
86processes with process group
87.Fa arg
88.It Dv KERN_PROC_SESSION
89processes with session
90.Fa arg
91.It Dv KERN_PROC_TTY
92processes with
93.Xr tty 4
94.Fa arg
95.It Dv KERN_PROC_UID
96processes with effective user ID
97.Fa arg
98.It Dv KERN_PROC_RUID
99processes with real user ID
100.Fa arg
101.El
102.Pp
103The number of processes found is returned in the reference parameter
104.Fa cnt .
105The processes are returned as a contiguous array of
106.Vt kinfo_proc
107structures, the definition for which is available in
108.Aq Pa sys/sysctl.h .
109This memory is locally allocated, and subsequent calls to
110.Fn kvm_getprocs
111and
112.Fn kvm_close
113will overwrite this storage.
114.Pp
115.Fn kvm_getargv
116returns a null-terminated argument vector that corresponds to the
117command line arguments passed to process indicated by
118.Fa p .
119Most likely, these arguments correspond to the values passed to
120.Xr exec 3
121on process creation.
122This information is, however,
123deliberately under control of the process itself.
124Note that the original command name can be found, unaltered,
125in the
126.Va p_comm
127field of the process structure returned by
128.Fn kvm_getprocs .
129.Pp
130The
131.Fa nchr
132argument indicates the maximum number of characters, including null bytes,
133to use in building the strings.
134If this amount is exceeded, the string
135causing the overflow is truncated and the partial result is returned.
136This is handy for programs like
137.Xr ps 1
138and
139.Xr w 1
140that print only a one line summary of a command and should not copy
141out large amounts of text only to ignore it.
142If
143.Fa nchr
144is zero, no limit is imposed and all argument strings are returned in
145their entirety.
146.Pp
147The memory allocated to the
148.Li argv
149pointers and string storage is owned by the
150.Xr kvm 3
151library.
152Subsequent
153.Fn kvm_getprocs
154and
155.Xr kvm_close 3
156calls will clobber this storage.
157.Pp
158The
159.Fn kvm_getenvv
160function is similar to
161.Fn kvm_getargv
162but returns the vector of environment strings.
163This data is also alterable by the process.
164.Pp
165.Fn kvm_getproc2
166is similar to
167.Fn kvm_getprocs
168but returns an array of
169.Vt kinfo_proc2
170structures.
171Additionally, only the first
172.Fa elemsize
173bytes of each array entry are returned.
174If the size of the
175.Vt kinfo_proc2
176structure increases in size in a future release of
177.Ox ,
178the kernel will only return the requested amount of data for
179each array entry and programs that use
180.Fn kvm_getproc2
181will continue to function without the need for recompilation.
182.Pp
183The
184.Fn kvm_getargv2
185and
186.Fn kvm_getenvv2
187functions are equivalents to the
188.Fn kvm_getargv
189and
190.Fn kvm_getenvv
191functions that use a
192.Vt kinfo_proc2
193structure to specify the process.
194.Sh RETURN VALUES
195.Fn kvm_getprocs ,
196.Fn kvm_getargv ,
197.Fn kvm_getenvv ,
198.Fn kvm_getproc2 ,
199.Fn kvm_getargv2 ,
200and
201.Fn kvm_getenvv2
202all return
203.Dv NULL
204on failure.
205.Sh SEE ALSO
206.Xr kvm 3 ,
207.Xr kvm_close 3 ,
208.Xr kvm_geterr 3 ,
209.Xr kvm_nlist 3 ,
210.Xr kvm_open 3 ,
211.Xr kvm_openfiles 3 ,
212.Xr kvm_read 3 ,
213.Xr kvm_write 3
214.Sh BUGS
215These routines do not belong in the
216.Xr kvm 3
217interface.
218