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