GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bin/ps/keyword.c Lines: 49 52 94.2 %
Date: 2016-12-06 Branches: 32 40 80.0 %

Line Branch Exec Source
1
/*	$OpenBSD: keyword.c,v 1.43 2015/12/30 14:59:10 tedu 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
#include <sys/proc.h>
37
38
#include <err.h>
39
#include <errno.h>
40
#include <stddef.h>
41
#include <stdio.h>
42
#include <stdlib.h>
43
#include <string.h>
44
45
#include "ps.h"
46
47
#include <sys/ucred.h>
48
#include <sys/sysctl.h>
49
50
int needheader;
51
52
static VAR *findvar(char *);
53
static int  vcmp(const void *, const void *);
54
55
#ifdef NOTINUSE
56
int	utime(), stime(), ixrss(), idrss(), isrss();
57
	{{"utime"}, "UTIME", USER, utime, 4},
58
	{{"stime"}, "STIME", USER, stime, 4},
59
	{{"ixrss"}, "IXRSS", USER, ixrss, 4},
60
	{{"idrss"}, "IDRSS", USER, idrss, 4},
61
	{{"isrss"}, "ISRSS", USER, isrss, 4},
62
#endif
63
64
/* Compute offset in common structures. */
65
#define	POFF(x)	offsetof(struct kinfo_proc, x)
66
67
#define	UIDFMT	"u"
68
#define	UIDLEN	5
69
#define	UID(n1, n2, fn, off) \
70
	{ n1, n2, NULL, 0, fn, UIDLEN, 0, off, UINT32, UIDFMT }
71
#define	GID(n1, n2, fn, off)	UID(n1, n2, fn, off)
72
73
#define	PIDFMT	"d"
74
#define	PIDLEN	5
75
#define	PID(n1, n2, fn, off) \
76
	{ n1, n2, NULL, 0, fn, PIDLEN, 0, off, INT32, PIDFMT }
77
78
#define	TIDFMT	"d"
79
#define TIDLEN	7
80
#define	TID(n1, n2, fn, off) \
81
	{ n1, n2, NULL, 0, fn, TIDLEN, 0, off, INT32, TIDFMT }
82
83
#define	USERLEN	8
84
#define	CWDLEN	40
85
86
/* Bit types must match their respective entries in struct kinfo_proc */
87
/* Entries must be sorted in lexical ascending order! */
88
VAR var[] = {
89
	{"%cpu", "%CPU", NULL, NLIST, pcpu, 4},
90
	{"%mem", "%MEM", NULL, NLIST, pmem, 4},
91
	{"acflag", "ACFLG", NULL, 0, pvar, 3, 0, POFF(p_acflag), UINT16, "x"},
92
	{"acflg", "", "acflag"},
93
	{"args", "", "command"},
94
	{"blocked", "", "sigmask"},
95
	{"caught", "", "sigcatch"},
96
	{"comm", "COMMAND", "ucomm"},
97
	{"command", "COMMAND", NULL, COMM|LJUST|USER, command, 16},
98
	{"cpu", "CPU", NULL, 0, pvar, 3, 0, POFF(p_estcpu), UINT32, "d"},
99
	{"cpuid", "CPUID", NULL, 0, pvar, 8, 0, POFF(p_cpuid), UINT64, "lld"},
100
	{"cputime", "", "time"},
101
	{"cwd", "CWD", NULL, LJUST, curwd, CWDLEN},
102
	{"dsiz", "DSIZ", NULL, 0, dsize, 4},
103
	{"emul", "EMUL", NULL, LJUST, emulname, KI_EMULNAMELEN - 1},
104
	{"etime", "ELAPSED", "start"},
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
	{"pmem", "", "%mem"},
146
	PID("ppid", "PPID", pvar, POFF(p_ppid)),
147
	{"pri", "PRI", NULL, 0, pri, 3},
148
	{"procflags", "PROCF", NULL, 0, pvar, 7, 0, POFF(p_psflags), INT32, "x"},
149
	{"re", "RE", NULL, INF127, pvar, 3, 0, POFF(p_swtime), UINT32, "u"},
150
	GID("rgid", "RGID", pvar, POFF(p_rgid)),
151
	/* XXX */
152
	{"rgroup", "RGROUP", NULL, LJUST, rgname, USERLEN},
153
	{"rlink", "RLINK", NULL, 0, pvar, 8, 0, POFF(p_back), UINT64, "llx"},
154
	{"rss", "RSS", NULL, 0, p_rssize, 5},
155
	{"rssize", "", "rsz"},
156
	{"rsz", "RSZ", NULL, 0, rssize, 4},
157
	{"rtable", "RTABLE", NULL, 0, pvar, 0, 0, POFF(p_rtableid), INT32, "d"},
158
	UID("ruid", "RUID", pvar, POFF(p_ruid)),
159
	{"ruser", "RUSER", NULL, LJUST, runame, USERLEN},
160
	{"sess", "SESS", NULL, 0, pvar, PTRWIDTH, 0, POFF(p_sess), UINT64, "llx"},
161
	{"sig", "PENDING", NULL, 0, pvar, 8, 0, POFF(p_siglist), INT32, "x"},
162
	{"sigcatch", "CAUGHT", NULL, 0, pvar, 8, 0, POFF(p_sigcatch), UINT32, "x"},
163
	{"sigignore", "IGNORED",
164
		NULL, 0, pvar, 8, 0, POFF(p_sigignore), UINT32, "x"},
165
	{"sigmask", "BLOCKED", NULL, 0, pvar, 8, 0, POFF(p_sigmask), UINT32, "x"},
166
	{"sl", "SL", NULL, INF127, pvar, 3, 0, POFF(p_slptime), UINT32, "u"},
167
	{"ssiz", "SSIZ", NULL, 0, ssize, 4},
168
	{"start", "STARTED", NULL, LJUST|USER, started, 8},
169
	{"stat", "", "state"},
170
	{"state", "STAT", NULL, LJUST|NLIST, printstate, 5},
171
	GID("svgid", "SVGID", pvar, POFF(p_svgid)),
172
	UID("svuid", "SVUID", pvar, POFF(p_svuid)),
173
	{"tdev", "TDEV", NULL, 0, tdev, 4},
174
	TID("tid", "TID", pvar, POFF(p_tid)),
175
	{"time", "TIME", NULL, USER, cputime, 9},
176
	PID("tpgid", "TPGID", pvar, POFF(p_tpgid)),
177
	{"tsess", "TSESS", NULL, 0, pvar, PTRWIDTH, 0, POFF(p_tsess), UINT64, "llx"},
178
	{"tsiz", "TSIZ", NULL, 0, tsize, 4},
179
	{"tt", "TT", NULL, LJUST, tname, 3},
180
	{"tty", "TTY", NULL, LJUST, longtname, 8},
181
	{"ucomm", "UCOMM", NULL, LJUST, ucomm, MAXCOMLEN},
182
	UID("uid", "UID", pvar, POFF(p_uid)),
183
	{"upr", "UPR", NULL, 0, pvar, 3, 0, POFF(p_usrpri), UINT8, "d"},
184
	{"user", "USER", NULL, LJUST, euname, USERLEN},
185
	{"usrpri", "", "upr"},
186
	{"vsize", "", "vsz"},
187
	{"vsz", "VSZ", NULL, 0, vsize, 5},
188
	{"wchan", "WCHAN", NULL, LJUST, wchan, KI_WMESGLEN - 1},
189
	{"xstat", "XSTAT", NULL, 0, pvar, 4, 0, POFF(p_xstat), UINT16, "x"},
190
	{""},
191
};
192
193
void
194
showkey(void)
195
2
{
196
	VAR *v;
197
	int i;
198
	char *p, *sep;
199
200
2
	i = 0;
201
2
	sep = "";
202
192
	for (v = var; *(p = v->name); ++v) {
203
190
		int len = strlen(p);
204

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

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

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

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

460
	if (v && v->alias) {
265
42
		p = v->alias;
266

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