1*dab4556fSkamil.\" $NetBSD: kvm_getprocs.3,v 1.17 2018/01/09 21:17:45 kamil Exp $ 2346e67f8Sthorpej.\" 30215cc7dScgd.\" Copyright (c) 1992, 1993 40215cc7dScgd.\" The Regents of the University of California. All rights reserved. 50215cc7dScgd.\" 60215cc7dScgd.\" This code is derived from software developed by the Computer Systems 70215cc7dScgd.\" Engineering group at Lawrence Berkeley Laboratory under DARPA contract 80215cc7dScgd.\" BG 91-66 and contributed to Berkeley. 90215cc7dScgd.\" 100215cc7dScgd.\" Redistribution and use in source and binary forms, with or without 110215cc7dScgd.\" modification, are permitted provided that the following conditions 120215cc7dScgd.\" are met: 130215cc7dScgd.\" 1. Redistributions of source code must retain the above copyright 140215cc7dScgd.\" notice, this list of conditions and the following disclaimer. 150215cc7dScgd.\" 2. Redistributions in binary form must reproduce the above copyright 160215cc7dScgd.\" notice, this list of conditions and the following disclaimer in the 170215cc7dScgd.\" documentation and/or other materials provided with the distribution. 18eb7c1594Sagc.\" 3. Neither the name of the University nor the names of its contributors 190215cc7dScgd.\" may be used to endorse or promote products derived from this software 200215cc7dScgd.\" without specific prior written permission. 210215cc7dScgd.\" 220215cc7dScgd.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 230215cc7dScgd.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 240215cc7dScgd.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 250215cc7dScgd.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 260215cc7dScgd.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 270215cc7dScgd.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 280215cc7dScgd.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 290215cc7dScgd.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 300215cc7dScgd.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 310215cc7dScgd.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 320215cc7dScgd.\" SUCH DAMAGE. 330215cc7dScgd.\" 340215cc7dScgd.\" @(#)kvm_getprocs.3 8.1 (Berkeley) 6/4/93 350215cc7dScgd.\" 36*dab4556fSkamil.Dd January 9, 2018 370215cc7dScgd.Dt KVM_GETPROCS 3 380215cc7dScgd.Os 390215cc7dScgd.Sh NAME 400215cc7dScgd.Nm kvm_getprocs , 410215cc7dScgd.Nm kvm_getargv , 42fb3eb352Sabhinav.Nm kvm_getenvv , 43fb3eb352Sabhinav.Nm kvm_getproc2 , 44fb3eb352Sabhinav.Nm kvm_getargv2 , 45fb3eb352Sabhinav.Nm kvm_getenvv2 460215cc7dScgd.Nd access user process state 47312aca53Sperry.Sh LIBRARY 48312aca53Sperry.Lb libkvm 490215cc7dScgd.Sh SYNOPSIS 50472351e1Swiz.In kvm.h 5170eabe78Sjmmv.In sys/param.h 52472351e1Swiz.In sys/sysctl.h 530215cc7dScgd.\" .Fa kvm_t *kd 540215cc7dScgd.Ft struct kinfo_proc * 550215cc7dScgd.Fn kvm_getprocs "kvm_t *kd" "int op" "int arg" "int *cnt" 560215cc7dScgd.Ft char ** 570215cc7dScgd.Fn kvm_getargv "kvm_t *kd" "const struct kinfo_proc *p" "int nchr" 580215cc7dScgd.Ft char ** 590215cc7dScgd.Fn kvm_getenvv "kvm_t *kd" "const struct kinfo_proc *p" "int nchr" 60ec323af4Ssimonb.Ft struct kinfo_proc2 * 61*dab4556fSkamil.Fn kvm_getproc2 "kvm_t *kd" "int op" "int arg" "size_t elemsize" "int *cnt" 62ec323af4Ssimonb.Ft char ** 63ec323af4Ssimonb.Fn kvm_getargv2 "kvm_t *kd" "const struct kinfo_proc2 *p" "int nchr" 64ec323af4Ssimonb.Ft char ** 65ec323af4Ssimonb.Fn kvm_getenvv2 "kvm_t *kd" "const struct kinfo_proc2 *p" "int nchr" 660215cc7dScgd.Sh DESCRIPTION 670215cc7dScgd.Fn kvm_getprocs 680215cc7dScgdreturns a (sub-)set of active processes in the kernel indicated by 690215cc7dScgd.Fa kd . 700215cc7dScgdThe 710215cc7dScgd.Fa op 720215cc7dScgdand 730215cc7dScgd.Fa arg 74bec910b3Swizarguments constitute a predicate 75bec910b3Swizwhich limits the set of processes returned. 76bec910b3SwizThe value of 770215cc7dScgd.Fa op 780215cc7dScgddescribes the filtering predicate as follows: 790215cc7dScgd.Pp 800215cc7dScgd.Bl -tag -width 20n -offset indent -compact 81ef2bde6bSmrg.It Sy KERN_PROC_ALL 820215cc7dScgdall processes 83ef2bde6bSmrg.It Sy KERN_PROC_PID 840215cc7dScgdprocesses with process id 850215cc7dScgd.Fa arg 86ef2bde6bSmrg.It Sy KERN_PROC_PGRP 870215cc7dScgdprocesses with process group 880215cc7dScgd.Fa arg 89ef2bde6bSmrg.It Sy KERN_PROC_SESSION 9099bcb5e9Swizprocesses with session id 910215cc7dScgd.Fa arg 92ef2bde6bSmrg.It Sy KERN_PROC_TTY 93ec323af4Ssimonbprocesses with tty device 940215cc7dScgd.Fa arg 95ef2bde6bSmrg.It Sy KERN_PROC_UID 960215cc7dScgdprocesses with effective user id 970215cc7dScgd.Fa arg 98ef2bde6bSmrg.It Sy KERN_PROC_RUID 990215cc7dScgdprocesses with real user id 1000215cc7dScgd.Fa arg 101ec323af4Ssimonb.It Sy KERN_PROC_GID 102ec323af4Ssimonbprocesses with effective group id 103ec323af4Ssimonb.Fa arg 104ec323af4Ssimonb.It Sy KERN_PROC_RGID 105ec323af4Ssimonbprocesses with real group id 106ec323af4Ssimonb.Fa arg 1070215cc7dScgd.El 1080215cc7dScgd.Pp 1090215cc7dScgdThe number of processes found is returned in the reference parameter 1100215cc7dScgd.Fa cnt . 111ec323af4SsimonbThe processes are returned as a contiguous array of 112ec323af4Ssimonb.Sy kinfo_proc 113ec323af4Ssimonbstructures. 1140215cc7dScgdThis memory is locally allocated, and subsequent calls to 1150215cc7dScgd.Fn kvm_getprocs 1160215cc7dScgdand 1170215cc7dScgd.Fn kvm_close 1180215cc7dScgdwill overwrite this storage. 1190215cc7dScgd.Pp 120e465cebbSsimonbIf the 121e465cebbSsimonb.Fa op 122e465cebbSsimonbargument for 123e465cebbSsimonb.Fn kvm_getprocs 124e465cebbSsimonbis 125e465cebbSsimonb.Sy KERN_PROC_TTY , 126e465cebbSsimonb.Fa arg 127e465cebbSsimonbcan also be 128e465cebbSsimonb.Sy KERN_PROC_TTY_NODEV 129e465cebbSsimonbto select processes with no controlling tty and 130e465cebbSsimonb.Sy KERN_PROC_TTY_REVOKE 131e465cebbSsimonbto select processes which have had their controlling tty 132e465cebbSsimonbrevoked. 133e465cebbSsimonb.Pp 1340215cc7dScgd.Fn kvm_getargv 1350215cc7dScgdreturns a null-terminated argument vector that corresponds to the 1360215cc7dScgdcommand line arguments passed to process indicated by 1370215cc7dScgd.Fa p . 1380215cc7dScgdMost likely, these arguments correspond to the values passed to 1390215cc7dScgd.Xr exec 3 140bec910b3Swizon process creation. 141bec910b3SwizThis information is, however, 1420215cc7dScgddeliberately under control of the process itself. 1430215cc7dScgdNote that the original command name can be found, unaltered, 1440215cc7dScgdin the p_comm field of the process structure returned by 1450215cc7dScgd.Fn kvm_getprocs . 1460215cc7dScgd.Pp 1470215cc7dScgdThe 1480215cc7dScgd.Fa nchr 1490215cc7dScgdargument indicates the maximum number of characters, including null bytes, 150bec910b3Swizto use in building the strings. 151bec910b3SwizIf this amount is exceeded, the string 1520215cc7dScgdcausing the overflow is truncated and the partial result is returned. 1530215cc7dScgdThis is handy for programs like 1540215cc7dScgd.Xr ps 1 1550215cc7dScgdand 1560215cc7dScgd.Xr w 1 1570215cc7dScgdthat print only a one line summary of a command and should not copy 1580215cc7dScgdout large amounts of text only to ignore it. 1590215cc7dScgdIf 1600215cc7dScgd.Fa nchr 1610215cc7dScgdis zero, no limit is imposed and all argument strings are returned in 1620215cc7dScgdtheir entirety. 1630215cc7dScgd.Pp 1640215cc7dScgdThe memory allocated to the argv pointers and string storage 165bec910b3Swizis owned by the kvm library. 166bec910b3SwizSubsequent 1670215cc7dScgd.Fn kvm_getprocs 1680215cc7dScgdand 1690215cc7dScgd.Xr kvm_close 3 1700215cc7dScgdcalls will clobber this storage. 1710215cc7dScgd.Pp 1720215cc7dScgdThe 1730215cc7dScgd.Fn kvm_getenvv 1740215cc7dScgdfunction is similar to 1750215cc7dScgd.Fn kvm_getargv 176bec910b3Swizbut returns the vector of environment strings. 177bec910b3SwizThis data is also alterable by the process. 178ec323af4Ssimonb.Pp 179ec323af4Ssimonb.Fn kvm_getproc2 180ec323af4Ssimonbis similar to 181ec323af4Ssimonb.Fn kvm_getprocs 182ec323af4Ssimonbbut returns an array of 183ec323af4Ssimonb.Sy kinfo_proc2 184bec910b3Swizstructures. 185bec910b3SwizAdditionally, only the first 186ec323af4Ssimonb.Fa elemsize 187ec323af4Ssimonbbytes of each array entry are returned. 188ec323af4SsimonbIf the size of the 189ec323af4Ssimonb.Sy kinfo_proc2 190ec323af4Ssimonbstructure increases in size in a future release of 191ec323af4Ssimonb.Nx 192ec323af4Ssimonbthe kernel will only return the requested amount of data for 193ec323af4Ssimonbeach array entry and programs that use 194ec323af4Ssimonb.Fn kvm_getproc2 195ec323af4Ssimonbwill continue to function without the need for recompilation. 196ec323af4Ssimonb.Pp 197ec323af4SsimonbThe 198ec323af4Ssimonb.Fn kvm_getargv2 199ec323af4Ssimonband 200ec323af4Ssimonb.Fn kvm_getenvv2 201ec323af4Ssimonbare equivalents to the 202ec323af4Ssimonb.Fn kvm_getargv 203ec323af4Ssimonband 204ec323af4Ssimonb.Fn kvm_getenvv 205ec323af4Ssimonbfunctions but use a 206ec323af4Ssimonb.Sy kinfo_proc2 207ec323af4Ssimonbstructure to specify the process. 208ec323af4Ssimonb.Pp 209ec323af4SsimonbIf called against an active kernel, the 210ec323af4Ssimonb.Fn kvm_getproc2 , 211ec323af4Ssimonb.Fn kvm_getargv2 , 212ec323af4Ssimonband 213ec323af4Ssimonb.Fn kvm_getenvv2 214ec323af4Ssimonbfunctions will use the 215ec323af4Ssimonb.Xr sysctl 3 216ec323af4Ssimonbinterface and do not require access to the kernel memory device 217ec323af4Ssimonbfile or swap device. 2180215cc7dScgd.Sh RETURN VALUES 2190215cc7dScgd.Fn kvm_getprocs , 2200215cc7dScgd.Fn kvm_getargv , 2210215cc7dScgd.Fn kvm_getenvv , 222ec323af4Ssimonb.Fn kvm_getproc2 , 223ec323af4Ssimonb.Fn kvm_getargv2 , 224ec323af4Ssimonband 225ec323af4Ssimonb.Fn kvm_getenvv2 2260215cc7dScgdall return 2270215cc7dScgd.Dv NULL 2280215cc7dScgdon failure. 2290215cc7dScgd.Sh SEE ALSO 2300215cc7dScgd.Xr kvm 3 , 2310215cc7dScgd.Xr kvm_close 3 , 2320215cc7dScgd.Xr kvm_geterr 3 , 2330215cc7dScgd.Xr kvm_nlist 3 , 2340215cc7dScgd.Xr kvm_open 3 , 2350215cc7dScgd.Xr kvm_openfiles 3 , 2360215cc7dScgd.Xr kvm_read 3 , 2370215cc7dScgd.Xr kvm_write 3 2381d6df4a8Swiz.Sh BUGS 2391d6df4a8SwizThese routines do not belong in the kvm interface. 240