* Remove a procedural layer in the scheduler clock code by having
[dragonfly.git] / bin / ps / keyword.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
1de703da
MD
32 *
33 * @(#)keyword.c 8.5 (Berkeley) 4/2/94
34 * $FreeBSD: src/bin/ps/keyword.c,v 1.24.2.3 2002/10/10 20:05:32 jmallett Exp $
dcc99b62 35 * $DragonFly: src/bin/ps/keyword.c,v 1.19 2005/06/27 18:38:01 dillon Exp $
984263bc
MD
36 */
37
984263bc
MD
38#include <sys/param.h>
39#include <sys/time.h>
023c70f2 40#include <sys/queue.h>
984263bc 41#include <sys/resource.h>
984263bc
MD
42#include <sys/sysctl.h>
43#include <sys/ucred.h>
44#include <sys/user.h>
45
46#include <err.h>
47#include <errno.h>
48#include <stddef.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <utmp.h>
53
54#include "ps.h"
55
e9b022bd 56static struct varent *makevarent(const char *);
023c70f2
JS
57static int vcmp(const void *, const void *);
58
59struct varent_head var_head = STAILQ_HEAD_INITIALIZER(var_head);
984263bc
MD
60
61#ifdef NOTINUSE
62int utime(), stime(), ixrss(), idrss(), isrss();
63 {{"utime"}, "UTIME", USER, utime, NULL, 4},
64 {{"stime"}, "STIME", USER, stime, NULL, 4},
65 {{"ixrss"}, "IXRSS", USER, ixrss, NULL, 4},
66 {{"idrss"}, "IDRSS", USER, idrss, NULL, 4},
67 {{"isrss"}, "ISRSS", USER, isrss, NULL, 4},
68#endif
69
70/* Compute offset in common structures. */
71#define POFF(x) offsetof(struct proc, x)
17c88c3a 72#define TOFF(x) offsetof(struct thread, x)
984263bc
MD
73#define EOFF(x) offsetof(struct eproc, x)
74#define UOFF(x) offsetof(struct usave, x)
75#define ROFF(x) offsetof(struct rusage, x)
76
77#define UIDFMT "u"
78#define UIDLEN 5
79#define PIDFMT "d"
80#define PIDLEN 5
81#define USERLEN UT_NAMESIZE
82
023c70f2
JS
83static const VAR var[] = {
84 {"%cpu", "%CPU", NULL, 0, pcpu, NULL, 4, 0, 0, NULL, NULL},
85 {"%mem", "%MEM", NULL, 0, pmem, NULL, 4, 0, 0, NULL, NULL},
159c4469 86 {"acflag", "ACFLG", NULL, 0, pvar, NULL, 3, POFF(p_acflag), USHORT, "x",
023c70f2
JS
87 NULL},
88 {"acflg", "", "acflag", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
89 {"blocked", "", "sigmask", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
90 {"caught", "", "sigcatch", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
159c4469 91 {"command", "COMMAND", NULL, COMM|LJUST|USER, command, NULL, 16, 0, 0, NULL,
023c70f2 92 NULL},
dcc99b62 93 {"cpu", "CPU", NULL, 0, pvar, NULL, 3, POFF(p_usdata.bsd4.estcpu), UINT, "d", NULL},
023c70f2
JS
94 {"lastcpu", "C", NULL, 0, pvar, NULL, 3, EOFF(e_cpuid), UINT, "d", NULL},
95 {"cputime", "", "time", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
96 {"f", "F", NULL, 0, pvar, NULL, 7, POFF(p_flag), INT, "x", NULL},
97 {"flags", "", "f", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
352f5709 98 {"iac", "IAC", NULL, 0, pvar, NULL, 4, POFF(p_usdata.bsd4.interactive), CHAR, PIDFMT,
023c70f2
JS
99 NULL},
100 {"ignored", "", "sigignore", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
159c4469 101 {"inblk", "INBLK", NULL, USER, rvar, NULL, 4, ROFF(ru_inblock), LONG, "ld",
023c70f2
JS
102 NULL},
103 {"inblock", "", "inblk", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
104 {"jobc", "JOBC", NULL, 0, evar, NULL, 4, EOFF(e_jobc), SHORT, "d", NULL},
159c4469 105 {"ktrace", "KTRACE", NULL, 0, pvar, NULL, 8, POFF(p_traceflag), INT, "x",
023c70f2 106 NULL},
159c4469 107 {"ktracep", "KTRACEP", NULL, 0, pvar, NULL, 8, POFF(p_tracep), LONG, "lx",
023c70f2
JS
108 NULL},
109 {"lim", "LIM", NULL, 0, maxrss, NULL, 5, 0, 0, NULL, NULL},
159c4469 110 {"login", "LOGIN", NULL, LJUST, logname, NULL, MAXLOGNAME-1, 0, 0, NULL,
023c70f2
JS
111 NULL},
112 {"logname", "", "login", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
113 {"lstart", "STARTED", NULL, LJUST|USER, lstarted, NULL, 28, 0, 0, NULL, NULL},
159c4469 114 {"majflt", "MAJFLT", NULL, USER, rvar, NULL, 4, ROFF(ru_majflt), LONG, "ld",
023c70f2 115 NULL},
159c4469 116 {"minflt", "MINFLT", NULL, USER, rvar, NULL, 4, ROFF(ru_minflt), LONG, "ld",
023c70f2 117 NULL},
159c4469 118 {"msgrcv", "MSGRCV", NULL, USER, rvar, NULL, 4, ROFF(ru_msgrcv), LONG, "ld",
023c70f2 119 NULL},
159c4469 120 {"msgsnd", "MSGSND", NULL, USER, rvar, NULL, 4, ROFF(ru_msgsnd), LONG, "ld",
023c70f2
JS
121 NULL},
122 {"ni", "", "nice", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
123 {"nice", "NI", NULL, 0, pnice, NULL, 3, 0, 0, NULL, NULL},
159c4469 124 {"nivcsw", "NIVCSW", NULL, USER, rvar, NULL, 5, ROFF(ru_nivcsw), LONG, "ld",
023c70f2
JS
125 NULL},
126 {"nsignals", "", "nsigs", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
159c4469 127 {"nsigs", "NSIGS", NULL, USER, rvar, NULL, 4, ROFF(ru_nsignals), LONG, "ld",
023c70f2 128 NULL},
159c4469 129 {"nswap", "NSWAP", NULL, USER, rvar, NULL, 4, ROFF(ru_nswap), LONG, "ld",
023c70f2 130 NULL},
159c4469 131 {"nvcsw", "NVCSW", NULL, USER, rvar, NULL, 5, ROFF(ru_nvcsw), LONG, "ld",
023c70f2
JS
132 NULL},
133 {"nwchan", "WCHAN", NULL, 0, tvar, NULL, 8, TOFF(td_wchan), KPTR, "lx", NULL},
159c4469 134 {"oublk", "OUBLK", NULL, USER, rvar, NULL, 4, ROFF(ru_oublock), LONG, "ld",
023c70f2
JS
135 NULL},
136 {"oublock", "", "oublk", 0, NULL, NULL, 0, 0 ,0, NULL, NULL},
137 {"p_ru", "P_RU", NULL, 0, pvar, NULL, 6, POFF(p_ru), KPTR, "lx", NULL},
138 {"paddr", "PADDR", NULL, 0, evar, NULL, 8, EOFF(e_paddr), KPTR, "lx", NULL},
139 {"pagein", "PAGEIN", NULL, USER, pagein, NULL, 6, 0, 0, NULL, NULL},
140 {"pcpu", "", "%cpu", 0, NULL, NULL, 0, 0,0, NULL, NULL},
141 {"pending", "", "sig", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
159c4469 142 {"pgid", "PGID", NULL, 0, evar, NULL, PIDLEN, EOFF(e_pgid), UINT, PIDFMT,
023c70f2 143 NULL},
159c4469 144 {"pid", "PID", NULL, 0, pvar, NULL, PIDLEN, POFF(p_pid), UINT, PIDFMT,
023c70f2
JS
145 NULL},
146 {"pmem", "", "%mem", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
159c4469 147 {"ppid", "PPID", NULL, 0, evar, NULL, PIDLEN, EOFF(e_ppid), UINT, PIDFMT,
023c70f2
JS
148 NULL},
149 {"pri", "PRI", NULL, 0, pri, NULL, 3, 0, 0, NULL, NULL},
150 {"re", "RE", NULL, 0, pvar, NULL, 3, POFF(p_swtime), UINT, "d", NULL},
6ac7b760 151 {"rgid", "RGID", NULL, 0, evar, NULL, UIDLEN, EOFF(e_ucred.cr_rgid),
023c70f2 152 UINT, UIDFMT, NULL},
159c4469 153 {"rlink", "RLINK", NULL, 0, pvar, NULL, 8, POFF(p_procq.tqe_prev), KPTR, "lx",
023c70f2
JS
154 NULL},
155 {"rss", "RSS", NULL, 0, p_rssize, NULL, 4, 0, 0, NULL, NULL},
156 {"rssize", "", "rsz", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
157 {"rsz", "RSZ", NULL, 0, rssize, NULL, 4, 0, 0, NULL, NULL},
158 {"rtprio", "RTPRIO", NULL, 0, rtprior, NULL, 7, POFF(p_rtprio), 0, NULL, NULL},
6ac7b760 159 {"ruid", "RUID", NULL, 0, evar, NULL, UIDLEN, EOFF(e_ucred.cr_ruid),
023c70f2 160 UINT, UIDFMT, NULL},
159c4469 161 {"ruser", "RUSER", NULL, LJUST|DSIZ, runame, s_runame, USERLEN, 0, 0, NULL,
023c70f2
JS
162 NULL},
163 {"sess", "SESS", NULL, 0, evar, NULL, 6, EOFF(e_sess), KPTR, "lx", NULL},
164 {"sig", "PENDING", NULL, 0, pvar, NULL, 8, POFF(p_siglist), INT, "x", NULL},
159c4469 165 {"sigcatch", "CAUGHT", NULL, 0, evar, NULL, 8, EOFF(e_procsig.ps_sigcatch),
023c70f2 166 UINT, "x", NULL},
159c4469 167 {"sigignore", "IGNORED", NULL, 0, evar, NULL, 8, EOFF(e_procsig.ps_sigignore),
023c70f2 168 UINT, "x", NULL},
159c4469 169 {"sigmask", "BLOCKED", NULL, 0, pvar, NULL, 8, POFF(p_sigmask), UINT, "x",
023c70f2
JS
170 NULL},
171 {"sl", "SL", NULL, 0, pvar, NULL, 3, POFF(p_slptime), UINT, "d", NULL},
172 {"start", "STARTED", NULL, LJUST|USER, started, NULL, 7, 0, 0, NULL, NULL},
173 {"stat", "", "state", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
174 {"state", "STAT", NULL, 0, state, NULL, 4, 0, 0, NULL, NULL},
159c4469 175 {"svgid", "SVGID", NULL, 0, evar, NULL, UIDLEN, EOFF(e_ucred.cr_svgid), UINT,
023c70f2 176 UIDFMT, NULL},
159c4469 177 {"svuid", "SVUID", NULL, 0, evar, NULL, UIDLEN, EOFF(e_ucred.cr_svuid), UINT,
023c70f2
JS
178 UIDFMT, NULL},
179 {"tdev", "TDEV", NULL, 0, tdev, NULL, 4, 0, 0, NULL, NULL},
cb56579b 180 {"tdpri", "TDPRI", NULL, 0, tdpri, NULL, 5, 0, 0, NULL, NULL},
023c70f2
JS
181 {"time", "TIME", NULL, USER, cputime, NULL, 9, 0, 0, NULL, NULL},
182 {"tpgid", "TPGID", NULL, 0, evar, NULL, 4, EOFF(e_tpgid), UINT, PIDFMT, NULL},
183 {"tsess", "TSESS", NULL, 0, evar, NULL, 6, EOFF(e_tsess), KPTR, "lx", NULL},
184 {"tsiz", "TSIZ", NULL, 0, tsize, NULL, 4, 0, 0, NULL, NULL},
185 {"tt", "TT ", NULL, 0, tname, NULL, 4, 0, 0, NULL, NULL},
186 {"tty", "TTY", NULL, LJUST, longtname, NULL, 8, 0, 0, NULL, NULL},
187 {"ucomm", "UCOMM", NULL, LJUST, ucomm, NULL, MAXCOMLEN, 0, 0, NULL, NULL},
984263bc 188 {"uid", "UID", NULL, 0, evar, NULL, UIDLEN, EOFF(e_ucred.cr_uid),
023c70f2
JS
189 UINT, UIDFMT, NULL},
190 {"user", "USER", NULL, LJUST|DSIZ, uname, s_uname, USERLEN, 0, 0, NULL, NULL},
191 {"vsize", "", "vsz", 0, 0, NULL, 0, 0, 0, NULL, NULL},
192 {"vsz", "VSZ", NULL, 0, vsize, NULL, 5, 0, 0, NULL, NULL},
193 {"wchan", "WCHAN", NULL, LJUST, wchan, NULL, 6, 0, 0, NULL, NULL},
194 {"xstat", "XSTAT", NULL, 0, pvar, NULL, 4, POFF(p_xstat), USHORT, "x", NULL},
195 {"", NULL, NULL, 0, NULL, NULL, 0, 0, 0, NULL, NULL},
984263bc
MD
196};
197
198void
b5744197 199showkey(void)
984263bc 200{
023c70f2 201 const VAR *v;
984263bc 202 int i;
c443019b 203 const char *p, *sep;
984263bc
MD
204
205 i = 0;
206 sep = "";
207 for (v = var; *(p = v->name); ++v) {
208 int len = strlen(p);
209 if (termwidth && (i += len + 1) > termwidth) {
210 i = len;
211 sep = "\n";
212 }
f9af826d 213 printf("%s%s", sep, p);
984263bc
MD
214 sep = " ";
215 }
f9af826d 216 printf("\n");
984263bc
MD
217}
218
219void
c443019b 220parsefmt(const char *fmt)
984263bc 221{
e9b022bd 222 struct varent *vent;
c443019b 223 char *p, *op;
e9b022bd 224 const char *cp;
c443019b
CP
225
226 op = p = strdup(fmt);
227
e9b022bd 228 if (op == NULL)
c443019b 229 errx(1, "Not enough memory");
984263bc
MD
230
231#define FMTSEP " \t,\n"
e9b022bd 232 while (p != NULL && *p != '\0') {
984263bc
MD
233 while ((cp = strsep(&p, FMTSEP)) != NULL && *cp == '\0')
234 /* void */;
023c70f2 235 if (cp == NULL)
984263bc 236 continue;
023c70f2
JS
237 if ((vent = makevarent(cp)) == NULL)
238 continue;
239 STAILQ_INSERT_TAIL(&var_head, vent, link);
984263bc 240 }
023c70f2 241 if (STAILQ_EMPTY(&var_head)) {
984263bc
MD
242 warnx("no valid keywords; valid keywords:");
243 showkey();
244 exit(1);
245 }
c443019b
CP
246
247 free(op);
984263bc
MD
248}
249
023c70f2 250static struct varent *
e9b022bd 251makevarent(const char *p)
984263bc 252{
023c70f2
JS
253 struct varent *vent = NULL;
254 const VAR *v;
255 VAR key;
984263bc 256 char *hp;
984263bc
MD
257
258 hp = strchr(p, '=');
259 if (hp)
260 *hp++ = '\0';
261
262 key.name = p;
263 v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
264
265 if (v && v->alias) {
266 if (hp) {
267 warnx("%s: illegal keyword specification", p);
268 eval = 1;
269 }
270 parsefmt(v->alias);
023c70f2 271 return(NULL);
984263bc
MD
272 }
273 if (!v) {
274 warnx("%s: keyword not found", p);
275 eval = 1;
023c70f2
JS
276 return(NULL);
277 }
278 if ((vent = malloc(sizeof(struct varent))) == NULL)
279 err(1, NULL);
280 vent->var = v;
281 vent->width = v->width;
e9b022bd
JS
282 if (hp) {
283 vent->header = strdup(hp);
284 if (vent->header == NULL)
285 err(1, "Not enough memory");
286 }
023c70f2
JS
287 else
288 vent->header = v->header;
289 return (vent);
984263bc
MD
290}
291
292static int
b5744197 293vcmp(const void *a, const void *b)
984263bc 294{
c443019b 295 return (strcmp(((const VAR *)a)->name, ((const VAR *)b)->name));
984263bc 296}