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 #if 0 35 #ifndef lint 36 static char sccsid[] = "@(#)keyword.c 8.5 (Berkeley) 4/2/94"; 37 #endif /* not lint */ 38 #endif 39 #include <sys/cdefs.h> 40 __FBSDID("$FreeBSD$"); 41 42 #include <sys/param.h> 43 #include <sys/time.h> 44 #include <sys/resource.h> 45 #include <sys/proc.h> 46 #include <sys/sysctl.h> 47 #include <sys/user.h> 48 49 #include <err.h> 50 #include <stddef.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 55 #include "ps.h" 56 57 static VAR *findvar(char *); 58 static int vcmp(const void *, const void *); 59 60 #ifdef NOTINUSE 61 int utime(), stime(), ixrss(), idrss(), isrss(); 62 {{"utime"}, "UTIME", USER, utime, NULL, 4}, 63 {{"stime"}, "STIME", USER, stime, NULL, 4}, 64 {{"ixrss"}, "IXRSS", USER, ixrss, NULL, 4}, 65 {{"idrss"}, "IDRSS", USER, idrss, NULL, 4}, 66 {{"isrss"}, "ISRSS", USER, isrss, NULL, 4}, 67 #endif 68 69 /* Compute offset in common structures. */ 70 #define KOFF(x) offsetof(struct kinfo_proc, x) 71 #define ROFF(x) offsetof(struct rusage, x) 72 73 #define UIDFMT "u" 74 #define UIDLEN 5 75 #define PIDFMT "d" 76 #define PIDLEN 5 77 #define USERLEN (MAXLOGNAME - 1) 78 79 static VAR var[] = { 80 {"%cpu", "%CPU", NULL, 0, pcpu, NULL, 4, 0, CHAR, NULL, 0}, 81 {"%mem", "%MEM", NULL, 0, pmem, NULL, 4, 0, CHAR, NULL, 0}, 82 {"acflag", "ACFLG", NULL, 0, kvar, NULL, 3, KOFF(ki_acflag), USHORT, 83 "x", 0}, 84 {"acflg", "", "acflag", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 85 {"args", "COMMAND", NULL, COMM|LJUST|USER, arguments, NULL, 16, 0, 86 CHAR, NULL, 0}, 87 {"blocked", "", "sigmask", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 88 {"caught", "", "sigcatch", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 89 {"comm", "COMMAND", NULL, LJUST, ucomm, NULL, MAXCOMLEN, 0, CHAR, 90 NULL, 0}, 91 {"command", "COMMAND", NULL, COMM|LJUST|USER, command, NULL, 16, 0, 92 CHAR, NULL, 0}, 93 {"cpu", "CPU", NULL, 0, kvar, NULL, 3, KOFF(ki_estcpu), UINT, "d", 94 0}, 95 {"cputime", "", "time", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 96 {"etime", "ELAPSED", NULL, USER, elapsed, NULL, 9, 0, CHAR, NULL, 0}, 97 {"f", "F", NULL, 0, kvar, NULL, 7, KOFF(ki_flag), INT, "x", 0}, 98 {"flags", "", "f", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 99 {"ignored", "", "sigignore", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 100 {"inblk", "INBLK", NULL, USER, rvar, NULL, 4, ROFF(ru_inblock), LONG, 101 "ld", 0}, 102 {"inblock", "", "inblk", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 103 {"jobc", "JOBC", NULL, 0, kvar, NULL, 4, KOFF(ki_jobc), SHORT, "d", 104 0}, 105 {"ktrace", "KTRACE", NULL, 0, kvar, NULL, 8, KOFF(ki_traceflag), INT, 106 "x", 0}, 107 {"lim", "LIM", NULL, 0, maxrss, NULL, 5, 0, CHAR, NULL, 0}, 108 {"login", "LOGIN", NULL, LJUST, logname, NULL, MAXLOGNAME-1, 0, CHAR, 109 NULL, 0}, 110 {"logname", "", "login", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 111 {"lstart", "STARTED", NULL, LJUST|USER, lstarted, NULL, 28, 0, CHAR, 112 NULL, 0}, 113 {"lvl", "LVL", NULL, LJUST, lattr, NULL, 3, 0, CHAR, NULL, 0}, 114 {"majflt", "MAJFLT", NULL, USER, rvar, NULL, 4, ROFF(ru_majflt), 115 LONG, "ld", 0}, 116 {"minflt", "MINFLT", NULL, USER, rvar, NULL, 4, ROFF(ru_minflt), 117 LONG, "ld", 0}, 118 {"msgrcv", "MSGRCV", NULL, USER, rvar, NULL, 4, ROFF(ru_msgrcv), 119 LONG, "ld", 0}, 120 {"msgsnd", "MSGSND", NULL, USER, rvar, NULL, 4, ROFF(ru_msgsnd), 121 LONG, "ld", 0}, 122 {"mtxname", "MUTEX", NULL, LJUST, mtxname, NULL, 6, 0, CHAR, NULL, 123 0}, 124 {"mwchan", "MWCHAN", NULL, LJUST, mwchan, NULL, 6, 0, CHAR, NULL, 0}, 125 {"ni", "", "nice", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 126 {"nice", "NI", NULL, 0, kvar, NULL, 2, KOFF(ki_nice), CHAR, "d", 127 0}, 128 {"nivcsw", "NIVCSW", NULL, USER, rvar, NULL, 5, ROFF(ru_nivcsw), 129 LONG, "ld", 0}, 130 {"nsignals", "", "nsigs", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 131 {"nsigs", "NSIGS", NULL, USER, rvar, NULL, 4, ROFF(ru_nsignals), 132 LONG, "ld", 0}, 133 {"nswap", "NSWAP", NULL, USER, rvar, NULL, 4, ROFF(ru_nswap), 134 LONG, "ld", 0}, 135 {"nvcsw", "NVCSW", NULL, USER, rvar, NULL, 5, ROFF(ru_nvcsw), 136 LONG, "ld", 0}, 137 {"oublk", "OUBLK", NULL, USER, rvar, NULL, 4, ROFF(ru_oublock), 138 LONG, "ld", 0}, 139 {"oublock", "", "oublk", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 140 {"paddr", "PADDR", NULL, 0, kvar, NULL, 8, KOFF(ki_paddr), KPTR, 141 "lx", 0}, 142 {"pagein", "PAGEIN", NULL, USER, pagein, NULL, 6, 0, CHAR, NULL, 0}, 143 {"pcpu", "", "%cpu", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 144 {"pending", "", "sig", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 145 {"pgid", "PGID", NULL, 0, kvar, NULL, PIDLEN, KOFF(ki_pgid), UINT, 146 PIDFMT, 0}, 147 {"pid", "PID", NULL, 0, kvar, NULL, PIDLEN, KOFF(ki_pid), UINT, 148 PIDFMT, 0}, 149 {"pmem", "", "%mem", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 150 {"ppid", "PPID", NULL, 0, kvar, NULL, PIDLEN, KOFF(ki_ppid), UINT, 151 PIDFMT, 0}, 152 {"pri", "PRI", NULL, 0, pri, NULL, 3, 0, CHAR, NULL, 0}, 153 {"re", "RE", NULL, 0, kvar, NULL, 3, KOFF(ki_swtime), UINT, "d", 154 0}, 155 {"rgid", "RGID", NULL, 0, kvar, NULL, UIDLEN, KOFF(ki_rgid), 156 UINT, UIDFMT, 0}, 157 {"rgroup", "RGROUP", NULL, LJUST|DSIZ, rgroupname, s_rgroupname, 158 USERLEN, 0, CHAR, NULL, 0}, 159 {"rss", "RSS", NULL, 0, kvar, NULL, 4, KOFF(ki_rssize), UINT, "d", 160 0}, 161 {"rtprio", "RTPRIO", NULL, 0, priorityr, NULL, 7, KOFF(ki_pri), CHAR, 162 NULL, 0}, 163 {"ruid", "RUID", NULL, 0, kvar, NULL, UIDLEN, KOFF(ki_ruid), 164 UINT, UIDFMT, 0}, 165 {"ruser", "RUSER", NULL, LJUST|DSIZ, runame, s_runame, USERLEN, 166 0, CHAR, NULL, 0}, 167 {"sid", "SID", NULL, 0, kvar, NULL, PIDLEN, KOFF(ki_sid), UINT, 168 PIDFMT, 0}, 169 {"sig", "PENDING", NULL, 0, kvar, NULL, 8, KOFF(ki_siglist), INT, 170 "x", 0}, 171 {"sigcatch", "CAUGHT", NULL, 0, kvar, NULL, 8, KOFF(ki_sigcatch), 172 UINT, "x", 0}, 173 {"sigignore", "IGNORED", NULL, 0, kvar, NULL, 8, KOFF(ki_sigignore), 174 UINT, "x", 0}, 175 {"sigmask", "BLOCKED", NULL, 0, kvar, NULL, 8, KOFF(ki_sigmask), 176 UINT, "x", 0}, 177 {"sl", "SL", NULL, 0, kvar, NULL, 3, KOFF(ki_slptime), UINT, "d", 178 0}, 179 {"start", "STARTED", NULL, LJUST|USER, started, NULL, 7, 0, CHAR, NULL, 180 0}, 181 {"stat", "", "state", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 182 {"state", "STAT", NULL, 0, state, NULL, 4, 0, CHAR, NULL, 0}, 183 {"svgid", "SVGID", NULL, 0, kvar, NULL, UIDLEN, KOFF(ki_svgid), 184 UINT, UIDFMT, 0}, 185 {"svuid", "SVUID", NULL, 0, kvar, NULL, UIDLEN, KOFF(ki_svuid), 186 UINT, UIDFMT, 0}, 187 {"tdev", "TDEV", NULL, 0, tdev, NULL, 4, 0, CHAR, NULL, 0}, 188 {"time", "TIME", NULL, USER, cputime, NULL, 9, 0, CHAR, NULL, 0}, 189 {"tpgid", "TPGID", NULL, 0, kvar, NULL, 4, KOFF(ki_tpgid), UINT, 190 PIDFMT, 0}, 191 {"tsid", "TSID", NULL, 0, kvar, NULL, PIDLEN, KOFF(ki_tsid), UINT, 192 PIDFMT, 0}, 193 {"tsiz", "TSIZ", NULL, 0, tsize, NULL, 4, 0, CHAR, NULL, 0}, 194 {"tt", "TT ", NULL, 0, tname, NULL, 4, 0, CHAR, NULL, 0}, 195 {"tty", "TTY", NULL, LJUST, longtname, NULL, 8, 0, CHAR, NULL, 0}, 196 {"ucomm", "UCOMM", NULL, LJUST, ucomm, NULL, MAXCOMLEN, 0, CHAR, NULL, 197 0}, 198 {"uid", "UID", NULL, 0, kvar, NULL, UIDLEN, KOFF(ki_uid), UINT, 199 UIDFMT, 0}, 200 {"upr", "UPR", NULL, 0, kvar, NULL, 3, KOFF(ki_pri.pri_user), UCHAR, 201 "d", 0}, 202 {"user", "USER", NULL, LJUST|DSIZ, uname, s_uname, USERLEN, 0, CHAR, 203 NULL, 0}, 204 {"usrpri", "", "upr", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 205 {"vsize", "", "vsz", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 206 {"vsz", "VSZ", NULL, 0, vsize, NULL, 5, 0, CHAR, NULL, 0}, 207 {"wchan", "WCHAN", NULL, LJUST, wchan, NULL, 6, 0, CHAR, NULL, 0}, 208 {"xstat", "XSTAT", NULL, 0, kvar, NULL, 4, KOFF(ki_xstat), USHORT, 209 "x", 0}, 210 {"", NULL, NULL, 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 211 }; 212 213 void 214 showkey(void) 215 { 216 VAR *v; 217 int i; 218 const char *p, *sep; 219 220 i = 0; 221 sep = ""; 222 for (v = var; *(p = v->name); ++v) { 223 int len = strlen(p); 224 if (termwidth && (i += len + 1) > termwidth) { 225 i = len; 226 sep = "\n"; 227 } 228 (void) printf("%s%s", sep, p); 229 sep = " "; 230 } 231 (void) printf("\n"); 232 } 233 234 void 235 parsefmt(const char *p) 236 { 237 static struct varent *vtail; 238 char *tempstr, *tempstr1; 239 240 #define FMTSEP " \t,\n" 241 tempstr1 = tempstr = strdup(p); 242 while (tempstr && *tempstr) { 243 char *cp; 244 VAR *v; 245 struct varent *vent; 246 247 /* 248 * If an item contains an equals sign, it specifies a column 249 * header, may contain embedded separator characters and 250 * is always the last item. 251 */ 252 if (tempstr[strcspn(tempstr, "="FMTSEP)] != '=') 253 while ((cp = strsep(&tempstr, FMTSEP)) != NULL && 254 *cp == '\0') 255 /* void */; 256 else { 257 cp = tempstr; 258 tempstr = NULL; 259 } 260 if (cp == NULL || !(v = findvar(cp))) 261 continue; 262 if ((vent = malloc(sizeof(struct varent))) == NULL) 263 errx(1, "malloc failed"); 264 vent->var = malloc(sizeof(*vent->var)); 265 if (vent->var == NULL) 266 errx(1, "malloc failed"); 267 memcpy(vent->var, v, sizeof(*vent->var)); 268 vent->next = NULL; 269 if (vhead == NULL) 270 vhead = vtail = vent; 271 else { 272 vtail->next = vent; 273 vtail = vent; 274 } 275 } 276 free(tempstr1); 277 if (!vhead) 278 errx(1, "no valid keywords"); 279 } 280 281 static VAR * 282 findvar(char *p) 283 { 284 VAR *v, key; 285 char *hp; 286 287 hp = strchr(p, '='); 288 if (hp) 289 *hp++ = '\0'; 290 291 key.name = p; 292 v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp); 293 294 if (v && v->alias) { 295 if (hp) { 296 warnx("%s: illegal keyword specification", p); 297 eval = 1; 298 } 299 parsefmt(v->alias); 300 return ((VAR *)NULL); 301 } 302 if (!v) { 303 warnx("%s: keyword not found", p); 304 eval = 1; 305 } else if (hp) 306 v->header = strdup(hp); 307 return (v); 308 } 309 310 static int 311 vcmp(const void *a, const void *b) 312 { 313 return (strcmp(((const VAR *)a)->name, ((const VAR *)b)->name)); 314 } 315