Sync userland up with the kernel. This primarily adjusts ps, etc to handle
[dragonfly.git] / bin / 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  * 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.
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 $
35  * $DragonFly: src/bin/ps/keyword.c,v 1.4 2003/07/01 00:19:29 dillon Exp $
36  */
37
38 #include <sys/param.h>
39 #include <sys/time.h>
40 #include <sys/resource.h>
41 #include <sys/proc.h>
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
56 static VAR *findvar __P((char *));
57 static int  vcmp __P((const void *, const void *));
58
59 #ifdef NOTINUSE
60 int     utime(), stime(), ixrss(), idrss(), isrss();
61         {{"utime"}, "UTIME", USER, utime, NULL, 4},
62         {{"stime"}, "STIME", USER, stime, NULL, 4},
63         {{"ixrss"}, "IXRSS", USER, ixrss, NULL, 4},
64         {{"idrss"}, "IDRSS", USER, idrss, NULL, 4},
65         {{"isrss"}, "ISRSS", USER, isrss, NULL, 4},
66 #endif
67
68 /* Compute offset in common structures. */
69 #define POFF(x) offsetof(struct proc, x)
70 #define TOFF(x) offsetof(struct thread, x)
71 #define EOFF(x) offsetof(struct eproc, x)
72 #define UOFF(x) offsetof(struct usave, x)
73 #define ROFF(x) offsetof(struct rusage, x)
74
75 #define UIDFMT  "u"
76 #define UIDLEN  5
77 #define PIDFMT  "d"
78 #define PIDLEN  5
79 #define USERLEN UT_NAMESIZE
80
81 VAR var[] = {
82         {"%cpu", "%CPU", NULL, 0, pcpu, NULL, 4},
83         {"%mem", "%MEM", NULL, 0, pmem, NULL, 4},
84         {"acflag", "ACFLG",
85                 NULL, 0, pvar, NULL, 3, POFF(p_acflag), USHORT, "x"},
86         {"acflg", "", "acflag"},
87         {"blocked", "", "sigmask"},
88         {"caught", "", "sigcatch"},
89         {"command", "COMMAND", NULL, COMM|LJUST|USER, command, NULL, 16},
90         {"cpu", "CPU", NULL, 0, pvar, NULL, 3, POFF(p_estcpu), UINT, "d"},
91         {"cputime", "", "time"},
92         {"f", "F", NULL, 0, pvar, NULL, 7, POFF(p_flag), INT, "x"},
93         {"flags", "", "f"},
94         {"ignored", "", "sigignore"},
95         {"inblk", "INBLK",
96                 NULL, USER, rvar, NULL, 4, ROFF(ru_inblock), LONG, "ld"},
97         {"inblock", "", "inblk"},
98         {"jobc", "JOBC", NULL, 0, evar, NULL, 4, EOFF(e_jobc), SHORT, "d"},
99         {"ktrace", "KTRACE",
100                 NULL, 0, pvar, NULL, 8, POFF(p_traceflag), INT, "x"},
101         {"ktracep", "KTRACEP",
102                 NULL, 0, pvar, NULL, 8, POFF(p_tracep), LONG, "lx"},
103         {"lim", "LIM", NULL, 0, maxrss, NULL, 5},
104         {"login", "LOGIN", NULL, LJUST, logname, NULL, MAXLOGNAME-1},
105         {"logname", "", "login"},
106         {"lstart", "STARTED", NULL, LJUST|USER, lstarted, NULL, 28},
107         {"majflt", "MAJFLT",
108                 NULL, USER, rvar, NULL, 4, ROFF(ru_majflt), LONG, "ld"},
109         {"minflt", "MINFLT",
110                 NULL, USER, rvar, NULL, 4, ROFF(ru_minflt), LONG, "ld"},
111         {"msgrcv", "MSGRCV",
112                 NULL, USER, rvar, NULL, 4, ROFF(ru_msgrcv), LONG, "ld"},
113         {"msgsnd", "MSGSND",
114                 NULL, USER, rvar, NULL, 4, ROFF(ru_msgsnd), LONG, "ld"},
115         {"ni", "", "nice"},
116         {"nice", "NI", NULL, 0, pnice, NULL, 3 },
117         {"nivcsw", "NIVCSW",
118                 NULL, USER, rvar, NULL, 5, ROFF(ru_nivcsw), LONG, "ld"},
119         {"nsignals", "", "nsigs"},
120         {"nsigs", "NSIGS",
121                 NULL, USER, rvar, NULL, 4, ROFF(ru_nsignals), LONG, "ld"},
122         {"nswap", "NSWAP",
123                 NULL, USER, rvar, NULL, 4, ROFF(ru_nswap), LONG, "ld"},
124         {"nvcsw", "NVCSW",
125                 NULL, USER, rvar, NULL, 5, ROFF(ru_nvcsw), LONG, "ld"},
126         {"nwchan", "WCHAN", NULL, 0, tvar, NULL, 8, TOFF(td_wchan), KPTR, "lx"},
127         {"oublk", "OUBLK",
128                 NULL, USER, rvar, NULL, 4, ROFF(ru_oublock), LONG, "ld"},
129         {"oublock", "", "oublk"},
130         {"p_ru", "P_RU", NULL, 0, pvar, NULL, 6, POFF(p_ru), KPTR, "lx"},
131         {"paddr", "PADDR", NULL, 0, evar, NULL, 8, EOFF(e_paddr), KPTR, "lx"},
132         {"pagein", "PAGEIN", NULL, USER, pagein, NULL, 6},
133         {"pcpu", "", "%cpu"},
134         {"pending", "", "sig"},
135         {"pgid", "PGID",
136                 NULL, 0, evar, NULL, PIDLEN, EOFF(e_pgid), UINT, PIDFMT},
137         {"pid", "PID", NULL, 0, pvar, NULL, PIDLEN, POFF(p_pid), UINT, PIDFMT},
138         {"pmem", "", "%mem"},
139         {"ppid", "PPID",
140                 NULL, 0, evar, NULL, PIDLEN, EOFF(e_ppid), UINT, PIDFMT},
141         {"pri", "PRI", NULL, 0, pri, NULL, 3},
142         {"re", "RE", NULL, 0, pvar, NULL, 3, POFF(p_swtime), UINT, "d"},
143         {"rgid", "RGID", NULL, 0, evar, NULL, UIDLEN, EOFF(e_ucred.cr_rgid),
144                 UINT, UIDFMT},
145         {"rlink", "RLINK",
146                 NULL, 0, pvar, NULL, 8, POFF(p_procq.tqe_prev), KPTR, "lx"},
147         {"rss", "RSS", NULL, 0, p_rssize, NULL, 4},
148         {"rssize", "", "rsz"},
149         {"rsz", "RSZ", NULL, 0, rssize, NULL, 4},
150         {"rtprio", "RTPRIO", NULL, 0, rtprior, NULL, 7, POFF(p_rtprio)},
151         {"ruid", "RUID", NULL, 0, evar, NULL, UIDLEN, EOFF(e_ucred.cr_ruid),
152                 UINT, UIDFMT},
153         {"ruser", "RUSER", NULL, LJUST|DSIZ, runame, s_runame, USERLEN},
154         {"sess", "SESS", NULL, 0, evar, NULL, 6, EOFF(e_sess), KPTR, "lx"},
155         {"sig", "PENDING", NULL, 0, pvar, NULL, 8, POFF(p_siglist), INT, "x"},
156         {"sigcatch", "CAUGHT",
157                 NULL, 0, evar, NULL, 8, EOFF(e_procsig.ps_sigcatch), UINT, "x"},
158         {"sigignore", "IGNORED",
159                 NULL, 0, evar, NULL, 8, EOFF(e_procsig.ps_sigignore), UINT, "x"},
160         {"sigmask", "BLOCKED",
161                 NULL, 0, pvar, NULL, 8, POFF(p_sigmask), UINT, "x"},
162         {"sl", "SL", NULL, 0, pvar, NULL, 3, POFF(p_slptime), UINT, "d"},
163         {"start", "STARTED", NULL, LJUST|USER, started, NULL, 7},
164         {"stat", "", "state"},
165         {"state", "STAT", NULL, 0, state, NULL, 4},
166         {"svgid", "SVGID", NULL, 0,
167                 evar, NULL, UIDLEN, EOFF(e_ucred.cr_svgid), UINT, UIDFMT},
168         {"svuid", "SVUID", NULL, 0,
169                 evar, NULL, UIDLEN, EOFF(e_ucred.cr_svuid), UINT, UIDFMT},
170         {"tdev", "TDEV", NULL, 0, tdev, NULL, 4},
171         {"time", "TIME", NULL, USER, cputime, NULL, 9},
172         {"tpgid", "TPGID",
173                 NULL, 0, evar, NULL, 4, EOFF(e_tpgid), UINT, PIDFMT},
174         {"tsess", "TSESS", NULL, 0, evar, NULL, 6, EOFF(e_tsess), KPTR, "lx"},
175         {"tsiz", "TSIZ", NULL, 0, tsize, NULL, 4},
176         {"tt", "TT ", NULL, 0, tname, NULL, 4},
177         {"tty", "TTY", NULL, LJUST, longtname, NULL, 8},
178         {"ucomm", "UCOMM", NULL, LJUST, ucomm, NULL, MAXCOMLEN},
179         {"uid", "UID", NULL, 0, evar, NULL, UIDLEN, EOFF(e_ucred.cr_uid),
180                 UINT, UIDFMT},
181         {"user", "USER", NULL, LJUST|DSIZ, uname, s_uname, USERLEN},
182         {"vsize", "", "vsz"},
183         {"vsz", "VSZ", NULL, 0, vsize, NULL, 5},
184         {"wchan", "WCHAN", NULL, LJUST, wchan, NULL, 6},
185         {"xstat", "XSTAT", NULL, 0, pvar, NULL, 4, POFF(p_xstat), USHORT, "x"},
186         {""},
187 };
188
189 void
190 showkey()
191 {
192         VAR *v;
193         int i;
194         char *p, *sep;
195
196         i = 0;
197         sep = "";
198         for (v = var; *(p = v->name); ++v) {
199                 int len = strlen(p);
200                 if (termwidth && (i += len + 1) > termwidth) {
201                         i = len;
202                         sep = "\n";
203                 }
204                 (void) printf("%s%s", sep, p);
205                 sep = " ";
206         }
207         (void) printf("\n");
208 }
209
210 void
211 parsefmt(p)
212         char *p;
213 {
214         static struct varent *vtail;
215
216 #define FMTSEP  " \t,\n"
217         while (p && *p) {
218                 char *cp;
219                 VAR *v;
220                 struct varent *vent;
221
222                 while ((cp = strsep(&p, FMTSEP)) != NULL && *cp == '\0')
223                         /* void */;
224                 if (cp == NULL || !(v = findvar(cp)))
225                         continue;
226                 if ((vent = malloc(sizeof(struct varent))) == NULL)
227                         err(1, NULL);
228                 vent->var = v;
229                 vent->next = NULL;
230                 if (vhead == NULL)
231                         vhead = vtail = vent;
232                 else {
233                         vtail->next = vent;
234                         vtail = vent;
235                 }
236         }
237         if (!vhead) {
238                 warnx("no valid keywords; valid keywords:");
239                 showkey();
240                 exit(1);
241         }
242 }
243
244 static VAR *
245 findvar(p)
246         char *p;
247 {
248         VAR *v, key;
249         char *hp;
250         int vcmp();
251
252         hp = strchr(p, '=');
253         if (hp)
254                 *hp++ = '\0';
255
256         key.name = p;
257         v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
258
259         if (v && v->alias) {
260                 if (hp) {
261                         warnx("%s: illegal keyword specification", p);
262                         eval = 1;
263                 }
264                 parsefmt(v->alias);
265                 return ((VAR *)NULL);
266         }
267         if (!v) {
268                 warnx("%s: keyword not found", p);
269                 eval = 1;
270         } else if (hp)
271                 v->header = hp;
272         return (v);
273 }
274
275 static int
276 vcmp(a, b)
277         const void *a, *b;
278 {
279         return (strcmp(((VAR *)a)->name, ((VAR *)b)->name));
280 }