]> git.cameronkatri.com Git - apple_cmds.git/blob - adv_cmds/ps/keyword.c
md5: Don't symlink non working bins, setuid appropriate bins
[apple_cmds.git] / adv_cmds / ps / keyword.c
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #if 0
31 #ifndef lint
32 static char sccsid[] = "@(#)keyword.c 8.5 (Berkeley) 4/2/94";
33 #else
34 static const char rcsid[] =
35 "$FreeBSD: keyword.c,v 1.23 1999/01/26 02:38:09 julian Exp $";
36 #endif /* not lint */
37 #endif
38
39 #include <sys/param.h>
40 #include <sys/time.h>
41 #include <sys/resource.h>
42 #include <sys/proc.h>
43 #include <sys/sysctl.h>
44 #include <sys/user.h>
45
46 #include <err.h>
47 #include <stddef.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51
52 #include "ps.h"
53
54 VAR *findvar(char *, int, char **header);
55 static int vcmp(const void *, const void *);
56
57 /* Compute offset in common structures. */
58 #define POFF(x) offsetof(struct extern_proc, x)
59 #define EOFF(x) offsetof(struct eproc, x)
60 #define UOFF(x) offsetof(struct usave, x)
61 #define ROFF(x) offsetof(struct rusage, x)
62
63 #define EMULLEN 13 /* enough for "FreeBSD ELF32" */
64 #define LWPFMT "d"
65 #define LWPLEN 6
66 #define NLWPFMT "d"
67 #define NLWPLEN 4
68 #ifdef __APPLE__
69 #define UIDFMT "d"
70 #else
71 #define UIDFMT "u"
72 #endif
73 #define UIDLEN 5
74 #define PIDFMT "d"
75 #define PIDLEN 5
76 #define USERLEN 16
77
78 /* PLEASE KEEP THE TABLE BELOW SORTED ALPHABETICALLY!!! */
79 static VAR var[] = {
80 /* 4133537: 5 characters to accomodate 100% or more */
81 {"%cpu", "%CPU", NULL, 0, pcpu, NULL, 5, 0, CHAR, NULL, 0},
82 {"%mem", "%MEM", NULL, 0, pmem, NULL, 4},
83 {"acflag", "ACFLG",
84 NULL, 0, pvar, NULL, 3, POFF(p_acflag), USHORT, "x"},
85 {"acflg", "", "acflag"},
86 {"args", "ARGS", NULL, COMM|LJUST|USER|DSIZ, args, s_args, 64},
87 {"blocked", "", "sigmask"},
88 {"caught", "", "sigcatch"},
89 {"comm", "COMM", NULL, COMM|LJUST|USER|DSIZ, just_command, s_just_command, 16},
90 {"command", "COMMAND", NULL, COMM|LJUST|USER|DSIZ, command, s_command, 16},
91 {"cpu", "CPU", NULL, 0, pvar, NULL, 3, POFF(p_estcpu), UINT, "d"},
92 {"cputime", "", "time"},
93 {"etime", "ELAPSED", NULL, USER|DSIZ, p_etime, s_etime, 20},
94 /* 5861775: Make F column 8 characters. */
95 {"f", "F", NULL, 0, pvar, NULL, 8, POFF(p_flag), INT, "x"},
96 {"flags", "", "f"},
97 {"gid", "GID", NULL, 0, evar, NULL, UIDLEN, EOFF(e_ucred.cr_gid),
98 UINT, UIDFMT},
99 {"group", "GROUP", "gid"},
100 {"ignored", "", "sigignore"},
101 {"inblk", "INBLK",
102 NULL, USER, rvar, NULL, 4, ROFF(ru_inblock), LONG, "ld"},
103 {"inblock", "", "inblk"},
104 {"jobc", "JOBC", NULL, 0, evar, NULL, 4, EOFF(e_jobc), SHORT, "d"},
105 {"ktrace", "KTRACE",
106 NULL, 0, pvar, NULL, 8, POFF(p_traceflag), INT, "x"},
107 {"ktracep", "KTRACEP",
108 NULL, 0, pvar, NULL, 8, POFF(p_tracep), LONG, "lx"},
109 {"lim", "LIM", NULL, 0, maxrss, NULL, 5},
110 {"login", "LOGIN", NULL, LJUST, logname, NULL, MAXLOGNAME-1},
111 {"logname", "", "login"},
112 {"lstart", "STARTED", NULL, LJUST|USER, lstarted, NULL, 28},
113 {"majflt", "MAJFLT",
114 NULL, USER, rvar, NULL, 4, ROFF(ru_majflt), LONG, "ld"},
115 {"minflt", "MINFLT",
116 NULL, USER, rvar, NULL, 4, ROFF(ru_minflt), LONG, "ld"},
117 {"msgrcv", "MSGRCV",
118 NULL, USER, rvar, NULL, 4, ROFF(ru_msgrcv), LONG, "ld"},
119 {"msgsnd", "MSGSND",
120 NULL, USER, rvar, NULL, 4, ROFF(ru_msgsnd), LONG, "ld"},
121 {"ni", "", "nice"},
122 {"nice", "NI", NULL, 0, pvar, NULL, 2, POFF(p_nice), CHAR, "d"},
123 {"nivcsw", "NIVCSW",
124 NULL, USER, rvar, NULL, 5, ROFF(ru_nivcsw), LONG, "ld"},
125 {"nsignals", "", "nsigs"},
126 {"nsigs", "NSIGS",
127 NULL, USER, rvar, NULL, 4, ROFF(ru_nsignals), LONG, "ld"},
128 {"nswap", "NSWAP",
129 NULL, USER, rvar, NULL, 4, ROFF(ru_nswap), LONG, "ld"},
130 {"nvcsw", "NVCSW",
131 NULL, USER, rvar, NULL, 5, ROFF(ru_nvcsw), LONG, "ld"},
132 {"nwchan", "WCHAN", NULL, 0, pvar, NULL, 6, POFF(p_wchan), KPTR, "lx"},
133 {"oublk", "OUBLK",
134 NULL, USER, rvar, NULL, 4, ROFF(ru_oublock), LONG, "ld"},
135 {"oublock", "", "oublk"},
136 {"p_ru", "P_RU", NULL, 0, pvar, NULL, 6, POFF(p_ru), KPTR, "lx"},
137 {"paddr", "PADDR", NULL, 0, evar, NULL, sizeof(void *) * 2, EOFF(e_paddr), KPTR, "lx"},
138 {"pagein", "PAGEIN", NULL, USER, pagein, NULL, 6},
139 {"pcpu", "", "%cpu"},
140 {"pending", "", "sig"},
141 {"pgid", "PGID",
142 NULL, 0, evar, NULL, PIDLEN, EOFF(e_pgid), UINT, PIDFMT},
143 {"pid", "PID", NULL, 0, pvar, NULL, PIDLEN, POFF(p_pid), UINT, PIDFMT},
144 {"pmem", "", "%mem"},
145 {"ppid", "PPID",
146 NULL, 0, evar, NULL, PIDLEN, EOFF(e_ppid), UINT, PIDFMT},
147 {"pri", "PRI", NULL, 0, pri, NULL, 3},
148 {"pstime", "", "stime"},
149 {"putime", "", "utime"},
150 {"re", "RE", NULL, 0, pvar, NULL, 3, POFF(p_swtime), UINT, "d"},
151 {"rgid", "RGID", NULL, 0, evar, NULL, UIDLEN, EOFF(e_pcred.p_rgid),
152 UINT, UIDFMT},
153 {"rgroup", "RGROUP", "rgid"},
154 {"rss", "RSS", NULL, 0, p_rssize, NULL, 6},
155 #if FIXME
156 {"rtprio", "RTPRIO", NULL, 0, rtprior, NULL, 7, POFF(p_rtprio)},
157 #endif /* FIXME */
158 {"ruid", "RUID", NULL, 0, evar, NULL, UIDLEN, EOFF(e_pcred.p_ruid),
159 UINT, UIDFMT},
160 {"ruser", "RUSER", NULL, LJUST|DSIZ, runame, s_runame, USERLEN},
161 {"sess", "SESS", NULL, 0, evar, NULL, 6, EOFF(e_sess), KPTR, "lx"},
162 {"sig", "PENDING", NULL, 0, pvar, NULL, 8, POFF(p_siglist), INT, "x"},
163 #if FIXME
164 {"sigcatch", "CAUGHT",
165 NULL, 0, evar, NULL, 8, EOFF(e_procsig.ps_sigcatch), UINT, "x"},
166 {"sigignore", "IGNORED",
167 NULL, 0, evar, NULL, 8, EOFF(e_procsig.ps_sigignore), UINT, "x"},
168 #endif /* FIXME */
169 {"sigmask", "BLOCKED",
170 NULL, 0, pvar, NULL, 8, POFF(p_sigmask), UINT, "x"},
171 {"sl", "SL", NULL, 0, pvar, NULL, 3, POFF(p_slptime), UINT, "d"},
172 {"start", "STARTED", NULL, LJUST|USER, started, NULL, 7},
173 {"stat", "", "state"},
174 {"state", "STAT", NULL, 0, state, NULL, 4},
175 {"stime", "STIME", NULL, USER, pstime, NULL, 9},
176 {"svgid", "SVGID", NULL, 0,
177 evar, NULL, UIDLEN, EOFF(e_pcred.p_svgid), UINT, UIDFMT},
178 {"svuid", "SVUID", NULL, 0,
179 evar, NULL, UIDLEN, EOFF(e_pcred.p_svuid), UINT, UIDFMT},
180 {"tdev", "TDEV", NULL, 0, tdev, NULL, 4},
181 {"time", "TIME", NULL, USER, cputime, NULL, 9},
182 {"tpgid", "TPGID",
183 NULL, 0, evar, NULL, 4, EOFF(e_tpgid), UINT, PIDFMT},
184 {"tsess", "TSESS", NULL, 0, evar, NULL, 6, EOFF(e_tsess), KPTR, "lx"},
185 {"tsiz", "TSIZ", NULL, 0, tsize, NULL, 8},
186 {"tt", "TT ", NULL, 0, tname, NULL, 5},
187 {"tty", "TTY", NULL, LJUST, longtname, NULL, 8},
188 {"ucomm", "UCOMM", NULL, LJUST, ucomm, NULL, MAXCOMLEN},
189 {"uid", "UID", NULL, 0, evar, NULL, UIDLEN, EOFF(e_ucred.cr_uid),
190 UINT, UIDFMT},
191 {"upr", "UPR", NULL, 0, pvar, NULL, 3, POFF(p_usrpri), CHAR, "d"},
192 {"user", "USER", NULL, LJUST|DSIZ, uname, s_uname, USERLEN},
193 {"usrpri", "", "upr"},
194 {"utime", "UTIME", NULL, USER, putime, NULL, 9},
195 {"vsize", "", "vsz"},
196 {"vsz", "VSZ", NULL, 0, vsize, NULL, 8},
197 {"wchan", "WCHAN", NULL, LJUST, wchan, NULL, 6},
198 {"wq", "WQ", NULL, 0, wq, NULL, 4, 0, CHAR, NULL, 0},
199 {"wqb", "WQB", NULL, 0, wq, NULL, 4, 0, CHAR, NULL, 0},
200 {"wql", "WQL", NULL, 0, wq, NULL, 3, 0, CHAR, NULL, 0},
201 {"wqr", "WQR", NULL, 0, wq, NULL, 4, 0, CHAR, NULL, 0},
202 {"xstat", "XSTAT", NULL, 0, pvar, NULL, 4, POFF(p_xstat), USHORT, "x"},
203 {""},
204 };
205
206 void
207 showkey(void)
208 {
209 VAR *v;
210 int i;
211 const char *p, *sep;
212
213 i = 0;
214 sep = "";
215 for (v = var; *(p = v->name); ++v) {
216 int len = strlen(p);
217 if (termwidth && (i += len + 1) > termwidth) {
218 i = len;
219 sep = "\n";
220 }
221 (void) printf("%s%s", sep, p);
222 sep = " ";
223 }
224 (void) printf("\n");
225 }
226
227 void
228 parsefmt(const char *p, int user)
229 {
230 char *tempstr, *tempstr1;
231
232 #define FMTSEP " \t,\n"
233 tempstr1 = tempstr = strdup(p);
234 while (tempstr && *tempstr) {
235 char *cp, *hp;
236 VAR *v;
237 struct varent *vent;
238
239 #ifndef __APPLE__
240 /*
241 * If an item contains an equals sign, it specifies a column
242 * header, may contain embedded separator characters and
243 * is always the last item.
244 */
245 if (tempstr[strcspn(tempstr, "="FMTSEP)] != '=')
246 #endif /* !__APPLE__ */
247 while ((cp = strsep(&tempstr, FMTSEP)) != NULL &&
248 *cp == '\0')
249 /* void */;
250 #ifndef __APPLE__
251 else {
252 cp = tempstr;
253 tempstr = NULL;
254 }
255 #endif /* !__APPLE__ */
256 if (cp == NULL || !(v = findvar(cp, user, &hp)))
257 continue;
258 if (!user) {
259 /*
260 * If the user is NOT adding this field manually,
261 * get on with our lives if this VAR is already
262 * represented in the list.
263 */
264 vent = find_varentry(v);
265 if (vent != NULL)
266 continue;
267 }
268 if ((vent = malloc(sizeof(struct varent))) == NULL)
269 errx(1, "malloc failed");
270 vent->header = v->header;
271 if (hp) {
272 hp = strdup(hp);
273 if (hp)
274 vent->header = hp;
275 }
276 vent->var = malloc(sizeof(*vent->var));
277 if (vent->var == NULL)
278 errx(1, "malloc failed");
279 memcpy(vent->var, v, sizeof(*vent->var));
280 STAILQ_INSERT_TAIL(&varlist, vent, next_ve);
281 }
282 free(tempstr1);
283 if (STAILQ_EMPTY(&varlist)) {
284 warnx("no valid keywords; valid keywords:");
285 showkey();
286 exit(1);
287 }
288 }
289
290 VAR *
291 findvar(char *p, int user, char **header)
292 {
293 size_t rflen;
294 VAR *v, key;
295 char *hp, *realfmt;
296
297 hp = strchr(p, '=');
298 if (hp)
299 *hp++ = '\0';
300
301 key.name = p;
302 v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
303
304 if (v && v->alias) {
305 /*
306 * If the user specified an alternate-header for this
307 * (aliased) format-name, then we need to copy that
308 * alternate-header when making the recursive call to
309 * process the alias.
310 */
311 if (hp == NULL)
312 parsefmt(v->alias, user);
313 else {
314 /*
315 * XXX - This processing will not be correct for
316 * any alias which expands into a list of format
317 * keywords. Presently there are no aliases
318 * which do that.
319 */
320 rflen = strlen(v->alias) + strlen(hp) + 2;
321 realfmt = malloc(rflen);
322 snprintf(realfmt, rflen, "%s=%s", v->alias, hp);
323 parsefmt(realfmt, user);
324 }
325 return ((VAR *)NULL);
326 }
327 if (!v) {
328 warnx("%s: keyword not found", p);
329 eval = 1;
330 }
331 if (header)
332 *header = hp;
333 return (v);
334 }
335
336 static int
337 vcmp(const void *a, const void *b)
338 {
339 return (strcmp(((const VAR *)a)->name, ((const VAR *)b)->name));
340 }