1.\" $OpenBSD: kvm_getprocs.3,v 1.8 2001/05/05 00:28:36 angelos 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. All advertising materials mentioning features or use of this software 20.\" must display the following acknowledgement: 21.\" This product includes software developed by the University of 22.\" California, Berkeley and its contributors. 23.\" 4. Neither the name of the University nor the names of its contributors 24.\" may be used to endorse or promote products derived from this software 25.\" without specific prior written permission. 26.\" 27.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37.\" SUCH DAMAGE. 38.\" 39.\" @(#)kvm_getprocs.3 8.1 (Berkeley) 6/4/93 40.\" 41.Dd June 4, 1993 42.Dt KVM_GETPROCS 3 43.Os 44.Sh NAME 45.Nm kvm_getprocs , 46.Nm kvm_getargv , 47.Nm kvm_getenvv 48.Nd access user process state 49.Sh SYNOPSIS 50.Fd #include <kvm.h> 51.Fd #include <sys/sysctl.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.Sh DESCRIPTION 59.Fn kvm_getprocs 60returns a (sub-)set of active processes in the kernel indicated by 61.Fa kd . 62The 63.Fa op 64and 65.Fa arg 66arguments constitute a predicate which limits the set of processes 67returned. 68The value of 69.Fa op 70describes the filtering predicate as follows: 71.Pp 72.Bl -tag -width 20n -offset indent -compact 73.It Dv KERN_PROC_KTHREAD 74all processes (user-level plus kernel threads) 75.It Dv KERN_PROC_ALL 76all user-level processes 77.It Dv KERN_PROC_PID 78processes with process ID 79.Fa arg 80.It Dv KERN_PROC_PGRP 81processes with process group 82.Fa arg 83.It Dv KERN_PROC_SESSION 84processes with session 85.Fa arg 86.It Dv KERN_PROC_TTY 87processes with tty 88.Fa arg 89.It Dv KERN_PROC_UID 90processes with effective user ID 91.Fa arg 92.It Dv KERN_PROC_RUID 93processes with real user ID 94.Fa arg 95.El 96.Pp 97The number of processes found is returned in the reference parameter 98.Fa cnt . 99The processes are returned as a contiguous array of 100.Li kinfo_proc 101structures. 102This memory is locally allocated, and subsequent calls to 103.Fn kvm_getprocs 104and 105.Fn kvm_close 106will overwrite this storage. 107.Pp 108.Fn kvm_getargv 109returns a null-terminated argument vector that corresponds to the 110command line arguments passed to process indicated by 111.Fa p . 112Most likely, these arguments correspond to the values passed to 113.Xr exec 3 114on process creation. 115This information is, however, 116deliberately under control of the process itself. 117Note that the original command name can be found, unaltered, 118in the 119.Fa p_comm 120field of the process structure returned by 121.Fn kvm_getprocs . 122.Pp 123The 124.Fa nchr 125argument indicates the maximum number of characters, including null bytes, 126to use in building the strings. 127If this amount is exceeded, the string 128causing the overflow is truncated and the partial result is returned. 129This is handy for programs like 130.Xr ps 1 131and 132.Xr w 1 133that print only a one line summary of a command and should not copy 134out large amounts of text only to ignore it. 135If 136.Fa nchr 137is zero, no limit is imposed and all argument strings are returned in 138their entirety. 139.Pp 140The memory allocated to the argv pointers and string storage 141is owned by the kvm library. 142Subsequent 143.Fn kvm_getprocs 144and 145.Xr kvm_close 3 146calls will clobber this storage. 147.Pp 148The 149.Fn kvm_getenvv 150function is similar to 151.Fn kvm_getargv 152but returns the vector of environment strings. 153This data is also alterable by the process. 154.Sh RETURN VALUES 155.Fn kvm_getprocs , 156.Fn kvm_getargv , 157and 158.Fn kvm_getenvv , 159all return 160.Dv NULL 161on failure. 162.Sh BUGS 163These routines do not belong in the kvm interface. 164.Sh SEE ALSO 165.Xr kvm 3 , 166.Xr kvm_close 3 , 167.Xr kvm_geterr 3 , 168.Xr kvm_nlist 3 , 169.Xr kvm_open 3 , 170.Xr kvm_openfiles 3 , 171.Xr kvm_read 3 , 172.Xr kvm_write 3 173