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. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #if 0 31 #ifndef lint 32 static char sccsid[] = "@(#)keyword.c 8.5 (Berkeley) 4/2/94"; 33 #endif /* not lint */ 34 #endif 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include <sys/param.h> 39 #include <sys/time.h> 40 #include <sys/resource.h> 41 #include <sys/proc.h> 42 #include <sys/sysctl.h> 43 #include <sys/user.h> 44 45 #include <err.h> 46 #include <stddef.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <libxo/xo.h> 51 52 #include "ps.h" 53 54 static VAR *findvar(char *, int, char **header); 55 static int vcmp(const void *, const void *); 56 57 /* Compute offset in common structures. */ 58 #define KOFF(x) offsetof(struct kinfo_proc, x) 59 #define ROFF(x) offsetof(struct rusage, x) 60 61 #define LWPFMT "d" 62 #define NLWPFMT "d" 63 #define UIDFMT "u" 64 #define PIDFMT "d" 65 66 /* PLEASE KEEP THE TABLE BELOW SORTED ALPHABETICALLY!!! */ 67 static VAR var[] = { 68 {"%cpu", "%CPU", NULL, "percent-cpu", 0, pcpu, 0, CHAR, NULL, 0}, 69 {"%mem", "%MEM", NULL, "percent-memory", 0, pmem, 0, CHAR, NULL, 0}, 70 {"acflag", "ACFLG", NULL, "accounting-flag", 0, kvar, KOFF(ki_acflag), 71 USHORT, "x", 0}, 72 {"acflg", "", "acflag", NULL, 0, NULL, 0, CHAR, NULL, 0}, 73 {"args", "COMMAND", NULL, "arguments", COMM|LJUST|USER, arguments, 0, 74 CHAR, NULL, 0}, 75 {"blocked", "", "sigmask", NULL, 0, NULL, 0, CHAR, NULL, 0}, 76 {"caught", "", "sigcatch", NULL, 0, NULL, 0, CHAR, NULL, 0}, 77 {"class", "CLASS", NULL, "login-class", LJUST, loginclass, 0, CHAR, 78 NULL, 0}, 79 {"comm", "COMMAND", NULL, "command", LJUST, ucomm, 0, CHAR, NULL, 0}, 80 {"command", "COMMAND", NULL, "command", COMM|LJUST|USER, command, 0, 81 CHAR, NULL, 0}, 82 {"cow", "COW", NULL, "copy-on-write-faults", 0, kvar, KOFF(ki_cow), 83 UINT, "u", 0}, 84 {"cpu", "CPU", NULL, "cpu-usage", 0, kvar, KOFF(ki_estcpu), UINT, "d", 85 0}, 86 {"cputime", "", "time", NULL, 0, NULL, 0, CHAR, NULL, 0}, 87 {"dsiz", "DSIZ", NULL, "data-size", 0, kvar, KOFF(ki_dsize), PGTOK, 88 "ld", 0}, 89 {"egid", "", "gid", NULL, 0, NULL, 0, CHAR, NULL, 0}, 90 {"egroup", "", "group", NULL, 0, NULL, 0, CHAR, NULL, 0}, 91 {"emul", "EMUL", NULL, "emulation-envirnment", LJUST, emulname, 0, 92 CHAR, NULL, 0}, 93 {"etime", "ELAPSED", NULL, "elapsed-time", USER, elapsed, 0, CHAR, 94 NULL, 0}, 95 {"etimes", "ELAPSED", NULL, "elapsed-times", USER, elapseds, 0, CHAR, 96 NULL, 0}, 97 {"euid", "", "uid", NULL, 0, NULL, 0, CHAR, NULL, 0}, 98 {"f", "F", NULL, "flags", 0, kvar, KOFF(ki_flag), LONG, "lx", 0}, 99 {"f2", "F2", NULL, "flags2", 0, kvar, KOFF(ki_flag2), INT, "08x", 0}, 100 {"fib", "FIB", NULL, "fib", 0, kvar, KOFF(ki_fibnum), INT, "d", 0}, 101 {"flags", "", "f", NULL, 0, NULL, 0, CHAR, NULL, 0}, 102 {"flags2", "", "f2", NULL, 0, NULL, 0, CHAR, NULL, 0}, 103 {"gid", "GID", NULL, "gid", 0, kvar, KOFF(ki_groups), UINT, UIDFMT, 0}, 104 {"group", "GROUP", NULL, "group", LJUST, egroupname, 0, CHAR, NULL, 0}, 105 {"ignored", "", "sigignore", NULL, 0, NULL, 0, CHAR, NULL, 0}, 106 {"inblk", "INBLK", NULL, "read-blocks", USER, rvar, ROFF(ru_inblock), 107 LONG, "ld", 0}, 108 {"inblock", "", "inblk", NULL, 0, NULL, 0, CHAR, NULL, 0}, 109 {"jid", "JID", NULL, "jail-id", 0, kvar, KOFF(ki_jid), INT, "d", 0}, 110 {"jobc", "JOBC", NULL, "job-control-count", 0, kvar, KOFF(ki_jobc), 111 SHORT, "d", 0}, 112 {"ktrace", "KTRACE", NULL, "ktrace", 0, kvar, KOFF(ki_traceflag), INT, 113 "x", 0}, 114 {"label", "LABEL", NULL, "label", LJUST, label, 0, CHAR, NULL, 0}, 115 {"lim", "LIM", NULL, "memory-limit", 0, maxrss, 0, CHAR, NULL, 0}, 116 {"lockname", "LOCK", NULL, "lock-name", LJUST, lockname, 0, CHAR, NULL, 117 0}, 118 {"login", "LOGIN", NULL, "login-name", LJUST, logname, 0, CHAR, NULL, 119 0}, 120 {"logname", "", "login", NULL, 0, NULL, 0, CHAR, NULL, 0}, 121 {"lstart", "STARTED", NULL, "start-time", LJUST|USER, lstarted, 0, 122 CHAR, NULL, 0}, 123 {"lwp", "LWP", NULL, "process-thread-id", 0, kvar, KOFF(ki_tid), UINT, 124 LWPFMT, 0}, 125 {"majflt", "MAJFLT", NULL, "major-faults", USER, rvar, ROFF(ru_majflt), 126 LONG, "ld", 0}, 127 {"minflt", "MINFLT", NULL, "minor-faults", USER, rvar, ROFF(ru_minflt), 128 LONG, "ld", 0}, 129 {"msgrcv", "MSGRCV", NULL, "received-messages", USER, rvar, 130 ROFF(ru_msgrcv), LONG, "ld", 0}, 131 {"msgsnd", "MSGSND", NULL, "sent-messages", USER, rvar, 132 ROFF(ru_msgsnd), LONG, "ld", 0}, 133 {"mwchan", "MWCHAN", NULL, "wait-channel", LJUST, mwchan, 0, CHAR, 134 NULL, 0}, 135 {"ni", "", "nice", NULL, 0, NULL, 0, CHAR, NULL, 0}, 136 {"nice", "NI", NULL, "nice", 0, kvar, KOFF(ki_nice), CHAR, "d", 0}, 137 {"nivcsw", "NIVCSW", NULL, "involuntary-context-switches", USER, rvar, 138 ROFF(ru_nivcsw), LONG, "ld", 0}, 139 {"nlwp", "NLWP", NULL, "threads", 0, kvar, KOFF(ki_numthreads), UINT, 140 NLWPFMT, 0}, 141 {"nsignals", "", "nsigs", NULL, 0, NULL, 0, CHAR, NULL, 0}, 142 {"nsigs", "NSIGS", NULL, "signals-taken", USER, rvar, 143 ROFF(ru_nsignals), LONG, "ld", 0}, 144 {"nswap", "NSWAP", NULL, "swaps", USER, rvar, ROFF(ru_nswap), LONG, 145 "ld", 0}, 146 {"nvcsw", "NVCSW", NULL, "voluntary-context-switches", USER, rvar, 147 ROFF(ru_nvcsw), LONG, "ld", 0}, 148 {"nwchan", "NWCHAN", NULL, "wait-channel-address", LJUST, nwchan, 0, 149 CHAR, NULL, 0}, 150 {"oublk", "OUBLK", NULL, "written-blocks", USER, rvar, 151 ROFF(ru_oublock), LONG, "ld", 0}, 152 {"oublock", "", "oublk", NULL, 0, NULL, 0, CHAR, NULL, 0}, 153 {"paddr", "PADDR", NULL, "process-address", 0, kvar, KOFF(ki_paddr), 154 KPTR, "lx", 0}, 155 {"pagein", "PAGEIN", NULL, "pageins", USER, pagein, 0, CHAR, NULL, 0}, 156 {"pcpu", "", "%cpu", NULL, 0, NULL, 0, CHAR, NULL, 0}, 157 {"pending", "", "sig", NULL, 0, NULL, 0, CHAR, NULL, 0}, 158 {"pgid", "PGID", NULL, "process-group", 0, kvar, KOFF(ki_pgid), UINT, 159 PIDFMT, 0}, 160 {"pid", "PID", NULL, "pid", 0, kvar, KOFF(ki_pid), UINT, PIDFMT, 0}, 161 {"pmem", "", "%mem", NULL, 0, NULL, 0, CHAR, NULL, 0}, 162 {"ppid", "PPID", NULL, "ppid", 0, kvar, KOFF(ki_ppid), UINT, PIDFMT, 0}, 163 {"pri", "PRI", NULL, "priority", 0, pri, 0, CHAR, NULL, 0}, 164 {"re", "RE", NULL, "residency-time", INF127, kvar, KOFF(ki_swtime), 165 UINT, "d", 0}, 166 {"rgid", "RGID", NULL, "real-gid", 0, kvar, KOFF(ki_rgid), UINT, 167 UIDFMT, 0}, 168 {"rgroup", "RGROUP", NULL, "real-group", LJUST, rgroupname, 0, CHAR, 169 NULL, 0}, 170 {"rss", "RSS", NULL, "rss", 0, kvar, KOFF(ki_rssize), PGTOK, "ld", 0}, 171 {"rtprio", "RTPRIO", NULL, "realtime-priority", 0, priorityr, 172 KOFF(ki_pri), CHAR, NULL, 0}, 173 {"ruid", "RUID", NULL, "real-uid", 0, kvar, KOFF(ki_ruid), UINT, 174 UIDFMT, 0}, 175 {"ruser", "RUSER", NULL, "real-user", LJUST, runame, 0, CHAR, NULL, 0}, 176 {"sid", "SID", NULL, "sid", 0, kvar, KOFF(ki_sid), UINT, PIDFMT, 0}, 177 {"sig", "PENDING", NULL, "signals-pending", 0, kvar, KOFF(ki_siglist), 178 INT, "x", 0}, 179 {"sigcatch", "CAUGHT", NULL, "signals-caught", 0, kvar, 180 KOFF(ki_sigcatch), UINT, "x", 0}, 181 {"sigignore", "IGNORED", NULL, "signals-ignored", 0, kvar, 182 KOFF(ki_sigignore), UINT, "x", 0}, 183 {"sigmask", "BLOCKED", NULL, "signal-mask", 0, kvar, KOFF(ki_sigmask), 184 UINT, "x", 0}, 185 {"sl", "SL", NULL, "sleep-time", INF127, kvar, KOFF(ki_slptime), UINT, 186 "d", 0}, 187 {"ssiz", "SSIZ", NULL, "stack-size", 0, kvar, KOFF(ki_ssize), PGTOK, 188 "ld", 0}, 189 {"start", "STARTED", NULL, "start-time", LJUST|USER, started, 0, CHAR, 190 NULL, 0}, 191 {"stat", "", "state", NULL, 0, NULL, 0, CHAR, NULL, 0}, 192 {"state", "STAT", NULL, "state", LJUST, state, 0, CHAR, NULL, 0}, 193 {"svgid", "SVGID", NULL, "saved-gid", 0, kvar, KOFF(ki_svgid), UINT, 194 UIDFMT, 0}, 195 {"svuid", "SVUID", NULL, "saved-uid", 0, kvar, KOFF(ki_svuid), UINT, 196 UIDFMT, 0}, 197 {"systime", "SYSTIME", NULL, "system-time", USER, systime, 0, CHAR, 198 NULL, 0}, 199 {"tdaddr", "TDADDR", NULL, "thread-address", 0, kvar, KOFF(ki_tdaddr), 200 KPTR, "lx", 0}, 201 {"tdev", "TDEV", NULL, "terminal-device", 0, tdev, 0, CHAR, NULL, 0}, 202 {"tdnam", "TDNAM", NULL, "terminal-device-name", LJUST, tdnam, 0, CHAR, 203 NULL, 0}, 204 {"time", "TIME", NULL, "cpu-time", USER, cputime, 0, CHAR, NULL, 0}, 205 {"tpgid", "TPGID", NULL, "terminal-process-gid", 0, kvar, 206 KOFF(ki_tpgid), UINT, PIDFMT, 0}, 207 {"tracer", "TRACER", NULL, "tracer", 0, kvar, KOFF(ki_tracer), UINT, 208 PIDFMT, 0}, 209 {"tsid", "TSID", NULL, "terminal-sid", 0, kvar, KOFF(ki_tsid), UINT, 210 PIDFMT, 0}, 211 {"tsiz", "TSIZ", NULL, "text-size", 0, kvar, KOFF(ki_tsize), PGTOK, 212 "ld", 0}, 213 {"tt", "TT ", NULL, "terminal-name", 0, tname, 0, CHAR, NULL, 0}, 214 {"tty", "TTY", NULL, "tty", LJUST, longtname, 0, CHAR, NULL, 0}, 215 {"ucomm", "UCOMM", NULL, "accounting-name", LJUST, ucomm, 0, CHAR, 216 NULL, 0}, 217 {"uid", "UID", NULL, "uid", 0, kvar, KOFF(ki_uid), UINT, UIDFMT, 0}, 218 {"upr", "UPR", NULL, "user-priority", 0, upr, 0, CHAR, NULL, 0}, 219 {"uprocp", "UPROCP", NULL, "process-address", 0, kvar, KOFF(ki_paddr), 220 KPTR, "lx", 0}, 221 {"user", "USER", NULL, "user", LJUST, uname, 0, CHAR, NULL, 0}, 222 {"usertime", "USERTIME", NULL, "user-time", USER, usertime, 0, CHAR, 223 NULL, 0}, 224 {"usrpri", "", "upr", NULL, 0, NULL, 0, CHAR, NULL, 0}, 225 {"vsize", "", "vsz", NULL, 0, NULL, 0, CHAR, NULL, 0}, 226 {"vsz", "VSZ", NULL, "virtual-size", 0, vsize, 0, CHAR, NULL, 0}, 227 {"wchan", "WCHAN", NULL, "wait-channel", LJUST, wchan, 0, CHAR, NULL, 228 0}, 229 {"xstat", "XSTAT", NULL, "exit-status", 0, kvar, KOFF(ki_xstat), 230 USHORT, "x", 0}, 231 {"", NULL, NULL, NULL, 0, NULL, 0, CHAR, NULL, 0}, 232 }; 233 234 void 235 showkey(void) 236 { 237 VAR *v; 238 int i; 239 const char *p, *sep; 240 241 i = 0; 242 sep = ""; 243 xo_open_list("key"); 244 for (v = var; *(p = v->name); ++v) { 245 int len = strlen(p); 246 if (termwidth && (i += len + 1) > termwidth) { 247 i = len; 248 sep = "\n"; 249 } 250 xo_emit("{P:/%s}{l:key/%s}", sep, p); 251 sep = " "; 252 } 253 xo_emit("\n"); 254 xo_close_list("key"); 255 xo_finish(); 256 } 257 258 void 259 parsefmt(const char *p, int user) 260 { 261 char *tempstr, *tempstr1; 262 263 #define FMTSEP " \t,\n" 264 tempstr1 = tempstr = strdup(p); 265 while (tempstr && *tempstr) { 266 char *cp, *hp; 267 VAR *v; 268 struct varent *vent; 269 270 /* 271 * If an item contains an equals sign, it specifies a column 272 * header, may contain embedded separator characters and 273 * is always the last item. 274 */ 275 if (tempstr[strcspn(tempstr, "="FMTSEP)] != '=') 276 while ((cp = strsep(&tempstr, FMTSEP)) != NULL && 277 *cp == '\0') 278 /* void */; 279 else { 280 cp = tempstr; 281 tempstr = NULL; 282 } 283 if (cp == NULL || !(v = findvar(cp, user, &hp))) 284 continue; 285 if (!user) { 286 /* 287 * If the user is NOT adding this field manually, 288 * get on with our lives if this VAR is already 289 * represented in the list. 290 */ 291 vent = find_varentry(v); 292 if (vent != NULL) 293 continue; 294 } 295 if ((vent = malloc(sizeof(struct varent))) == NULL) 296 errx(1, "malloc failed"); 297 vent->header = v->header; 298 if (hp) { 299 hp = strdup(hp); 300 if (hp) 301 vent->header = hp; 302 } 303 vent->var = malloc(sizeof(*vent->var)); 304 if (vent->var == NULL) 305 errx(1, "malloc failed"); 306 memcpy(vent->var, v, sizeof(*vent->var)); 307 STAILQ_INSERT_TAIL(&varlist, vent, next_ve); 308 } 309 free(tempstr1); 310 if (STAILQ_EMPTY(&varlist)) { 311 warnx("no valid keywords; valid keywords:"); 312 showkey(); 313 exit(1); 314 } 315 } 316 317 static VAR * 318 findvar(char *p, int user, char **header) 319 { 320 size_t rflen; 321 VAR *v, key; 322 char *hp, *realfmt; 323 324 hp = strchr(p, '='); 325 if (hp) 326 *hp++ = '\0'; 327 328 key.name = p; 329 v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp); 330 331 if (v && v->alias) { 332 /* 333 * If the user specified an alternate-header for this 334 * (aliased) format-name, then we need to copy that 335 * alternate-header when making the recursive call to 336 * process the alias. 337 */ 338 if (hp == NULL) 339 parsefmt(v->alias, user); 340 else { 341 /* 342 * XXX - This processing will not be correct for 343 * any alias which expands into a list of format 344 * keywords. Presently there are no aliases 345 * which do that. 346 */ 347 rflen = strlen(v->alias) + strlen(hp) + 2; 348 realfmt = malloc(rflen); 349 if (realfmt == NULL) 350 errx(1, "malloc failed"); 351 snprintf(realfmt, rflen, "%s=%s", v->alias, hp); 352 parsefmt(realfmt, user); 353 free(realfmt); 354 } 355 return ((VAR *)NULL); 356 } 357 if (!v) { 358 warnx("%s: keyword not found", p); 359 eval = 1; 360 } 361 if (header) 362 *header = hp; 363 return (v); 364 } 365 366 static int 367 vcmp(const void *a, const void *b) 368 { 369 return (strcmp(((const VAR *)a)->name, ((const VAR *)b)->name)); 370 } 371