1 /* $OpenBSD: keyword.c,v 1.37 2012/04/11 17:33:26 guenther Exp $ */
2 /* $NetBSD: keyword.c,v 1.12.6.1 1996/05/30 21:25:13 cgd Exp $ */
5 * Copyright (c) 1990, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
33 #include <sys/param.h>
35 #include <sys/resource.h>
47 #include <sys/ucred.h>
48 #include <sys/sysctl.h>
52 static VAR
*findvar(char *);
53 static int vcmp(const void *, const void *);
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},
64 /* Compute offset in common structures. */
65 #define POFF(x) offsetof(struct kinfo_proc, x)
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)
75 #define PID(n1, n2, fn, off) \
76 { n1, n2, NULL, 0, fn, PIDLEN, 0, off, INT32, PIDFMT }
80 #define TID(n1, n2, fn, off) \
81 { n1, n2, NULL, 0, fn, TIDLEN, 0, off, INT32, TIDFMT }
86 /* Bit types must match their respective entries in struct kinfo_proc */
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", "", "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", "", "start"},
104 {"f", "F", NULL
, 0, pvar
, 7, 0, POFF(p_flag
), INT32
, "x"},
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"},
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
, MAXLOGNAME
},
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 {"minflt", "MINFLT", NULL
, USER
, pvar
, 4, 0, POFF(p_uru_minflt
), UINT64
, "lld"},
121 {"maxrss", "MAXRSS", NULL
, USER
, pvar
, 4, 0, POFF(p_uru_maxrss
), 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"},
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"},
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"},
136 {"p_ru", "P_RU", NULL
, 0, pvar
, PTRWIDTH
, 0, POFF(p_ru
), UINT64
, "llx"},
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 {"re", "RE", NULL
, INF127
, pvar
, 3, 0, POFF(p_swtime
), UINT32
, "u"},
148 GID("rgid", "RGID", pvar
, POFF(p_rgid
)),
150 {"rgroup", "RGROUP", NULL
, LJUST
, rgname
, USERLEN
},
151 {"rlink", "RLINK", NULL
, 0, pvar
, 8, 0, POFF(p_back
), UINT64
, "llx"},
152 {"rss", "RSS", NULL
, 0, p_rssize
, 5},
153 {"rssize", "", "rsz"},
154 {"rsz", "RSZ", NULL
, 0, rssize
, 4},
155 {"rtable", "RTABLE", NULL
, 0, pvar
, 0, 0, POFF(p_rtableid
), INT32
, "d"},
156 UID("ruid", "RUID", pvar
, POFF(p_ruid
)),
157 {"ruser", "RUSER", NULL
, LJUST
, runame
, USERLEN
},
158 {"sess", "SESS", NULL
, 0, pvar
, PTRWIDTH
, 0, POFF(p_sess
), UINT64
, "llx"},
159 {"sig", "PENDING", NULL
, 0, pvar
, 8, 0, POFF(p_siglist
), INT32
, "x"},
160 {"sigcatch", "CAUGHT", NULL
, 0, pvar
, 8, 0, POFF(p_sigcatch
), UINT32
, "x"},
161 {"sigignore", "IGNORED",
162 NULL
, 0, pvar
, 8, 0, POFF(p_sigignore
), UINT32
, "x"},
163 {"sigmask", "BLOCKED", NULL
, 0, pvar
, 8, 0, POFF(p_sigmask
), UINT32
, "x"},
164 {"sl", "SL", NULL
, INF127
, pvar
, 3, 0, POFF(p_slptime
), UINT32
, "u"},
165 {"ssiz", "SSIZ", NULL
, 0, ssize
, 4},
166 {"start", "STARTED", NULL
, LJUST
|USER
, started
, 8},
167 {"stat", "", "state"},
168 {"state", "STAT", NULL
, LJUST
|NLIST
, state
, 5},
169 GID("svgid", "SVGID", pvar
, POFF(p_svgid
)),
170 UID("svuid", "SVUID", pvar
, POFF(p_svuid
)),
171 {"tdev", "TDEV", NULL
, 0, tdev
, 4},
172 TID("tid", "TID", pvar
, POFF(p_tid
)),
173 {"time", "TIME", NULL
, USER
, cputime
, 9},
174 PID("tpgid", "TPGID", pvar
, POFF(p_tpgid
)),
175 {"tsess", "TSESS", NULL
, 0, pvar
, PTRWIDTH
, 0, POFF(p_tsess
), UINT64
, "llx"},
176 {"tsiz", "TSIZ", NULL
, 0, tsize
, 4},
177 {"tt", "TT", NULL
, LJUST
, tname
, 3},
178 {"tty", "TTY", NULL
, LJUST
, longtname
, 8},
179 {"ucomm", "UCOMM", NULL
, LJUST
, ucomm
, MAXCOMLEN
},
180 UID("uid", "UID", pvar
, POFF(p_uid
)),
181 {"upr", "UPR", NULL
, 0, pvar
, 3, 0, POFF(p_usrpri
), UINT8
, "d"},
182 {"user", "USER", NULL
, LJUST
, euname
, USERLEN
},
183 {"usrpri", "", "upr"},
184 {"vsize", "", "vsz"},
185 {"vsz", "VSZ", NULL
, 0, vsize
, 5},
186 {"wchan", "WCHAN", NULL
, LJUST
, wchan
, KI_WMESGLEN
- 1},
187 {"xstat", "XSTAT", NULL
, 0, pvar
, 4, 0, POFF(p_xstat
), UINT16
, "x"},
200 for (v
= var
; *(p
= v
->name
); ++v
) {
202 if (termwidth
&& (i
+= len
+ 1) > termwidth
) {
206 (void) printf("%s%s", sep
, p
);
215 static struct varent
*vtail
;
217 #define FMTSEP " \t,\n"
223 while ((cp
= strsep(&p
, FMTSEP
)) != NULL
&& *cp
== '\0')
227 if (!(v
= findvar(cp
)) || v
->parsed
== 1)
230 if ((vent
= malloc(sizeof(struct varent
))) == NULL
)
235 vhead
= vtail
= vent
;
240 needheader
|= v
->header
[0] != '\0';
243 errx(1, "no valid keywords");
251 int vcmp(const void *, const void *);
260 v
= bsearch(&key
, var
, sizeof(var
)/sizeof(VAR
) - 1, sizeof(VAR
), vcmp
);
264 warnx("%s: illegal keyword specification", p
);
271 warnx("%s: keyword not found", p
);
281 vcmp(const void *a
, const void *b
)
283 return (strcmp(((VAR
*)a
)->name
, ((VAR
*)b
)->name
));