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