keyword.c revision 1.23 1 1.23 simonb /* $NetBSD: keyword.c,v 1.23 2000/05/26 03:04:28 simonb Exp $ */
2 1.8 cgd
3 1.1 cgd /*-
4 1.7 cgd * Copyright (c) 1990, 1993, 1994
5 1.7 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.15 christos #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.8 cgd #if 0
39 1.7 cgd static char sccsid[] = "@(#)keyword.c 8.5 (Berkeley) 4/2/94";
40 1.8 cgd #else
41 1.23 simonb __RCSID("$NetBSD: keyword.c,v 1.23 2000/05/26 03:04:28 simonb Exp $");
42 1.8 cgd #endif
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <sys/param.h>
46 1.1 cgd #include <sys/time.h>
47 1.16 mrg #include <sys/proc.h>
48 1.1 cgd #include <sys/resource.h>
49 1.16 mrg #include <sys/sysctl.h>
50 1.16 mrg #include <sys/ucred.h>
51 1.7 cgd
52 1.7 cgd #include <err.h>
53 1.1 cgd #include <errno.h>
54 1.21 simonb #include <kvm.h>
55 1.7 cgd #include <stddef.h>
56 1.1 cgd #include <stdio.h>
57 1.1 cgd #include <stdlib.h>
58 1.1 cgd #include <string.h>
59 1.18 christos #include <signal.h>
60 1.7 cgd
61 1.1 cgd #include "ps.h"
62 1.1 cgd
63 1.7 cgd static VAR *findvar __P((char *));
64 1.7 cgd static int vcmp __P((const void *, const void *));
65 1.1 cgd
66 1.18 christos
67 1.18 christos #define SIGWIDTH (((_NSIG / 32) + 1) * 8)
68 1.18 christos
69 1.1 cgd #ifdef NOTINUSE
70 1.1 cgd int utime(), stime(), ixrss(), idrss(), isrss();
71 1.1 cgd {{"utime"}, "UTIME", USER, utime, 4},
72 1.1 cgd {{"stime"}, "STIME", USER, stime, 4},
73 1.1 cgd {{"ixrss"}, "IXRSS", USER, ixrss, 4},
74 1.1 cgd {{"idrss"}, "IDRSS", USER, idrss, 4},
75 1.1 cgd {{"isrss"}, "ISRSS", USER, isrss, 4},
76 1.1 cgd #endif
77 1.1 cgd
78 1.1 cgd /* Compute offset in common structures. */
79 1.23 simonb #define POFF(x) offsetof(struct kinfo_proc2, x)
80 1.1 cgd
81 1.1 cgd #define UIDFMT "u"
82 1.1 cgd #define UIDLEN 5
83 1.12 cgd #define UID(n1, n2, fn, off) \
84 1.12 cgd { n1, n2, NULL, 0, fn, UIDLEN, off, UINT32, UIDFMT }
85 1.12 cgd #define GID(n1, n2, fn, off) UID(n1, n2, fn, off)
86 1.12 cgd
87 1.1 cgd #define PIDFMT "d"
88 1.1 cgd #define PIDLEN 5
89 1.12 cgd #define PID(n1, n2, fn, off) \
90 1.12 cgd { n1, n2, NULL, 0, fn, PIDLEN, off, INT32, PIDFMT }
91 1.12 cgd
92 1.1 cgd #define USERLEN 8
93 1.1 cgd
94 1.1 cgd VAR var[] = {
95 1.1 cgd {"%cpu", "%CPU", NULL, 0, pcpu, 4},
96 1.1 cgd {"%mem", "%MEM", NULL, 0, pmem, 4},
97 1.7 cgd {"acflag", "ACFLG", NULL, 0, pvar, 3, POFF(p_acflag), USHORT, "x"},
98 1.1 cgd {"acflg", "", "acflag"},
99 1.1 cgd {"blocked", "", "sigmask"},
100 1.1 cgd {"caught", "", "sigcatch"},
101 1.7 cgd {"command", "COMMAND", NULL, COMM|LJUST|USER, command, 16},
102 1.12 cgd {"cpu", "CPU", NULL, 0, pvar, 3, POFF(p_estcpu), UINT, "d"},
103 1.1 cgd {"cputime", "", "time"},
104 1.12 cgd {"f", "F", NULL, 0, pvar, 7, POFF(p_flag), INT, "x"},
105 1.1 cgd {"flags", "", "f"},
106 1.9 cgd {"holdcnt", "HOLDCNT", NULL, 0, pvar, 8, POFF(p_holdcnt), INT, "d"},
107 1.1 cgd {"ignored", "", "sigignore"},
108 1.23 simonb {"inblk", "INBLK", NULL, USER, pvar, 4, POFF(p_uru_inblock), LONG, "d"},
109 1.1 cgd {"inblock", "", "inblk"},
110 1.23 simonb {"jobc", "JOBC", NULL, 0, evar, 4, POFF(p_jobc), SHORT, "d"},
111 1.12 cgd {"ktrace", "KTRACE", NULL, 0, pvar, 8, POFF(p_traceflag), INT, "x"},
112 1.12 cgd /* XXX */
113 1.12 cgd {"ktracep", "KTRACEP", NULL, 0, pvar, 8, POFF(p_tracep), KPTR, "x"},
114 1.1 cgd {"lim", "LIM", NULL, 0, maxrss, 5},
115 1.1 cgd {"login", "LOGIN", NULL, LJUST, logname, MAXLOGNAME},
116 1.1 cgd {"logname", "", "login"},
117 1.1 cgd {"lstart", "STARTED", NULL, LJUST|USER, lstarted, 28},
118 1.23 simonb {"majflt", "MAJFLT", NULL, USER, pvar, 4, POFF(p_uru_majflt), LONG, "d"},
119 1.23 simonb {"minflt", "MINFLT", NULL, USER, pvar, 4, POFF(p_uru_minflt), LONG, "d"},
120 1.23 simonb {"msgrcv", "MSGRCV", NULL, USER, pvar, 4, POFF(p_uru_msgrcv), LONG, "d"},
121 1.23 simonb {"msgsnd", "MSGSND", NULL, USER, pvar, 4, POFF(p_uru_msgsnd), LONG, "d"},
122 1.1 cgd {"ni", "", "nice"},
123 1.23 simonb {"nice", "NI", NULL, 0, pnice, 3},
124 1.23 simonb {"nivcsw", "NIVCSW", NULL, USER, pvar, 5, POFF(p_uru_nivcsw), LONG, "d"},
125 1.1 cgd {"nsignals", "", "nsigs"},
126 1.23 simonb {"nsigs", "NSIGS", NULL, USER, pvar, 4, POFF(p_uru_nsignals), LONG, "d"},
127 1.23 simonb {"nswap", "NSWAP", NULL, USER, pvar, 4, POFF(p_uru_nswap), LONG, "d"},
128 1.23 simonb {"nvcsw", "NVCSW", NULL, USER, pvar, 5, POFF(p_uru_nvcsw), LONG, "d"},
129 1.12 cgd /* XXX */
130 1.1 cgd {"nwchan", "WCHAN", NULL, 0, pvar, 6, POFF(p_wchan), KPTR, "x"},
131 1.23 simonb {"oublk", "OUBLK", NULL, USER, pvar, 4, POFF(p_uru_oublock), LONG, "d"},
132 1.1 cgd {"oublock", "", "oublk"},
133 1.12 cgd /* XXX */
134 1.1 cgd {"p_ru", "P_RU", NULL, 0, pvar, 6, POFF(p_ru), KPTR, "x"},
135 1.12 cgd /* XXX */
136 1.23 simonb {"paddr", "PADDR", NULL, 0, evar, 6, POFF(p_paddr), KPTR, "x"},
137 1.1 cgd {"pagein", "PAGEIN", NULL, USER, pagein, 6},
138 1.1 cgd {"pcpu", "", "%cpu"},
139 1.1 cgd {"pending", "", "sig"},
140 1.23 simonb PID("pgid", "PGID", evar, POFF(p__pgid)),
141 1.12 cgd PID("pid", "PID", pvar, POFF(p_pid)),
142 1.1 cgd {"pmem", "", "%mem"},
143 1.23 simonb PID("ppid", "PPID", evar, POFF(p_ppid)),
144 1.1 cgd {"pri", "PRI", NULL, 0, pri, 3},
145 1.12 cgd {"re", "RE", NULL, INF127, pvar, 3, POFF(p_swtime), UINT, "d"},
146 1.23 simonb GID("rgid", "RGID", evar, POFF(p_rgid)),
147 1.12 cgd /* XXX */
148 1.6 cgd {"rlink", "RLINK", NULL, 0, pvar, 8, POFF(p_back), KPTR, "x"},
149 1.1 cgd {"rss", "RSS", NULL, 0, p_rssize, 4},
150 1.1 cgd {"rssize", "", "rsz"},
151 1.1 cgd {"rsz", "RSZ", NULL, 0, rssize, 4},
152 1.23 simonb UID("ruid", "RUID", evar, POFF(p_ruid)),
153 1.1 cgd {"ruser", "RUSER", NULL, LJUST, runame, USERLEN},
154 1.23 simonb {"sess", "SESS", NULL, 0, evar, 6, POFF(p_sess), KPTR24, "x"},
155 1.23 simonb PID("sid", "SID", evar, POFF(p_sid)),
156 1.18 christos {"sig", "PENDING",
157 1.18 christos NULL, 0, pvar, SIGWIDTH, POFF(p_siglist), SIGLIST, "s"},
158 1.18 christos {"sigcatch", "CAUGHT",
159 1.18 christos NULL, 0, pvar, SIGWIDTH, POFF(p_sigcatch), SIGLIST, "s"},
160 1.1 cgd {"sigignore", "IGNORED",
161 1.18 christos NULL, 0, pvar, SIGWIDTH, POFF(p_sigignore), SIGLIST, "s"},
162 1.18 christos {"sigmask", "BLOCKED",
163 1.18 christos NULL, 0, pvar, SIGWIDTH, POFF(p_sigmask), SIGLIST, "s"},
164 1.12 cgd {"sl", "SL", NULL, INF127, pvar, 3, POFF(p_slptime), UINT, "d"},
165 1.1 cgd {"start", "STARTED", NULL, LJUST|USER, started, 8},
166 1.1 cgd {"stat", "", "state"},
167 1.1 cgd {"state", "STAT", NULL, 0, state, 4},
168 1.23 simonb GID("svgid", "SVGID", evar, POFF(p_gid)),
169 1.23 simonb UID("svuid", "SVUID", evar, POFF(p_uid)),
170 1.1 cgd {"tdev", "TDEV", NULL, 0, tdev, 4},
171 1.1 cgd {"time", "TIME", NULL, USER, cputime, 9},
172 1.23 simonb PID("tpgid", "TGPID", evar, POFF(p_tpgid)),
173 1.23 simonb {"tsess", "TSESS", NULL, 0, evar, 6, POFF(p_tsess), KPTR, "x"},
174 1.1 cgd {"tsiz", "TSIZ", NULL, 0, tsize, 4},
175 1.1 cgd {"tt", "TT", NULL, LJUST, tname, 3},
176 1.1 cgd {"tty", "TTY", NULL, LJUST, longtname, 8},
177 1.1 cgd {"ucomm", "UCOMM", NULL, LJUST, ucomm, MAXCOMLEN},
178 1.23 simonb UID("uid", "UID", evar, POFF(p_uid)),
179 1.12 cgd {"upr", "UPR", NULL, 0, pvar, 3, POFF(p_usrpri), UCHAR, "d"},
180 1.1 cgd {"user", "USER", NULL, LJUST, uname, USERLEN},
181 1.1 cgd {"usrpri", "", "upr"},
182 1.1 cgd {"vsize", "", "vsz"},
183 1.1 cgd {"vsz", "VSZ", NULL, 0, vsize, 5},
184 1.1 cgd {"wchan", "WCHAN", NULL, LJUST, wchan, 6},
185 1.1 cgd {"xstat", "XSTAT", NULL, 0, pvar, 4, POFF(p_xstat), USHORT, "x"},
186 1.1 cgd {""},
187 1.1 cgd };
188 1.1 cgd
189 1.7 cgd void
190 1.1 cgd showkey()
191 1.1 cgd {
192 1.7 cgd VAR *v;
193 1.7 cgd int i;
194 1.7 cgd char *p, *sep;
195 1.1 cgd
196 1.1 cgd i = 0;
197 1.1 cgd sep = "";
198 1.1 cgd for (v = var; *(p = v->name); ++v) {
199 1.7 cgd int len = strlen(p);
200 1.1 cgd if (termwidth && (i += len + 1) > termwidth) {
201 1.1 cgd i = len;
202 1.1 cgd sep = "\n";
203 1.1 cgd }
204 1.1 cgd (void) printf("%s%s", sep, p);
205 1.1 cgd sep = " ";
206 1.1 cgd }
207 1.1 cgd (void) printf("\n");
208 1.1 cgd }
209 1.1 cgd
210 1.7 cgd void
211 1.1 cgd parsefmt(p)
212 1.1 cgd char *p;
213 1.1 cgd {
214 1.1 cgd static struct varent *vtail;
215 1.1 cgd
216 1.1 cgd #define FMTSEP " \t,\n"
217 1.1 cgd while (p && *p) {
218 1.7 cgd char *cp;
219 1.7 cgd VAR *v;
220 1.7 cgd struct varent *vent;
221 1.7 cgd
222 1.1 cgd while ((cp = strsep(&p, FMTSEP)) != NULL && *cp == '\0')
223 1.1 cgd /* void */;
224 1.22 mjl if (cp == NULL || !(v = findvar(cp)))
225 1.1 cgd continue;
226 1.1 cgd if ((vent = malloc(sizeof(struct varent))) == NULL)
227 1.20 drochner err(1, NULL);
228 1.1 cgd vent->var = v;
229 1.1 cgd vent->next = NULL;
230 1.1 cgd if (vhead == NULL)
231 1.1 cgd vhead = vtail = vent;
232 1.1 cgd else {
233 1.1 cgd vtail->next = vent;
234 1.1 cgd vtail = vent;
235 1.1 cgd }
236 1.1 cgd }
237 1.1 cgd if (!vhead)
238 1.7 cgd errx(1, "no valid keywords");
239 1.1 cgd }
240 1.1 cgd
241 1.1 cgd static VAR *
242 1.1 cgd findvar(p)
243 1.1 cgd char *p;
244 1.1 cgd {
245 1.1 cgd VAR *v, key;
246 1.1 cgd char *hp;
247 1.1 cgd
248 1.1 cgd key.name = p;
249 1.1 cgd
250 1.7 cgd hp = strchr(p, '=');
251 1.1 cgd if (hp)
252 1.1 cgd *hp++ = '\0';
253 1.1 cgd
254 1.1 cgd key.name = p;
255 1.7 cgd v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
256 1.1 cgd
257 1.1 cgd if (v && v->alias) {
258 1.1 cgd if (hp) {
259 1.7 cgd warnx("%s: illegal keyword specification", p);
260 1.1 cgd eval = 1;
261 1.1 cgd }
262 1.1 cgd parsefmt(v->alias);
263 1.7 cgd return ((VAR *)NULL);
264 1.1 cgd }
265 1.1 cgd if (!v) {
266 1.7 cgd warnx("%s: keyword not found", p);
267 1.1 cgd eval = 1;
268 1.13 cgd return ((VAR *)NULL);
269 1.1 cgd }
270 1.1 cgd if (hp)
271 1.1 cgd v->header = hp;
272 1.7 cgd return (v);
273 1.1 cgd }
274 1.1 cgd
275 1.7 cgd static int
276 1.1 cgd vcmp(a, b)
277 1.7 cgd const void *a, *b;
278 1.1 cgd {
279 1.7 cgd return (strcmp(((VAR *)a)->name, ((VAR *)b)->name));
280 1.1 cgd }
281