2323.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2424.\" SUCH DAMAGE.
2525.\"
26- .Dd May 10, 2020
26+ .Dd November 4, 2025
2727.Dt KINFO_GETPROC 3
2828.Os
2929.Sh NAME
@@ -42,15 +42,176 @@ This function is used for obtaining process information from the kernel.
4242.Pp
4343The
4444.Ar pid
45- field contains the process identifier.
45+ argument specifies the process identifier.
4646This should be a process that you have privilege to access.
47+ .Pp
4748This function is a wrapper around
4849.Xr sysctl 3
4950with the
5051.Dv KERN_PROC_PID
5152mib.
52- While the kernel returns a packed structure, this function expands the
53- data into a fixed record format.
53+ It retrieves the process information from the kernel and returns it as a
54+ .Vt struct kinfo_proc
55+ allocated with
56+ .Xr malloc 3 .
57+ .Ss Process Information Structure
58+ The
59+ .Vt struct kinfo_proc
60+ contains numerous fields describing a process:
61+ .Bd -literal
62+ struct kinfo_proc {
63+ int ki_structsize; /* size of this structure */
64+ int ki_layout; /* reserved: layout identifier */
65+ pid_t ki_pid; /* process identifier */
66+ pid_t ki_ppid; /* parent process identifier */
67+ pid_t ki_pgid; /* process group identifier */
68+ pid_t ki_tpgid; /* tty process group identifier */
69+ pid_t ki_sid; /* session identifier */
70+ pid_t ki_tsid; /* terminal session identifier */
71+ sigset_t ki_siglist; /* signals arrived but not delivered */
72+ sigset_t ki_sigmask; /* current signal mask */
73+ sigset_t ki_sigignore; /* signals being ignored */
74+ sigset_t ki_sigcatch; /* signals being caught by user */
75+ uid_t ki_uid; /* effective user id */
76+ uid_t ki_ruid; /* real user id */
77+ uid_t ki_svuid; /* saved effective user id */
78+ gid_t ki_rgid; /* real group id */
79+ gid_t ki_svgid; /* saved effective group id */
80+ short ki_ngroups; /* number of groups */
81+ gid_t ki_groups[KI_NGROUPS]; /* groups */
82+ vm_size_t ki_size; /* virtual size */
83+ segsz_t ki_rssize; /* resident set size in pages */
84+ segsz_t ki_swrss; /* resident set size before last swap */
85+ segsz_t ki_tsize; /* text size (pages) */
86+ segsz_t ki_dsize; /* data size (pages) */
87+ segsz_t ki_ssize; /* stack size (pages) */
88+ u_short ki_xstat; /* exit status for wait & stop signal */
89+ u_short ki_acflag; /* accounting flags */
90+ fixpt_t ki_pctcpu; /* %cpu for process during ki_swtime */
91+ u_int64_t ki_runtime; /* real time in microsec */
92+ struct timeval ki_start; /* starting time */
93+ struct timeval ki_childtime; /* time used by process children */
94+ long ki_flag; /* P_* flags */
95+ long ki_kiflag; /* KI_* flags */
96+ int ki_traceflag; /* kernel trace points */
97+ char ki_stat; /* S* process status */
98+ signed char ki_nice; /* process "nice" value */
99+ char ki_lock; /* process lock (prevent swap) count */
100+ char ki_tdname[TDNAMLEN+1]; /* thread name */
101+ char ki_wmesg[WMESGLEN+1]; /* wchan message */
102+ char ki_login[LOGNAMELEN+1]; /* setlogin name */
103+ char ki_lockname[LOCKNAMELEN+1]; /* lock name */
104+ char ki_comm[COMMLEN+1]; /* command name */
105+ char ki_emul[KI_EMULNAMELEN+1]; /* emulation name */
106+ char ki_loginclass[LOGINCLASSLEN+1]; /* login class */
107+ uint64_t ki_tdev; /* controlling tty dev */
108+ int ki_oncpu; /* which cpu we are on (machdep) */
109+ int ki_lastcpu; /* last cpu we were on (machdep) */
110+ int ki_tracer; /* pid of tracing process */
111+ int ki_flag2; /* P2_* flags */
112+ int ki_fibnum; /* default FIB number */
113+ int ki_jid; /* process jail ID */
114+ int ki_numthreads; /* number of threads in total */
115+ lwpid_t ki_tid; /* thread id */
116+ struct priority ki_pri; /* process priority */
117+ struct rusage ki_rusage; /* process rusage statistics */
118+ struct rusage ki_rusage_ch; /* rusage of children processes */
119+ long ki_sflag; /* PS_* flags */
120+ ... /* additional fields */
121+ };
122+ .Ed
123+ .Pp
124+ Key fields include:
125+ .Bl -tag -width " .Va ki_rusage"
126+ .It Va ki_pid
127+ Process identifier.
128+ .It Va ki_ppid
129+ Parent process identifier.
130+ .It Va ki_pgid
131+ Process group identifier.
132+ .It Va ki_sid
133+ Session identifier.
134+ .It Va ki_uid
135+ Effective user ID.
136+ .It Va ki_ruid
137+ Real user ID.
138+ .It Va ki_rgid
139+ Real group ID.
140+ .It Va ki_comm
141+ Command name (up to
142+ .Dv COMMLEN
143+ characters).
144+ .It Va ki_stat
145+ Process status character.
146+ See
147+ .Xr ps 1
148+ for status character meanings (e.g., R=runnable, S=sleeping, Z=zombie).
149+ .It Va ki_flag
150+ Process flags indicating process state and attributes.
151+ Flags include
152+ .Dv P_TRACED
153+ (being traced),
154+ .Dv P_WEXIT
155+ (exiting),
156+ .Dv P_SUGID
157+ (had set-id privileges),
158+ .Dv P_JAILED
159+ (in jail), and others defined in
160+ .In sys/proc.h .
161+ .It Va ki_kiflag
162+ Additional kernel information flags defined in
163+ .In sys/user.h ,
164+ such as
165+ .Dv KI_CTTY
166+ (has controlling tty) and
167+ .Dv KI_SLEADER
168+ (session leader).
169+ .It Va ki_flag2
170+ Extended process flags (P2_* flags) defined in
171+ .In sys/proc.h ,
172+ including security and tracing-related flags.
173+ .It Va ki_nice
174+ Process scheduling priority.
175+ .It Va ki_size
176+ Virtual memory size in bytes.
177+ .It Va ki_rssize
178+ Resident set size in pages.
179+ .It Va ki_tsize
180+ Text (code) size in pages.
181+ .It Va ki_dsize
182+ Data size in pages.
183+ .It Va ki_ssize
184+ Stack size in pages.
185+ .It Va ki_start
186+ Process start time as a
187+ .Vt struct timeval .
188+ .It Va ki_runtime
189+ Accumulated runtime in microseconds.
190+ .It Va ki_rusage
191+ Resource usage statistics as a
192+ .Vt struct rusage .
193+ See
194+ .Xr getrusage 2
195+ for details.
196+ .It Va ki_pctcpu
197+ CPU usage percentage (fixed point value).
198+ .It Va ki_numthreads
199+ Number of threads in the process.
200+ .It Va ki_jid
201+ Jail ID, or 0 if not jailed.
202+ .It Va ki_sflag
203+ Process session flags.
204+ .It Va ki_oncpu , Va ki_lastcpu
205+ Current and last CPU the process ran on.
206+ These fields are machine-dependent.
207+ .El
208+ .Pp
209+ Additional fields provide signal masks, credentials, kernel addresses,
210+ and internal state.
211+ Some fields contain kernel pointers and are only meaningful within the
212+ kernel.
213+ Some fields are machine-dependent and their layout may vary across
214+ architectures.
54215.Sh RETURN VALUES
55216On success the
56217.Fn kinfo_getproc
@@ -66,12 +227,91 @@ On failure the
66227.Fn kinfo_getproc
67228function returns
68229.Dv NULL .
230+ .Sh EXAMPLES
231+ The following example retrieves and displays process memory information:
232+ .Bd -literal -offset indent
233+ #include <sys/types.h>
234+ #include <sys/user.h>
235+ #include <err.h>
236+ #include <libutil.h>
237+ #include <stdio.h>
238+ #include <stdlib.h>
239+ #include <unistd.h>
240+
241+ int
242+ main(int argc, char *argv[])
243+ {
244+ struct kinfo_proc *kp;
245+ int pagesize;
246+ pid_t pid;
247+
248+ if (argc != 2)
249+ errx(1, "usage: %s pid", argv[0]);
250+
251+ pid = atoi(argv[1]);
252+ pagesize = getpagesize();
253+
254+ kp = kinfo_getproc(pid);
255+ if (kp == NULL)
256+ err(1, "kinfo_getproc");
257+
258+ printf("Process %d (%s) memory usage:\\n " ,
259+ kp->ki_pid, kp->ki_comm);
260+ printf(" Virtual size: %zu bytes\\n " ,
261+ (size_t)kp->ki_size);
262+ printf(" Resident size: %zu KB\\n " ,
263+ (size_t)(kp->ki_rssize * pagesize) / 1024);
264+ printf(" Text size: %zu KB\\n " ,
265+ (size_t)(kp->ki_tsize * pagesize) / 1024);
266+ printf(" Data size: %zu KB\\n " ,
267+ (size_t)(kp->ki_dsize * pagesize) / 1024);
268+ printf(" Stack size: %zu KB\\n " ,
269+ (size_t)(kp->ki_ssize * pagesize) / 1024);
270+
271+ free(kp);
272+ return (0);
273+ }
274+ .Ed
69275.Sh SEE ALSO
276+ .Xr ps 1 ,
277+ .Xr getpagesize 2 ,
278+ .Xr getrusage 2 ,
70279.Xr free 3 ,
280+ .Xr kinfo_getallproc 3 ,
71281.Xr malloc 3 ,
72282.Xr sysctl 3
73283.Sh HISTORY
74284The
75285.Fn kinfo_getproc
76286function first appeared in
77287.Fx 7.0 .
288+ .Sh CAVEATS
289+ The
290+ .Vt struct kinfo_proc
291+ structure is not a stable ABI.
292+ Fields may be added, removed, or reordered between
293+ .Fx
294+ releases.
295+ The
296+ .Va ki_structsize
297+ field should be checked to detect changes.
298+ .Pp
299+ Some structure fields are machine-dependent and may not be present
300+ or may have different sizes on all architectures.
301+ Applications that use machine-dependent fields are not portable
302+ across architectures.
303+ .Pp
304+ The
305+ .Vt struct kinfo_proc
306+ structure and its field names are specific to
307+ .Fx .
308+ Other BSD systems provide similar functionality but with different
309+ structure definitions and field names.
310+ Portable applications should avoid relying on the structure layout.
311+ .Pp
312+ Many fields contain kernel virtual addresses which are only meaningful
313+ within the kernel and should not be dereferenced by user programs.
314+ .Pp
315+ The resource usage information in
316+ .Va ki_rusage
317+ may be updated asynchronously and may not reflect instantaneous values.
0 commit comments