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