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