Home | History | Annotate | Line # | Download | only in ps
      1 /*	$NetBSD: keyword.c,v 1.60 2021/09/15 13:16:57 christos 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.60 2021/09/15 13:16:57 christos 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 <ctype.h>
     53 #include <stddef.h>
     54 #include <stdio.h>
     55 #include <stdlib.h>
     56 #include <string.h>
     57 #include <signal.h>
     58 #include <util.h>
     59 
     60 #include "ps.h"
     61 
     62 static VAR *findvar(const char *);
     63 static int  vcmp(const void *, const void *);
     64 
     65 /* Compute offset in common structures. */
     66 #define	POFF(x)	offsetof(struct kinfo_proc2, x)
     67 #define	LOFF(x)	offsetof(struct kinfo_lwp, x)
     68 
     69 #define	UIDFMT	"u"
     70 #define	UID(n1, n2, of) \
     71 	{ .name = n1, .header = n2, .flag = 0, .oproc = pvar, \
     72 	  .off = POFF(of), .type = UINT32, .fmt = UIDFMT }
     73 #define	GID(n1, n2, off)	UID(n1, n2, off)
     74 
     75 #define	PIDFMT	"d"
     76 #define	PID(n1, n2, of) \
     77 	{ .name = n1, .header = n2, .flag = 0, .oproc = pvar, \
     78 	  .off = POFF(of), .type = INT32, .fmt = PIDFMT }
     79 
     80 #define	LVAR(n1, n2, fl, of, ty, fm) \
     81 	{ .name = n1, .header = n2, .flag = (fl) | LWP, .oproc = pvar, \
     82 	  .off = LOFF(of), .type = ty, .fmt = fm }
     83 #define	PVAR(n1, n2, fl, of, ty, fm) \
     84 	{ .name = n1, .header = n2, .flag = (fl) | 0, .oproc = pvar, \
     85 	  .off = POFF(of), .type = ty, .fmt = fm }
     86 #define	PUVAR(n1, n2, fl, of, ty, fm) \
     87 	{ .name = n1, .header = n2, .flag = (fl) | UAREA, .oproc = pvar, \
     88 	  .off = POFF(of), .type = ty, .fmt = fm }
     89 #define VAR3(n1, n2, fl) \
     90 	{ .name = n1, .header = n2, .flag = fl }
     91 #define VAR4(n1, n2, fl, op) \
     92 	{ .name = n1, .header = n2, .flag = fl, .oproc = op, }
     93 #define VAR6(n1, n2, fl, op, of, ty) \
     94 	{ .name = n1, .header = n2, .flag = fl, .oproc = op, \
     95 	  .off = of, .type = ty }
     96 
     97 /* NB: table must be sorted, in vi use:
     98  *	:/^VAR/,/end_sort/! sort -t\" +1
     99  * breaking long lines just makes the sort harder
    100  *
    101  * We support all the fields required by P1003.1-2004 (SUSv3), with
    102  * the correct default headers, except for the "tty" field, where the
    103  * standard says the header should be "TT", but we have "TTY".
    104  */
    105 VAR var[] = {
    106 	VAR6("%cpu", "%CPU", 0, pcpu, 0, PCPU),
    107 	VAR6("%mem", "%MEM", 0, pmem, POFF(p_vm_rssize), INT32),
    108 	PVAR("acflag", "ACFLG", 0, p_acflag, PROCACFLAG, "x"),
    109 	VAR3("acflg", "acflag", ALIAS),
    110 	VAR3("args", "command", ALIAS),
    111 	VAR3("blocked", "sigmask", ALIAS),
    112 	VAR3("caught", "sigcatch", ALIAS),
    113 	VAR4("comm", "COMMAND", COMM|ARGV0|LJUST, command),
    114 	VAR4("command", "COMMAND", COMM|LJUST, command),
    115 	PVAR("cpu", "CPU", 0, p_estcpu, UINT, "u"),
    116 	VAR4("cpuid", "CPUID", LWP, cpuid),
    117 	VAR3("cputime", "time", ALIAS),
    118 	VAR6("ctime", "CTIME", 0, putimeval, POFF(p_uctime_sec), TIMEVAL),
    119 	GID("egid", "EGID", p_gid),
    120 	VAR4("egroup", "EGROUP", LJUST, gname),
    121 	VAR4("emul", "EMUL", LJUST, emul),
    122 	VAR6("etime", "ELAPSED", 0, elapsed, POFF(p_ustart_sec), TIMEVAL),
    123 	UID("euid", "EUID", p_uid),
    124 	VAR4("euser", "EUSER", LJUST, usrname),
    125 	PVAR("f", "F", 0, p_flag, PROCFLAG, "x"),
    126 	VAR3("flags", "f", ALIAS),
    127 	GID("gid", "GID", p_gid),
    128 	VAR4("group", "GROUP", LJUST, gname),
    129 	VAR4("groupnames", "GROUPNAMES", LJUST, groupnames),
    130 	VAR4("groups", "GROUPS", LJUST, groups),
    131 	/* holdcnt: unused, left for compat. */
    132 	LVAR("holdcnt", "HOLDCNT", 0, l_holdcnt, INT, "d"),
    133 	PUVAR("idrss", "IDRSS", 0, p_uru_idrss, UINT64, PRIu64),
    134 	VAR3("ignored", "sigignore", ALIAS),
    135 	PUVAR("inblk", "INBLK", 0, p_uru_inblock, UINT64, PRIu64),
    136 	VAR3("inblock", "inblk", ALIAS),
    137 	PUVAR("isrss", "ISRSS", 0, p_uru_isrss, UINT64, PRId64),
    138 	PUVAR("ixrss", "IXRSS", 0, p_uru_ixrss, UINT64, PRId64),
    139 	PVAR("jobc", "JOBC", 0, p_jobc, SHORT, "d"),
    140 	PVAR("ktrace", "KTRACE", 0, p_traceflag, KTRACEFLAG, "x"),
    141 /*XXX*/	PVAR("ktracep", "KTRACEP", 0, p_tracep, KPTR, PRIx64),
    142 	LVAR("laddr", "LADDR", 0, l_laddr, KPTR, PRIx64),
    143 	LVAR("lid", "LID", 0, l_lid, INT32, "d"),
    144 	VAR4("lim", "LIM", 0, maxrss),
    145 	VAR4("lname", "LNAME", LJUST|LWP, lname),
    146 	VAR4("login", "LOGIN", LJUST, logname),
    147 	VAR3("logname", "login", ALIAS),
    148 	VAR6("lstart", "STARTED", LJUST, lstarted, POFF(p_ustart_sec), UINT32),
    149 	VAR4("lstate", "STAT", LJUST|LWP, lstate),
    150 	VAR6("ltime", "LTIME", LWP, lcputime, 0, CPUTIME),
    151 	PUVAR("majflt", "MAJFLT", 0, p_uru_majflt, UINT64, PRIu64),
    152 	PUVAR("maxrss", "MAXRSS", 0, p_uru_maxrss, UINT64, PRIu64),
    153 	PUVAR("minflt", "MINFLT", 0, p_uru_minflt, UINT64, PRIu64),
    154 	PUVAR("msgrcv", "MSGRCV", 0, p_uru_msgrcv, UINT64, PRIu64),
    155 	PUVAR("msgsnd", "MSGSND", 0, p_uru_msgsnd, UINT64, PRIu64),
    156 	VAR3("ni", "nice", ALIAS),
    157 	VAR6("nice", "NI", 0, pnice, POFF(p_nice), UCHAR),
    158 	PUVAR("nivcsw", "NIVCSW", 0, p_uru_nivcsw, UINT64, PRIu64),
    159 	PVAR("nlwp", "NLWP", 0, p_nlwps, UINT64, PRId64),
    160 	VAR3("nsignals", "nsigs", ALIAS),
    161 	PUVAR("nsigs", "NSIGS", 0, p_uru_nsignals, UINT64, PRIu64),
    162 	/* nswap: unused, left for compat. */
    163 	PUVAR("nswap", "NSWAP", 0, p_uru_nswap, UINT64, PRIu64),
    164 	PUVAR("nvcsw", "NVCSW", 0, p_uru_nvcsw, UINT64, PRIu64),
    165 /*XXX*/	LVAR("nwchan", "WCHAN", 0, l_wchan, KPTR, PRIx64),
    166 	PUVAR("oublk", "OUBLK", 0, p_uru_oublock, UINT64, PRIu64),
    167 	VAR3("oublock", "oublk", ALIAS),
    168 /*XXX*/	PVAR("p_ru", "P_RU", 0, p_ru, KPTR, PRIx64),
    169 /*XXX*/	PVAR("paddr", "PADDR", 0, p_paddr, KPTR, PRIx64),
    170 	PUVAR("pagein", "PAGEIN", 0, p_uru_majflt, UINT64, PRIu64),
    171 	VAR3("pcpu", "%cpu", ALIAS),
    172 	VAR3("pending", "sig", ALIAS),
    173 	PID("pgid", "PGID", p__pgid),
    174 	PID("pid", "PID", p_pid),
    175 	VAR3("pmem", "%mem", ALIAS),
    176 	PID("ppid", "PPID", p_ppid),
    177 	VAR4("pri", "PRI", LWP, pri),
    178 	LVAR("re", "RE", INF127, l_swtime, UINT, "u"),
    179 	GID("rgid", "RGID", p_rgid),
    180 	VAR4("rgroup", "RGROUP", LJUST, rgname),
    181 /*XXX*/	LVAR("rlink", "RLINK", 0, l_back, KPTR, PRIx64),
    182 	PVAR("rlwp", "RLWP", 0, p_nrlwps, UINT64, PRId64),
    183 	VAR6("rss", "RSS", 0, p_rssize, POFF(p_vm_rssize), INT32),
    184 	VAR3("rssize", "rsz", ALIAS),
    185 	VAR6("rsz", "RSZ", 0, rssize, POFF(p_vm_rssize), INT32),
    186 	UID("ruid", "RUID", p_ruid),
    187 	VAR4("ruser", "RUSER", LJUST, runame),
    188 	PVAR("sess", "SESS", 0, p_sess, KPTR24, PRIx64),
    189 	PID("sid", "SID", p_sid),
    190 	PVAR("sig", "PENDING", 0, p_siglist, SIGLIST, "s"),
    191 	PVAR("sigcatch", "CAUGHT", 0, p_sigcatch, SIGLIST, "s"),
    192 	PVAR("sigignore", "IGNORED", 0, p_sigignore, SIGLIST, "s"),
    193 	PVAR("sigmask", "BLOCKED", 0, p_sigmask, SIGLIST, "s"),
    194 	LVAR("sl", "SL", INF127, l_slptime, UINT, "u"),
    195 	VAR6("start", "STARTED", 0, started, POFF(p_ustart_sec), UINT32),
    196 	VAR3("stat", "state", ALIAS),
    197 	VAR4("state", "STAT", LJUST, state),
    198 	VAR6("stime", "STIME", 0, putimeval, POFF(p_ustime_sec), TIMEVAL),
    199 	GID("svgid", "SVGID", p_svgid),
    200 	VAR4("svgroup", "SVGROUP", LJUST, svgname),
    201 	UID("svuid", "SVUID", p_svuid),
    202 	VAR4("svuser", "SVUSER", LJUST, svuname),
    203 	/* "tdev" is UINT32, but we do this for sorting purposes */
    204 	VAR6("tdev", "TDEV", 0, tdev, POFF(p_tdev), INT32),
    205 	VAR6("time", "TIME", 0, cputime, 0, CPUTIME),
    206 	PID("tpgid", "TPGID", p_tpgid),
    207 	PVAR("tsess", "TSESS", 0, p_tsess, KPTR, PRIx64),
    208 	VAR6("tsiz", "TSIZ", 0, tsize, POFF(p_vm_tsize), INT32),
    209 	VAR6("tt", "TTY", LJUST, tname, POFF(p_tdev), INT32),
    210 	VAR6("tty", "TTY", LJUST, longtname, POFF(p_tdev), INT32),
    211 	LVAR("uaddr", "UADDR", 0, l_addr, KPTR, PRIx64),
    212 	VAR4("ucomm", "UCOMM", LJUST, ucomm),
    213 	UID("uid", "UID", p_uid),
    214 	LVAR("upr", "UPR", 0, l_usrpri, UCHAR, "u"),
    215 	VAR4("user", "USER", LJUST, usrname),
    216 	VAR3("usrpri", "upr", ALIAS),
    217 	VAR6("utime", "UTIME", 0, putimeval, POFF(p_uutime_sec), TIMEVAL),
    218 	VAR3("vsize", "vsz", ALIAS),
    219 	VAR6("vsz", "VSZ", 0, vsize, 0, VSIZE),
    220 	VAR4("wchan", "WCHAN", LJUST|LWP, wchan),
    221 	PVAR("xstat", "XSTAT", 0, p_xstat, USHORT, "x"),
    222 /* "zzzz" end_sort */
    223 	{ .name = "" },
    224 };
    225 
    226 void
    227 showkey(void)
    228 {
    229 	VAR *v;
    230 	int i;
    231 	const char *p;
    232 	const char *sep;
    233 
    234 	i = 0;
    235 	sep = "";
    236 	for (v = var; *(p = v->name); ++v) {
    237 		int len = strlen(p);
    238 		if (termwidth && (i += len + 1) > termwidth) {
    239 			i = len;
    240 			sep = "\n";
    241 		}
    242 		(void)printf("%s%s", sep, p);
    243 		sep = " ";
    244 	}
    245 	(void)printf("\n");
    246 }
    247 
    248 /*
    249  * Parse the string pp, and insert or append entries to the list
    250  * referenced by listptr.  If pos in non-null and *pos is non-null, then
    251  * *pos specifies where to insert (instead of appending).  If pos is
    252  * non-null, then a new value is returned through *pos referring to the
    253  * last item inserted.
    254  */
    255 static void
    256 parsevarlist(const char *pp, struct varlist *listptr, struct varent **pos)
    257 {
    258 	char *p, *sp, *equalsp;
    259 
    260 	/* dup to avoid zapping arguments.  We will free sp later. */
    261 	p = sp = estrdup(pp);
    262 
    263 	/*
    264 	 * Everything after the first '=' is part of a custom header.
    265 	 * Temporarily replace it with '\0' to simplify other code.
    266 	 */
    267 	equalsp = strchr(p, '=');
    268 	if (equalsp)
    269 	    *equalsp = '\0';
    270 
    271 #define	FMTSEP	" \t,\n"
    272 	while (p && *p) {
    273 		char *cp;
    274 		VAR *v;
    275 		struct varent *vent;
    276 
    277 		/*
    278 		 * skip separators before the first keyword, and
    279 		 * look for the separator after the keyword.
    280 		 */
    281 		for (cp = p; *cp != '\0'; cp++) {
    282 		    p = strpbrk(cp, FMTSEP);
    283 		    if (p != cp)
    284 			break;
    285 		}
    286 		if (*cp == '\0')
    287 		    break;
    288 		/*
    289 		 * Now cp points to the start of a keyword,
    290 		 * and p is NULL or points past the end of the keyword.
    291 		 *
    292 		 * Terminate the keyword with '\0', or reinstate the
    293 		 * '=' that was removed earlier, if appropriate.
    294 		 */
    295 		if (p) {
    296 			*p = '\0';
    297 			p++;
    298 		} else if (equalsp) {
    299 			*equalsp = '=';
    300 		}
    301 
    302 		/*
    303 		 * If findvar() likes the keyword or keyword=header,
    304 		 * add it to our list.  If findvar() doesn't like it,
    305 		 * it will print a warning, so we ignore it.
    306 		 */
    307 		if ((v = findvar(cp)) == NULL)
    308 			continue;
    309 		vent = emalloc(sizeof(*vent));
    310 		vent->var = v;
    311 		if (pos && *pos)
    312 		    SIMPLEQ_INSERT_AFTER(listptr, *pos, vent, next);
    313 		else {
    314 		    SIMPLEQ_INSERT_TAIL(listptr, vent, next);
    315 		}
    316 		if (pos)
    317 		    *pos = vent;
    318 	}
    319  	free(sp);
    320 	if (SIMPLEQ_EMPTY(listptr))
    321 		errx(EXIT_FAILURE, "no valid keywords");
    322 }
    323 
    324 void
    325 parsefmt(const char *p)
    326 {
    327 
    328 	parsevarlist(p, &displaylist, NULL);
    329 }
    330 
    331 void
    332 parsefmt_insert(const char *p, struct varent **pos)
    333 {
    334 
    335 	parsevarlist(p, &displaylist, pos);
    336 }
    337 
    338 void
    339 parsesort(const char *p)
    340 {
    341 
    342 	parsevarlist(p, &sortlist, NULL);
    343 }
    344 
    345 /* Search through a list for an entry with a specified name. */
    346 struct varent *
    347 varlist_find(struct varlist *list, const char *name)
    348 {
    349 	struct varent *vent;
    350 
    351 	SIMPLEQ_FOREACH(vent, list, next) {
    352 		if (strcmp(vent->var->name, name) == 0)
    353 			break;
    354 	}
    355 	return vent;
    356 }
    357 
    358 static VAR *
    359 findvar(const char *p)
    360 {
    361 	VAR *v;
    362 	char *hp;
    363 	char pp[1024];
    364 	strlcpy(pp, p, sizeof(pp));
    365 
    366 	hp = strchr(pp, '=');
    367 	if (hp)
    368 		*hp++ = '\0';
    369 	for (char *dp = pp; *dp; dp++)
    370 		*dp = tolower((unsigned char)*dp);
    371 
    372 	v = bsearch(pp, var, __arraycount(var) - 1, sizeof(*var), vcmp);
    373 	if (v && v->flag & ALIAS)
    374 		v = findvar(v->header);
    375 	if (!v) {
    376 		warnx("%s: keyword not found", p);
    377 		eval = 1;
    378 		return NULL;
    379 	}
    380 
    381 	if (!hp && *p == *pp)
    382 		return v;
    383 
    384 	struct var *newvar = emalloc(sizeof(*newvar));
    385 	*newvar = *v;
    386 
    387 	if (hp) {
    388 		/*
    389 		 * Override the header.
    390 		 *
    391 		 * We need to copy the entry first, and override the
    392 		 * header in the copy, because the same field might be
    393 		 * used multiple times with different headers.  We also
    394 		 * need to strdup the header.
    395 		 */
    396 		newvar->header = estrdup(hp);
    397 		/*
    398 		 * According to P1003.1-2004, if the header text is null,
    399 		 * such as -o user=, the field width will be at least as
    400 		 * wide as the default header text.
    401 		 */
    402 		if (*hp == '\0')
    403 			newvar->width = strlen(v->header);
    404 	}
    405 
    406 	if (*p != *pp)
    407 		newvar->flag |= ALTPR|LJUST;
    408 
    409 	return newvar;
    410 }
    411 
    412 static int
    413 vcmp(const void *a, const void *b)
    414 {
    415         return strcmp(a, ((const VAR *)b)->name);
    416 }
    417