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