xref: /freebsd/bin/ps/keyword.c (revision afe61c15161c324a7af299a9b8457aba5afc92db)
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static char sccsid[] = "@(#)keyword.c	8.5 (Berkeley) 4/2/94";
36 #endif /* not lint */
37 
38 #include <sys/param.h>
39 #include <sys/time.h>
40 #include <sys/resource.h>
41 #include <sys/proc.h>
42 
43 #include <err.h>
44 #include <errno.h>
45 #include <stddef.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 
50 #include "ps.h"
51 
52 #ifdef P_PPWAIT
53 #define NEWVM
54 #endif
55 
56 #ifdef NEWVM
57 #include <sys/ucred.h>
58 #include <sys/sysctl.h>
59 #endif
60 
61 static VAR *findvar __P((char *));
62 static int  vcmp __P((const void *, const void *));
63 
64 #ifdef NOTINUSE
65 int	utime(), stime(), ixrss(), idrss(), isrss();
66 	{{"utime"}, "UTIME", USER, utime, 4},
67 	{{"stime"}, "STIME", USER, stime, 4},
68 	{{"ixrss"}, "IXRSS", USER, ixrss, 4},
69 	{{"idrss"}, "IDRSS", USER, idrss, 4},
70 	{{"isrss"}, "ISRSS", USER, isrss, 4},
71 #endif
72 
73 /* Compute offset in common structures. */
74 #define	POFF(x)	offsetof(struct proc, x)
75 #define	EOFF(x)	offsetof(struct eproc, x)
76 #define	UOFF(x)	offsetof(struct usave, x)
77 #define	ROFF(x)	offsetof(struct rusage, x)
78 
79 #define	UIDFMT	"u"
80 #define	UIDLEN	5
81 #define	PIDFMT	"d"
82 #define	PIDLEN	5
83 #define	USERLEN	8
84 
85 VAR var[] = {
86 #ifdef NEWVM
87 	{"%cpu", "%CPU", NULL, 0, pcpu, 4},
88 	{"%mem", "%MEM", NULL, 0, pmem, 4},
89 	{"acflag", "ACFLG", NULL, 0, pvar, 3, POFF(p_acflag), USHORT, "x"},
90 	{"acflg", "", "acflag"},
91 	{"blocked", "", "sigmask"},
92 	{"caught", "", "sigcatch"},
93 	{"command", "COMMAND", NULL, COMM|LJUST|USER, command, 16},
94 	{"cpu", "CPU", NULL, 0, pvar, 3, POFF(p_estcpu), ULONG, "d"},
95 	{"cputime", "", "time"},
96 	{"f", "F", NULL, 0, pvar, 7, POFF(p_flag), LONG, "x"},
97 	{"flags", "", "f"},
98 	{"ignored", "", "sigignore"},
99 	{"inblk", "INBLK", NULL, USER, rvar, 4, ROFF(ru_inblock), LONG, "d"},
100 	{"inblock", "", "inblk"},
101 	{"jobc", "JOBC", NULL, 0, evar, 4, EOFF(e_jobc), SHORT, "d"},
102 	{"ktrace", "KTRACE", NULL, 0, pvar, 8, POFF(p_traceflag), LONG, "x"},
103 	{"ktracep", "KTRACEP", NULL, 0, pvar, 8, POFF(p_tracep), LONG, "x"},
104 	{"lim", "LIM", NULL, 0, maxrss, 5},
105 	{"login", "LOGIN", NULL, LJUST, logname, MAXLOGNAME},
106 	{"logname", "", "login"},
107 	{"lstart", "STARTED", NULL, LJUST|USER, lstarted, 28},
108 	{"majflt", "MAJFLT", NULL, USER, rvar, 4, ROFF(ru_majflt), LONG, "d"},
109 	{"minflt", "MINFLT", NULL, USER, rvar, 4, ROFF(ru_minflt), LONG, "d"},
110 	{"msgrcv", "MSGRCV", NULL, USER, rvar, 4, ROFF(ru_msgrcv), LONG, "d"},
111 	{"msgsnd", "MSGSND", NULL, USER, rvar, 4, ROFF(ru_msgsnd), LONG, "d"},
112 	{"ni", "", "nice"},
113 	{"nice", "NI", NULL, 0, pvar, 2, POFF(p_nice), CHAR, "d"},
114 	{"nivcsw", "NIVCSW", NULL, USER, rvar, 5, ROFF(ru_nivcsw), LONG, "d"},
115 	{"nsignals", "", "nsigs"},
116 	{"nsigs", "NSIGS", NULL, USER, rvar, 4, ROFF(ru_nsignals), LONG, "d"},
117 	{"nswap", "NSWAP", NULL, USER, rvar, 4, ROFF(ru_nswap), LONG, "d"},
118 	{"nvcsw", "NVCSW", NULL, USER, rvar, 5, ROFF(ru_nvcsw), LONG, "d"},
119 	{"nwchan", "WCHAN", NULL, 0, pvar, 6, POFF(p_wchan), KPTR, "x"},
120 	{"oublk", "OUBLK", NULL, USER, rvar, 4, ROFF(ru_oublock), LONG, "d"},
121 	{"oublock", "", "oublk"},
122 	{"p_ru", "P_RU", NULL, 0, pvar, 6, POFF(p_ru), KPTR, "x"},
123 	{"paddr", "PADDR", NULL, 0, evar, 6, EOFF(e_paddr), KPTR, "x"},
124 	{"pagein", "PAGEIN", NULL, USER, pagein, 6},
125 	{"pcpu", "", "%cpu"},
126 	{"pending", "", "sig"},
127 	{"pgid", "PGID", NULL, 0, evar, PIDLEN, EOFF(e_pgid), ULONG, PIDFMT},
128 	{"pid", "PID", NULL, 0, pvar, PIDLEN, POFF(p_pid), LONG, PIDFMT},
129 	{"pmem", "", "%mem"},
130 	{"ppid", "PPID", NULL, 0, evar, PIDLEN, EOFF(e_ppid), LONG, PIDFMT},
131 	{"pri", "PRI", NULL, 0, pri, 3},
132 	{"re", "RE", NULL, 0, pvar, 3, POFF(p_swtime), ULONG, "d"},
133 	{"rgid", "RGID", NULL, 0, evar, UIDLEN, EOFF(e_pcred.p_rgid),
134 		ULONG, UIDFMT},
135 	{"rlink", "RLINK", NULL, 0, pvar, 8, POFF(p_back), KPTR, "x"},
136 	{"rss", "RSS", NULL, 0, p_rssize, 4},
137 	{"rssize", "", "rsz"},
138 	{"rsz", "RSZ", NULL, 0, rssize, 4},
139 	{"ruid", "RUID", NULL, 0, evar, UIDLEN, EOFF(e_pcred.p_ruid),
140 		ULONG, UIDFMT},
141 	{"ruser", "RUSER", NULL, LJUST, runame, USERLEN},
142 	{"sess", "SESS", NULL, 0, evar, 6, EOFF(e_sess), KPTR, "x"},
143 	{"sig", "PENDING", NULL, 0, pvar, 8, POFF(p_siglist), LONG, "x"},
144 	{"sigcatch", "CAUGHT", NULL, 0, pvar, 8, POFF(p_sigcatch), LONG, "x"},
145 	{"sigignore", "IGNORED",
146 		NULL, 0, pvar, 8, POFF(p_sigignore), LONG, "x"},
147 	{"sigmask", "BLOCKED", NULL, 0, pvar, 8, POFF(p_sigmask), LONG, "x"},
148 	{"sl", "SL", NULL, 0, pvar, 3, POFF(p_slptime), ULONG, "d"},
149 	{"start", "STARTED", NULL, LJUST|USER, started, 8},
150 	{"stat", "", "state"},
151 	{"state", "STAT", NULL, 0, state, 4},
152 	{"svgid", "SVGID",
153 		NULL, 0, evar, UIDLEN, EOFF(e_pcred.p_svgid), ULONG, UIDFMT},
154 	{"svuid", "SVUID",
155 		NULL, 0, evar, UIDLEN, EOFF(e_pcred.p_svuid), ULONG, UIDFMT},
156 	{"tdev", "TDEV", NULL, 0, tdev, 4},
157 	{"time", "TIME", NULL, USER, cputime, 9},
158 	{"tpgid", "TPGID", NULL, 0, evar, 4, EOFF(e_tpgid), ULONG, PIDFMT},
159 	{"tsess", "TSESS", NULL, 0, evar, 6, EOFF(e_tsess), KPTR, "x"},
160 	{"tsiz", "TSIZ", NULL, 0, tsize, 4},
161 	{"tt", "TT", NULL, LJUST, tname, 3},
162 	{"tty", "TTY", NULL, LJUST, longtname, 8},
163 	{"ucomm", "UCOMM", NULL, LJUST, ucomm, MAXCOMLEN},
164 	{"uid", "UID", NULL, 0, evar, UIDLEN, EOFF(e_ucred.cr_uid),
165 		ULONG, UIDFMT},
166 	{"upr", "UPR", NULL, 0, pvar, 3, POFF(p_usrpri), CHAR, "d"},
167 	{"user", "USER", NULL, LJUST, uname, USERLEN},
168 	{"usrpri", "", "upr"},
169 	{"vsize", "", "vsz"},
170 	{"vsz", "VSZ", NULL, 0, vsize, 5},
171 	{"wchan", "WCHAN", NULL, LJUST, wchan, 6},
172 	{"xstat", "XSTAT", NULL, 0, pvar, 4, POFF(p_xstat), USHORT, "x"},
173 #else
174 	{"%cpu", "%CPU", NULL, 0, pcpu, 4},
175 	{"%mem", "%MEM", NULL, 0, pmem, 4},
176 	{"acflag", "ACFLG", NULL, USER, uvar, 3, UOFF(u_acflag), SHORT, "x"},
177 	{"acflg", "", "acflag"},
178 	{"blocked", "", "sigmask"},
179 	{"caught", "", "sigcatch"},
180 	{"command", "COMMAND", NULL, COMM|LJUST|USER, command, 16},
181 	{"cpu", "CPU", NULL, 0, pvar, 3, POFF(p_cpu), ULONG, "d"},
182 	{"cputime", "", "time"},
183 	{"f", "F", NULL, 0, pvar, 7, POFF(p_flag), LONG, "x"},
184 	{"flags", "", "f"},
185 	{"ignored", "", "sigignore"},
186 	{"inblk", "INBLK", NULL, USER, rvar, 4, ROFF(ru_inblock), LONG, "d"},
187 	{"inblock", "", "inblk"},
188 	{"jobc", "JOBC", NULL, 0, evar, 4, EOFF(e_jobc), SHORT, "d"},
189 	{"ktrace", "KTRACE", NULL, 0, pvar, 8, POFF(p_traceflag), LONG, "x"},
190 	{"ktracep", "KTRACEP", NULL, 0, pvar, 8, POFF(p_tracep), LONG, "x"},
191 	{"lim", "LIM", NULL, 0, maxrss, 5},
192 	{"logname", "LOGNAME", NULL, LJUST, logname, MAXLOGNAME},
193 	{"lstart", "STARTED", NULL, LJUST|USER, lstarted, 28},
194 	{"majflt", "MAJFLT", NULL, USER, rvar, 4, ROFF(ru_majflt), LONG, "d"},
195 	{"minflt", "MINFLT", NULL, USER, rvar, 4, ROFF(ru_minflt), LONG, "d"},
196 	{"msgrcv", "MSGRCV", NULL, USER, rvar, 4, ROFF(ru_msgrcv), LONG, "d"},
197 	{"msgsnd", "MSGSND", NULL, USER, rvar, 4, ROFF(ru_msgsnd), LONG, "d"},
198 	{"ni", "", "nice"},
199 	{"nice", "NI", NULL, 0, pvar, 2, POFF(p_nice), CHAR, "d"},
200 	{"nivcsw", "NIVCSW", NULL, USER, rvar, 5, ROFF(ru_nivcsw), LONG, "d"},
201 	{"nsignals", "", "nsigs"},
202 	{"nsigs", "NSIGS", NULL, USER, rvar, 4, ROFF(ru_nsignals), LONG, "d"},
203 	{"nswap", "NSWAP", NULL, USER, rvar, 4, ROFF(ru_nswap), LONG, "d"},
204 	{"nvcsw", "NVCSW", NULL, USER, rvar, 5, ROFF(ru_nvcsw), LONG, "d"},
205 	{"nwchan", "WCHAN", NULL, 0, pvar, 6, POFF(p_wchan), KPTR, "x"},
206 	{"oublk", "OUBLK", NULL, USER, rvar, 4, ROFF(ru_oublock), LONG, "d"},
207 	{"oublock", "", "oublk"},
208 	{"p_ru", "P_RU", NULL, 0, pvar, 6, POFF(p_ru), KPTR, "x"},
209 	{"paddr", "PADDR", NULL, 0, evar, 6, EOFF(e_paddr), KPTR, "x"},
210 	{"pagein", "PAGEIN", NULL, USER, pagein, 6},
211 	{"pcpu", "", "%cpu"},
212 	{"pending", "", "sig"},
213 	{"pgid", "PGID", NULL, 0, evar, PIDLEN, EOFF(e_pgid), ULONG, PIDFMT},
214 	{"pid", "PID", NULL, 0, pvar, PIDLEN, POFF(p_pid), LONG, PIDFMT},
215 	{"pmem", "", "%mem"},
216 	{"poip", "POIP", NULL, 0, pvar, 4, POFF(p_poip), SHORT, "d"},
217 	{"ppid", "PPID", NULL, 0, pvar, PIDLEN, POFF(p_ppid), LONG, PIDFMT},
218 	{"pri", "PRI", NULL, 0, pri, 3},
219 	{"re", "RE", NULL, 0, pvar, 3, POFF(p_swtime), ULONG, "d"},
220 	{"rgid", "RGID", NULL, 0, pvar, UIDLEN, POFF(p_rgid), USHORT, UIDFMT},
221 	{"rlink", "RLINK", NULL, 0, pvar, 8, POFF(p_rlink), KPTR, "x"},
222 	{"rss", "RSS", NULL, 0, p_rssize, 4},
223 	{"rssize", "", "rsz"},
224 	{"rsz", "RSZ", NULL, 0, rssize, 4},
225 	{"ruid", "RUID", NULL, 0, pvar, UIDLEN, POFF(p_ruid), USHORT, UIDFMT},
226 	{"ruser", "RUSER", NULL, LJUST, runame, USERLEN},
227 	{"sess", "SESS", NULL, 0, evar, 6, EOFF(e_sess), KPTR, "x"},
228 	{"sig", "PENDING", NULL, 0, pvar, 8, POFF(p_sig), LONG, "x"},
229 	{"sigcatch", "CAUGHT", NULL, 0, pvar, 8, POFF(p_sigcatch), LONG, "x"},
230 	{"sigignore", "IGNORED",
231 		NULL, 0, pvar, 8, POFF(p_sigignore), LONG, "x"},
232 	{"sigmask", "BLOCKED", NULL, 0, pvar, 8, POFF(p_sigmask), LONG, "x"},
233 	{"sl", "SL", NULL, 0, pvar, 3, POFF(p_slptime), ULONG, "d"},
234 	{"start", "STARTED", NULL, LJUST|USER, started, 8},
235 	{"stat", "", "state"},
236 	{"state", "STAT", NULL, 0, state, 4},
237 	{"svgid", "SVGID",
238 		NULL, 0, pvar, UIDLEN, POFF(p_svgid), USHORT, UIDFMT},
239 	{"svuid", "SVUID",
240 		NULL, 0, pvar, UIDLEN, POFF(p_svuid), USHORT, UIDFMT},
241 	{"tdev", "TDEV", NULL, 0, tdev, 4},
242 	{"time", "TIME", NULL, USER, cputime, 9},
243 	{"tpgid", "TPGID", NULL, 0, evar, 4, EOFF(e_tpgid), ULONG, PIDFMT},
244 	{"trs", "TRS", NULL, 0, trss, 3},
245 	{"tsess", "TSESS", NULL, 0, evar, 6, EOFF(e_tsess), KPTR, "x"},
246 	{"tsiz", "TSIZ", NULL, 0, tsize, 4},
247 	{"tt", "TT", NULL, LJUST, tname, 3},
248 	{"tty", "TTY", NULL, LJUST, longtname, 8},
249 	{"ucomm", "UCOMM", NULL, LJUST, ucomm, MAXCOMLEN},
250 	{"uid", "UID", NULL, 0, pvar, UIDLEN, POFF(p_uid),USHORT, UIDFMT},
251 	{"upr", "UPR", NULL, 0, pvar, 3, POFF(p_usrpri), CHAR, "d"},
252 	{"uprocp", "UPROCP", NULL, USER, uvar, 6, UOFF(u_procp), KPTR, "x"},
253 	{"user", "USER", NULL, LJUST, uname, USERLEN},
254 	{"usrpri", "", "upr"},
255 	{"vsize", "", "vsz"},
256 	{"vsz", "VSZ", NULL, 0, vsize, 5},
257 	{"wchan", "WCHAN", NULL, LJUST, wchan, 6},
258 	{"xstat", "XSTAT", NULL, 0, pvar, 4, POFF(p_xstat), USHORT, "x"},
259 #endif
260 	{""},
261 };
262 
263 void
264 showkey()
265 {
266 	VAR *v;
267 	int i;
268 	char *p, *sep;
269 
270 	i = 0;
271 	sep = "";
272 	for (v = var; *(p = v->name); ++v) {
273 		int len = strlen(p);
274 		if (termwidth && (i += len + 1) > termwidth) {
275 			i = len;
276 			sep = "\n";
277 		}
278 		(void) printf("%s%s", sep, p);
279 		sep = " ";
280 	}
281 	(void) printf("\n");
282 }
283 
284 void
285 parsefmt(p)
286 	char *p;
287 {
288 	static struct varent *vtail;
289 
290 #define	FMTSEP	" \t,\n"
291 	while (p && *p) {
292 		char *cp;
293 		VAR *v;
294 		struct varent *vent;
295 
296 		while ((cp = strsep(&p, FMTSEP)) != NULL && *cp == '\0')
297 			/* void */;
298 		if (!(v = findvar(cp)))
299 			continue;
300 		if ((vent = malloc(sizeof(struct varent))) == NULL)
301 			err(1, NULL);
302 		vent->var = v;
303 		vent->next = NULL;
304 		if (vhead == NULL)
305 			vhead = vtail = vent;
306 		else {
307 			vtail->next = vent;
308 			vtail = vent;
309 		}
310 	}
311 	if (!vhead)
312 		errx(1, "no valid keywords");
313 }
314 
315 static VAR *
316 findvar(p)
317 	char *p;
318 {
319 	VAR *v, key;
320 	char *hp;
321 	int vcmp();
322 
323 	key.name = p;
324 
325 	hp = strchr(p, '=');
326 	if (hp)
327 		*hp++ = '\0';
328 
329 	key.name = p;
330 	v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
331 
332 	if (v && v->alias) {
333 		if (hp) {
334 			warnx("%s: illegal keyword specification", p);
335 			eval = 1;
336 		}
337 		parsefmt(v->alias);
338 		return ((VAR *)NULL);
339 	}
340 	if (!v) {
341 		warnx("%s: keyword not found", p);
342 		eval = 1;
343 	}
344 	if (hp)
345 		v->header = hp;
346 	return (v);
347 }
348 
349 static int
350 vcmp(a, b)
351         const void *a, *b;
352 {
353         return (strcmp(((VAR *)a)->name, ((VAR *)b)->name));
354 }
355