GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bin/ps/keyword.c Lines: 42 46 91.3 %
Date: 2017-11-07 Branches: 33 42 78.6 %

Line Branch Exec Source
1
/*	$OpenBSD: keyword.c,v 1.45 2017/01/24 22:40:09 deraadt 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/param.h>	/* MAXCOMLEN */
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
85
/* Bit types must match their respective entries in struct kinfo_proc */
86
/* Entries must be sorted in lexical ascending order! */
87
VAR var[] = {
88
	{"%cpu", "%CPU", NULL, NLIST, pcpu, 4},
89
	{"%mem", "%MEM", NULL, NLIST, pmem, 4},
90
	{"acflag", "ACFLG", NULL, 0, pvar, 3, 0, POFF(p_acflag), UINT16, "x"},
91
	{"acflg", "", "acflag"},
92
	{"args", "", "command"},
93
	{"blocked", "", "sigmask"},
94
	{"caught", "", "sigcatch"},
95
	{"comm", "COMMAND", "ucomm"},
96
	{"command", "COMMAND", NULL, COMM|LJUST|USER, command, 16},
97
	{"cpu", "CPU", NULL, 0, pvar, 3, 0, POFF(p_estcpu), UINT32, "d"},
98
	{"cpuid", "CPUID", NULL, 0, pvar, 8, 0, POFF(p_cpuid), UINT64, "lld"},
99
	{"cputime", "", "time"},
100
	{"cwd", "CWD", NULL, LJUST, curwd, CWDLEN},
101
	{"dsiz", "DSIZ", NULL, 0, dsize, 4},
102
	{"emul", "EMUL", NULL, LJUST, emulname, KI_EMULNAMELEN - 1},
103
	{"etime", "ELAPSED", NULL, USER, elapsed, 12},
104
	{"f", "F", NULL, 0, pvar, 7, 0, POFF(p_flag), INT32, "x"},
105
	{"flags", "", "f"},
106
	GID("gid", "GID", pvar, POFF(p_gid)),
107
	{"group", "GROUP", NULL, LJUST, gname, USERLEN},
108
	{"ignored", "", "sigignore"},
109
	{"inblk", "INBLK", NULL, USER, pvar, 4, 0, POFF(p_uru_inblock), UINT64, "lld"},
110
	{"inblock", "", "inblk"},
111
	{"jobc", "JOBC", NULL, 0, pvar, 4, 0, POFF(p_jobc), INT16, "d"},
112
	{"ktrace", "KTRACE", NULL, 0, pvar, 8, 0, POFF(p_traceflag), INT32, "x"},
113
	/* XXX */
114
	{"ktracep", "KTRACEP", NULL, 0, pvar, PTRWIDTH, 0, POFF(p_tracep), UINT64, "llx"},
115
	{"lim", "LIM", NULL, 0, maxrss, 5},
116
	{"login", "LOGIN", NULL, LJUST, logname, LOGIN_NAME_MAX},
117
	{"logname", "", "login"},
118
	{"lstart", "STARTED", NULL, LJUST|USER, lstarted, 28},
119
	{"majflt", "MAJFLT", NULL, USER, pvar, 4, 0, POFF(p_uru_majflt), UINT64, "lld"},
120
	{"maxrss", "MAXRSS", NULL, USER, pvar, 4, 0, POFF(p_uru_maxrss), UINT64, "lld"},
121
	{"minflt", "MINFLT", NULL, USER, pvar, 4, 0, POFF(p_uru_minflt), UINT64, "lld"},
122
	{"msgrcv", "MSGRCV", NULL, USER, pvar, 4, 0, POFF(p_uru_msgrcv), UINT64, "lld"},
123
	{"msgsnd", "MSGSND", NULL, USER, pvar, 4, 0, POFF(p_uru_msgsnd), UINT64, "lld"},
124
	{"ni", "", "nice"},
125
	{"nice", "NI", NULL, 0, pnice, 3},
126
	{"nivcsw", "NIVCSW", NULL, USER, pvar, 5, 0, POFF(p_uru_nivcsw), UINT64, "lld"},
127
	{"nsignals", "", "nsigs"},
128
	{"nsigs", "NSIGS", NULL, USER, pvar, 4, 0, POFF(p_uru_nsignals), UINT64, "lld"},
129
	{"nswap", "NSWAP", NULL, USER, pvar, 4, 0, POFF(p_uru_nswap), UINT64, "lld"},
130
	{"nvcsw", "NVCSW", NULL, USER, pvar, 5, 0, POFF(p_uru_nvcsw), UINT64, "lld"},
131
	/* XXX */
132
	{"nwchan", "WCHAN", NULL, 0, pvar, PTRWIDTH, 0, POFF(p_wchan), UINT64, "llx"},
133
	{"oublk", "OUBLK", NULL, USER, pvar, 4, 0, POFF(p_uru_oublock), UINT64, "lld"},
134
	{"oublock", "", "oublk"},
135
	/* XXX */
136
	{"p_ru", "P_RU", NULL, 0, pvar, PTRWIDTH, 0, POFF(p_ru), UINT64, "llx"},
137
	/* XXX */
138
	{"paddr", "PADDR", NULL, 0, pvar, PTRWIDTH, 0, POFF(p_paddr), UINT64, "llx"},
139
	{"pagein", "PAGEIN", NULL, USER, pagein, 6},
140
	{"pcpu", "", "%cpu"},
141
	{"pending", "", "sig"},
142
	PID("pgid", "PGID", pvar, POFF(p__pgid)),
143
	PID("pid", "PID", pvar, POFF(p_pid)),
144
	{"pmem", "", "%mem"},
145
	PID("ppid", "PPID", pvar, POFF(p_ppid)),
146
	{"pri", "PRI", NULL, 0, pri, 3},
147
	{"procflags", "PROCF", NULL, 0, pvar, 7, 0, POFF(p_psflags), INT32, "x"},
148
	{"re", "RE", NULL, INF127, pvar, 3, 0, POFF(p_swtime), UINT32, "u"},
149
	GID("rgid", "RGID", pvar, POFF(p_rgid)),
150
	/* XXX */
151
	{"rgroup", "RGROUP", NULL, LJUST, rgname, USERLEN},
152
	{"rlink", "RLINK", NULL, 0, pvar, 8, 0, POFF(p_back), UINT64, "llx"},
153
	{"rss", "RSS", NULL, 0, p_rssize, 5},
154
	{"rssize", "", "rsz"},
155
	{"rsz", "RSZ", NULL, 0, rssize, 4},
156
	{"rtable", "RTABLE", NULL, 0, pvar, 0, 0, POFF(p_rtableid), INT32, "d"},
157
	UID("ruid", "RUID", pvar, POFF(p_ruid)),
158
	{"ruser", "RUSER", NULL, LJUST, runame, USERLEN},
159
	{"sess", "SESS", NULL, 0, pvar, PTRWIDTH, 0, POFF(p_sess), UINT64, "llx"},
160
	{"sig", "PENDING", NULL, 0, pvar, 8, 0, POFF(p_siglist), INT32, "x"},
161
	{"sigcatch", "CAUGHT", NULL, 0, pvar, 8, 0, POFF(p_sigcatch), UINT32, "x"},
162
	{"sigignore", "IGNORED",
163
		NULL, 0, pvar, 8, 0, POFF(p_sigignore), UINT32, "x"},
164
	{"sigmask", "BLOCKED", NULL, 0, pvar, 8, 0, POFF(p_sigmask), UINT32, "x"},
165
	{"sl", "SL", NULL, INF127, pvar, 3, 0, POFF(p_slptime), UINT32, "u"},
166
	{"ssiz", "SSIZ", NULL, 0, ssize, 4},
167
	{"start", "STARTED", NULL, LJUST|USER, started, 8},
168
	{"stat", "", "state"},
169
	{"state", "STAT", NULL, LJUST|NLIST, printstate, 5},
170
	GID("svgid", "SVGID", pvar, POFF(p_svgid)),
171
	UID("svuid", "SVUID", pvar, POFF(p_svuid)),
172
	{"tdev", "TDEV", NULL, 0, tdev, 4},
173
	TID("tid", "TID", pvar, POFF(p_tid)),
174
	{"time", "TIME", NULL, USER, cputime, 9},
175
	PID("tpgid", "TPGID", pvar, POFF(p_tpgid)),
176
	{"tsess", "TSESS", NULL, 0, pvar, PTRWIDTH, 0, POFF(p_tsess), UINT64, "llx"},
177
	{"tsiz", "TSIZ", NULL, 0, tsize, 4},
178
	{"tt", "TT", NULL, LJUST, tname, 3},
179
	{"tty", "TTY", NULL, LJUST, longtname, 8},
180
	{"ucomm", "UCOMM", NULL, LJUST, ucomm, MAXCOMLEN},
181
	UID("uid", "UID", pvar, POFF(p_uid)),
182
	{"upr", "UPR", NULL, 0, pvar, 3, 0, POFF(p_usrpri), UINT8, "d"},
183
	{"user", "USER", NULL, LJUST, euname, USERLEN},
184
	{"usrpri", "", "upr"},
185
	{"vsize", "", "vsz"},
186
	{"vsz", "VSZ", NULL, 0, vsize, 5},
187
	{"wchan", "WCHAN", NULL, LJUST, wchan, KI_WMESGLEN - 1},
188
	{"xstat", "XSTAT", NULL, 0, pvar, 4, 0, POFF(p_xstat), UINT16, "x"},
189
	{""},
190
};
191
192
void
193
showkey(void)
194
{
195
	VAR *v;
196
	int i;
197
	char *p, *sep;
198
199
	i = 0;
200
	sep = "";
201
4439
	for (v = var; *(p = v->name); ++v) {
202
2185
		int len = strlen(p);
203

4370
		if (termwidth && (i += len + 1) > termwidth) {
204
			i = len;
205
			sep = "\n";
206
92
		}
207
2185
		(void) printf("%s%s", sep, p);
208
		sep = " ";
209
	}
210
23
	(void) printf("\n");
211
23
}
212
213
void
214
parsefmt(char *p)
215
{
216
	static struct varent *vtail;
217
218
#define	FMTSEP	" \t,\n"
219

19433
	while (p && *p) {
220
		char *cp;
221
		VAR *v;
222
		struct varent *vent;
223
224

23172
		while ((cp = strsep(&p, FMTSEP)) != NULL && *cp == '\0')
225
			/* void */;
226
7724
		if (!cp)
227
			break;
228

15425
		if (!(v = findvar(cp)) || v->parsed == 1)
229
483
			continue;
230
7241
		v->parsed = 1;
231
7241
		if ((vent = malloc(sizeof(struct varent))) == NULL)
232
			err(1, NULL);
233
7241
		vent->var = v;
234
7241
		vent->next = NULL;
235
7241
		if (vhead == NULL)
236
3962
			vhead = vtail = vent;
237
		else {
238
3279
			vtail->next = vent;
239
3279
			vtail = vent;
240
		}
241
7241
		needheader |= v->header[0] != '\0';
242
7241
	}
243
3985
	if (!vhead)
244
		errx(1, "no valid keywords");
245
3962
}
246
247
static VAR *
248
findvar(char *p)
249
{
250
15448
	VAR *v, key;
251
	char *hp;
252
253
7724
	key.name = p;
254
255
7724
	hp = strchr(p, '=');
256
7724
	if (hp)
257
		*hp++ = '\0';
258
259
aliased:
260
8331
	key.name = p;
261
8331
	v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
262
263

16639
	if (v && v->alias) {
264
		p = v->alias;
265

1214
		if (hp == NULL && v->header[0] != '\0')
266
170
			hp = v->header;
267
		goto aliased;
268
	}
269
7724
	if (!v) {
270
23
		warnx("%s: keyword not found", p);
271
23
		eval = 1;
272
23
		return (NULL);
273
	}
274
7701
	if (hp)
275
170
		v->header = hp;
276
7701
	return (v);
277
7724
}
278
279
static int
280
vcmp(const void *a, const void *b)
281
{
282
94926
	return (strcmp(((VAR *)a)->name, ((VAR *)b)->name));
283
}