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 * 4. 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 51 #include "ps.h" 52 53 static VAR *findvar(char *, int, char **header); 54 static int vcmp(const void *, const void *); 55 56 /* Compute offset in common structures. */ 57 #define KOFF(x) offsetof(struct kinfo_proc, x) 58 #define ROFF(x) offsetof(struct rusage, x) 59 60 #define EMULLEN 13 /* enough for "FreeBSD ELF32" */ 61 #define LWPFMT "d" 62 #define LWPLEN 6 63 #define NLWPFMT "d" 64 #define NLWPLEN 4 65 #define UIDFMT "u" 66 #define UIDLEN 5 67 #define PIDFMT "d" 68 #define PIDLEN 5 69 #define USERLEN (MAXLOGNAME - 1) 70 71 /* PLEASE KEEP THE TABLE BELOW SORTED ALPHABETICALLY!!! */ 72 static VAR var[] = { 73 {"%cpu", "%CPU", NULL, 0, pcpu, NULL, 4, 0, CHAR, NULL, 0}, 74 {"%mem", "%MEM", NULL, 0, pmem, NULL, 4, 0, CHAR, NULL, 0}, 75 {"acflag", "ACFLG", NULL, 0, kvar, NULL, 3, KOFF(ki_acflag), USHORT, 76 "x", 0}, 77 {"acflg", "", "acflag", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 78 {"args", "COMMAND", NULL, COMM|LJUST|USER, arguments, NULL, 16, 0, 79 CHAR, NULL, 0}, 80 {"blocked", "", "sigmask", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 81 {"caught", "", "sigcatch", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 82 {"comm", "COMMAND", NULL, LJUST, ucomm, NULL, MAXCOMLEN, 0, CHAR, 83 NULL, 0}, 84 {"command", "COMMAND", NULL, COMM|LJUST|USER, command, NULL, 16, 0, 85 CHAR, NULL, 0}, 86 {"cpu", "CPU", NULL, 0, kvar, NULL, 3, KOFF(ki_estcpu), UINT, "d", 87 0}, 88 {"cputime", "", "time", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 89 {"emul", "EMUL", NULL, LJUST, emulname, NULL, EMULLEN, 0, CHAR, 90 NULL, 0}, 91 {"etime", "ELAPSED", NULL, USER, elapsed, NULL, 12, 0, CHAR, NULL, 0}, 92 {"f", "F", NULL, 0, kvar, NULL, 7, KOFF(ki_flag), INT, "x", 0}, 93 {"flags", "", "f", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 94 {"ignored", "", "sigignore", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 95 {"inblk", "INBLK", NULL, USER, rvar, NULL, 4, ROFF(ru_inblock), LONG, 96 "ld", 0}, 97 {"inblock", "", "inblk", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 98 {"jid", "JID", NULL, 0, kvar, NULL, 6, KOFF(ki_jid), INT, "d", 0}, 99 {"jobc", "JOBC", NULL, 0, kvar, NULL, 4, KOFF(ki_jobc), SHORT, "d", 100 0}, 101 {"ktrace", "KTRACE", NULL, 0, kvar, NULL, 8, KOFF(ki_traceflag), INT, 102 "x", 0}, 103 {"label", "LABEL", NULL, LJUST|DSIZ, label, s_label, SHRT_MAX, 0, CHAR, 104 NULL, 0}, 105 {"lim", "LIM", NULL, 0, maxrss, NULL, 5, 0, CHAR, NULL, 0}, 106 {"lockname", "LOCK", NULL, LJUST, lockname, NULL, 6, 0, CHAR, NULL, 107 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 {"lwp", "LWP", NULL, 0, kvar, NULL, LWPLEN, KOFF(ki_tid), UINT, 114 LWPFMT, 0}, 115 {"majflt", "MAJFLT", NULL, USER, rvar, NULL, 4, ROFF(ru_majflt), 116 LONG, "ld", 0}, 117 {"minflt", "MINFLT", NULL, USER, rvar, NULL, 4, ROFF(ru_minflt), 118 LONG, "ld", 0}, 119 {"msgrcv", "MSGRCV", NULL, USER, rvar, NULL, 4, ROFF(ru_msgrcv), 120 LONG, "ld", 0}, 121 {"msgsnd", "MSGSND", NULL, USER, rvar, NULL, 4, ROFF(ru_msgsnd), 122 LONG, "ld", 0}, 123 {"mwchan", "MWCHAN", NULL, LJUST, mwchan, NULL, 6, 0, CHAR, NULL, 0}, 124 {"ni", "", "nice", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 125 {"nice", "NI", NULL, 0, kvar, NULL, 2, KOFF(ki_nice), CHAR, "d", 126 0}, 127 {"nivcsw", "NIVCSW", NULL, USER, rvar, NULL, 5, ROFF(ru_nivcsw), 128 LONG, "ld", 0}, 129 {"nlwp", "NLWP", NULL, 0, kvar, NULL, NLWPLEN, KOFF(ki_numthreads), 130 UINT, NLWPFMT, 0}, 131 {"nsignals", "", "nsigs", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 132 {"nsigs", "NSIGS", NULL, USER, rvar, NULL, 4, ROFF(ru_nsignals), 133 LONG, "ld", 0}, 134 {"nswap", "NSWAP", NULL, USER, rvar, NULL, 4, ROFF(ru_nswap), 135 LONG, "ld", 0}, 136 {"nvcsw", "NVCSW", NULL, USER, rvar, NULL, 5, ROFF(ru_nvcsw), 137 LONG, "ld", 0}, 138 {"nwchan", "NWCHAN", NULL, LJUST, nwchan, NULL, 8, 0, CHAR, NULL, 0}, 139 {"oublk", "OUBLK", NULL, USER, rvar, NULL, 4, ROFF(ru_oublock), 140 LONG, "ld", 0}, 141 {"oublock", "", "oublk", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 142 {"paddr", "PADDR", NULL, 0, kvar, NULL, 8, KOFF(ki_paddr), KPTR, 143 "lx", 0}, 144 {"pagein", "PAGEIN", NULL, USER, pagein, NULL, 6, 0, CHAR, NULL, 0}, 145 {"pcpu", "", "%cpu", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 146 {"pending", "", "sig", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 147 {"pgid", "PGID", NULL, 0, kvar, NULL, PIDLEN, KOFF(ki_pgid), UINT, 148 PIDFMT, 0}, 149 {"pid", "PID", NULL, 0, kvar, NULL, PIDLEN, KOFF(ki_pid), UINT, 150 PIDFMT, 0}, 151 {"pmem", "", "%mem", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 152 {"ppid", "PPID", NULL, 0, kvar, NULL, PIDLEN, KOFF(ki_ppid), UINT, 153 PIDFMT, 0}, 154 {"pri", "PRI", NULL, 0, pri, NULL, 3, 0, CHAR, NULL, 0}, 155 {"re", "RE", NULL, INF127, kvar, NULL, 3, KOFF(ki_swtime), UINT, "d", 156 0}, 157 {"rgid", "RGID", NULL, 0, kvar, NULL, UIDLEN, KOFF(ki_rgid), 158 UINT, UIDFMT, 0}, 159 {"rgroup", "RGROUP", NULL, LJUST|DSIZ, rgroupname, s_rgroupname, 160 USERLEN, 0, CHAR, NULL, 0}, 161 {"rss", "RSS", NULL, 0, kvar, NULL, 5, KOFF(ki_rssize), PGTOK, "ld", 0}, 162 {"rtprio", "RTPRIO", NULL, 0, priorityr, NULL, 7, KOFF(ki_pri), CHAR, 163 NULL, 0}, 164 {"ruid", "RUID", NULL, 0, kvar, NULL, UIDLEN, KOFF(ki_ruid), 165 UINT, UIDFMT, 0}, 166 {"ruser", "RUSER", NULL, LJUST|DSIZ, runame, s_runame, USERLEN, 167 0, CHAR, NULL, 0}, 168 {"sid", "SID", NULL, 0, kvar, NULL, PIDLEN, KOFF(ki_sid), UINT, 169 PIDFMT, 0}, 170 {"sig", "PENDING", NULL, 0, kvar, NULL, 8, KOFF(ki_siglist), INT, 171 "x", 0}, 172 {"sigcatch", "CAUGHT", NULL, 0, kvar, NULL, 8, KOFF(ki_sigcatch), 173 UINT, "x", 0}, 174 {"sigignore", "IGNORED", NULL, 0, kvar, NULL, 8, KOFF(ki_sigignore), 175 UINT, "x", 0}, 176 {"sigmask", "BLOCKED", NULL, 0, kvar, NULL, 8, KOFF(ki_sigmask), 177 UINT, "x", 0}, 178 {"sl", "SL", NULL, INF127, kvar, NULL, 3, KOFF(ki_slptime), UINT, "d", 179 0}, 180 {"start", "STARTED", NULL, LJUST|USER, started, NULL, 7, 0, CHAR, NULL, 181 0}, 182 {"stat", "", "state", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 183 {"state", "STAT", NULL, 0, state, NULL, 4, 0, CHAR, NULL, 0}, 184 {"svgid", "SVGID", NULL, 0, kvar, NULL, UIDLEN, KOFF(ki_svgid), 185 UINT, UIDFMT, 0}, 186 {"svuid", "SVUID", NULL, 0, kvar, NULL, UIDLEN, KOFF(ki_svuid), 187 UINT, UIDFMT, 0}, 188 {"tdev", "TDEV", NULL, 0, tdev, NULL, 4, 0, CHAR, NULL, 0}, 189 {"tdnam", "TDNAM", NULL, LJUST, tdnam, NULL, COMMLEN, 0, CHAR, NULL, 0}, 190 {"time", "TIME", NULL, USER, cputime, NULL, 9, 0, CHAR, NULL, 0}, 191 {"tpgid", "TPGID", NULL, 0, kvar, NULL, 4, KOFF(ki_tpgid), UINT, 192 PIDFMT, 0}, 193 {"tsid", "TSID", NULL, 0, kvar, NULL, PIDLEN, KOFF(ki_tsid), UINT, 194 PIDFMT, 0}, 195 {"tsiz", "TSIZ", NULL, 0, kvar, NULL, 4, KOFF(ki_tsize), PGTOK, "ld", 0}, 196 {"tt", "TT ", NULL, 0, tname, NULL, 4, 0, CHAR, NULL, 0}, 197 {"tty", "TTY", NULL, LJUST, longtname, NULL, 8, 0, CHAR, NULL, 0}, 198 {"ucomm", "UCOMM", NULL, LJUST, ucomm, NULL, MAXCOMLEN, 0, CHAR, NULL, 199 0}, 200 {"uid", "UID", NULL, 0, kvar, NULL, UIDLEN, KOFF(ki_uid), UINT, 201 UIDFMT, 0}, 202 {"upr", "UPR", NULL, 0, upr, NULL, 3, 0, CHAR, NULL, 0}, 203 {"uprocp", "UPROCP", NULL, 0, kvar, NULL, 8, KOFF(ki_paddr), KPTR, 204 "lx", 0}, 205 {"user", "USER", NULL, LJUST|DSIZ, uname, s_uname, USERLEN, 0, CHAR, 206 NULL, 0}, 207 {"usrpri", "", "upr", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 208 {"vsize", "", "vsz", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 209 {"vsz", "VSZ", NULL, 0, vsize, NULL, 5, 0, CHAR, NULL, 0}, 210 {"wchan", "WCHAN", NULL, LJUST, wchan, NULL, 6, 0, CHAR, NULL, 0}, 211 {"xstat", "XSTAT", NULL, 0, kvar, NULL, 4, KOFF(ki_xstat), USHORT, 212 "x", 0}, 213 {"", NULL, NULL, 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, 214 }; 215 216 void 217 showkey(void) 218 { 219 VAR *v; 220 int i; 221 const char *p, *sep; 222 223 i = 0; 224 sep = ""; 225 for (v = var; *(p = v->name); ++v) { 226 int len = strlen(p); 227 if (termwidth && (i += len + 1) > termwidth) { 228 i = len; 229 sep = "\n"; 230 } 231 (void) printf("%s%s", sep, p); 232 sep = " "; 233 } 234 (void) printf("\n"); 235 } 236 237 void 238 parsefmt(const char *p, int user) 239 { 240 char *tempstr, *tempstr1; 241 242 #define FMTSEP " \t,\n" 243 tempstr1 = tempstr = strdup(p); 244 while (tempstr && *tempstr) { 245 char *cp, *hp; 246 VAR *v; 247 struct varent *vent; 248 249 /* 250 * If an item contains an equals sign, it specifies a column 251 * header, may contain embedded separator characters and 252 * is always the last item. 253 */ 254 if (tempstr[strcspn(tempstr, "="FMTSEP)] != '=') 255 while ((cp = strsep(&tempstr, FMTSEP)) != NULL && 256 *cp == '\0') 257 /* void */; 258 else { 259 cp = tempstr; 260 tempstr = NULL; 261 } 262 if (cp == NULL || !(v = findvar(cp, user, &hp))) 263 continue; 264 if (!user) { 265 /* 266 * If the user is NOT adding this field manually, 267 * get on with our lives if this VAR is already 268 * represented in the list. 269 */ 270 vent = find_varentry(v); 271 if (vent != NULL) 272 continue; 273 } 274 if ((vent = malloc(sizeof(struct varent))) == NULL) 275 errx(1, "malloc failed"); 276 vent->header = v->header; 277 if (hp) { 278 hp = strdup(hp); 279 if (hp) 280 vent->header = hp; 281 } 282 vent->var = malloc(sizeof(*vent->var)); 283 if (vent->var == NULL) 284 errx(1, "malloc failed"); 285 memcpy(vent->var, v, sizeof(*vent->var)); 286 STAILQ_INSERT_TAIL(&varlist, vent, next_ve); 287 } 288 free(tempstr1); 289 if (STAILQ_EMPTY(&varlist)) { 290 warnx("no valid keywords; valid keywords:"); 291 showkey(); 292 exit(1); 293 } 294 } 295 296 static VAR * 297 findvar(char *p, int user, char **header) 298 { 299 size_t rflen; 300 VAR *v, key; 301 char *hp, *realfmt; 302 303 hp = strchr(p, '='); 304 if (hp) 305 *hp++ = '\0'; 306 307 key.name = p; 308 v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp); 309 310 if (v && v->alias) { 311 /* 312 * If the user specified an alternate-header for this 313 * (aliased) format-name, then we need to copy that 314 * alternate-header when making the recursive call to 315 * process the alias. 316 */ 317 if (hp == NULL) 318 parsefmt(v->alias, user); 319 else { 320 /* 321 * XXX - This processing will not be correct for 322 * any alias which expands into a list of format 323 * keywords. Presently there are no aliases 324 * which do that. 325 */ 326 rflen = strlen(v->alias) + strlen(hp) + 2; 327 realfmt = malloc(rflen); 328 snprintf(realfmt, rflen, "%s=%s", v->alias, hp); 329 parsefmt(realfmt, user); 330 } 331 return ((VAR *)NULL); 332 } 333 if (!v) { 334 warnx("%s: keyword not found", p); 335 eval = 1; 336 } 337 if (header) 338 *header = hp; 339 return (v); 340 } 341 342 static int 343 vcmp(const void *a, const void *b) 344 { 345 return (strcmp(((const VAR *)a)->name, ((const VAR *)b)->name)); 346 } 347