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