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