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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)keyword.c 8.5 (Berkeley) 4/2/94 34 * $FreeBSD: src/bin/ps/keyword.c,v 1.24.2.3 2002/10/10 20:05:32 jmallett Exp $ 35 * $DragonFly: src/bin/ps/keyword.c,v 1.6 2003/07/25 05:28:53 dillon Exp $ 36 */ 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/ucred.h> 44 #include <sys/user.h> 45 46 #include <err.h> 47 #include <errno.h> 48 #include <stddef.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <utmp.h> 53 54 #include "ps.h" 55 56 static VAR *findvar __P((char *)); 57 static int vcmp __P((const void *, const void *)); 58 59 #ifdef NOTINUSE 60 int utime(), stime(), ixrss(), idrss(), isrss(); 61 {{"utime"}, "UTIME", USER, utime, NULL, 4}, 62 {{"stime"}, "STIME", USER, stime, NULL, 4}, 63 {{"ixrss"}, "IXRSS", USER, ixrss, NULL, 4}, 64 {{"idrss"}, "IDRSS", USER, idrss, NULL, 4}, 65 {{"isrss"}, "ISRSS", USER, isrss, NULL, 4}, 66 #endif 67 68 /* Compute offset in common structures. */ 69 #define POFF(x) offsetof(struct proc, x) 70 #define TOFF(x) offsetof(struct thread, x) 71 #define EOFF(x) offsetof(struct eproc, x) 72 #define UOFF(x) offsetof(struct usave, x) 73 #define ROFF(x) offsetof(struct rusage, x) 74 75 #define UIDFMT "u" 76 #define UIDLEN 5 77 #define PIDFMT "d" 78 #define PIDLEN 5 79 #define USERLEN UT_NAMESIZE 80 81 VAR var[] = { 82 {"%cpu", "%CPU", NULL, 0, pcpu, NULL, 4}, 83 {"%mem", "%MEM", NULL, 0, pmem, NULL, 4}, 84 {"acflag", "ACFLG", 85 NULL, 0, pvar, NULL, 3, POFF(p_acflag), USHORT, "x"}, 86 {"acflg", "", "acflag"}, 87 {"blocked", "", "sigmask"}, 88 {"caught", "", "sigcatch"}, 89 {"command", "COMMAND", NULL, COMM|LJUST|USER, command, NULL, 16}, 90 {"cpu", "CPU", NULL, 0, pvar, NULL, 3, POFF(p_estcpu), UINT, "d"}, 91 {"lastcpu", "C", NULL, 0, pvar, NULL, 3, EOFF(e_cpuid), UINT, "d"}, 92 {"cputime", "", "time"}, 93 {"f", "F", NULL, 0, pvar, NULL, 7, POFF(p_flag), INT, "x"}, 94 {"flags", "", "f"}, 95 {"ignored", "", "sigignore"}, 96 {"inblk", "INBLK", 97 NULL, USER, rvar, NULL, 4, ROFF(ru_inblock), LONG, "ld"}, 98 {"inblock", "", "inblk"}, 99 {"jobc", "JOBC", NULL, 0, evar, NULL, 4, EOFF(e_jobc), SHORT, "d"}, 100 {"ktrace", "KTRACE", 101 NULL, 0, pvar, NULL, 8, POFF(p_traceflag), INT, "x"}, 102 {"ktracep", "KTRACEP", 103 NULL, 0, pvar, NULL, 8, POFF(p_tracep), LONG, "lx"}, 104 {"lim", "LIM", NULL, 0, maxrss, NULL, 5}, 105 {"login", "LOGIN", NULL, LJUST, logname, NULL, MAXLOGNAME-1}, 106 {"logname", "", "login"}, 107 {"lstart", "STARTED", NULL, LJUST|USER, lstarted, NULL, 28}, 108 {"majflt", "MAJFLT", 109 NULL, USER, rvar, NULL, 4, ROFF(ru_majflt), LONG, "ld"}, 110 {"minflt", "MINFLT", 111 NULL, USER, rvar, NULL, 4, ROFF(ru_minflt), LONG, "ld"}, 112 {"msgrcv", "MSGRCV", 113 NULL, USER, rvar, NULL, 4, ROFF(ru_msgrcv), LONG, "ld"}, 114 {"msgsnd", "MSGSND", 115 NULL, USER, rvar, NULL, 4, ROFF(ru_msgsnd), LONG, "ld"}, 116 {"ni", "", "nice"}, 117 {"nice", "NI", NULL, 0, pnice, NULL, 3 }, 118 {"nivcsw", "NIVCSW", 119 NULL, USER, rvar, NULL, 5, ROFF(ru_nivcsw), LONG, "ld"}, 120 {"nsignals", "", "nsigs"}, 121 {"nsigs", "NSIGS", 122 NULL, USER, rvar, NULL, 4, ROFF(ru_nsignals), LONG, "ld"}, 123 {"nswap", "NSWAP", 124 NULL, USER, rvar, NULL, 4, ROFF(ru_nswap), LONG, "ld"}, 125 {"nvcsw", "NVCSW", 126 NULL, USER, rvar, NULL, 5, ROFF(ru_nvcsw), LONG, "ld"}, 127 {"nwchan", "WCHAN", NULL, 0, tvar, NULL, 8, TOFF(td_wchan), KPTR, "lx"}, 128 {"oublk", "OUBLK", 129 NULL, USER, rvar, NULL, 4, ROFF(ru_oublock), LONG, "ld"}, 130 {"oublock", "", "oublk"}, 131 {"p_ru", "P_RU", NULL, 0, pvar, NULL, 6, POFF(p_ru), KPTR, "lx"}, 132 {"paddr", "PADDR", NULL, 0, evar, NULL, 8, EOFF(e_paddr), KPTR, "lx"}, 133 {"pagein", "PAGEIN", NULL, USER, pagein, NULL, 6}, 134 {"pcpu", "", "%cpu"}, 135 {"pending", "", "sig"}, 136 {"pgid", "PGID", 137 NULL, 0, evar, NULL, PIDLEN, EOFF(e_pgid), UINT, PIDFMT}, 138 {"pid", "PID", NULL, 0, pvar, NULL, PIDLEN, POFF(p_pid), UINT, PIDFMT}, 139 {"pmem", "", "%mem"}, 140 {"ppid", "PPID", 141 NULL, 0, evar, NULL, PIDLEN, EOFF(e_ppid), UINT, PIDFMT}, 142 {"pri", "PRI", NULL, 0, pri, NULL, 3}, 143 {"re", "RE", NULL, 0, pvar, NULL, 3, POFF(p_swtime), UINT, "d"}, 144 {"rgid", "RGID", NULL, 0, evar, NULL, UIDLEN, EOFF(e_ucred.cr_rgid), 145 UINT, UIDFMT}, 146 {"rlink", "RLINK", 147 NULL, 0, pvar, NULL, 8, POFF(p_procq.tqe_prev), KPTR, "lx"}, 148 {"rss", "RSS", NULL, 0, p_rssize, NULL, 4}, 149 {"rssize", "", "rsz"}, 150 {"rsz", "RSZ", NULL, 0, rssize, NULL, 4}, 151 {"rtprio", "RTPRIO", NULL, 0, rtprior, NULL, 7, POFF(p_rtprio)}, 152 {"ruid", "RUID", NULL, 0, evar, NULL, UIDLEN, EOFF(e_ucred.cr_ruid), 153 UINT, UIDFMT}, 154 {"ruser", "RUSER", NULL, LJUST|DSIZ, runame, s_runame, USERLEN}, 155 {"sess", "SESS", NULL, 0, evar, NULL, 6, EOFF(e_sess), KPTR, "lx"}, 156 {"sig", "PENDING", NULL, 0, pvar, NULL, 8, POFF(p_siglist), INT, "x"}, 157 {"sigcatch", "CAUGHT", 158 NULL, 0, evar, NULL, 8, EOFF(e_procsig.ps_sigcatch), UINT, "x"}, 159 {"sigignore", "IGNORED", 160 NULL, 0, evar, NULL, 8, EOFF(e_procsig.ps_sigignore), UINT, "x"}, 161 {"sigmask", "BLOCKED", 162 NULL, 0, pvar, NULL, 8, POFF(p_sigmask), UINT, "x"}, 163 {"sl", "SL", NULL, 0, pvar, NULL, 3, POFF(p_slptime), UINT, "d"}, 164 {"start", "STARTED", NULL, LJUST|USER, started, NULL, 7}, 165 {"stat", "", "state"}, 166 {"state", "STAT", NULL, 0, state, NULL, 4}, 167 {"svgid", "SVGID", NULL, 0, 168 evar, NULL, UIDLEN, EOFF(e_ucred.cr_svgid), UINT, UIDFMT}, 169 {"svuid", "SVUID", NULL, 0, 170 evar, NULL, UIDLEN, EOFF(e_ucred.cr_svuid), UINT, UIDFMT}, 171 {"tdev", "TDEV", NULL, 0, tdev, NULL, 4}, 172 {"time", "TIME", NULL, USER, cputime, NULL, 9}, 173 {"tpgid", "TPGID", 174 NULL, 0, evar, NULL, 4, EOFF(e_tpgid), UINT, PIDFMT}, 175 {"tsess", "TSESS", NULL, 0, evar, NULL, 6, EOFF(e_tsess), KPTR, "lx"}, 176 {"tsiz", "TSIZ", NULL, 0, tsize, NULL, 4}, 177 {"tt", "TT ", NULL, 0, tname, NULL, 4}, 178 {"tty", "TTY", NULL, LJUST, longtname, NULL, 8}, 179 {"ucomm", "UCOMM", NULL, LJUST, ucomm, NULL, MAXCOMLEN}, 180 {"uid", "UID", NULL, 0, evar, NULL, UIDLEN, EOFF(e_ucred.cr_uid), 181 UINT, UIDFMT}, 182 {"user", "USER", NULL, LJUST|DSIZ, uname, s_uname, USERLEN}, 183 {"vsize", "", "vsz"}, 184 {"vsz", "VSZ", NULL, 0, vsize, NULL, 5}, 185 {"wchan", "WCHAN", NULL, LJUST, wchan, NULL, 6}, 186 {"xstat", "XSTAT", NULL, 0, pvar, NULL, 4, POFF(p_xstat), USHORT, "x"}, 187 {""}, 188 }; 189 190 void 191 showkey() 192 { 193 VAR *v; 194 int i; 195 char *p, *sep; 196 197 i = 0; 198 sep = ""; 199 for (v = var; *(p = v->name); ++v) { 200 int len = strlen(p); 201 if (termwidth && (i += len + 1) > termwidth) { 202 i = len; 203 sep = "\n"; 204 } 205 (void) printf("%s%s", sep, p); 206 sep = " "; 207 } 208 (void) printf("\n"); 209 } 210 211 void 212 parsefmt(p) 213 char *p; 214 { 215 static struct varent *vtail; 216 217 #define FMTSEP " \t,\n" 218 while (p && *p) { 219 char *cp; 220 VAR *v; 221 struct varent *vent; 222 223 while ((cp = strsep(&p, FMTSEP)) != NULL && *cp == '\0') 224 /* void */; 225 if (cp == NULL || !(v = findvar(cp))) 226 continue; 227 if ((vent = malloc(sizeof(struct varent))) == NULL) 228 err(1, NULL); 229 vent->var = v; 230 vent->next = NULL; 231 if (vhead == NULL) 232 vhead = vtail = vent; 233 else { 234 vtail->next = vent; 235 vtail = vent; 236 } 237 } 238 if (!vhead) { 239 warnx("no valid keywords; valid keywords:"); 240 showkey(); 241 exit(1); 242 } 243 } 244 245 static VAR * 246 findvar(p) 247 char *p; 248 { 249 VAR *v, key; 250 char *hp; 251 int vcmp(); 252 253 hp = strchr(p, '='); 254 if (hp) 255 *hp++ = '\0'; 256 257 key.name = p; 258 v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp); 259 260 if (v && v->alias) { 261 if (hp) { 262 warnx("%s: illegal keyword specification", p); 263 eval = 1; 264 } 265 parsefmt(v->alias); 266 return ((VAR *)NULL); 267 } 268 if (!v) { 269 warnx("%s: keyword not found", p); 270 eval = 1; 271 } else if (hp) 272 v->header = hp; 273 return (v); 274 } 275 276 static int 277 vcmp(a, b) 278 const void *a, *b; 279 { 280 return (strcmp(((VAR *)a)->name, ((VAR *)b)->name)); 281 } 282