1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/param.h>
33 #include <sys/time.h>
34 #include <sys/resource.h>
35 #include <sys/proc.h>
36 #include <sys/sysctl.h>
37 #include <sys/user.h>
38
39 #include <stddef.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <libxo/xo.h>
44
45 #include "ps.h"
46
47 static VAR *findvar(char *, int, char **header);
48 static int vcmp(const void *, const void *);
49
50 /* Compute offset in common structures. */
51 #define KOFF(x) offsetof(struct kinfo_proc, x)
52 #define ROFF(x) offsetof(struct rusage, x)
53
54 #define LWPFMT "d"
55 #define NLWPFMT "d"
56 #define UIDFMT "u"
57 #define PIDFMT "d"
58
59 /* PLEASE KEEP THE TABLE BELOW SORTED ALPHABETICALLY!!! */
60 static VAR var[] = {
61 {"%cpu", "%CPU", NULL, "percent-cpu", 0, pcpu, 0, CHAR, NULL, 0},
62 {"%mem", "%MEM", NULL, "percent-memory", 0, pmem, 0, CHAR, NULL, 0},
63 {"acflag", "ACFLG", NULL, "accounting-flag", 0, kvar, KOFF(ki_acflag),
64 USHORT, "x", 0},
65 {"acflg", "", "acflag", NULL, 0, NULL, 0, CHAR, NULL, 0},
66 {"args", "COMMAND", NULL, "arguments", COMM|LJUST|USER, arguments, 0,
67 CHAR, NULL, 0},
68 {"blocked", "", "sigmask", NULL, 0, NULL, 0, CHAR, NULL, 0},
69 {"caught", "", "sigcatch", NULL, 0, NULL, 0, CHAR, NULL, 0},
70 {"class", "CLASS", NULL, "login-class", LJUST, loginclass, 0, CHAR,
71 NULL, 0},
72 {"comm", "COMMAND", NULL, "command", LJUST, ucomm, 0, CHAR, NULL, 0},
73 {"command", "COMMAND", NULL, "command", COMM|LJUST|USER, command, 0,
74 CHAR, NULL, 0},
75 {"cow", "COW", NULL, "copy-on-write-faults", 0, kvar, KOFF(ki_cow),
76 UINT, "u", 0},
77 {"cpu", "C", NULL, "on-cpu", 0, cpunum, 0, CHAR, NULL, 0},
78 {"cputime", "", "time", NULL, 0, NULL, 0, CHAR, NULL, 0},
79 {"dsiz", "DSIZ", NULL, "data-size", 0, kvar, KOFF(ki_dsize), PGTOK,
80 "ld", 0},
81 {"egid", "", "gid", NULL, 0, NULL, 0, CHAR, NULL, 0},
82 {"egroup", "", "group", NULL, 0, NULL, 0, CHAR, NULL, 0},
83 {"emul", "EMUL", NULL, "emulation-envirnment", LJUST, emulname, 0,
84 CHAR, NULL, 0},
85 {"etime", "ELAPSED", NULL, "elapsed-time", USER, elapsed, 0, CHAR,
86 NULL, 0},
87 {"etimes", "ELAPSED", NULL, "elapsed-times", USER, elapseds, 0, CHAR,
88 NULL, 0},
89 {"euid", "", "uid", NULL, 0, NULL, 0, CHAR, NULL, 0},
90 {"f", "F", NULL, "flags", 0, kvar, KOFF(ki_flag), LONG, "lx", 0},
91 {"f2", "F2", NULL, "flags2", 0, kvar, KOFF(ki_flag2), INT, "08x", 0},
92 {"fib", "FIB", NULL, "fib", 0, kvar, KOFF(ki_fibnum), INT, "d", 0},
93 {"flags", "", "f", NULL, 0, NULL, 0, CHAR, NULL, 0},
94 {"flags2", "", "f2", NULL, 0, NULL, 0, CHAR, NULL, 0},
95 {"gid", "GID", NULL, "gid", 0, kvar, KOFF(ki_groups), UINT, UIDFMT, 0},
96 {"group", "GROUP", NULL, "group", LJUST, egroupname, 0, CHAR, NULL, 0},
97 {"ignored", "", "sigignore", NULL, 0, NULL, 0, CHAR, NULL, 0},
98 {"inblk", "INBLK", NULL, "read-blocks", USER, rvar, ROFF(ru_inblock),
99 LONG, "ld", 0},
100 {"inblock", "", "inblk", NULL, 0, NULL, 0, CHAR, NULL, 0},
101 {"jail", "JAIL", NULL, "jail-name", LJUST, jailname, 0, CHAR, NULL, 0},
102 {"jid", "JID", NULL, "jail-id", 0, kvar, KOFF(ki_jid), INT, "d", 0},
103 {"jobc", "JOBC", NULL, "job-control-count", 0, kvar, KOFF(ki_jobc),
104 SHORT, "d", 0},
105 {"ktrace", "KTRACE", NULL, "ktrace", 0, kvar, KOFF(ki_traceflag), INT,
106 "x", 0},
107 {"label", "LABEL", NULL, "label", LJUST, label, 0, CHAR, NULL, 0},
108 {"lim", "LIM", NULL, "memory-limit", 0, maxrss, 0, CHAR, NULL, 0},
109 {"lockname", "LOCK", NULL, "lock-name", LJUST, lockname, 0, CHAR, NULL,
110 0},
111 {"login", "LOGIN", NULL, "login-name", LJUST, logname, 0, CHAR, NULL,
112 0},
113 {"logname", "", "login", NULL, 0, NULL, 0, CHAR, NULL, 0},
114 {"lstart", "STARTED", NULL, "start-time", LJUST|USER, lstarted, 0,
115 CHAR, NULL, 0},
116 {"lwp", "LWP", NULL, "thread-id", 0, kvar, KOFF(ki_tid), UINT,
117 LWPFMT, 0},
118 {"majflt", "MAJFLT", NULL, "major-faults", USER, rvar, ROFF(ru_majflt),
119 LONG, "ld", 0},
120 {"minflt", "MINFLT", NULL, "minor-faults", USER, rvar, ROFF(ru_minflt),
121 LONG, "ld", 0},
122 {"msgrcv", "MSGRCV", NULL, "received-messages", USER, rvar,
123 ROFF(ru_msgrcv), LONG, "ld", 0},
124 {"msgsnd", "MSGSND", NULL, "sent-messages", USER, rvar,
125 ROFF(ru_msgsnd), LONG, "ld", 0},
126 {"mwchan", "MWCHAN", NULL, "wait-channel", LJUST, mwchan, 0, CHAR,
127 NULL, 0},
128 {"ni", "", "nice", NULL, 0, NULL, 0, CHAR, NULL, 0},
129 {"nice", "NI", NULL, "nice", 0, kvar, KOFF(ki_nice), CHAR, "d", 0},
130 {"nivcsw", "NIVCSW", NULL, "involuntary-context-switches", USER, rvar,
131 ROFF(ru_nivcsw), LONG, "ld", 0},
132 {"nlwp", "NLWP", NULL, "threads", 0, kvar, KOFF(ki_numthreads), UINT,
133 NLWPFMT, 0},
134 {"nsignals", "", "nsigs", NULL, 0, NULL, 0, CHAR, NULL, 0},
135 {"nsigs", "NSIGS", NULL, "signals-taken", USER, rvar,
136 ROFF(ru_nsignals), LONG, "ld", 0},
137 {"nswap", "NSWAP", NULL, "swaps", USER, rvar, ROFF(ru_nswap), LONG,
138 "ld", 0},
139 {"nvcsw", "NVCSW", NULL, "voluntary-context-switches", USER, rvar,
140 ROFF(ru_nvcsw), LONG, "ld", 0},
141 {"nwchan", "NWCHAN", NULL, "wait-channel-address", LJUST, nwchan, 0,
142 CHAR, NULL, 0},
143 {"oublk", "OUBLK", NULL, "written-blocks", USER, rvar,
144 ROFF(ru_oublock), LONG, "ld", 0},
145 {"oublock", "", "oublk", NULL, 0, NULL, 0, CHAR, NULL, 0},
146 {"paddr", "PADDR", NULL, "process-address", 0, kvar, KOFF(ki_paddr),
147 KPTR, "lx", 0},
148 {"pagein", "PAGEIN", NULL, "pageins", USER, pagein, 0, CHAR, NULL, 0},
149 {"pcpu", "", "%cpu", NULL, 0, NULL, 0, CHAR, NULL, 0},
150 {"pending", "", "sig", NULL, 0, NULL, 0, CHAR, NULL, 0},
151 {"pgid", "PGID", NULL, "process-group", 0, kvar, KOFF(ki_pgid), UINT,
152 PIDFMT, 0},
153 {"pid", "PID", NULL, "pid", 0, kvar, KOFF(ki_pid), UINT, PIDFMT, 0},
154 {"pmem", "", "%mem", NULL, 0, NULL, 0, CHAR, NULL, 0},
155 {"ppid", "PPID", NULL, "ppid", 0, kvar, KOFF(ki_ppid), UINT, PIDFMT, 0},
156 {"pri", "PRI", NULL, "priority", 0, pri, 0, CHAR, NULL, 0},
157 {"re", "RE", NULL, "residency-time", INF127, kvar, KOFF(ki_swtime),
158 UINT, "d", 0},
159 {"rgid", "RGID", NULL, "real-gid", 0, kvar, KOFF(ki_rgid), UINT,
160 UIDFMT, 0},
161 {"rgroup", "RGROUP", NULL, "real-group", LJUST, rgroupname, 0, CHAR,
162 NULL, 0},
163 {"rss", "RSS", NULL, "rss", 0, kvar, KOFF(ki_rssize), PGTOK, "ld", 0},
164 {"rtprio", "RTPRIO", NULL, "realtime-priority", 0, priorityr,
165 KOFF(ki_pri), CHAR, NULL, 0},
166 {"ruid", "RUID", NULL, "real-uid", 0, kvar, KOFF(ki_ruid), UINT,
167 UIDFMT, 0},
168 {"ruser", "RUSER", NULL, "real-user", LJUST, runame, 0, CHAR, NULL, 0},
169 {"sid", "SID", NULL, "sid", 0, kvar, KOFF(ki_sid), UINT, PIDFMT, 0},
170 {"sig", "PENDING", NULL, "signals-pending", 0, kvar, KOFF(ki_siglist),
171 INT, "x", 0},
172 {"sigcatch", "CAUGHT", NULL, "signals-caught", 0, kvar,
173 KOFF(ki_sigcatch), UINT, "x", 0},
174 {"sigignore", "IGNORED", NULL, "signals-ignored", 0, kvar,
175 KOFF(ki_sigignore), UINT, "x", 0},
176 {"sigmask", "BLOCKED", NULL, "signal-mask", 0, kvar, KOFF(ki_sigmask),
177 UINT, "x", 0},
178 {"sl", "SL", NULL, "sleep-time", INF127, kvar, KOFF(ki_slptime), UINT,
179 "d", 0},
180 {"ssiz", "SSIZ", NULL, "stack-size", 0, kvar, KOFF(ki_ssize), PGTOK,
181 "ld", 0},
182 {"start", "STARTED", NULL, "start-time", LJUST|USER, started, 0, CHAR,
183 NULL, 0},
184 {"stat", "", "state", NULL, 0, NULL, 0, CHAR, NULL, 0},
185 {"state", "STAT", NULL, "state", LJUST, state, 0, CHAR, NULL, 0},
186 {"svgid", "SVGID", NULL, "saved-gid", 0, kvar, KOFF(ki_svgid), UINT,
187 UIDFMT, 0},
188 {"svuid", "SVUID", NULL, "saved-uid", 0, kvar, KOFF(ki_svuid), UINT,
189 UIDFMT, 0},
190 {"systime", "SYSTIME", NULL, "system-time", USER, systime, 0, CHAR,
191 NULL, 0},
192 {"tdaddr", "TDADDR", NULL, "thread-address", 0, kvar, KOFF(ki_tdaddr),
193 KPTR, "lx", 0},
194 {"tdev", "TDEV", NULL, "terminal-device", 0, tdev, 0, CHAR, NULL, 0},
195 {"tdnam", "", "tdname", NULL, 0, NULL, 0, CHAR, NULL, 0},
196 {"tdname", "TDNAME", NULL, "thread-name", LJUST, tdnam, 0, CHAR,
197 NULL, 0},
198 {"tid", "", "lwp", NULL, 0, NULL, 0, CHAR, NULL, 0},
199 {"time", "TIME", NULL, "cpu-time", USER, cputime, 0, CHAR, NULL, 0},
200 {"tpgid", "TPGID", NULL, "terminal-process-gid", 0, kvar,
201 KOFF(ki_tpgid), UINT, PIDFMT, 0},
202 {"tracer", "TRACER", NULL, "tracer", 0, kvar, KOFF(ki_tracer), UINT,
203 PIDFMT, 0},
204 {"tsid", "TSID", NULL, "terminal-sid", 0, kvar, KOFF(ki_tsid), UINT,
205 PIDFMT, 0},
206 {"tsiz", "TSIZ", NULL, "text-size", 0, kvar, KOFF(ki_tsize), PGTOK,
207 "ld", 0},
208 {"tt", "TT ", NULL, "terminal-name", 0, tname, 0, CHAR, NULL, 0},
209 {"tty", "TTY", NULL, "tty", LJUST, longtname, 0, CHAR, NULL, 0},
210 {"ucomm", "UCOMM", NULL, "accounting-name", LJUST, ucomm, 0, CHAR,
211 NULL, 0},
212 {"uid", "UID", NULL, "uid", 0, kvar, KOFF(ki_uid), UINT, UIDFMT, 0},
213 {"upr", "UPR", NULL, "user-priority", 0, upr, 0, CHAR, NULL, 0},
214 {"uprocp", "UPROCP", NULL, "process-address", 0, kvar, KOFF(ki_paddr),
215 KPTR, "lx", 0},
216 {"user", "USER", NULL, "user", LJUST, username, 0, CHAR, NULL, 0},
217 {"usertime", "USERTIME", NULL, "user-time", USER, usertime, 0, CHAR,
218 NULL, 0},
219 {"usrpri", "", "upr", NULL, 0, NULL, 0, CHAR, NULL, 0},
220 {"vmaddr", "VMADDR", NULL, "vmspace-address", 0, kvar, KOFF(ki_vmspace),
221 KPTR, "lx", 0},
222 {"vsize", "", "vsz", NULL, 0, NULL, 0, CHAR, NULL, 0},
223 {"vsz", "VSZ", NULL, "virtual-size", 0, vsize, 0, CHAR, NULL, 0},
224 {"wchan", "WCHAN", NULL, "wait-channel", LJUST, wchan, 0, CHAR, NULL,
225 0},
226 {"xstat", "XSTAT", NULL, "exit-status", 0, kvar, KOFF(ki_xstat),
227 USHORT, "x", 0},
228 {"", NULL, NULL, NULL, 0, NULL, 0, CHAR, NULL, 0},
229 };
230
231 void
showkey(void)232 showkey(void)
233 {
234 VAR *v;
235 int i;
236 const char *p, *sep;
237
238 i = 0;
239 sep = "";
240 xo_open_list("key");
241 for (v = var; *(p = v->name); ++v) {
242 int len = strlen(p);
243 if (termwidth && (i += len + 1) > termwidth) {
244 i = len;
245 sep = "\n";
246 }
247 xo_emit("{P:/%hs}{l:key/%hs}", sep, p);
248 sep = " ";
249 }
250 xo_emit("\n");
251 xo_close_list("key");
252 if (xo_finish() < 0)
253 xo_err(1, "stdout");
254 }
255
256 void
parsefmt(const char * p,int user)257 parsefmt(const char *p, int user)
258 {
259 char *tempstr, *tempstr1;
260
261 #define FMTSEP " \t,\n"
262 tempstr1 = tempstr = strdup(p);
263 while (tempstr && *tempstr) {
264 char *cp, *hp;
265 VAR *v;
266 struct varent *vent;
267
268 /*
269 * If an item contains an equals sign, it specifies a column
270 * header, may contain embedded separator characters and
271 * is always the last item.
272 */
273 if (tempstr[strcspn(tempstr, "="FMTSEP)] != '=')
274 while ((cp = strsep(&tempstr, FMTSEP)) != NULL &&
275 *cp == '\0')
276 /* void */;
277 else {
278 cp = tempstr;
279 tempstr = NULL;
280 }
281 if (cp == NULL || !(v = findvar(cp, user, &hp)))
282 continue;
283 if (!user) {
284 /*
285 * If the user is NOT adding this field manually,
286 * get on with our lives if this VAR is already
287 * represented in the list.
288 */
289 vent = find_varentry(v);
290 if (vent != NULL)
291 continue;
292 }
293 if ((vent = malloc(sizeof(struct varent))) == NULL)
294 xo_errx(1, "malloc failed");
295 vent->header = v->header;
296 if (hp) {
297 hp = strdup(hp);
298 if (hp)
299 vent->header = hp;
300 }
301 vent->var = malloc(sizeof(*vent->var));
302 if (vent->var == NULL)
303 xo_errx(1, "malloc failed");
304 memcpy(vent->var, v, sizeof(*vent->var));
305 STAILQ_INSERT_TAIL(&varlist, vent, next_ve);
306 }
307 free(tempstr1);
308 if (STAILQ_EMPTY(&varlist)) {
309 xo_warnx("no valid keywords; valid keywords:");
310 showkey();
311 exit(1);
312 }
313 }
314
315 static VAR *
findvar(char * p,int user,char ** header)316 findvar(char *p, int user, char **header)
317 {
318 size_t rflen;
319 VAR *v, key;
320 char *hp, *realfmt;
321
322 hp = strchr(p, '=');
323 if (hp)
324 *hp++ = '\0';
325
326 key.name = p;
327 v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
328
329 if (v && v->alias) {
330 /*
331 * If the user specified an alternate-header for this
332 * (aliased) format-name, then we need to copy that
333 * alternate-header when making the recursive call to
334 * process the alias.
335 */
336 if (hp == NULL)
337 parsefmt(v->alias, user);
338 else {
339 /*
340 * XXX - This processing will not be correct for
341 * any alias which expands into a list of format
342 * keywords. Presently there are no aliases
343 * which do that.
344 */
345 rflen = strlen(v->alias) + strlen(hp) + 2;
346 realfmt = malloc(rflen);
347 if (realfmt == NULL)
348 xo_errx(1, "malloc failed");
349 snprintf(realfmt, rflen, "%s=%s", v->alias, hp);
350 parsefmt(realfmt, user);
351 free(realfmt);
352 }
353 return ((VAR *)NULL);
354 }
355 if (!v) {
356 xo_warnx("%s: keyword not found", p);
357 eval = 1;
358 }
359 if (header)
360 *header = hp;
361 return (v);
362 }
363
364 static int
vcmp(const void * a,const void * b)365 vcmp(const void *a, const void *b)
366 {
367 return (strcmp(((const VAR *)a)->name, ((const VAR *)b)->name));
368 }
369