Commit | Line | Data |
---|---|---|
2717716e WJ |
1 | /*- |
2 | * Copyright (c) 1990 The Regents of the University of California. | |
3 | * 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 5.9 (Berkeley) 6/3/91"; | |
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 | #include <errno.h> | |
43 | #include <stdio.h> | |
44 | #include <stdlib.h> | |
45 | #include <stddef.h> | |
46 | #include <string.h> | |
47 | #include "ps.h" | |
48 | ||
49 | #ifdef SPPWAIT | |
50 | #define NEWVM | |
51 | #endif | |
52 | ||
53 | #ifdef NEWVM | |
54 | #include <sys/ucred.h> | |
55 | #include <sys/kinfo_proc.h> | |
56 | #endif | |
57 | ||
58 | int command(), cputime(), evar(), logname(), longtname(), lstarted(), | |
59 | maxrss(), p_rssize(), pagein(), pcpu(), pmem(), pri(), pvar(), | |
60 | rssize(), runame(), rvar(), started(), state(), tdev(), tname(), | |
61 | tsize(), ucomm(), uname(), uvar(), vsize(), wchan(); | |
62 | #ifndef NEWVM | |
63 | int trss(); | |
64 | #endif | |
65 | ||
66 | #ifdef NOTINUSE | |
67 | int utime(), stime(), ixrss(), idrss(), isrss(); | |
68 | {{"utime"}, "UTIME", USER, utime, 4}, | |
69 | {{"stime"}, "STIME", USER, stime, 4}, | |
70 | {{"ixrss"}, "IXRSS", USER, ixrss, 4}, | |
71 | {{"idrss"}, "IDRSS", USER, idrss, 4}, | |
72 | {{"isrss"}, "ISRSS", USER, isrss, 4}, | |
73 | #endif | |
74 | ||
75 | /* Compute offset in common structures. */ | |
76 | #define POFF(x) offsetof(struct proc, x) | |
77 | #define EOFF(x) offsetof(struct eproc, x) | |
78 | #define UOFF(x) offsetof(struct usave, x) | |
79 | #define ROFF(x) offsetof(struct rusage, x) | |
80 | ||
81 | #define UIDFMT "u" | |
82 | #define UIDLEN 5 | |
83 | #define PIDFMT "d" | |
84 | #define PIDLEN 5 | |
85 | #define USERLEN 8 | |
86 | ||
87 | VAR var[] = { | |
88 | #ifdef NEWVM | |
89 | {"%cpu", "%CPU", NULL, 0, pcpu, 4}, | |
90 | {"%mem", "%MEM", NULL, 0, pmem, 4}, | |
91 | {"acflag", "ACFLG", NULL, 0, pvar, 3, POFF(p_acflag), SHORT, "x"}, | |
92 | {"acflg", "", "acflag"}, | |
93 | {"blocked", "", "sigmask"}, | |
94 | {"caught", "", "sigcatch"}, | |
95 | {"command", "COMMAND", NULL, COMM|LJUST, command, 16}, | |
96 | {"cpu", "CPU", NULL, 0, pvar, 3, POFF(p_cpu), UCHAR, "d"}, | |
97 | {"cputime", "", "time"}, | |
98 | {"f", "F", NULL, 0, pvar, 7, POFF(p_flag), LONG, "x"}, | |
99 | {"flags", "", "f"}, | |
100 | {"ignored", "", "sigignore"}, | |
101 | {"inblk", "INBLK", NULL, USER, rvar, 4, ROFF(ru_inblock), LONG, "d"}, | |
102 | {"inblock", "", "inblk"}, | |
103 | {"jobc", "JOBC", NULL, 0, evar, 4, EOFF(e_jobc), SHORT, "d"}, | |
104 | {"ktrace", "KTRACE", NULL, 0, pvar, 8, POFF(p_traceflag), LONG, "x"}, | |
105 | {"ktracep", "KTRACEP", NULL, 0, pvar, 8, POFF(p_tracep), LONG, "x"}, | |
106 | {"lim", "LIM", NULL, 0, maxrss, 5}, | |
107 | {"login", "LOGIN", NULL, LJUST, logname, MAXLOGNAME}, | |
108 | {"logname", "", "login"}, | |
109 | {"lstart", "STARTED", NULL, LJUST|USER, lstarted, 28}, | |
110 | {"majflt", "MAJFLT", NULL, USER, rvar, 4, ROFF(ru_majflt), LONG, "d"}, | |
111 | {"minflt", "MINFLT", NULL, USER, rvar, 4, ROFF(ru_minflt), LONG, "d"}, | |
112 | {"msgrcv", "MSGRCV", NULL, USER, rvar, 4, ROFF(ru_msgrcv), LONG, "d"}, | |
113 | {"msgsnd", "MSGSND", NULL, USER, rvar, 4, ROFF(ru_msgsnd), LONG, "d"}, | |
114 | {"ni", "", "nice"}, | |
115 | {"nice", "NI", NULL, 0, pvar, 2, POFF(p_nice), CHAR, "d"}, | |
116 | {"nivcsw", "NIVCSW", NULL, USER, rvar, 5, ROFF(ru_nivcsw), LONG, "d"}, | |
117 | {"nsignals", "", "nsigs"}, | |
118 | {"nsigs", "NSIGS", NULL, USER, rvar, 4, ROFF(ru_nsignals), LONG, "d"}, | |
119 | {"nswap", "NSWAP", NULL, USER, rvar, 4, ROFF(ru_nswap), LONG, "d"}, | |
120 | {"nvcsw", "NVCSW", NULL, USER, rvar, 5, ROFF(ru_nvcsw), LONG, "d"}, | |
121 | {"nwchan", "WCHAN", NULL, 0, pvar, 6, POFF(p_wchan), KPTR, "x"}, | |
122 | {"oublk", "OUBLK", NULL, USER, rvar, 4, ROFF(ru_oublock), LONG, "d"}, | |
123 | {"oublock", "", "oublk"}, | |
124 | {"p_ru", "P_RU", NULL, 0, pvar, 6, POFF(p_ru), KPTR, "x"}, | |
125 | {"paddr", "PADDR", NULL, 0, evar, 6, EOFF(e_paddr), KPTR, "x"}, | |
126 | {"pagein", "PAGEIN", NULL, USER, pagein, 6}, | |
127 | {"pcpu", "", "%cpu"}, | |
128 | {"pending", "", "sig"}, | |
129 | {"pgid", "PGID", NULL, 0, evar, PIDLEN, EOFF(e_pgid), USHORT, PIDFMT}, | |
130 | {"pid", "PID", NULL, 0, pvar, PIDLEN, POFF(p_pid),SHORT, PIDFMT}, | |
131 | {"pmem", "", "%mem"}, | |
132 | {"ppid", "PPID", NULL, 0, evar, PIDLEN, EOFF(e_ppid), SHORT, PIDFMT}, | |
133 | {"pri", "PRI", NULL, 0, pri, 3}, | |
134 | {"re", "RE", NULL, 0, pvar, 3, POFF(p_time), CHAR, "d"}, | |
135 | {"rgid", "RGID", NULL, 0, evar, UIDLEN, EOFF(e_pcred.p_rgid), | |
136 | USHORT, UIDFMT}, | |
137 | {"rlink", "RLINK", NULL, 0, pvar, 8, POFF(p_rlink), KPTR, "x"}, | |
138 | {"rss", "RSS", NULL, 0, p_rssize, 4}, | |
139 | {"rssize", "", "rsz"}, | |
140 | {"rsz", "RSZ", NULL, 0, rssize, 4}, | |
141 | {"ruid", "RUID", NULL, 0, evar, UIDLEN, EOFF(e_pcred.p_ruid), | |
142 | USHORT, UIDFMT}, | |
143 | {"ruser", "RUSER", NULL, LJUST, runame, USERLEN}, | |
144 | {"sess", "SESS", NULL, 0, evar, 6, EOFF(e_sess), KPTR, "x"}, | |
145 | {"sig", "PENDING", NULL, 0, pvar, 8, POFF(p_sig), LONG, "x"}, | |
146 | {"sigcatch", "CAUGHT", NULL, 0, pvar, 8, POFF(p_sigcatch), LONG, "x"}, | |
147 | {"sigignore", "IGNORED", | |
148 | NULL, 0, pvar, 8, POFF(p_sigignore), LONG, "x"}, | |
149 | {"sigmask", "BLOCKED", NULL, 0, pvar, 8, POFF(p_sigmask), LONG, "x"}, | |
150 | {"sl", "SL", NULL, 0, pvar, 3, POFF(p_slptime), CHAR, "d"}, | |
151 | {"start", "STARTED", NULL, LJUST|USER, started, 8}, | |
152 | {"stat", "", "state"}, | |
153 | {"state", "STAT", NULL, 0, state, 4}, | |
154 | {"svgid", "SVGID", | |
155 | NULL, 0, evar, UIDLEN, EOFF(e_pcred.p_svgid), USHORT, UIDFMT}, | |
156 | {"svuid", "SVUID", | |
157 | NULL, 0, evar, UIDLEN, EOFF(e_pcred.p_svuid), USHORT, UIDFMT}, | |
158 | {"tdev", "TDEV", NULL, 0, tdev, 4}, | |
159 | {"time", "TIME", NULL, USER, cputime, 9}, | |
160 | {"tpgid", "TPGID", NULL, 0, evar, 4, EOFF(e_tpgid), USHORT, PIDFMT}, | |
161 | {"tsess", "TSESS", NULL, 0, evar, 6, EOFF(e_tsess), KPTR, "x"}, | |
162 | {"tsiz", "TSIZ", NULL, 0, tsize, 4}, | |
163 | {"tt", "TT", NULL, LJUST, tname, 3}, | |
164 | {"tty", "TTY", NULL, LJUST, longtname, 8}, | |
165 | {"ucomm", "UCOMM", NULL, LJUST, ucomm, MAXCOMLEN}, | |
166 | {"uid", "UID", NULL, 0, evar, UIDLEN, EOFF(e_ucred.cr_uid), | |
167 | USHORT, UIDFMT}, | |
168 | {"upr", "UPR", NULL, 0, pvar, 3, POFF(p_usrpri), CHAR, "d"}, | |
169 | {"user", "USER", NULL, LJUST, uname, USERLEN}, | |
170 | {"usrpri", "", "upr"}, | |
171 | {"vsize", "", "vsz"}, | |
172 | {"vsz", "VSZ", NULL, 0, vsize, 5}, | |
173 | {"wchan", "WCHAN", NULL, LJUST, wchan, 6}, | |
174 | {"xstat", "XSTAT", NULL, 0, pvar, 4, POFF(p_xstat), USHORT, "x"}, | |
175 | #else | |
176 | {"%cpu", "%CPU", NULL, 0, pcpu, 4}, | |
177 | {"%mem", "%MEM", NULL, 0, pmem, 4}, | |
178 | {"acflag", "ACFLG", NULL, USER, uvar, 3, UOFF(u_acflag), SHORT, "x"}, | |
179 | {"acflg", "", "acflag"}, | |
180 | {"blocked", "", "sigmask"}, | |
181 | {"caught", "", "sigcatch"}, | |
182 | {"command", "COMMAND", NULL, COMM|LJUST|USER, command, 16}, | |
183 | {"cpu", "CPU", NULL, 0, pvar, 3, POFF(p_cpu), UCHAR, "d"}, | |
184 | {"cputime", "", "time"}, | |
185 | {"f", "F", NULL, 0, pvar, 7, POFF(p_flag), LONG, "x"}, | |
186 | {"flags", "", "f"}, | |
187 | {"ignored", "", "sigignore"}, | |
188 | {"inblk", "INBLK", NULL, USER, rvar, 4, ROFF(ru_inblock), LONG, "d"}, | |
189 | {"inblock", "", "inblk"}, | |
190 | {"jobc", "JOBC", NULL, 0, evar, 4, EOFF(e_jobc), SHORT, "d"}, | |
191 | {"ktrace", "KTRACE", NULL, 0, pvar, 8, POFF(p_traceflag), LONG, "x"}, | |
192 | {"ktracep", "KTRACEP", NULL, 0, pvar, 8, POFF(p_tracep), LONG, "x"}, | |
193 | {"lim", "LIM", NULL, 0, maxrss, 5}, | |
194 | {"logname", "LOGNAME", NULL, LJUST, logname, MAXLOGNAME}, | |
195 | {"lstart", "STARTED", NULL, LJUST|USER, lstarted, 28}, | |
196 | {"majflt", "MAJFLT", NULL, USER, rvar, 4, ROFF(ru_majflt), LONG, "d"}, | |
197 | {"minflt", "MINFLT", NULL, USER, rvar, 4, ROFF(ru_minflt), LONG, "d"}, | |
198 | {"msgrcv", "MSGRCV", NULL, USER, rvar, 4, ROFF(ru_msgrcv), LONG, "d"}, | |
199 | {"msgsnd", "MSGSND", NULL, USER, rvar, 4, ROFF(ru_msgsnd), LONG, "d"}, | |
200 | {"ni", "", "nice"}, | |
201 | {"nice", "NI", NULL, 0, pvar, 2, POFF(p_nice), CHAR, "d"}, | |
202 | {"nivcsw", "NIVCSW", NULL, USER, rvar, 5, ROFF(ru_nivcsw), LONG, "d"}, | |
203 | {"nsignals", "", "nsigs"}, | |
204 | {"nsigs", "NSIGS", NULL, USER, rvar, 4, ROFF(ru_nsignals), LONG, "d"}, | |
205 | {"nswap", "NSWAP", NULL, USER, rvar, 4, ROFF(ru_nswap), LONG, "d"}, | |
206 | {"nvcsw", "NVCSW", NULL, USER, rvar, 5, ROFF(ru_nvcsw), LONG, "d"}, | |
207 | {"nwchan", "WCHAN", NULL, 0, pvar, 6, POFF(p_wchan), KPTR, "x"}, | |
208 | {"oublk", "OUBLK", NULL, USER, rvar, 4, ROFF(ru_oublock), LONG, "d"}, | |
209 | {"oublock", "", "oublk"}, | |
210 | {"p_ru", "P_RU", NULL, 0, pvar, 6, POFF(p_ru), KPTR, "x"}, | |
211 | {"paddr", "PADDR", NULL, 0, evar, 6, EOFF(e_paddr), KPTR, "x"}, | |
212 | {"pagein", "PAGEIN", NULL, USER, pagein, 6}, | |
213 | {"pcpu", "", "%cpu"}, | |
214 | {"pending", "", "sig"}, | |
215 | {"pgid", "PGID", NULL, 0, evar, PIDLEN, EOFF(e_pgid), USHORT, PIDFMT}, | |
216 | {"pid", "PID", NULL, 0, pvar, PIDLEN, POFF(p_pid),SHORT, PIDFMT}, | |
217 | {"pmem", "", "%mem"}, | |
218 | {"poip", "POIP", NULL, 0, pvar, 4, POFF(p_poip), SHORT, "d"}, | |
219 | {"ppid", "PPID", NULL, 0, pvar, PIDLEN, POFF(p_ppid), SHORT, PIDFMT}, | |
220 | {"pri", "PRI", NULL, 0, pri, 3}, | |
221 | {"re", "RE", NULL, 0, pvar, 3, POFF(p_time), CHAR, "d"}, | |
222 | {"rgid", "RGID", NULL, 0, pvar, UIDLEN, POFF(p_rgid), USHORT, UIDFMT}, | |
223 | {"rlink", "RLINK", NULL, 0, pvar, 8, POFF(p_rlink), KPTR, "x"}, | |
224 | {"rss", "RSS", NULL, 0, p_rssize, 4}, | |
225 | {"rssize", "", "rsz"}, | |
226 | {"rsz", "RSZ", NULL, 0, rssize, 4}, | |
227 | {"ruid", "RUID", NULL, 0, pvar, UIDLEN, POFF(p_ruid), USHORT, UIDFMT}, | |
228 | {"ruser", "RUSER", NULL, LJUST, runame, USERLEN}, | |
229 | {"sess", "SESS", NULL, 0, evar, 6, EOFF(e_sess), KPTR, "x"}, | |
230 | {"sig", "PENDING", NULL, 0, pvar, 8, POFF(p_sig), LONG, "x"}, | |
231 | {"sigcatch", "CAUGHT", NULL, 0, pvar, 8, POFF(p_sigcatch), LONG, "x"}, | |
232 | {"sigignore", "IGNORED", | |
233 | NULL, 0, pvar, 8, POFF(p_sigignore), LONG, "x"}, | |
234 | {"sigmask", "BLOCKED", NULL, 0, pvar, 8, POFF(p_sigmask), LONG, "x"}, | |
235 | {"sl", "SL", NULL, 0, pvar, 3, POFF(p_slptime), CHAR, "d"}, | |
236 | {"start", "STARTED", NULL, LJUST|USER, started, 8}, | |
237 | {"stat", "", "state"}, | |
238 | {"state", "STAT", NULL, 0, state, 4}, | |
239 | {"svgid", "SVGID", | |
240 | NULL, 0, pvar, UIDLEN, POFF(p_svgid), USHORT, UIDFMT}, | |
241 | {"svuid", "SVUID", | |
242 | NULL, 0, pvar, UIDLEN, POFF(p_svuid), USHORT, UIDFMT}, | |
243 | {"tdev", "TDEV", NULL, 0, tdev, 4}, | |
244 | {"time", "TIME", NULL, USER, cputime, 9}, | |
245 | {"tpgid", "TPGID", NULL, 0, evar, 4, EOFF(e_tpgid), USHORT, PIDFMT}, | |
246 | {"trs", "TRS", NULL, 0, trss, 3}, | |
247 | {"tsess", "TSESS", NULL, 0, evar, 6, EOFF(e_tsess), KPTR, "x"}, | |
248 | {"tsiz", "TSIZ", NULL, 0, tsize, 4}, | |
249 | {"tt", "TT", NULL, LJUST, tname, 3}, | |
250 | {"tty", "TTY", NULL, LJUST, longtname, 8}, | |
251 | {"ucomm", "UCOMM", NULL, LJUST, ucomm, MAXCOMLEN}, | |
252 | {"uid", "UID", NULL, 0, pvar, UIDLEN, POFF(p_uid),USHORT, UIDFMT}, | |
253 | {"upr", "UPR", NULL, 0, pvar, 3, POFF(p_usrpri), CHAR, "d"}, | |
254 | {"uprocp", "UPROCP", NULL, USER, uvar, 6, UOFF(u_procp), KPTR, "x"}, | |
255 | {"user", "USER", NULL, LJUST, uname, USERLEN}, | |
256 | {"usrpri", "", "upr"}, | |
257 | {"vsize", "", "vsz"}, | |
258 | {"vsz", "VSZ", NULL, 0, vsize, 5}, | |
259 | {"wchan", "WCHAN", NULL, LJUST, wchan, 6}, | |
260 | {"xstat", "XSTAT", NULL, 0, pvar, 4, POFF(p_xstat), USHORT, "x"}, | |
261 | #endif | |
262 | {""}, | |
263 | }; | |
264 | ||
265 | showkey() | |
266 | { | |
267 | extern int termwidth; | |
268 | register VAR *v; | |
269 | register int i, len; | |
270 | register char *p, *sep; | |
271 | ||
272 | i = 0; | |
273 | sep = ""; | |
274 | for (v = var; *(p = v->name); ++v) { | |
275 | len = strlen(p); | |
276 | if (termwidth && (i += len + 1) > termwidth) { | |
277 | i = len; | |
278 | sep = "\n"; | |
279 | } | |
280 | (void) printf("%s%s", sep, p); | |
281 | sep = " "; | |
282 | } | |
283 | (void) printf("\n"); | |
284 | } | |
285 | ||
286 | parsefmt(p) | |
287 | char *p; | |
288 | { | |
289 | static struct varent *vtail; | |
290 | register VAR *v; | |
291 | register char *cp; | |
292 | register struct varent *vent; | |
293 | static VAR *findvar(); | |
294 | ||
295 | #define FMTSEP " \t,\n" | |
296 | while (p && *p) { | |
297 | while ((cp = strsep(&p, FMTSEP)) != NULL && *cp == '\0') | |
298 | /* void */; | |
299 | if (!(v = findvar(cp))) | |
300 | continue; | |
301 | if ((vent = malloc(sizeof(struct varent))) == NULL) | |
302 | err("%s", strerror(errno)); | |
303 | vent->var = v; | |
304 | vent->next = NULL; | |
305 | if (vhead == NULL) | |
306 | vhead = vtail = vent; | |
307 | else { | |
308 | vtail->next = vent; | |
309 | vtail = vent; | |
310 | } | |
311 | } | |
312 | if (!vhead) | |
313 | err("no valid keywords\n"); | |
314 | } | |
315 | ||
316 | static VAR * | |
317 | findvar(p) | |
318 | char *p; | |
319 | { | |
320 | extern int eval; | |
321 | VAR *v, key; | |
322 | char *hp; | |
323 | int vcmp(); | |
324 | ||
325 | key.name = p; | |
326 | ||
327 | hp = index(p, '='); | |
328 | if (hp) | |
329 | *hp++ = '\0'; | |
330 | ||
331 | key.name = p; | |
332 | v = (VAR *)bsearch(&key, var, | |
333 | sizeof(var)/sizeof(VAR), sizeof(VAR), vcmp); | |
334 | ||
335 | if (v && v->alias) { | |
336 | if (hp) { | |
337 | (void)fprintf(stderr, | |
338 | "ps: %s: illegal keyword specification\n", p); | |
339 | eval = 1; | |
340 | } | |
341 | parsefmt(v->alias); | |
342 | return((VAR *)NULL); | |
343 | } | |
344 | if (!v) { | |
345 | (void)fprintf(stderr, "ps: keyword %s not found\n", p); | |
346 | eval = 1; | |
347 | } | |
348 | if (hp) | |
349 | v->header = hp; | |
350 | return(v); | |
351 | } | |
352 | ||
353 | vcmp(a, b) | |
354 | VAR *a, *b; | |
355 | { | |
356 | return(strcmp(a->name, b->name)); | |
357 | } |