1 /* $OpenBSD: keyword.c,v 1.52 2022/02/25 18:05:49 rob Exp $ */ 2 /* $NetBSD: keyword.c,v 1.12.6.1 1996/05/30 21:25:13 cgd Exp $ */ 3 4 /*- 5 * Copyright (c) 1990, 1993, 1994 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/types.h> 34 #include <sys/time.h> 35 #include <sys/resource.h> 36 37 #include <err.h> 38 #include <errno.h> 39 #include <stddef.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 44 #include "ps.h" 45 46 #include <sys/ucred.h> 47 #include <sys/sysctl.h> 48 49 int needheader; 50 51 static VAR *findvar(char *); 52 static int vcmp(const void *, const void *); 53 54 #ifdef NOTINUSE 55 int utime(), stime(), ixrss(), idrss(), isrss(); 56 {{"utime"}, "UTIME", USER, utime, 4}, 57 {{"stime"}, "STIME", USER, stime, 4}, 58 {{"ixrss"}, "IXRSS", USER, ixrss, 4}, 59 {{"idrss"}, "IDRSS", USER, idrss, 4}, 60 {{"isrss"}, "ISRSS", USER, isrss, 4}, 61 #endif 62 63 /* Compute offset in common structures. */ 64 #define POFF(x) offsetof(struct kinfo_proc, x) 65 66 #define UIDFMT "u" 67 #define UIDLEN 5 68 #define UID(n1, n2, fn, off) \ 69 { n1, n2, NULL, 0, fn, UIDLEN, 0, off, UINT32, UIDFMT } 70 #define GID(n1, n2, fn, off) UID(n1, n2, fn, off) 71 72 #define PIDFMT "d" 73 #define PIDLEN 5 74 #define PID(n1, n2, fn, off) \ 75 { n1, n2, NULL, 0, fn, PIDLEN, 0, off, INT32, PIDFMT } 76 77 #define TIDFMT "d" 78 #define TIDLEN 7 79 #define TID(n1, n2, fn, off) \ 80 { n1, n2, NULL, 0, fn, TIDLEN, 0, off, INT32, TIDFMT } 81 82 #define USERLEN 8 83 #define CWDLEN 40 84 #define UCOMMLEN (sizeof(((struct kinfo_proc *)NULL)->p_comm) - 1) 85 #define WCHANLEN (sizeof(((struct kinfo_proc *)NULL)->p_wmesg) - 1) 86 87 /* Bit types must match their respective entries in struct kinfo_proc */ 88 /* Entries must be sorted in lexical ascending order! */ 89 VAR var[] = { 90 {"%cpu", "%CPU", NULL, NLIST, pcpu, 4}, 91 {"%mem", "%MEM", NULL, NLIST, pmem, 4}, 92 {"acflag", "ACFLG", NULL, 0, pvar, 3, 0, POFF(p_acflag), UINT32, "x"}, 93 {"acflg", "", "acflag"}, 94 {"args", "", "command"}, 95 {"blocked", "", "sigmask"}, 96 {"caught", "", "sigcatch"}, 97 {"comm", "COMMAND", "ucomm"}, 98 {"command", "COMMAND", NULL, COMM|LJUST|USER, command, 16}, 99 {"cpu", "CPU", NULL, 0, pvar, 3, 0, POFF(p_estcpu), UINT32, "d"}, 100 {"cpuid", "CPUID", NULL, 0, pvar, 8, 0, POFF(p_cpuid), UINT64, "lld"}, 101 {"cputime", "", "time"}, 102 {"cwd", "CWD", NULL, LJUST, curwd, CWDLEN}, 103 {"dsiz", "DSIZ", NULL, 0, dsize, 4}, 104 {"etime", "ELAPSED", NULL, USER, elapsed, 12}, 105 {"f", "F", NULL, 0, pvar, 7, 0, POFF(p_flag), INT32, "x"}, 106 {"flags", "", "f"}, 107 GID("gid", "GID", pvar, POFF(p_gid)), 108 {"group", "GROUP", NULL, LJUST, gname, USERLEN}, 109 {"ignored", "", "sigignore"}, 110 {"inblk", "INBLK", NULL, USER, pvar, 4, 0, POFF(p_uru_inblock), UINT64, "lld"}, 111 {"inblock", "", "inblk"}, 112 {"jobc", "JOBC", NULL, 0, pvar, 4, 0, POFF(p_jobc), INT16, "d"}, 113 {"ktrace", "KTRACE", NULL, 0, pvar, 8, 0, POFF(p_traceflag), INT32, "x"}, 114 /* XXX */ 115 {"ktracep", "KTRACEP", NULL, 0, pvar, PTRWIDTH, 0, POFF(p_tracep), UINT64, "llx"}, 116 {"lim", "LIM", NULL, 0, maxrss, 5}, 117 {"login", "LOGIN", NULL, LJUST, logname, LOGIN_NAME_MAX}, 118 {"logname", "", "login"}, 119 {"lstart", "STARTED", NULL, LJUST|USER, lstarted, 28}, 120 {"majflt", "MAJFLT", NULL, USER, pvar, 4, 0, POFF(p_uru_majflt), UINT64, "lld"}, 121 {"maxrss", "MAXRSS", NULL, USER, pvar, 4, 0, POFF(p_uru_maxrss), UINT64, "lld"}, 122 {"minflt", "MINFLT", NULL, USER, pvar, 4, 0, POFF(p_uru_minflt), UINT64, "lld"}, 123 {"msgrcv", "MSGRCV", NULL, USER, pvar, 4, 0, POFF(p_uru_msgrcv), UINT64, "lld"}, 124 {"msgsnd", "MSGSND", NULL, USER, pvar, 4, 0, POFF(p_uru_msgsnd), UINT64, "lld"}, 125 {"ni", "", "nice"}, 126 {"nice", "NI", NULL, 0, pnice, 3}, 127 {"nivcsw", "NIVCSW", NULL, USER, pvar, 5, 0, POFF(p_uru_nivcsw), UINT64, "lld"}, 128 {"nsignals", "", "nsigs"}, 129 {"nsigs", "NSIGS", NULL, USER, pvar, 4, 0, POFF(p_uru_nsignals), UINT64, "lld"}, 130 {"nswap", "NSWAP", NULL, USER, pvar, 4, 0, POFF(p_uru_nswap), UINT64, "lld"}, 131 {"nvcsw", "NVCSW", NULL, USER, pvar, 5, 0, POFF(p_uru_nvcsw), UINT64, "lld"}, 132 /* XXX */ 133 {"nwchan", "WCHAN", NULL, 0, pvar, PTRWIDTH, 0, POFF(p_wchan), UINT64, "llx"}, 134 {"oublk", "OUBLK", NULL, USER, pvar, 4, 0, POFF(p_uru_oublock), UINT64, "lld"}, 135 {"oublock", "", "oublk"}, 136 /* XXX */ 137 {"p_ru", "P_RU", NULL, 0, pvar, PTRWIDTH, 0, POFF(p_ru), UINT64, "llx"}, 138 /* XXX */ 139 {"paddr", "PADDR", NULL, 0, pvar, PTRWIDTH, 0, POFF(p_paddr), UINT64, "llx"}, 140 {"pagein", "PAGEIN", NULL, USER, pagein, 6}, 141 {"pcpu", "", "%cpu"}, 142 {"pending", "", "sig"}, 143 PID("pgid", "PGID", pvar, POFF(p__pgid)), 144 PID("pid", "PID", pvar, POFF(p_pid)), 145 {"pledge", "PLEDGE", NULL, LJUST|NLIST, printpledge, 64}, 146 {"pmem", "", "%mem"}, 147 PID("ppid", "PPID", pvar, POFF(p_ppid)), 148 {"pri", "PRI", NULL, 0, pri, 3}, 149 {"procflags", "PROCF", NULL, 0, pvar, 7, 0, POFF(p_psflags), INT32, "x"}, 150 {"re", "RE", NULL, INF127, pvar, 3, 0, POFF(p_swtime), UINT32, "u"}, 151 GID("rgid", "RGID", pvar, POFF(p_rgid)), 152 /* XXX */ 153 {"rgroup", "RGROUP", NULL, LJUST, rgname, USERLEN}, 154 {"rlink", "RLINK", NULL, 0, pvar, 8, 0, POFF(p_back), UINT64, "llx"}, 155 {"rss", "RSS", NULL, 0, p_rssize, 5}, 156 {"rssize", "", "rsz"}, 157 {"rsz", "RSZ", NULL, 0, rssize, 4}, 158 {"rtable", "RTABLE", NULL, 0, pvar, 0, 0, POFF(p_rtableid), INT32, "d"}, 159 UID("ruid", "RUID", pvar, POFF(p_ruid)), 160 {"ruser", "RUSER", NULL, LJUST, runame, USERLEN}, 161 {"sess", "SESS", NULL, 0, pvar, PTRWIDTH, 0, POFF(p_sess), UINT64, "llx"}, 162 {"sig", "PENDING", NULL, 0, pvar, 8, 0, POFF(p_siglist), INT32, "x"}, 163 {"sigcatch", "CAUGHT", NULL, 0, pvar, 8, 0, POFF(p_sigcatch), UINT32, "x"}, 164 {"sigignore", "IGNORED", 165 NULL, 0, pvar, 8, 0, POFF(p_sigignore), UINT32, "x"}, 166 {"sigmask", "BLOCKED", NULL, 0, pvar, 8, 0, POFF(p_sigmask), UINT32, "x"}, 167 {"sl", "SL", NULL, INF127, pvar, 3, 0, POFF(p_slptime), UINT32, "u"}, 168 {"ssiz", "SSIZ", NULL, 0, ssize, 4}, 169 {"start", "STARTED", NULL, LJUST|USER, started, 8}, 170 {"stat", "", "state"}, 171 {"state", "STAT", NULL, LJUST|NLIST, printstate, 6}, 172 {"supgid", "SUPGID", NULL, LJUST, supgid, 64}, 173 {"supgrp", "SUPGRP", NULL, LJUST, supgrp, 64}, 174 GID("svgid", "SVGID", pvar, POFF(p_svgid)), 175 UID("svuid", "SVUID", pvar, POFF(p_svuid)), 176 {"tdev", "TDEV", NULL, 0, tdev, 4}, 177 TID("tid", "TID", pvar, POFF(p_tid)), 178 {"time", "TIME", NULL, USER, cputime, 9}, 179 PID("tpgid", "TPGID", pvar, POFF(p_tpgid)), 180 {"tsess", "TSESS", NULL, 0, pvar, PTRWIDTH, 0, POFF(p_tsess), UINT64, "llx"}, 181 {"tsiz", "TSIZ", NULL, 0, tsize, 4}, 182 {"tt", "TT", NULL, LJUST, tname, 3}, 183 {"tty", "TTY", NULL, LJUST, longtname, 8}, 184 {"ucomm", "UCOMM", NULL, LJUST, ucomm, UCOMMLEN}, 185 UID("uid", "UID", pvar, POFF(p_uid)), 186 {"upr", "UPR", NULL, 0, pvar, 3, 0, POFF(p_usrpri), UINT8, "d"}, 187 {"user", "USER", NULL, LJUST, euname, USERLEN}, 188 {"usrpri", "", "upr"}, 189 {"vsize", "", "vsz"}, 190 {"vsz", "VSZ", NULL, 0, vsize, 5}, 191 {"wchan", "WCHAN", NULL, LJUST, wchan, WCHANLEN}, 192 {"xstat", "XSTAT", NULL, 0, pvar, 4, 0, POFF(p_xstat), UINT16, "x"}, 193 {""}, 194 }; 195 196 void 197 showkey(void) 198 { 199 VAR *v; 200 int i; 201 char *p, *sep; 202 203 i = 0; 204 sep = ""; 205 for (v = var; *(p = v->name); ++v) { 206 int len = strlen(p); 207 if (termwidth && (i += len + 1) > termwidth) { 208 i = len; 209 sep = "\n"; 210 } 211 (void) printf("%s%s", sep, p); 212 sep = " "; 213 } 214 (void) printf("\n"); 215 } 216 217 void 218 parsefmt(char *p) 219 { 220 static struct varent *vtail; 221 222 #define FMTSEP " \t,\n" 223 while (p && *p) { 224 char *cp; 225 VAR *v; 226 struct varent *vent; 227 228 while ((cp = strsep(&p, FMTSEP)) != NULL && *cp == '\0') 229 /* void */; 230 if (!cp) 231 break; 232 if (!(v = findvar(cp)) || v->parsed == 1) 233 continue; 234 v->parsed = 1; 235 if ((vent = malloc(sizeof(struct varent))) == NULL) 236 err(1, NULL); 237 vent->var = v; 238 vent->next = NULL; 239 if (vhead == NULL) 240 vhead = vtail = vent; 241 else { 242 vtail->next = vent; 243 vtail = vent; 244 } 245 needheader |= v->header[0] != '\0'; 246 } 247 if (!vhead) 248 errx(1, "no valid keywords"); 249 } 250 251 static VAR * 252 findvar(char *p) 253 { 254 VAR *v, key; 255 char *hp; 256 257 key.name = p; 258 259 hp = strchr(p, '='); 260 if (hp) 261 *hp++ = '\0'; 262 263 aliased: 264 key.name = p; 265 v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp); 266 267 if (v && v->alias) { 268 p = v->alias; 269 if (hp == NULL && v->header[0] != '\0') 270 hp = v->header; 271 goto aliased; 272 } 273 if (!v) { 274 warnx("%s: keyword not found", p); 275 eval = 1; 276 return (NULL); 277 } 278 if (hp) 279 v->header = hp; 280 return (v); 281 } 282 283 static int 284 vcmp(const void *a, const void *b) 285 { 286 return (strcmp(((VAR *)a)->name, ((VAR *)b)->name)); 287 } 288