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