14b88c807SRodney W. Grimes /*- 24b88c807SRodney W. Grimes * Copyright (c) 1990, 1993, 1994 34b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 44b88c807SRodney W. Grimes * 54b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 64b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 74b88c807SRodney W. Grimes * are met: 84b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 94b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 104b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 114b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 124b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 134b88c807SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 144b88c807SRodney W. Grimes * must display the following acknowledgement: 154b88c807SRodney W. Grimes * This product includes software developed by the University of 164b88c807SRodney W. Grimes * California, Berkeley and its contributors. 174b88c807SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 184b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 194b88c807SRodney W. Grimes * without specific prior written permission. 204b88c807SRodney W. Grimes * 214b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 224b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 234b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 244b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 254b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 264b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 274b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 284b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 294b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 304b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 314b88c807SRodney W. Grimes * SUCH DAMAGE. 324b88c807SRodney W. Grimes */ 334b88c807SRodney W. Grimes 344b88c807SRodney W. Grimes #ifndef lint 35aeb7f2b6SSteve Price #if 0 36aeb7f2b6SSteve Price static char sccsid[] = "@(#)keyword.c 8.5 (Berkeley) 4/2/94"; 37aeb7f2b6SSteve Price #else 38aeb7f2b6SSteve Price static const char rcsid[] = 392a456239SPeter Wemm "$FreeBSD$"; 40aeb7f2b6SSteve Price #endif 414b88c807SRodney W. Grimes #endif /* not lint */ 424b88c807SRodney W. Grimes 434b88c807SRodney W. Grimes #include <sys/param.h> 444b88c807SRodney W. Grimes #include <sys/time.h> 454b88c807SRodney W. Grimes #include <sys/resource.h> 464b88c807SRodney W. Grimes #include <sys/proc.h> 47c1cee2f6SSteve Price #include <sys/sysctl.h> 48c1cee2f6SSteve Price #include <sys/ucred.h> 49c1cee2f6SSteve Price #include <sys/user.h> 504b88c807SRodney W. Grimes 514b88c807SRodney W. Grimes #include <err.h> 524b88c807SRodney W. Grimes #include <errno.h> 534b88c807SRodney W. Grimes #include <stddef.h> 544b88c807SRodney W. Grimes #include <stdio.h> 554b88c807SRodney W. Grimes #include <stdlib.h> 564b88c807SRodney W. Grimes #include <string.h> 5732f6553eSAndrey A. Chernov #include <utmp.h> 584b88c807SRodney W. Grimes 594b88c807SRodney W. Grimes #include "ps.h" 604b88c807SRodney W. Grimes 614b88c807SRodney W. Grimes static VAR *findvar __P((char *)); 624b88c807SRodney W. Grimes static int vcmp __P((const void *, const void *)); 634b88c807SRodney W. Grimes 644b88c807SRodney W. Grimes #ifdef NOTINUSE 654b88c807SRodney W. Grimes int utime(), stime(), ixrss(), idrss(), isrss(); 666a2d726bSJordan K. Hubbard {{"utime"}, "UTIME", USER, utime, NULL, 4}, 676a2d726bSJordan K. Hubbard {{"stime"}, "STIME", USER, stime, NULL, 4}, 686a2d726bSJordan K. Hubbard {{"ixrss"}, "IXRSS", USER, ixrss, NULL, 4}, 696a2d726bSJordan K. Hubbard {{"idrss"}, "IDRSS", USER, idrss, NULL, 4}, 706a2d726bSJordan K. Hubbard {{"isrss"}, "ISRSS", USER, isrss, NULL, 4}, 714b88c807SRodney W. Grimes #endif 724b88c807SRodney W. Grimes 734b88c807SRodney W. Grimes /* Compute offset in common structures. */ 741f7d2501SKirk McKusick #define KOFF(x) offsetof(struct kinfo_proc, x) 754b88c807SRodney W. Grimes #define UOFF(x) offsetof(struct usave, x) 764b88c807SRodney W. Grimes #define ROFF(x) offsetof(struct rusage, x) 774b88c807SRodney W. Grimes 784b88c807SRodney W. Grimes #define UIDFMT "u" 794b88c807SRodney W. Grimes #define UIDLEN 5 804b88c807SRodney W. Grimes #define PIDFMT "d" 814b88c807SRodney W. Grimes #define PIDLEN 5 8232f6553eSAndrey A. Chernov #define USERLEN UT_NAMESIZE 834b88c807SRodney W. Grimes 844b88c807SRodney W. Grimes VAR var[] = { 856a2d726bSJordan K. Hubbard {"%cpu", "%CPU", NULL, 0, pcpu, NULL, 4}, 866a2d726bSJordan K. Hubbard {"%mem", "%MEM", NULL, 0, pmem, NULL, 4}, 876a2d726bSJordan K. Hubbard {"acflag", "ACFLG", 881f7d2501SKirk McKusick NULL, 0, kvar, NULL, 3, KOFF(ki_acflag), USHORT, "x"}, 894b88c807SRodney W. Grimes {"acflg", "", "acflag"}, 904b88c807SRodney W. Grimes {"blocked", "", "sigmask"}, 914b88c807SRodney W. Grimes {"caught", "", "sigcatch"}, 926a2d726bSJordan K. Hubbard {"command", "COMMAND", NULL, COMM|LJUST|USER, command, NULL, 16}, 931f7d2501SKirk McKusick {"cpu", "CPU", NULL, 0, kvar, NULL, 3, KOFF(ki_estcpu), UINT, "d"}, 944b88c807SRodney W. Grimes {"cputime", "", "time"}, 951f7d2501SKirk McKusick {"f", "F", NULL, 0, kvar, NULL, 7, KOFF(ki_flag), INT, "x"}, 964b88c807SRodney W. Grimes {"flags", "", "f"}, 974b88c807SRodney W. Grimes {"ignored", "", "sigignore"}, 986a2d726bSJordan K. Hubbard {"inblk", "INBLK", 993929d518SDoug Rabson NULL, USER, rvar, NULL, 4, ROFF(ru_inblock), LONG, "ld"}, 1004b88c807SRodney W. Grimes {"inblock", "", "inblk"}, 1011f7d2501SKirk McKusick {"jobc", "JOBC", NULL, 0, kvar, NULL, 4, KOFF(ki_jobc), SHORT, "d"}, 1026a2d726bSJordan K. Hubbard {"ktrace", "KTRACE", 1031f7d2501SKirk McKusick NULL, 0, kvar, NULL, 8, KOFF(ki_traceflag), INT, "x"}, 1046a2d726bSJordan K. Hubbard {"lim", "LIM", NULL, 0, maxrss, NULL, 5}, 1056a2d726bSJordan K. Hubbard {"login", "LOGIN", NULL, LJUST, logname, NULL, MAXLOGNAME-1}, 1064b88c807SRodney W. Grimes {"logname", "", "login"}, 1076a2d726bSJordan K. Hubbard {"lstart", "STARTED", NULL, LJUST|USER, lstarted, NULL, 28}, 1086a2d726bSJordan K. Hubbard {"majflt", "MAJFLT", 1093929d518SDoug Rabson NULL, USER, rvar, NULL, 4, ROFF(ru_majflt), LONG, "ld"}, 1106a2d726bSJordan K. Hubbard {"minflt", "MINFLT", 1113929d518SDoug Rabson NULL, USER, rvar, NULL, 4, ROFF(ru_minflt), LONG, "ld"}, 112fd5f30bfSJohn Baldwin {"mtxname", "MUTEX", NULL, LJUST, mtxname, NULL, 6}, 1136a2d726bSJordan K. Hubbard {"msgrcv", "MSGRCV", 1143929d518SDoug Rabson NULL, USER, rvar, NULL, 4, ROFF(ru_msgrcv), LONG, "ld"}, 1156a2d726bSJordan K. Hubbard {"msgsnd", "MSGSND", 1163929d518SDoug Rabson NULL, USER, rvar, NULL, 4, ROFF(ru_msgsnd), LONG, "ld"}, 1174b88c807SRodney W. Grimes {"ni", "", "nice"}, 1181f7d2501SKirk McKusick {"nice", "NI", NULL, 0, kvar, NULL, 2, KOFF(ki_nice), CHAR, "d"}, 1196a2d726bSJordan K. Hubbard {"nivcsw", "NIVCSW", 1203929d518SDoug Rabson NULL, USER, rvar, NULL, 5, ROFF(ru_nivcsw), LONG, "ld"}, 1214b88c807SRodney W. Grimes {"nsignals", "", "nsigs"}, 1226a2d726bSJordan K. Hubbard {"nsigs", "NSIGS", 1233929d518SDoug Rabson NULL, USER, rvar, NULL, 4, ROFF(ru_nsignals), LONG, "ld"}, 1246a2d726bSJordan K. Hubbard {"nswap", "NSWAP", 1253929d518SDoug Rabson NULL, USER, rvar, NULL, 4, ROFF(ru_nswap), LONG, "ld"}, 1266a2d726bSJordan K. Hubbard {"nvcsw", "NVCSW", 1273929d518SDoug Rabson NULL, USER, rvar, NULL, 5, ROFF(ru_nvcsw), LONG, "ld"}, 1281f7d2501SKirk McKusick {"nwchan", "WCHAN", NULL, 0, kvar, NULL, 6, KOFF(ki_wchan), KPTR, "lx"}, 1296a2d726bSJordan K. Hubbard {"oublk", "OUBLK", 1303929d518SDoug Rabson NULL, USER, rvar, NULL, 4, ROFF(ru_oublock), LONG, "ld"}, 1314b88c807SRodney W. Grimes {"oublock", "", "oublk"}, 1321f7d2501SKirk McKusick {"paddr", "PADDR", NULL, 0, kvar, NULL, 6, KOFF(ki_paddr), KPTR, "lx"}, 1336a2d726bSJordan K. Hubbard {"pagein", "PAGEIN", NULL, USER, pagein, NULL, 6}, 1344b88c807SRodney W. Grimes {"pcpu", "", "%cpu"}, 1354b88c807SRodney W. Grimes {"pending", "", "sig"}, 1366a2d726bSJordan K. Hubbard {"pgid", "PGID", 1371f7d2501SKirk McKusick NULL, 0, kvar, NULL, PIDLEN, KOFF(ki_pgid), UINT, PIDFMT}, 1381f7d2501SKirk McKusick {"pid", "PID", NULL, 0, kvar, NULL, PIDLEN, KOFF(ki_pid), UINT, PIDFMT}, 1394b88c807SRodney W. Grimes {"pmem", "", "%mem"}, 1406a2d726bSJordan K. Hubbard {"ppid", "PPID", 1411f7d2501SKirk McKusick NULL, 0, kvar, NULL, PIDLEN, KOFF(ki_ppid), UINT, PIDFMT}, 1426a2d726bSJordan K. Hubbard {"pri", "PRI", NULL, 0, pri, NULL, 3}, 1431f7d2501SKirk McKusick {"re", "RE", NULL, 0, kvar, NULL, 3, KOFF(ki_swtime), UINT, "d"}, 1441f7d2501SKirk McKusick {"rgid", "RGID", NULL, 0, kvar, NULL, UIDLEN, KOFF(ki_rgid), 1453929d518SDoug Rabson UINT, UIDFMT}, 1461f7d2501SKirk McKusick {"rtprio", "RTPRIO", NULL, 0, rtprior, NULL, 7, KOFF(ki_rtprio)}, 1471f7d2501SKirk McKusick {"ruid", "RUID", NULL, 0, kvar, NULL, UIDLEN, KOFF(ki_ruid), 1483929d518SDoug Rabson UINT, UIDFMT}, 1496a2d726bSJordan K. Hubbard {"ruser", "RUSER", NULL, LJUST|DSIZ, runame, s_runame, USERLEN}, 1501f7d2501SKirk McKusick {"sig", "PENDING", NULL, 0, kvar, NULL, 8, KOFF(ki_siglist), INT, "x"}, 151d8c85307SJulian Elischer {"sigcatch", "CAUGHT", 1521f7d2501SKirk McKusick NULL, 0, kvar, NULL, 8, KOFF(ki_sigcatch), UINT, "x"}, 153d8c85307SJulian Elischer {"sigignore", "IGNORED", 1541f7d2501SKirk McKusick NULL, 0, kvar, NULL, 8, KOFF(ki_sigignore), UINT, "x"}, 1556a2d726bSJordan K. Hubbard {"sigmask", "BLOCKED", 1561f7d2501SKirk McKusick NULL, 0, kvar, NULL, 8, KOFF(ki_sigmask), UINT, "x"}, 1571f7d2501SKirk McKusick {"sl", "SL", NULL, 0, kvar, NULL, 3, KOFF(ki_slptime), UINT, "d"}, 1586a2d726bSJordan K. Hubbard {"start", "STARTED", NULL, LJUST|USER, started, NULL, 7}, 1594b88c807SRodney W. Grimes {"stat", "", "state"}, 1606a2d726bSJordan K. Hubbard {"state", "STAT", NULL, 0, state, NULL, 4}, 1616a2d726bSJordan K. Hubbard {"svgid", "SVGID", NULL, 0, 1621f7d2501SKirk McKusick kvar, NULL, UIDLEN, KOFF(ki_svgid), UINT, UIDFMT}, 1636a2d726bSJordan K. Hubbard {"svuid", "SVUID", NULL, 0, 1641f7d2501SKirk McKusick kvar, NULL, UIDLEN, KOFF(ki_svuid), UINT, UIDFMT}, 1656a2d726bSJordan K. Hubbard {"tdev", "TDEV", NULL, 0, tdev, NULL, 4}, 1666a2d726bSJordan K. Hubbard {"time", "TIME", NULL, USER, cputime, NULL, 9}, 1676a2d726bSJordan K. Hubbard {"tpgid", "TPGID", 1681f7d2501SKirk McKusick NULL, 0, kvar, NULL, 4, KOFF(ki_tpgid), UINT, PIDFMT}, 1696a2d726bSJordan K. Hubbard {"tsiz", "TSIZ", NULL, 0, tsize, NULL, 4}, 1706a2d726bSJordan K. Hubbard {"tt", "TT ", NULL, 0, tname, NULL, 4}, 1716a2d726bSJordan K. Hubbard {"tty", "TTY", NULL, LJUST, longtname, NULL, 8}, 1726a2d726bSJordan K. Hubbard {"ucomm", "UCOMM", NULL, LJUST, ucomm, NULL, MAXCOMLEN}, 1731f7d2501SKirk McKusick {"uid", "UID", NULL, 0, kvar, NULL, UIDLEN, KOFF(ki_uid), 1743929d518SDoug Rabson UINT, UIDFMT}, 1751f7d2501SKirk McKusick {"upr", "UPR", NULL, 0, kvar, NULL, 3, KOFF(ki_usrpri), CHAR, "d"}, 1766a2d726bSJordan K. Hubbard {"user", "USER", NULL, LJUST|DSIZ, uname, s_uname, USERLEN}, 1774b88c807SRodney W. Grimes {"usrpri", "", "upr"}, 1784b88c807SRodney W. Grimes {"vsize", "", "vsz"}, 1796a2d726bSJordan K. Hubbard {"vsz", "VSZ", NULL, 0, vsize, NULL, 5}, 1806a2d726bSJordan K. Hubbard {"wchan", "WCHAN", NULL, LJUST, wchan, NULL, 6}, 1811f7d2501SKirk McKusick {"xstat", "XSTAT", NULL, 0, kvar, NULL, 4, KOFF(ki_xstat), USHORT, "x"}, 1824b88c807SRodney W. Grimes {""}, 1834b88c807SRodney W. Grimes }; 1844b88c807SRodney W. Grimes 1854b88c807SRodney W. Grimes void 1864b88c807SRodney W. Grimes showkey() 1874b88c807SRodney W. Grimes { 1884b88c807SRodney W. Grimes VAR *v; 1894b88c807SRodney W. Grimes int i; 1904b88c807SRodney W. Grimes char *p, *sep; 1914b88c807SRodney W. Grimes 1924b88c807SRodney W. Grimes i = 0; 1934b88c807SRodney W. Grimes sep = ""; 1944b88c807SRodney W. Grimes for (v = var; *(p = v->name); ++v) { 1954b88c807SRodney W. Grimes int len = strlen(p); 1964b88c807SRodney W. Grimes if (termwidth && (i += len + 1) > termwidth) { 1974b88c807SRodney W. Grimes i = len; 1984b88c807SRodney W. Grimes sep = "\n"; 1994b88c807SRodney W. Grimes } 2004b88c807SRodney W. Grimes (void) printf("%s%s", sep, p); 2014b88c807SRodney W. Grimes sep = " "; 2024b88c807SRodney W. Grimes } 2034b88c807SRodney W. Grimes (void) printf("\n"); 2044b88c807SRodney W. Grimes } 2054b88c807SRodney W. Grimes 2064b88c807SRodney W. Grimes void 2074b88c807SRodney W. Grimes parsefmt(p) 2084b88c807SRodney W. Grimes char *p; 2094b88c807SRodney W. Grimes { 2104b88c807SRodney W. Grimes static struct varent *vtail; 2114b88c807SRodney W. Grimes 2124b88c807SRodney W. Grimes #define FMTSEP " \t,\n" 2134b88c807SRodney W. Grimes while (p && *p) { 2144b88c807SRodney W. Grimes char *cp; 2154b88c807SRodney W. Grimes VAR *v; 2164b88c807SRodney W. Grimes struct varent *vent; 2174b88c807SRodney W. Grimes 2184b88c807SRodney W. Grimes while ((cp = strsep(&p, FMTSEP)) != NULL && *cp == '\0') 2194b88c807SRodney W. Grimes /* void */; 2204b88c807SRodney W. Grimes if (!(v = findvar(cp))) 2214b88c807SRodney W. Grimes continue; 2224b88c807SRodney W. Grimes if ((vent = malloc(sizeof(struct varent))) == NULL) 2234b88c807SRodney W. Grimes err(1, NULL); 2244b88c807SRodney W. Grimes vent->var = v; 2254b88c807SRodney W. Grimes vent->next = NULL; 2264b88c807SRodney W. Grimes if (vhead == NULL) 2274b88c807SRodney W. Grimes vhead = vtail = vent; 2284b88c807SRodney W. Grimes else { 2294b88c807SRodney W. Grimes vtail->next = vent; 2304b88c807SRodney W. Grimes vtail = vent; 2314b88c807SRodney W. Grimes } 2324b88c807SRodney W. Grimes } 2334b88c807SRodney W. Grimes if (!vhead) 2344b88c807SRodney W. Grimes errx(1, "no valid keywords"); 2354b88c807SRodney W. Grimes } 2364b88c807SRodney W. Grimes 2374b88c807SRodney W. Grimes static VAR * 2384b88c807SRodney W. Grimes findvar(p) 2394b88c807SRodney W. Grimes char *p; 2404b88c807SRodney W. Grimes { 2414b88c807SRodney W. Grimes VAR *v, key; 2424b88c807SRodney W. Grimes char *hp; 2434b88c807SRodney W. Grimes int vcmp(); 2444b88c807SRodney W. Grimes 2454b88c807SRodney W. Grimes hp = strchr(p, '='); 2464b88c807SRodney W. Grimes if (hp) 2474b88c807SRodney W. Grimes *hp++ = '\0'; 2484b88c807SRodney W. Grimes 2494b88c807SRodney W. Grimes key.name = p; 2504b88c807SRodney W. Grimes v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp); 2514b88c807SRodney W. Grimes 2524b88c807SRodney W. Grimes if (v && v->alias) { 2534b88c807SRodney W. Grimes if (hp) { 2544b88c807SRodney W. Grimes warnx("%s: illegal keyword specification", p); 2554b88c807SRodney W. Grimes eval = 1; 2564b88c807SRodney W. Grimes } 2574b88c807SRodney W. Grimes parsefmt(v->alias); 2584b88c807SRodney W. Grimes return ((VAR *)NULL); 2594b88c807SRodney W. Grimes } 2604b88c807SRodney W. Grimes if (!v) { 2614b88c807SRodney W. Grimes warnx("%s: keyword not found", p); 2624b88c807SRodney W. Grimes eval = 1; 263e27525d9SSteve Price } else if (hp) 2644b88c807SRodney W. Grimes v->header = hp; 2654b88c807SRodney W. Grimes return (v); 2664b88c807SRodney W. Grimes } 2674b88c807SRodney W. Grimes 2684b88c807SRodney W. Grimes static int 2694b88c807SRodney W. Grimes vcmp(a, b) 2704b88c807SRodney W. Grimes const void *a, *b; 2714b88c807SRodney W. Grimes { 2724b88c807SRodney W. Grimes return (strcmp(((VAR *)a)->name, ((VAR *)b)->name)); 2734b88c807SRodney W. Grimes } 274