keyword.c revision 1.52 1 /* $NetBSD: keyword.c,v 1.52 2008/02/10 17:47:59 christos Exp $ */
2
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/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)keyword.c 8.5 (Berkeley) 4/2/94";
36 #else
37 __RCSID("$NetBSD: keyword.c,v 1.52 2008/02/10 17:47:59 christos Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <sys/param.h>
42 #include <sys/time.h>
43 #include <sys/lwp.h>
44 #include <sys/proc.h>
45 #include <sys/resource.h>
46 #include <sys/sysctl.h>
47 #include <sys/ucred.h>
48
49 #include <err.h>
50 #include <errno.h>
51 #include <kvm.h>
52 #include <stddef.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <signal.h>
57
58 #include "ps.h"
59
60 static VAR *findvar(const char *);
61 static int vcmp(const void *, const void *);
62
63 #if 0 /* kernel doesn't calculate these */
64 PUVAR("idrss", "IDRSS", 0, p_uru_idrss, UINT64, PRIu64),
65 PUVAR("isrss", "ISRSS", 0, p_uru_isrss, UINT64, PRId64),
66 PUVAR("ixrss", "IXRSS", 0, p_uru_ixrss, UINT64, PRId64),
67 PUVAR("maxrss", "MAXRSS", 0, p_uru_maxrss, UINT64, PRIu64),
68 #endif
69
70 /* Compute offset in common structures. */
71 #define POFF(x) offsetof(struct kinfo_proc2, x)
72 #define LOFF(x) offsetof(struct kinfo_lwp, x)
73
74 #define UIDFMT "u"
75 #define UID(n1, n2, of) \
76 { .name = n1, .header = n2, .flag = 0, .oproc = pvar, \
77 .off = POFF(of), .type = UINT32, .fmt = UIDFMT }
78 #define GID(n1, n2, off) UID(n1, n2, off)
79
80 #define PIDFMT "d"
81 #define PID(n1, n2, of) \
82 { .name = n1, .header = n2, .flag = 0, .oproc = pvar, \
83 .off = POFF(of), .type = INT32, .fmt = PIDFMT }
84
85 #define LVAR(n1, n2, fl, of, ty, fm) \
86 { .name = n1, .header = n2, .flag = (fl) | LWP, .oproc = pvar, \
87 .off = LOFF(of), .type = ty, .fmt = fm }
88 #define PVAR(n1, n2, fl, of, ty, fm) \
89 { .name = n1, .header = n2, .flag = (fl) | 0, .oproc = pvar, \
90 .off = POFF(of), .type = ty, .fmt = fm }
91 #define PUVAR(n1, n2, fl, of, ty, fm) \
92 { .name = n1, .header = n2, .flag = (fl) | UAREA, .oproc = pvar, \
93 .off = POFF(of), .type = ty, .fmt = fm }
94 #define VAR3(n1, n2, fl) \
95 { .name = n1, .header = n2, .flag = fl }
96 #define VAR4(n1, n2, fl, op) \
97 { .name = n1, .header = n2, .flag = fl, .oproc = op, }
98 #define VAR6(n1, n2, fl, op, of, ty) \
99 { .name = n1, .header = n2, .flag = fl, .oproc = op, \
100 .off = of, .type = ty }
101
102 /* NB: table must be sorted, in vi use:
103 * :/^VAR/,/end_sort/! sort -t\" +1
104 * breaking long lines just makes the sort harder
105 *
106 * We support all the fields required by P1003.1-2004 (SUSv3), with
107 * the correct default headers, except for the "tty" field, where the
108 * standard says the header should be "TT", but we have "TTY".
109 */
110 VAR var[] = {
111 VAR6("%cpu", "%CPU", 0, pcpu, 0, PCPU),
112 VAR6("%mem", "%MEM", 0, pmem, POFF(p_vm_rssize), INT32),
113 PVAR("acflag", "ACFLG", 0, p_acflag, USHORT, "x"),
114 VAR3("acflg", "acflag", ALIAS),
115 VAR3("args", "command", ALIAS),
116 VAR3("blocked", "sigmask", ALIAS),
117 VAR3("caught", "sigcatch", ALIAS),
118 VAR4("comm", "COMMAND", COMM|ARGV0|LJUST, command),
119 VAR4("command", "COMMAND", COMM|LJUST, command),
120 PVAR("cpu", "CPU", 0, p_estcpu, UINT, "u"),
121 VAR4("cpuid", "CPUID", LWP, cpuid),
122 VAR3("cputime", "time", ALIAS),
123 VAR6("ctime", "CTIME", 0, putimeval, POFF(p_uctime_sec), TIMEVAL),
124 GID("egid", "EGID", p_gid),
125 VAR4("egroup", "EGROUP", LJUST, gname),
126 VAR4("emul", "EMUL", LJUST, emul),
127 VAR6("etime", "ELAPSED", 0, elapsed, POFF(p_ustart_sec), TIMEVAL),
128 UID("euid", "EUID", p_uid),
129 VAR4("euser", "EUSER", LJUST, uname),
130 PVAR("f", "F", 0, p_flag, INT, "x"),
131 VAR3("flags", "f", ALIAS),
132 GID("gid", "GID", p_gid),
133 VAR4("group", "GROUP", LJUST, gname),
134 VAR4("groupnames", "GROUPNAMES", LJUST, groupnames),
135 VAR4("groups", "GROUPS", LJUST, groups),
136 LVAR("holdcnt", "HOLDCNT", 0, l_holdcnt, INT, "d"),
137 VAR3("ignored", "sigignore", ALIAS),
138 PUVAR("inblk", "INBLK", 0, p_uru_inblock, UINT64, PRIu64),
139 VAR3("inblock", "inblk", ALIAS),
140 PVAR("jobc", "JOBC", 0, p_jobc, SHORT, "d"),
141 PVAR("ktrace", "KTRACE", 0, p_traceflag, INT, "x"),
142 /*XXX*/ PVAR("ktracep", "KTRACEP", 0, p_tracep, KPTR, PRIx64),
143 LVAR("laddr", "LADDR", 0, l_laddr, KPTR, PRIx64),
144 LVAR("lid", "LID", 0, l_lid, INT32, "d"),
145 VAR4("lim", "LIM", 0, maxrss),
146 VAR4("lname", "LNAME", LJUST|LWP, lname),
147 VAR4("login", "LOGIN", LJUST, logname),
148 VAR3("logname", "login", ALIAS),
149 VAR6("lstart", "STARTED", LJUST, lstarted, POFF(p_ustart_sec), UINT32),
150 VAR4("lstate", "STAT", LJUST|LWP, lstate),
151 PUVAR("majflt", "MAJFLT", 0, p_uru_majflt, UINT64, PRIu64),
152 PUVAR("minflt", "MINFLT", 0, p_uru_minflt, UINT64, PRIu64),
153 PUVAR("msgrcv", "MSGRCV", 0, p_uru_msgrcv, UINT64, PRIu64),
154 PUVAR("msgsnd", "MSGSND", 0, p_uru_msgsnd, UINT64, PRIu64),
155 VAR3("ni", "nice", ALIAS),
156 VAR6("nice", "NI", 0, pnice, POFF(p_nice), UCHAR),
157 PUVAR("nivcsw", "NIVCSW", 0, p_uru_nivcsw, UINT64, PRIu64),
158 PVAR("nlwp", "NLWP", 0, p_nlwps, UINT64, PRId64),
159 VAR3("nsignals", "nsigs", ALIAS),
160 PUVAR("nsigs", "NSIGS", 0, p_uru_nsignals, UINT64, PRIu64),
161 PUVAR("nswap", "NSWAP", 0, p_uru_nswap, UINT64, PRIu64),
162 PUVAR("nvcsw", "NVCSW", 0, p_uru_nvcsw, UINT64, PRIu64),
163 /*XXX*/ LVAR("nwchan", "WCHAN", 0, l_wchan, KPTR, PRIx64),
164 PUVAR("oublk", "OUBLK", 0, p_uru_oublock, UINT64, PRIu64),
165 VAR3("oublock", "oublk", ALIAS),
166 /*XXX*/ PVAR("p_ru", "P_RU", 0, p_ru, KPTR, PRIx64),
167 /*XXX*/ PVAR("paddr", "PADDR", 0, p_paddr, KPTR, PRIx64),
168 PUVAR("pagein", "PAGEIN", 0, p_uru_majflt, UINT64, PRIu64),
169 VAR3("pcpu", "%cpu", ALIAS),
170 VAR3("pending", "sig", ALIAS),
171 PID("pgid", "PGID", p__pgid),
172 PID("pid", "PID", p_pid),
173 VAR3("pmem", "%mem", ALIAS),
174 PID("ppid", "PPID", p_ppid),
175 VAR4("pri", "PRI", LWP, pri),
176 LVAR("re", "RE", INF127, l_swtime, UINT, "u"),
177 GID("rgid", "RGID", p_rgid),
178 VAR4("rgroup", "RGROUP", LJUST, rgname),
179 /*XXX*/ LVAR("rlink", "RLINK", 0, l_back, KPTR, PRIx64),
180 PVAR("rlwp", "RLWP", 0, p_nrlwps, UINT64, PRId64),
181 VAR6("rss", "RSS", 0, p_rssize, POFF(p_vm_rssize), INT32),
182 VAR3("rssize", "rsz", ALIAS),
183 VAR6("rsz", "RSZ", 0, rssize, POFF(p_vm_rssize), INT32),
184 UID("ruid", "RUID", p_ruid),
185 VAR4("ruser", "RUSER", LJUST, runame),
186 PVAR("sess", "SESS", 0, p_sess, KPTR24, PRIx64),
187 PID("sid", "SID", p_sid),
188 PVAR("sig", "PENDING", 0, p_siglist, SIGLIST, "s"),
189 PVAR("sigcatch", "CAUGHT", 0, p_sigcatch, SIGLIST, "s"),
190 PVAR("sigignore", "IGNORED", 0, p_sigignore, SIGLIST, "s"),
191 PVAR("sigmask", "BLOCKED", 0, p_sigmask, SIGLIST, "s"),
192 LVAR("sl", "SL", INF127, l_slptime, UINT, "u"),
193 VAR6("start", "STARTED", 0, started, POFF(p_ustart_sec), UINT32),
194 VAR3("stat", "state", ALIAS),
195 VAR4("state", "STAT", LJUST, state),
196 VAR6("stime", "STIME", 0, putimeval, POFF(p_ustime_sec), TIMEVAL),
197 GID("svgid", "SVGID", p_svgid),
198 VAR4("svgroup", "SVGROUP", LJUST, svgname),
199 UID("svuid", "SVUID", p_svuid),
200 VAR4("svuser", "SVUSER", LJUST, svuname),
201 /* "tdev" is UINT32, but we do this for sorting purposes */
202 VAR6("tdev", "TDEV", 0, tdev, POFF(p_tdev), INT32),
203 VAR6("time", "TIME", 0, cputime, 0, CPUTIME),
204 PID("tpgid", "TPGID", p_tpgid),
205 PVAR("tsess", "TSESS", 0, p_tsess, KPTR, PRIx64),
206 VAR6("tsiz", "TSIZ", 0, tsize, POFF(p_vm_tsize), INT32),
207 VAR6("tt", "TTY", LJUST, tname, POFF(p_tdev), INT32),
208 VAR6("tty", "TTY", LJUST, longtname, POFF(p_tdev), INT32),
209 LVAR("uaddr", "UADDR", 0, l_addr, KPTR, PRIx64),
210 VAR4("ucomm", "UCOMM", LJUST, ucomm),
211 UID("uid", "UID", p_uid),
212 LVAR("upr", "UPR", 0, l_usrpri, UCHAR, "u"),
213 VAR4("user", "USER", LJUST, uname),
214 VAR3("usrpri", "upr", ALIAS),
215 VAR6("utime", "UTIME", 0, putimeval, POFF(p_uutime_sec), TIMEVAL),
216 VAR3("vsize", "vsz", ALIAS),
217 VAR6("vsz", "VSZ", 0, vsize, 0, VSIZE),
218 VAR4("wchan", "WCHAN", LJUST|LWP, wchan),
219 PVAR("xstat", "XSTAT", 0, p_xstat, USHORT, "x"),
220 /* "zzzz" end_sort */
221 { .name = "" },
222 };
223
224 void
225 showkey(void)
226 {
227 VAR *v;
228 int i;
229 const char *p;
230 const char *sep;
231
232 i = 0;
233 sep = "";
234 for (v = var; *(p = v->name); ++v) {
235 int len = strlen(p);
236 if (termwidth && (i += len + 1) > termwidth) {
237 i = len;
238 sep = "\n";
239 }
240 (void)printf("%s%s", sep, p);
241 sep = " ";
242 }
243 (void)printf("\n");
244 }
245
246 /*
247 * Parse the string pp, and insert or append entries to the list
248 * referenced by listptr. If pos in non-null and *pos is non-null, then
249 * *pos specifies where to insert (instead of appending). If pos is
250 * non-null, then a new value is returned through *pos referring to the
251 * last item inserted.
252 */
253 static void
254 parsevarlist(const char *pp, struct varlist *listptr, struct varent **pos)
255 {
256 char *p, *sp, *equalsp;
257
258 /* dup to avoid zapping arguments. We will free sp later. */
259 p = sp = strdup(pp);
260
261 /*
262 * Everything after the first '=' is part of a custom header.
263 * Temporarily replace it with '\0' to simplify other code.
264 */
265 equalsp = strchr(p, '=');
266 if (equalsp)
267 *equalsp = '\0';
268
269 #define FMTSEP " \t,\n"
270 while (p && *p) {
271 char *cp;
272 VAR *v;
273 struct varent *vent;
274
275 /*
276 * skip separators before the first keyword, and
277 * look for the separator after the keyword.
278 */
279 for (cp = p; *cp != '\0'; cp++) {
280 p = strpbrk(cp, FMTSEP);
281 if (p != cp)
282 break;
283 }
284 if (*cp == '\0')
285 break;
286 /*
287 * Now cp points to the start of a keyword,
288 * and p is NULL or points past the end of the keyword.
289 *
290 * Terminate the keyword with '\0', or reinstate the
291 * '=' that was removed earlier, if appropriate.
292 */
293 if (p) {
294 *p = '\0';
295 p++;
296 } else if (equalsp) {
297 *equalsp = '=';
298 }
299
300 /*
301 * If findvar() likes the keyword or keyword=header,
302 * add it to our list. If findvar() doesn't like it,
303 * it will print a warning, so we ignore it.
304 */
305 if ((v = findvar(cp)) == NULL)
306 continue;
307 if ((vent = malloc(sizeof(struct varent))) == NULL)
308 err(1, NULL);
309 vent->var = v;
310 if (pos && *pos)
311 SIMPLEQ_INSERT_AFTER(listptr, *pos, vent, next);
312 else {
313 SIMPLEQ_INSERT_TAIL(listptr, vent, next);
314 }
315 if (pos)
316 *pos = vent;
317 }
318 free(sp);
319 if (SIMPLEQ_EMPTY(listptr))
320 errx(1, "no valid keywords");
321 }
322
323 void
324 parsefmt(const char *p)
325 {
326
327 parsevarlist(p, &displaylist, NULL);
328 }
329
330 void
331 parsefmt_insert(const char *p, struct varent **pos)
332 {
333
334 parsevarlist(p, &displaylist, pos);
335 }
336
337 void
338 parsesort(const char *p)
339 {
340
341 parsevarlist(p, &sortlist, NULL);
342 }
343
344 /* Search through a list for an entry with a specified name. */
345 struct varent *
346 varlist_find(struct varlist *list, const char *name)
347 {
348 struct varent *vent;
349
350 SIMPLEQ_FOREACH(vent, list, next) {
351 if (strcmp(vent->var->name, name) == 0)
352 break;
353 }
354 return vent;
355 }
356
357 static VAR *
358 findvar(const char *p)
359 {
360 VAR *v;
361 char *hp;
362
363 hp = strchr(p, '=');
364 if (hp)
365 *hp++ = '\0';
366
367 v = bsearch(p, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
368 if (v && v->flag & ALIAS)
369 v = findvar(v->header);
370 if (!v) {
371 warnx("%s: keyword not found", p);
372 eval = 1;
373 return NULL;
374 }
375
376 if (v && hp) {
377 /*
378 * Override the header.
379 *
380 * We need to copy the entry first, and override the
381 * header in the copy, because the same field might be
382 * used multiple times with different headers. We also
383 * need to strdup the header.
384 */
385 struct var *newvar;
386 char *newheader;
387
388 if ((newvar = malloc(sizeof(struct var))) == NULL)
389 err(1, NULL);
390 if ((newheader = strdup(hp)) == NULL)
391 err(1, NULL);
392 memcpy(newvar, v, sizeof(struct var));
393 newvar->header = newheader;
394
395 /*
396 * According to P1003.1-2004, if the header text is null,
397 * such as -o user=, the field width will be at least as
398 * wide as the default header text.
399 */
400 if (*hp == '\0')
401 newvar->width = strlen(v->header);
402
403 v = newvar;
404 }
405 return v;
406 }
407
408 static int
409 vcmp(const void *a, const void *b)
410 {
411 return strcmp(a, ((const VAR *)b)->name);
412 }
413