| 1 |
.\" |
| 2 |
.\" Copyright (c) 2020-2024 Jim Warner <[email protected]> |
| 3 |
.\" Copyright (c) 2020-2024 Craig Small <[email protected]> |
| 4 |
.\" |
| 5 |
.\" This manual is free software; you can redistribute it and/or |
| 6 |
.\" modify it under the terms of the GNU Lesser General Public |
| 7 |
.\" License as published by the Free Software Foundation; either |
| 8 |
.\" version 2.1 of the License, or (at your option) any later version. |
| 9 |
.\" |
| 10 |
.\" |
| 11 |
.TH PROCPS_PIDS 3 2004-09-03 procps-ng |
| 12 |
.\" Please adjust this date whenever revising the manpage. |
| 13 |
.\" |
| 14 |
.nh |
| 15 |
.SH NAME |
| 16 |
procps_pids \- API to access process information in the /proc filesystem |
| 17 |
.SH SYNOPSIS |
| 18 |
.nf |
| 19 |
#include <libproc2/pids.h> |
| 20 |
.P |
| 21 |
.RI "int\fB procps_pids_new \fR (struct pids_info **" info ", enum pids_item *" items ", int " numitems ); |
| 22 |
.RI "int\fB procps_pids_ref \fR (struct pids_info *" info ); |
| 23 |
.RI "int\fB procps_pids_unref\fR (struct pids_info **" info ); |
| 24 |
.P |
| 25 |
.RB "struct pids_stack *" procps_pids_get " (" |
| 26 |
.RI " struct pids_info *" info , |
| 27 |
.RI " enum pids_fetch_type " which ); |
| 28 |
.P |
| 29 |
.RB "struct pids_fetch *" procps_pids_reap " (" |
| 30 |
.RI " struct pids_info *" info , |
| 31 |
.RI " enum pids_fetch_type " which ); |
| 32 |
.P |
| 33 |
.RB "struct pids_fetch *" procps_pids_select " (" |
| 34 |
.RI " struct pids_info *" info , |
| 35 |
.RI " unsigned *" these , |
| 36 |
.RI " int " numthese , |
| 37 |
.RI " enum pids_select_type " which ); |
| 38 |
.P |
| 39 |
.RB "struct pids_stack **" procps_pids_sort " (" |
| 40 |
.RI " struct pids_info *" info , |
| 41 |
.RI " struct pids_stack *" stacks [], |
| 42 |
.RI " int " numstacked , |
| 43 |
.RI " enum pids_item " sortitem , |
| 44 |
.RI " enum pids_sort_order " order ); |
| 45 |
.P |
| 46 |
.RB "int " procps_pids_reset " (" |
| 47 |
.RI " struct pids_info *" info , |
| 48 |
.RI " enum pids_item *" newitems , |
| 49 |
.RI " int " newnumitems ); |
| 50 |
.P |
| 51 |
.RB "struct pids_stack *" fatal_proc_unmounted " (" |
| 52 |
.RI " struct pids_info *" info , |
| 53 |
.RI " int " return_self ); |
| 54 |
.P |
| 55 |
.fi |
| 56 |
.P |
| 57 |
Link with \fI\-lproc2\fP. |
| 58 |
.SH DESCRIPTION |
| 59 |
.SS Overview |
| 60 |
Central to this interface is a simple \[oq]result\[cq] |
| 61 |
structure reflecting an \[oq]item\[cq] plus its value (in a union |
| 62 |
with standard C language types as members). |
| 63 |
All \[oq]result\[cq] structures are automatically allocated and |
| 64 |
provided by the library. |
| 65 |
.P |
| 66 |
By specifying an array of \[oq]items\[cq], these structures can be |
| 67 |
organized as a \[oq]stack\[cq], potentially yielding many results |
| 68 |
with a single function call. |
| 69 |
Thus, a \[oq]stack\[cq] can be viewed as a variable length record |
| 70 |
whose content and order is determined solely by the user. |
| 71 |
.P |
| 72 |
As part of this interface there are two unique enumerators. |
| 73 |
The \[oq]noop\[cq] and \[oq]extra\[cq] items exist to hold user values. |
| 74 |
They are never set by the library, but the \[oq]extra\[cq] |
| 75 |
result will be zeroed with each library interaction. |
| 76 |
.P |
| 77 |
The pids.h file will be an essential document during |
| 78 |
user program development. |
| 79 |
There you will find available items, their return type |
| 80 |
(the \[oq]result\[cq] struct member name) |
| 81 |
and the source for such values. |
| 82 |
Additional enumerators and structures are also documented there. |
| 83 |
.SS Usage |
| 84 |
The following would be a typical sequence of calls to |
| 85 |
this interface. |
| 86 |
.P |
| 87 |
.nf |
| 88 |
.RB "1. " fatal_proc_unmounted() |
| 89 |
.RB "2. " procps_pids_new() |
| 90 |
.RB "3. " procps_pids_get() ", " procps_pids_reap() " or " procps_pids_select() |
| 91 |
.RB "4. " procps_pids_unref() |
| 92 |
.fi |
| 93 |
.P |
| 94 |
The \fBget\fR function is an iterator for successive PIDs/TIDs, |
| 95 |
returning those \[oq]items\[cq] previously identified via \fBnew\fR |
| 96 |
or \fBreset\fR. |
| 97 |
.P |
| 98 |
Two functions support unpredictable variable outcomes. |
| 99 |
The \fBreap\fR function gathers data for all processes while |
| 100 |
the \fBselect\fR function deals with specific PIDs or UIDs. |
| 101 |
Both can return multiple \[oq]stacks\[cq] each containing multiple |
| 102 |
\[oq]result\[cq] structures. |
| 103 |
Optionally, a user may choose to \fBsort\fR such results |
| 104 |
.P |
| 105 |
To exploit any \[oq]stack\[cq], |
| 106 |
and access individual \[oq]result\[cq] structures, |
| 107 |
a \fIrelative_enum\fR is required as shown in the \fBVAL\fR macro |
| 108 |
defined in the header file. |
| 109 |
Such values could be hard coded as: 0 through numitems-1. |
| 110 |
However, this need is typically satisfied by creating your own |
| 111 |
enumerators corresponding to the order of the \[oq]items\[cq] array. |
| 112 |
.SS Caveats |
| 113 |
The <pids> API differs from others in that those items |
| 114 |
of interest must be provided at \fBnew\fR or \fBreset\fR time, |
| 115 |
the latter being unique to this API. |
| 116 |
If either the \fIitems\fR or \fInumitems\fR parameter is zero at |
| 117 |
\fBnew\fR time, then \fBreset\fR becomes mandatory before |
| 118 |
issuing any other call. |
| 119 |
Otherwise, procps_pids_reset() should only be called when the order or |
| 120 |
number of items in your stacks is changed. |
| 121 |
.P |
| 122 |
For the \fBnew\fR and \fBunref\fR functions, the address of an \fIinfo\fR |
| 123 |
struct pointer must be supplied. |
| 124 |
With \fBnew\fR it must have been initialized to NULL. |
| 125 |
With \fBunref\fR it will be reset to NULL if the reference count reaches zero. |
| 126 |
.P |
| 127 |
The \fBget\fR and \fBreap\fR functions use the \fIwhich\fR parameter |
| 128 |
to specify whether just tasks or both tasks and threads are to be fetched. |
| 129 |
.P |
| 130 |
The \fBselect\fR function requires an array of PIDs or UIDs as |
| 131 |
\fIthese\fR along with \fInumthese\fR to identify which processes |
| 132 |
are to be fetched. |
| 133 |
This function then operates as a subset of \fBreap\fR. |
| 134 |
.P |
| 135 |
When using the \fBsort\fR function, the parameters \fIstacks\fR and |
| 136 |
\fInumstacked\fR would normally be those returned in the |
| 137 |
\[oq]pids_fetch\[cq] structure. |
| 138 |
.P |
| 139 |
Lastly, a \fBfatal_proc_unmounted\fR function may be called before |
| 140 |
any other function to ensure that the /proc/ directory is mounted. |
| 141 |
As such, the \fIinfo\fR parameter would be NULL and the |
| 142 |
\fIreturn_self\fR parameter zero. |
| 143 |
If, however, some items are desired for the issuing program (a |
| 144 |
\fIreturn_self\fR other than zero) then the \fBnew\fR call must precede |
| 145 |
it to identify the \fIitems\fR and obtain the required \fIinfo\fR pointer. |
| 146 |
.SH RETURN VALUE |
| 147 |
.SS Functions Returning an \[oq]int\[cq] |
| 148 |
An error will be indicated by a negative number that |
| 149 |
is always the inverse of some well known errno.h value. |
| 150 |
.P |
| 151 |
Success is indicated by a zero return value. |
| 152 |
However, the \fBref\fR and \fBunref\fR functions return |
| 153 |
the current \fIinfo\fR structure reference count. |
| 154 |
.SS Functions Returning an \[oq]address\[cq] |
| 155 |
An error will be indicated by a NULL return pointer |
| 156 |
with the reason found in the formal errno value. |
| 157 |
.P |
| 158 |
Success is indicated by a pointer to the named structure. |
| 159 |
However, if one survives the \fBfatal_proc_unmounted\fR call, |
| 160 |
NULL is always returned when \fIreturn_self\fR is zero. |
| 161 |
.SH DEBUGGING |
| 162 |
To aid in program development, there are two procps-ng provisions |
| 163 |
that can be exploited. |
| 164 |
.P |
| 165 |
The first is a supplied file named \[oq]libproc.supp\[cq] which may be |
| 166 |
useful when developing a \fImulti-threaded\fR application. |
| 167 |
When used with the valgrind \[oq]--suppressions=\[cq] option, warnings |
| 168 |
associated with the procps library itself are avoided. |
| 169 |
.P |
| 170 |
Such warnings arise because the library handles heap based |
| 171 |
allocations in a thread-safe manner. |
| 172 |
A \fIsingle-threaded\fR application will not receive those warnings. |
| 173 |
.P |
| 174 |
The second provision can help ensure \[oq]result\[cq] member references |
| 175 |
agree with library expectations. |
| 176 |
It assumes that a supplied macro in the header file is |
| 177 |
used to access the \[oq]result\[cq] value. |
| 178 |
.P |
| 179 |
This feature can be activated through either of the following |
| 180 |
methods and any discrepancies will be written to \fBstderr\fR. |
| 181 |
.IP 1) 3 |
| 182 |
Add CFLAGS='-DXTRA_PROCPS_DEBUG' to any other ./configure |
| 183 |
options your project may employ. |
| 184 |
.IP 2) 3 |
| 185 |
Add #include <libproc2/xtra-procps-debug.h> to any program |
| 186 |
\fIafter\fR the #include <libproc2/pids.h>. |
| 187 |
.PP |
| 188 |
This verification feature incurs substantial overhead. |
| 189 |
Therefore, it is important that it \fInot\fR be activated |
| 190 |
for a production/release build. |
| 191 |
.SH ENVIRONMENT VARIABLE(S) |
| 192 |
The value set for the following is unimportant, just its presence. |
| 193 |
.IP LIBPROC_HIDE_KERNEL |
| 194 |
This will hide kernel threads which would otherwise be returned with a |
| 195 |
.BR procps_pids_get ", " procps_pids_select " or " procps_pids_reap |
| 196 |
call. |
| 197 |
.SH SEE ALSO |
| 198 |
.BR procps (3), |
| 199 |
.BR procps_misc (3), |
| 200 |
.BR proc (5). |