1.\" $OpenBSD: kvm_getprocs.3,v 1.21 2019/08/11 15:48:08 deraadt 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: August 11 2019 $ 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.In sys/param.h 47.In sys/sysctl.h 48.In kvm.h 49.Ft struct kinfo_proc * 50.Fn kvm_getprocs "kvm_t *kd" "int op" "int arg" "size_t elemsize" "int *cnt" 51.Ft char ** 52.Fn kvm_getargv "kvm_t *kd" "const struct kinfo_proc *p" "int nchr" 53.Ft char ** 54.Fn kvm_getenvv "kvm_t *kd" "const struct kinfo_proc *p" "int nchr" 55.Sh DESCRIPTION 56.Fn kvm_getprocs 57returns a (sub-)set of active processes in the kernel indicated by 58.Fa kd . 59The 60.Fa op 61and 62.Fa arg 63arguments constitute a predicate which limits the set of processes returned. 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 84.Xr tty 4 85.Fa arg 86.It Dv KERN_PROC_UID 87processes with effective user ID 88.Fa arg 89.It Dv KERN_PROC_RUID 90processes with real user ID 91.Fa arg 92.El 93.Pp 94Only the first 95.Fa elemsize 96bytes of each array entry are returned. 97If the size of the 98.Vt kinfo_proc 99structure increases in size in a future release of 100.Ox , 101the library will only return the requested amount of data for 102each array entry and programs that use 103.Fn kvm_getprocs 104will continue to function without the need for recompilation. 105The number of processes found is returned in the reference parameter 106.Fa cnt . 107The processes are returned as a contiguous array of 108.Vt kinfo_proc 109structures, the definition for which is available in 110.In sys/sysctl.h . 111This memory is locally allocated, and subsequent calls to 112.Fn kvm_getprocs 113and 114.Fn kvm_close 115will overwrite this storage. 116.Pp 117.Fn kvm_getprocs 118sets the thread ID field accordingly for each thread except for the 119process (main thread) which has it set to \-1. 120.Pp 121.Fn kvm_getargv 122returns a null-terminated argument vector that corresponds to the 123command line arguments passed to process indicated by 124.Fa p . 125Most likely, these arguments correspond to the values passed to 126.Xr execve 2 127on process creation. 128This information is, however, 129deliberately under control of the process itself. 130Note that the original command name can be found, unaltered, 131in the 132.Va p_comm 133field of the process structure returned by 134.Fn kvm_getprocs . 135.Pp 136The 137.Fa nchr 138argument indicates the maximum number of characters, including null bytes, 139to use in building the strings. 140If this amount is exceeded, the string 141causing the overflow is truncated and the partial result is returned. 142This is handy for programs like 143.Xr ps 1 144and 145.Xr w 1 146that print only a one line summary of a command and should not copy 147out large amounts of text only to ignore it. 148If 149.Fa nchr 150is zero, no limit is imposed and all argument strings are returned in 151their entirety. 152.Pp 153The memory allocated to the 154.Li argv 155pointers and string storage is owned by the 156.Xr kvm 3 157library. 158Subsequent 159.Fn kvm_getprocs 160and 161.Xr kvm_close 3 162calls will clobber this storage. 163.Pp 164The 165.Fn kvm_getenvv 166function is similar to 167.Fn kvm_getargv 168but returns the vector of environment strings. 169This data is also alterable by the process. 170.Sh RETURN VALUES 171.Fn kvm_getprocs , 172.Fn kvm_getargv , 173and 174.Fn kvm_getenvv 175all return 176.Dv NULL 177on failure. 178.Sh SEE ALSO 179.Xr kvm 3 , 180.Xr kvm_geterr 3 , 181.Xr kvm_nlist 3 , 182.Xr kvm_open 3 , 183.Xr kvm_read 3 184.Sh BUGS 185These routines do not belong in the 186.Xr kvm 3 187interface. 188