Home | History | Annotate | Line # | Download | only in ps
keyword.c revision 1.41
      1 /*	$NetBSD: keyword.c,v 1.41 2004/07/28 07:48:22 simonb Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1990, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)keyword.c	8.5 (Berkeley) 4/2/94";
     36 #else
     37 __RCSID("$NetBSD: keyword.c,v 1.41 2004/07/28 07:48:22 simonb Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #include <sys/param.h>
     42 #include <sys/time.h>
     43 #include <sys/lwp.h>
     44 #include <sys/proc.h>
     45 #include <sys/resource.h>
     46 #include <sys/sysctl.h>
     47 #include <sys/ucred.h>
     48 
     49 #include <err.h>
     50 #include <errno.h>
     51 #include <kvm.h>
     52 #include <stddef.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <signal.h>
     57 
     58 #include "ps.h"
     59 
     60 static VAR *findvar(const char *);
     61 static int  vcmp(const void *, const void *);
     62 
     63 #if 0 	/* kernel doesn't calculate these */
     64 	PUVAR("idrss", "IDRSS", 0, p_uru_idrss, UINT64, PRIu64),
     65 	PUVAR("isrss", "ISRSS", 0, p_uru_isrss, UINT64, PRId64),
     66 	PUVAR("ixrss", "IXRSS", 0, p_uru_ixrss, UINT64, PRId64),
     67 	PUVAR("maxrss", "MAXRSS", 0, p_uru_maxrss, UINT64, PRIu64),
     68 #endif
     69 
     70 /* Compute offset in common structures. */
     71 #define	POFF(x)	offsetof(struct kinfo_proc2, x)
     72 #define	LOFF(x)	offsetof(struct kinfo_lwp, x)
     73 
     74 #define	UIDFMT	"u"
     75 #define	UID(n1, n2, off) \
     76 	{ n1, n2, 0, pvar, POFF(off), UINT32, UIDFMT }
     77 #define	GID(n1, n2, off)	UID(n1, n2, off)
     78 
     79 #define	PIDFMT	"d"
     80 #define	PID(n1, n2, off) \
     81 	{ n1, n2, 0, pvar, POFF(off), INT32, PIDFMT }
     82 
     83 #define	LVAR(n1, n2, fl, off, type, fmt) \
     84 	{ n1, n2, (fl) | LWP, pvar, LOFF(off), type, fmt }
     85 #define	PVAR(n1, n2, fl, off, type, fmt) \
     86 	{ n1, n2, (fl) | 0, pvar, POFF(off), type, fmt }
     87 #define	PUVAR(n1, n2, fl, off, type, fmt) \
     88 	{ n1, n2, (fl) | UAREA, pvar, POFF(off), type, fmt }
     89 
     90 /* NB: table must be sorted, in vi use:
     91  *	:/^VAR/,/end_sort/! sort -t\" +1
     92  * breaking long lines just makes the sort harder
     93  */
     94 VAR var[] = {
     95 	{"%cpu", "%CPU", 0, pcpu, 0, PCPU},
     96 	{"%mem", "%MEM", 0, pmem, POFF(p_vm_rssize), INT32},
     97 	PVAR("acflag", "ACFLG", 0, p_acflag, USHORT, "x"),
     98 	{"acflg", "acflag", ALIAS},
     99 	{"args", "command", ALIAS},
    100 	{"blocked", "sigmask", ALIAS},
    101 	{"caught", "sigcatch", ALIAS},
    102 	{"comm", "COMMAND", COMM|ARGV0|LJUST, command},
    103 	{"command", "COMMAND", COMM|LJUST, command},
    104 	PVAR("cpu", "CPU", 0, p_estcpu, UINT, "u"),
    105 	{"cputime", "time", ALIAS},
    106 	{"ctime", "CTIME", 0, putimeval, POFF(p_uctime_sec), TIMEVAL},
    107 	GID("egid", "EGID", p_gid),
    108 	{"egroup", "EGROUP", LJUST, gname},
    109 	{"etime", "ELAPSED", 0, elapsed, POFF(p_ustart_sec), TIMEVAL},
    110 	UID("euid", "EUID", p_uid),
    111 	{"euser", "EUSER", LJUST, uname},
    112 	PVAR("f", "F", 0, p_flag, INT, "x"),
    113 	{"flags", "f", ALIAS},
    114 	GID("gid", "GID", p_gid),
    115 	{"group", "GROUP", LJUST, gname},
    116 	{"groupnames", "GROUPNAMES", LJUST, groupnames},
    117 	{"groups", "GROUPS", LJUST, groups},
    118 	LVAR("holdcnt", "HOLDCNT", 0, l_holdcnt, INT, "d"),
    119 	{"ignored", "sigignore", ALIAS},
    120 	PUVAR("inblk", "INBLK", 0, p_uru_inblock, UINT64, PRIu64),
    121 	{"inblock", "inblk", ALIAS},
    122 	PVAR("jobc", "JOBC", 0, p_jobc, SHORT, "d"),
    123 	PVAR("ktrace", "KTRACE", 0, p_traceflag, INT, "x"),
    124 /*XXX*/	PVAR("ktracep", "KTRACEP", 0, p_tracep, KPTR, PRIx64),
    125 	LVAR("lid", "LID", 0, l_lid, INT32, "d"),
    126 	{"lim", "LIM", 0, maxrss},
    127 	{"login", "LOGIN", LJUST, logname},
    128 	{"logname", "login", ALIAS},
    129 	{"lstart", "STARTED", LJUST, lstarted, POFF(p_ustart_sec), UINT32},
    130 	{"lstate", "STAT", LJUST|LWP, lstate},
    131 	PUVAR("majflt", "MAJFLT", 0, p_uru_majflt, UINT64, PRIu64),
    132 	PUVAR("minflt", "MINFLT", 0, p_uru_minflt, UINT64, PRIu64),
    133 	PUVAR("msgrcv", "MSGRCV", 0, p_uru_msgrcv, UINT64, PRIu64),
    134 	PUVAR("msgsnd", "MSGSND", 0, p_uru_msgsnd, UINT64, PRIu64),
    135 	{"ni", "nice", ALIAS},
    136 	{"nice", "NI", 0, pnice, POFF(p_nice), UCHAR},
    137 	PUVAR("nivcsw", "NIVCSW", 0, p_uru_nivcsw, UINT64, PRIu64),
    138 	PVAR("nlwp", "NLWP", 0, p_nlwps, UINT64, PRId64),
    139 	{"nsignals", "nsigs", ALIAS},
    140 	PUVAR("nsigs", "NSIGS", 0, p_uru_nsignals, UINT64, PRIu64),
    141 	PUVAR("nswap", "NSWAP", 0, p_uru_nswap, UINT64, PRIu64),
    142 	PUVAR("nvcsw", "NVCSW", 0, p_uru_nvcsw, UINT64, PRIu64),
    143 /*XXX*/	LVAR("nwchan", "WCHAN", 0, l_wchan, KPTR, PRIx64),
    144 	PUVAR("oublk", "OUBLK", 0, p_uru_oublock, UINT64, PRIu64),
    145 	{"oublock", "oublk", ALIAS},
    146 /*XXX*/	PVAR("p_ru", "P_RU", 0, p_ru, KPTR, PRIx64),
    147 /*XXX*/	PVAR("paddr", "PADDR", 0, p_paddr, KPTR, PRIx64),
    148 	PUVAR("pagein", "PAGEIN", 0, p_uru_majflt, UINT64, PRIu64),
    149 	{"pcpu", "%cpu", ALIAS},
    150 	{"pending", "sig", ALIAS},
    151 	PID("pgid", "PGID", p__pgid),
    152 	PID("pid", "PID", p_pid),
    153 	{"pmem", "%mem", ALIAS},
    154 	PID("ppid", "PPID", p_ppid),
    155 	{"pri", "PRI", LWP, pri},
    156 	LVAR("re", "RE", INF127, l_swtime, UINT, "u"),
    157 	GID("rgid", "RGID", p_rgid),
    158 	{"rgroup", "RGROUP", LJUST, rgname},
    159 /*XXX*/	LVAR("rlink", "RLINK", 0, l_back, KPTR, PRIx64),
    160 	PVAR("rlwp", "RLWP", 0, p_nrlwps, UINT64, PRId64),
    161 	{"rss", "RSS", 0, p_rssize, POFF(p_vm_rssize), INT32},
    162 	{"rssize", "rsz", ALIAS},
    163 	{"rsz", "RSZ", 0, rssize, POFF(p_vm_rssize), INT32},
    164 	UID("ruid", "RUID", p_ruid),
    165 	{"ruser", "RUSER", LJUST, runame},
    166 	PVAR("sess", "SESS", 0, p_sess, KPTR24, PRIx64),
    167 	PID("sid", "SID", p_sid),
    168 	PVAR("sig", "PENDING", 0, p_siglist, SIGLIST, "s"),
    169 	PVAR("sigcatch", "CAUGHT", 0, p_sigcatch, SIGLIST, "s"),
    170 	PVAR("sigignore", "IGNORED", 0, p_sigignore, SIGLIST, "s"),
    171 	PVAR("sigmask", "BLOCKED", 0, p_sigmask, SIGLIST, "s"),
    172 	LVAR("sl", "SL", INF127, l_slptime, UINT, "u"),
    173 	{"start", "STARTED", 0, started, POFF(p_ustart_sec), UINT32},
    174 	{"stat", "state", ALIAS},
    175 	{"state", "STAT", LJUST, state},
    176 	{"stime", "STIME", 0, putimeval, POFF(p_ustime_sec), TIMEVAL},
    177 	GID("svgid", "SVGID", p_svgid),
    178 	{"svgroup", "SVGROUP", LJUST, svgname},
    179 	UID("svuid", "SVUID", p_svuid),
    180 	{"svuser", "SVUSER", LJUST, svuname},
    181 	/* "tdev" is UINT32, but we do this for sorting purposes */
    182 	{"tdev", "TDEV", 0, tdev, POFF(p_tdev), INT32},
    183 	{"time", "TIME", 0, cputime, 0, CPUTIME},
    184 	PID("tpgid", "TPGID", p_tpgid),
    185 	PVAR("tsess", "TSESS", 0, p_tsess, KPTR, PRIx64),
    186 	{"tsiz", "TSIZ", 0, tsize, POFF(p_vm_tsize), INT32},
    187 	{"tt", "TT", LJUST, tname, POFF(p_tdev), INT32},
    188 	{"tty", "TT", LJUST, longtname, POFF(p_tdev), INT32},
    189 	{"ucomm", "UCOMM", LJUST, ucomm},
    190 	UID("uid", "UID", p_uid),
    191 	LVAR("upr", "UPR", 0, l_usrpri, UCHAR, "u"),
    192 	{"user", "USER", LJUST, uname},
    193 	{"usrpri", "upr", ALIAS},
    194 	{"utime", "UTIME", 0, putimeval, POFF(p_uutime_sec), TIMEVAL},
    195 	{"vsize", "vsz", ALIAS},
    196 	{"vsz", "VSZ", 0, vsize, 0, VSIZE},
    197 	{"wchan", "WCHAN", LJUST|LWP, wchan},
    198 	PVAR("xstat", "XSTAT", 0, p_xstat, USHORT, "x"),
    199 /* "zzzz" end_sort */
    200 	{""},
    201 };
    202 
    203 void
    204 showkey(void)
    205 {
    206 	VAR *v;
    207 	int i;
    208 	char *p, *sep;
    209 
    210 	i = 0;
    211 	sep = "";
    212 	for (v = var; *(p = v->name); ++v) {
    213 		int len = strlen(p);
    214 		if (termwidth && (i += len + 1) > termwidth) {
    215 			i = len;
    216 			sep = "\n";
    217 		}
    218 		(void)printf("%s%s", sep, p);
    219 		sep = " ";
    220 	}
    221 	(void)printf("\n");
    222 }
    223 
    224 static void
    225 parsevarlist(const char *pp, struct varent **head, struct varent **tail)
    226 {
    227 	char *p;
    228 
    229 	/* dup to avoid zapping arguments, can't free because it
    230 	   might contain a header. */
    231 	p = strdup(pp);
    232 
    233 #define	FMTSEP	" \t,\n"
    234 	while (p && *p) {
    235 		char *cp;
    236 		VAR *v;
    237 		struct varent *vent;
    238 
    239 		while ((cp = strsep(&p, FMTSEP)) != NULL && *cp == '\0')
    240 			/* void */;
    241 		if (cp == NULL || !(v = findvar(cp)))
    242 			continue;
    243 		if ((vent = malloc(sizeof(struct varent))) == NULL)
    244 			err(1, NULL);
    245 		vent->var = v;
    246 		vent->next = NULL;
    247 		if (*head == NULL)
    248 			*head = vent;
    249 		else
    250 			(*tail)->next = vent;
    251 		*tail = vent;
    252 	}
    253 	if (!*head)
    254 		errx(1, "no valid keywords");
    255 }
    256 
    257 void
    258 parsefmt(const char *p)
    259 {
    260 	static struct varent *vtail;
    261 
    262 	parsevarlist(p, &vhead, &vtail);
    263 }
    264 
    265 void
    266 parsesort(const char *p)
    267 {
    268 	static struct varent *sorttail;
    269 
    270 	parsevarlist(p, &sorthead, &sorttail);
    271 }
    272 
    273 static VAR *
    274 findvar(const char *p)
    275 {
    276 	VAR *v;
    277 	char *hp;
    278 
    279 	hp = strchr(p, '=');
    280 	if (hp)
    281 		*hp++ = '\0';
    282 
    283 	v = bsearch(p, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
    284 	if (v && v->flag & ALIAS)
    285 		v = findvar(v->header);
    286 	if (!v) {
    287 		warnx("%s: keyword not found", p);
    288 		eval = 1;
    289 		return NULL;
    290 	}
    291 
    292 	if (v && hp)
    293 		v->header = hp;
    294 	return v;
    295 }
    296 
    297 static int
    298 vcmp(const void *a, const void *b)
    299 {
    300         return strcmp(a, ((VAR *)b)->name);
    301 }
    302