xref: /freebsd/bin/ps/keyword.c (revision d4ae33f0721c1b170fe37d97e395228ffcfb3f80)
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 	{"fib", "FIB", NULL, 0, kvar, KOFF(ki_fibnum), INT, "d", 0},
91 	{"flags", "", "f", 0, NULL, 0, CHAR, NULL, 0},
92 	{"gid", "GID", NULL, 0, kvar, KOFF(ki_groups), UINT, UIDFMT, 0},
93 	{"group", "GROUP", NULL, LJUST, egroupname, 0, CHAR, NULL, 0},
94 	{"ignored", "", "sigignore", 0, NULL, 0, CHAR, NULL, 0},
95 	{"inblk", "INBLK", NULL, USER, rvar, ROFF(ru_inblock), LONG, "ld", 0},
96 	{"inblock", "", "inblk", 0, NULL, 0, CHAR, NULL, 0},
97 	{"jid", "JID", NULL, 0, kvar, KOFF(ki_jid), INT, "d", 0},
98 	{"jobc", "JOBC", NULL, 0, kvar, KOFF(ki_jobc), SHORT, "d", 0},
99 	{"ktrace", "KTRACE", NULL, 0, kvar, KOFF(ki_traceflag), INT, "x", 0},
100 	{"label", "LABEL", NULL, LJUST, label, 0, CHAR, NULL, 0},
101 	{"lim", "LIM", NULL, 0, maxrss, 0, CHAR, NULL, 0},
102 	{"lockname", "LOCK", NULL, LJUST, lockname, 0, CHAR, NULL, 0},
103 	{"login", "LOGIN", NULL, LJUST, logname, 0, CHAR, NULL, 0},
104 	{"logname", "", "login", 0, NULL, 0, CHAR, NULL, 0},
105 	{"lstart", "STARTED", NULL, LJUST|USER, lstarted, 0, CHAR, NULL, 0},
106 	{"lwp", "LWP", NULL, 0, kvar, KOFF(ki_tid), UINT, LWPFMT, 0},
107 	{"majflt", "MAJFLT", NULL, USER, rvar, ROFF(ru_majflt), LONG, "ld", 0},
108 	{"minflt", "MINFLT", NULL, USER, rvar, ROFF(ru_minflt), LONG, "ld", 0},
109 	{"msgrcv", "MSGRCV", NULL, USER, rvar, ROFF(ru_msgrcv), LONG, "ld", 0},
110 	{"msgsnd", "MSGSND", NULL, USER, rvar, ROFF(ru_msgsnd), LONG, "ld", 0},
111 	{"mwchan", "MWCHAN", NULL, LJUST, mwchan, 0, CHAR, NULL, 0},
112 	{"ni", "", "nice", 0, NULL, 0, CHAR, NULL, 0},
113 	{"nice", "NI", NULL, 0, kvar, KOFF(ki_nice), CHAR, "d", 0},
114 	{"nivcsw", "NIVCSW", NULL, USER, rvar, ROFF(ru_nivcsw), LONG, "ld", 0},
115 	{"nlwp", "NLWP", NULL, 0, kvar, KOFF(ki_numthreads), UINT, NLWPFMT, 0},
116 	{"nsignals", "", "nsigs", 0, NULL, 0, CHAR, NULL, 0},
117 	{"nsigs", "NSIGS", NULL, USER, rvar, ROFF(ru_nsignals), LONG, "ld", 0},
118 	{"nswap", "NSWAP", NULL, USER, rvar, ROFF(ru_nswap), LONG, "ld", 0},
119 	{"nvcsw", "NVCSW", NULL, USER, rvar, ROFF(ru_nvcsw), LONG, "ld", 0},
120 	{"nwchan", "NWCHAN", NULL, LJUST, nwchan, 0, CHAR, NULL, 0},
121 	{"oublk", "OUBLK", NULL, USER, rvar, ROFF(ru_oublock), LONG, "ld", 0},
122 	{"oublock", "", "oublk", 0, NULL, 0, CHAR, NULL, 0},
123 	{"paddr", "PADDR", NULL, 0, kvar, KOFF(ki_paddr), KPTR, "lx", 0},
124 	{"pagein", "PAGEIN", NULL, USER, pagein, 0, CHAR, NULL, 0},
125 	{"pcpu", "", "%cpu", 0, NULL, 0, CHAR, NULL, 0},
126 	{"pending", "", "sig", 0, NULL, 0, CHAR, NULL, 0},
127 	{"pgid", "PGID", NULL, 0, kvar, KOFF(ki_pgid), UINT, PIDFMT, 0},
128 	{"pid", "PID", NULL, 0, kvar, KOFF(ki_pid), UINT, PIDFMT, 0},
129 	{"pmem", "", "%mem", 0, NULL, 0, CHAR, NULL, 0},
130 	{"ppid", "PPID", NULL, 0, kvar, KOFF(ki_ppid), UINT, PIDFMT, 0},
131 	{"pri", "PRI", NULL, 0, pri, 0, CHAR, NULL, 0},
132 	{"re", "RE", NULL, INF127, kvar, KOFF(ki_swtime), UINT, "d", 0},
133 	{"rgid", "RGID", NULL, 0, kvar, KOFF(ki_rgid), UINT, UIDFMT, 0},
134 	{"rgroup", "RGROUP", NULL, LJUST, rgroupname, 0, CHAR, NULL, 0},
135 	{"rss", "RSS", NULL, 0, kvar, KOFF(ki_rssize), PGTOK, "ld", 0},
136 	{"rtprio", "RTPRIO", NULL, 0, priorityr, KOFF(ki_pri), CHAR, NULL, 0},
137 	{"ruid", "RUID", NULL, 0, kvar, KOFF(ki_ruid), UINT, UIDFMT, 0},
138 	{"ruser", "RUSER", NULL, LJUST, runame, 0, CHAR, NULL, 0},
139 	{"sid", "SID", NULL, 0, kvar, KOFF(ki_sid), UINT, PIDFMT, 0},
140 	{"sig", "PENDING", NULL, 0, kvar, KOFF(ki_siglist), INT, "x", 0},
141 	{"sigcatch", "CAUGHT", NULL, 0, kvar, KOFF(ki_sigcatch), UINT, "x", 0},
142 	{"sigignore", "IGNORED", NULL, 0, kvar, KOFF(ki_sigignore),
143 		UINT, "x", 0},
144 	{"sigmask", "BLOCKED", NULL, 0, kvar, KOFF(ki_sigmask), UINT, "x", 0},
145 	{"sl", "SL", NULL, INF127, kvar, KOFF(ki_slptime), UINT, "d", 0},
146 	{"ssiz", "SSIZ", NULL, 0, kvar, KOFF(ki_ssize), PGTOK, "ld", 0},
147 	{"start", "STARTED", NULL, LJUST|USER, started, 0, CHAR, NULL, 0},
148 	{"stat", "", "state", 0, NULL, 0, CHAR, NULL, 0},
149 	{"state", "STAT", NULL, LJUST, state, 0, CHAR, NULL, 0},
150 	{"svgid", "SVGID", NULL, 0, kvar, KOFF(ki_svgid), UINT, UIDFMT, 0},
151 	{"svuid", "SVUID", NULL, 0, kvar, KOFF(ki_svuid), UINT, UIDFMT, 0},
152 	{"systime", "SYSTIME", NULL, USER, systime, 0, CHAR, NULL, 0},
153 	{"tdaddr", "TDADDR", NULL, 0, kvar, KOFF(ki_tdaddr), KPTR, "lx", 0},
154 	{"tdev", "TDEV", NULL, 0, tdev, 0, CHAR, NULL, 0},
155 	{"tdnam", "TDNAM", NULL, LJUST, tdnam, 0, CHAR, NULL, 0},
156 	{"time", "TIME", NULL, USER, cputime, 0, CHAR, NULL, 0},
157 	{"tpgid", "TPGID", NULL, 0, kvar, KOFF(ki_tpgid), UINT, PIDFMT, 0},
158 	{"tsid", "TSID", NULL, 0, kvar, KOFF(ki_tsid), UINT, PIDFMT, 0},
159 	{"tsiz", "TSIZ", NULL, 0, kvar, KOFF(ki_tsize), PGTOK, "ld", 0},
160 	{"tt", "TT ", NULL, 0, tname, 0, CHAR, NULL, 0},
161 	{"tty", "TTY", NULL, LJUST, longtname, 0, CHAR, NULL, 0},
162 	{"ucomm", "UCOMM", NULL, LJUST, ucomm, 0, CHAR, NULL, 0},
163 	{"uid", "UID", NULL, 0, kvar, KOFF(ki_uid), UINT, UIDFMT, 0},
164 	{"upr", "UPR", NULL, 0, upr, 0, CHAR, NULL, 0},
165 	{"uprocp", "UPROCP", NULL, 0, kvar, KOFF(ki_paddr), KPTR, "lx", 0},
166 	{"user", "USER", NULL, LJUST, uname, 0, CHAR, NULL, 0},
167 	{"usertime", "USERTIME", NULL, USER, usertime, 0, CHAR, NULL, 0},
168 	{"usrpri", "", "upr", 0, NULL, 0, CHAR, NULL, 0},
169 	{"vsize", "", "vsz", 0, NULL, 0, CHAR, NULL, 0},
170 	{"vsz", "VSZ", NULL, 0, vsize, 0, CHAR, NULL, 0},
171 	{"wchan", "WCHAN", NULL, LJUST, wchan, 0, CHAR, NULL, 0},
172 	{"xstat", "XSTAT", NULL, 0, kvar, KOFF(ki_xstat), USHORT, "x", 0},
173 	{"", NULL, NULL, 0, NULL, 0, CHAR, NULL, 0},
174 };
175 
176 void
177 showkey(void)
178 {
179 	VAR *v;
180 	int i;
181 	const char *p, *sep;
182 
183 	i = 0;
184 	sep = "";
185 	for (v = var; *(p = v->name); ++v) {
186 		int len = strlen(p);
187 		if (termwidth && (i += len + 1) > termwidth) {
188 			i = len;
189 			sep = "\n";
190 		}
191 		(void) printf("%s%s", sep, p);
192 		sep = " ";
193 	}
194 	(void) printf("\n");
195 }
196 
197 void
198 parsefmt(const char *p, int user)
199 {
200 	char *tempstr, *tempstr1;
201 
202 #define		FMTSEP	" \t,\n"
203 	tempstr1 = tempstr = strdup(p);
204 	while (tempstr && *tempstr) {
205 		char *cp, *hp;
206 		VAR *v;
207 		struct varent *vent;
208 
209 		/*
210 		 * If an item contains an equals sign, it specifies a column
211 		 * header, may contain embedded separator characters and
212 		 * is always the last item.
213 		 */
214 		if (tempstr[strcspn(tempstr, "="FMTSEP)] != '=')
215 			while ((cp = strsep(&tempstr, FMTSEP)) != NULL &&
216 			    *cp == '\0')
217 				/* void */;
218 		else {
219 			cp = tempstr;
220 			tempstr = NULL;
221 		}
222 		if (cp == NULL || !(v = findvar(cp, user, &hp)))
223 			continue;
224 		if (!user) {
225 			/*
226 			 * If the user is NOT adding this field manually,
227 			 * get on with our lives if this VAR is already
228 			 * represented in the list.
229 			 */
230 			vent = find_varentry(v);
231 			if (vent != NULL)
232 				continue;
233 		}
234 		if ((vent = malloc(sizeof(struct varent))) == NULL)
235 			errx(1, "malloc failed");
236 		vent->header = v->header;
237 		if (hp) {
238 			hp = strdup(hp);
239 			if (hp)
240 				vent->header = hp;
241 		}
242 		vent->var = malloc(sizeof(*vent->var));
243 		if (vent->var == NULL)
244 			errx(1, "malloc failed");
245 		memcpy(vent->var, v, sizeof(*vent->var));
246 		STAILQ_INSERT_TAIL(&varlist, vent, next_ve);
247 	}
248 	free(tempstr1);
249 	if (STAILQ_EMPTY(&varlist)) {
250 		warnx("no valid keywords; valid keywords:");
251 		showkey();
252 		exit(1);
253 	}
254 }
255 
256 static VAR *
257 findvar(char *p, int user, char **header)
258 {
259 	size_t rflen;
260 	VAR *v, key;
261 	char *hp, *realfmt;
262 
263 	hp = strchr(p, '=');
264 	if (hp)
265 		*hp++ = '\0';
266 
267 	key.name = p;
268 	v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
269 
270 	if (v && v->alias) {
271 		/*
272 		 * If the user specified an alternate-header for this
273 		 * (aliased) format-name, then we need to copy that
274 		 * alternate-header when making the recursive call to
275 		 * process the alias.
276 		 */
277 		if (hp == NULL)
278 			parsefmt(v->alias, user);
279 		else {
280 			/*
281 			 * XXX - This processing will not be correct for
282 			 * any alias which expands into a list of format
283 			 * keywords.  Presently there are no aliases
284 			 * which do that.
285 			 */
286 			rflen = strlen(v->alias) + strlen(hp) + 2;
287 			realfmt = malloc(rflen);
288 			if (realfmt == NULL)
289 				errx(1, "malloc failed");
290 			snprintf(realfmt, rflen, "%s=%s", v->alias, hp);
291 			parsefmt(realfmt, user);
292 			free(realfmt);
293 		}
294 		return ((VAR *)NULL);
295 	}
296 	if (!v) {
297 		warnx("%s: keyword not found", p);
298 		eval = 1;
299 	}
300 	if (header)
301 		*header = hp;
302 	return (v);
303 }
304 
305 static int
306 vcmp(const void *a, const void *b)
307 {
308         return (strcmp(((const VAR *)a)->name, ((const VAR *)b)->name));
309 }
310