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