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