xref: /openbsd-src/lib/libkvm/kvm_getprocs.3 (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1.\"	$OpenBSD: kvm_getprocs.3,v 1.10 2003/06/02 20:18:40 millert Exp $
2.\"	$NetBSD: kvm_getprocs.3,v 1.3 1996/05/20 16:58:03 mrg 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 June 4, 1993
38.Dt KVM_GETPROCS 3
39.Os
40.Sh NAME
41.Nm kvm_getprocs ,
42.Nm kvm_getargv ,
43.Nm kvm_getenvv
44.Nd access user process state
45.Sh SYNOPSIS
46.Fd #include <kvm.h>
47.Fd #include <sys/sysctl.h>
48.Ft struct kinfo_proc *
49.Fn kvm_getprocs "kvm_t *kd" "int op" "int arg" "int *cnt"
50.Ft char **
51.Fn kvm_getargv "kvm_t *kd" "const struct kinfo_proc *p" "int nchr"
52.Ft char **
53.Fn kvm_getenvv "kvm_t *kd" "const struct kinfo_proc *p" "int nchr"
54.Sh DESCRIPTION
55.Fn kvm_getprocs
56returns a (sub-)set of active processes in the kernel indicated by
57.Fa kd .
58The
59.Fa op
60and
61.Fa arg
62arguments constitute a predicate which limits the set of processes
63returned.
64The value of
65.Fa op
66describes the filtering predicate as follows:
67.Pp
68.Bl -tag -width 20n -offset indent -compact
69.It Dv KERN_PROC_KTHREAD
70all processes (user-level plus kernel threads)
71.It Dv KERN_PROC_ALL
72all user-level processes
73.It Dv KERN_PROC_PID
74processes with process ID
75.Fa arg
76.It Dv KERN_PROC_PGRP
77processes with process group
78.Fa arg
79.It Dv KERN_PROC_SESSION
80processes with session
81.Fa arg
82.It Dv KERN_PROC_TTY
83processes with tty
84.Fa arg
85.It Dv KERN_PROC_UID
86processes with effective user ID
87.Fa arg
88.It Dv KERN_PROC_RUID
89processes with real user ID
90.Fa arg
91.El
92.Pp
93The number of processes found is returned in the reference parameter
94.Fa cnt .
95The processes are returned as a contiguous array of
96.Li kinfo_proc
97structures.
98This memory is locally allocated, and subsequent calls to
99.Fn kvm_getprocs
100and
101.Fn kvm_close
102will overwrite this storage.
103.Pp
104.Fn kvm_getargv
105returns a null-terminated argument vector that corresponds to the
106command line arguments passed to process indicated by
107.Fa p .
108Most likely, these arguments correspond to the values passed to
109.Xr exec 3
110on process creation.
111This information is, however,
112deliberately under control of the process itself.
113Note that the original command name can be found, unaltered,
114in the
115.Fa p_comm
116field of the process structure returned by
117.Fn kvm_getprocs .
118.Pp
119The
120.Fa nchr
121argument indicates the maximum number of characters, including null bytes,
122to use in building the strings.
123If this amount is exceeded, the string
124causing the overflow is truncated and the partial result is returned.
125This is handy for programs like
126.Xr ps 1
127and
128.Xr w 1
129that print only a one line summary of a command and should not copy
130out large amounts of text only to ignore it.
131If
132.Fa nchr
133is zero, no limit is imposed and all argument strings are returned in
134their entirety.
135.Pp
136The memory allocated to the argv pointers and string storage
137is owned by the kvm library.
138Subsequent
139.Fn kvm_getprocs
140and
141.Xr kvm_close 3
142calls will clobber this storage.
143.Pp
144The
145.Fn kvm_getenvv
146function is similar to
147.Fn kvm_getargv
148but returns the vector of environment strings.
149This data is also alterable by the process.
150.Sh RETURN VALUES
151.Fn kvm_getprocs ,
152.Fn kvm_getargv ,
153and
154.Fn kvm_getenvv ,
155all return
156.Dv NULL
157on failure.
158.Sh SEE ALSO
159.Xr kvm 3 ,
160.Xr kvm_close 3 ,
161.Xr kvm_geterr 3 ,
162.Xr kvm_nlist 3 ,
163.Xr kvm_open 3 ,
164.Xr kvm_openfiles 3 ,
165.Xr kvm_read 3 ,
166.Xr kvm_write 3
167.Sh BUGS
168These routines do not belong in the kvm interface.
169