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