ps.c revision 1.32 1 1.32 drochner /* $NetBSD: ps.c,v 1.32 1999/11/09 15:06:31 drochner Exp $ */
2 1.13 cgd
3 1.1 cgd /*-
4 1.11 cgd * Copyright (c) 1990, 1993, 1994
5 1.11 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.19 christos #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.19 christos __COPYRIGHT("@(#) Copyright (c) 1990, 1993, 1994\n\
39 1.19 christos The Regents of the University of California. All rights reserved.\n");
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.13 cgd #if 0
44 1.11 cgd static char sccsid[] = "@(#)ps.c 8.4 (Berkeley) 4/2/94";
45 1.13 cgd #else
46 1.32 drochner __RCSID("$NetBSD: ps.c,v 1.32 1999/11/09 15:06:31 drochner Exp $");
47 1.13 cgd #endif
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.1 cgd #include <sys/param.h>
51 1.1 cgd #include <sys/user.h>
52 1.1 cgd #include <sys/time.h>
53 1.1 cgd #include <sys/resource.h>
54 1.1 cgd #include <sys/proc.h>
55 1.1 cgd #include <sys/stat.h>
56 1.1 cgd #include <sys/ioctl.h>
57 1.11 cgd #include <sys/sysctl.h>
58 1.11 cgd
59 1.11 cgd #include <ctype.h>
60 1.11 cgd #include <err.h>
61 1.11 cgd #include <errno.h>
62 1.11 cgd #include <fcntl.h>
63 1.11 cgd #include <kvm.h>
64 1.17 pk #include <limits.h>
65 1.1 cgd #include <nlist.h>
66 1.11 cgd #include <paths.h>
67 1.25 mycroft #include <pwd.h>
68 1.1 cgd #include <stdio.h>
69 1.1 cgd #include <stdlib.h>
70 1.1 cgd #include <string.h>
71 1.11 cgd #include <unistd.h>
72 1.11 cgd
73 1.1 cgd #include "ps.h"
74 1.1 cgd
75 1.10 cgd #ifdef P_PPWAIT
76 1.1 cgd #define NEWVM
77 1.1 cgd #endif
78 1.1 cgd
79 1.1 cgd KINFO *kinfo;
80 1.1 cgd struct varent *vhead, *vtail;
81 1.1 cgd
82 1.1 cgd int eval; /* exit value */
83 1.1 cgd int rawcpu; /* -C */
84 1.1 cgd int sumrusage; /* -S */
85 1.28 bgrayson int dontuseprocfs=0; /* -K */
86 1.1 cgd int termwidth; /* width of screen (0 == infinity) */
87 1.1 cgd int totwidth; /* calculated width of requested variables */
88 1.1 cgd
89 1.31 jdolecek int needuser, needcomm, needenv, commandonly, use_procfs;
90 1.1 cgd
91 1.1 cgd enum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT;
92 1.1 cgd
93 1.29 jdolecek static KINFO *getkinfo_kvm __P((kvm_t *, int, int, int *, int));
94 1.11 cgd static char *kludge_oldps_options __P((char *));
95 1.11 cgd static int pscomp __P((const void *, const void *));
96 1.11 cgd static void saveuser __P((KINFO *));
97 1.11 cgd static void scanvars __P((void));
98 1.11 cgd static void usage __P((void));
99 1.19 christos int main __P((int, char *[]));
100 1.1 cgd
101 1.1 cgd char dfmt[] = "pid tt state time command";
102 1.1 cgd char jfmt[] = "user pid ppid pgid sess jobc state tt time command";
103 1.1 cgd char lfmt[] = "uid pid ppid cpu pri nice vsz rss wchan state tt time command";
104 1.1 cgd char o1[] = "pid";
105 1.1 cgd char o2[] = "tt state time command";
106 1.1 cgd char ufmt[] = "user pid %cpu %mem vsz rss tt state start time command";
107 1.11 cgd char vfmt[] = "pid state time sl re pagein vsz rss lim tsiz %cpu %mem command";
108 1.11 cgd
109 1.11 cgd kvm_t *kd;
110 1.1 cgd
111 1.11 cgd int
112 1.1 cgd main(argc, argv)
113 1.1 cgd int argc;
114 1.11 cgd char *argv[];
115 1.1 cgd {
116 1.11 cgd struct varent *vent;
117 1.1 cgd struct winsize ws;
118 1.21 mrg gid_t egid = getegid();
119 1.25 mycroft int ch, flag, i, fmt, lineno, nentries;
120 1.11 cgd int prtheader, wflag, what, xflg;
121 1.17 pk char *nlistf, *memf, *swapf, errbuf[_POSIX2_LINE_MAX];
122 1.26 kim char *ttname;
123 1.1 cgd
124 1.21 mrg (void)setegid(getgid());
125 1.1 cgd if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
126 1.1 cgd ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
127 1.1 cgd ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ws) == -1) ||
128 1.1 cgd ws.ws_col == 0)
129 1.1 cgd termwidth = 79;
130 1.1 cgd else
131 1.1 cgd termwidth = ws.ws_col - 1;
132 1.1 cgd
133 1.1 cgd if (argc > 1)
134 1.1 cgd argv[1] = kludge_oldps_options(argv[1]);
135 1.1 cgd
136 1.25 mycroft fmt = prtheader = wflag = xflg = 0;
137 1.25 mycroft what = KERN_PROC_UID;
138 1.25 mycroft flag = getuid();
139 1.1 cgd memf = nlistf = swapf = NULL;
140 1.1 cgd while ((ch = getopt(argc, argv,
141 1.28 bgrayson "acCeghjKLlM:mN:O:o:p:rSTt:U:uvW:wx")) != -1)
142 1.1 cgd switch((char)ch) {
143 1.1 cgd case 'a':
144 1.25 mycroft what = KERN_PROC_ALL;
145 1.25 mycroft flag = 0;
146 1.1 cgd break;
147 1.12 mycroft case 'c':
148 1.12 mycroft commandonly = 1;
149 1.12 mycroft break;
150 1.11 cgd case 'e': /* XXX set ufmt */
151 1.11 cgd needenv = 1;
152 1.11 cgd break;
153 1.1 cgd case 'C':
154 1.1 cgd rawcpu = 1;
155 1.1 cgd break;
156 1.1 cgd case 'g':
157 1.11 cgd break; /* no-op */
158 1.1 cgd case 'h':
159 1.1 cgd prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
160 1.1 cgd break;
161 1.1 cgd case 'j':
162 1.1 cgd parsefmt(jfmt);
163 1.15 mycroft fmt = 1;
164 1.1 cgd jfmt[0] = '\0';
165 1.1 cgd break;
166 1.28 bgrayson case 'K':
167 1.28 bgrayson dontuseprocfs=1;
168 1.28 bgrayson break;
169 1.11 cgd case 'L':
170 1.1 cgd showkey();
171 1.1 cgd exit(0);
172 1.23 mycroft /* NOTREACHED */
173 1.1 cgd case 'l':
174 1.1 cgd parsefmt(lfmt);
175 1.15 mycroft fmt = 1;
176 1.1 cgd lfmt[0] = '\0';
177 1.1 cgd break;
178 1.1 cgd case 'M':
179 1.1 cgd memf = optarg;
180 1.1 cgd break;
181 1.1 cgd case 'm':
182 1.1 cgd sortby = SORTMEM;
183 1.1 cgd break;
184 1.1 cgd case 'N':
185 1.1 cgd nlistf = optarg;
186 1.1 cgd break;
187 1.1 cgd case 'O':
188 1.1 cgd parsefmt(o1);
189 1.1 cgd parsefmt(optarg);
190 1.1 cgd parsefmt(o2);
191 1.1 cgd o1[0] = o2[0] = '\0';
192 1.15 mycroft fmt = 1;
193 1.1 cgd break;
194 1.1 cgd case 'o':
195 1.1 cgd parsefmt(optarg);
196 1.15 mycroft fmt = 1;
197 1.1 cgd break;
198 1.1 cgd case 'p':
199 1.25 mycroft what = KERN_PROC_PID;
200 1.25 mycroft flag = atol(optarg);
201 1.1 cgd xflg = 1;
202 1.1 cgd break;
203 1.1 cgd case 'r':
204 1.1 cgd sortby = SORTCPU;
205 1.1 cgd break;
206 1.1 cgd case 'S':
207 1.1 cgd sumrusage = 1;
208 1.1 cgd break;
209 1.1 cgd case 'T':
210 1.22 mycroft if ((ttname = ttyname(STDIN_FILENO)) == NULL)
211 1.11 cgd errx(1, "stdin: not a terminal");
212 1.22 mycroft goto tty;
213 1.22 mycroft case 't':
214 1.22 mycroft ttname = optarg;
215 1.22 mycroft tty: {
216 1.11 cgd struct stat sb;
217 1.11 cgd char *ttypath, pathbuf[MAXPATHLEN];
218 1.1 cgd
219 1.26 kim if (strcmp(ttname, "co") == 0)
220 1.1 cgd ttypath = _PATH_CONSOLE;
221 1.26 kim else if (*ttname != '/')
222 1.11 cgd (void)snprintf(ttypath = pathbuf,
223 1.26 kim sizeof(pathbuf), "%s%s", _PATH_TTY, ttname);
224 1.1 cgd else
225 1.26 kim ttypath = ttname;
226 1.11 cgd if (stat(ttypath, &sb) == -1)
227 1.11 cgd err(1, "%s", ttypath);
228 1.11 cgd if (!S_ISCHR(sb.st_mode))
229 1.11 cgd errx(1, "%s: not a terminal", ttypath);
230 1.25 mycroft what = KERN_PROC_TTY;
231 1.25 mycroft flag = sb.st_rdev;
232 1.1 cgd break;
233 1.1 cgd }
234 1.25 mycroft case 'U':
235 1.25 mycroft if (*optarg != '\0') {
236 1.25 mycroft struct passwd *pw;
237 1.25 mycroft char *ep;
238 1.25 mycroft
239 1.25 mycroft what = KERN_PROC_UID;
240 1.25 mycroft pw = getpwnam(optarg);
241 1.25 mycroft if (pw == NULL) {
242 1.25 mycroft errno = 0;
243 1.25 mycroft flag = strtoul(optarg, &ep, 10);
244 1.25 mycroft if (errno)
245 1.25 mycroft err(1, "%s", optarg);
246 1.25 mycroft if (*ep != '\0')
247 1.25 mycroft errx(1, "%s: illegal user name",
248 1.25 mycroft optarg);
249 1.25 mycroft } else
250 1.25 mycroft flag = pw->pw_uid;
251 1.25 mycroft }
252 1.25 mycroft break;
253 1.1 cgd case 'u':
254 1.1 cgd parsefmt(ufmt);
255 1.1 cgd sortby = SORTCPU;
256 1.15 mycroft fmt = 1;
257 1.1 cgd ufmt[0] = '\0';
258 1.1 cgd break;
259 1.1 cgd case 'v':
260 1.1 cgd parsefmt(vfmt);
261 1.1 cgd sortby = SORTMEM;
262 1.15 mycroft fmt = 1;
263 1.1 cgd vfmt[0] = '\0';
264 1.1 cgd break;
265 1.1 cgd case 'W':
266 1.1 cgd swapf = optarg;
267 1.1 cgd break;
268 1.1 cgd case 'w':
269 1.6 cgd if (wflag)
270 1.6 cgd termwidth = UNLIMITED;
271 1.6 cgd else if (termwidth < 131)
272 1.1 cgd termwidth = 131;
273 1.11 cgd wflag++;
274 1.1 cgd break;
275 1.1 cgd case 'x':
276 1.1 cgd xflg = 1;
277 1.1 cgd break;
278 1.1 cgd case '?':
279 1.1 cgd default:
280 1.1 cgd usage();
281 1.1 cgd }
282 1.1 cgd argc -= optind;
283 1.1 cgd argv += optind;
284 1.1 cgd
285 1.1 cgd #define BACKWARD_COMPATIBILITY
286 1.1 cgd #ifdef BACKWARD_COMPATIBILITY
287 1.1 cgd if (*argv) {
288 1.1 cgd nlistf = *argv;
289 1.1 cgd if (*++argv) {
290 1.1 cgd memf = *argv;
291 1.1 cgd if (*++argv)
292 1.1 cgd swapf = *argv;
293 1.1 cgd }
294 1.1 cgd }
295 1.1 cgd #endif
296 1.11 cgd /*
297 1.21 mrg * Discard setgid privileges. If not the running kernel, we toss
298 1.21 mrg * them away totally so that bad guys can't print interesting stuff
299 1.21 mrg * from kernel memory, otherwise switch back to kmem for the
300 1.21 mrg * duration of the kvm_openfiles() call.
301 1.11 cgd */
302 1.11 cgd if (nlistf != NULL || memf != NULL || swapf != NULL)
303 1.21 mrg (void)setgid(getgid());
304 1.21 mrg else
305 1.21 mrg (void)setegid(egid);
306 1.11 cgd
307 1.11 cgd kd = kvm_openfiles(nlistf, memf, swapf, O_RDONLY, errbuf);
308 1.29 jdolecek if (kd == 0) {
309 1.29 jdolecek if (dontuseprocfs)
310 1.29 jdolecek errx(1, "%s", errbuf);
311 1.29 jdolecek else {
312 1.29 jdolecek warnx("kvm_openfiles: %s", errbuf);
313 1.29 jdolecek fprintf(stderr, "ps: falling back to /proc-based lookup\n");
314 1.29 jdolecek }
315 1.29 jdolecek }
316 1.21 mrg
317 1.21 mrg if (nlistf == NULL && memf == NULL && swapf == NULL)
318 1.21 mrg (void)setgid(getgid());
319 1.1 cgd
320 1.15 mycroft if (!fmt)
321 1.1 cgd parsefmt(dfmt);
322 1.1 cgd
323 1.1 cgd /*
324 1.1 cgd * scan requested variables, noting what structures are needed,
325 1.1 cgd * and adjusting header widths as appropiate.
326 1.1 cgd */
327 1.1 cgd scanvars();
328 1.29 jdolecek
329 1.1 cgd /*
330 1.1 cgd * select procs
331 1.1 cgd */
332 1.29 jdolecek if (!kd || !(kinfo = getkinfo_kvm(kd, what, flag, &nentries, needuser)))
333 1.27 bgrayson {
334 1.28 bgrayson /* If/when the /proc-based code is ripped out
335 1.28 bgrayson * again, make sure all references to the -K
336 1.28 bgrayson * option are also pulled (getopt(), usage(),
337 1.28 bgrayson * man page). See the man page comments about
338 1.28 bgrayson * this for more details. */
339 1.27 bgrayson /* sysctl() ought to provide some sort of
340 1.27 bgrayson * always-working-but-minimal-functionality
341 1.27 bgrayson * method of providing at least some of the
342 1.27 bgrayson * process information. Unfortunately, such a
343 1.27 bgrayson * change will require too much work to be put
344 1.27 bgrayson * into 1.4. For now, enable this experimental
345 1.27 bgrayson * /proc-based support instead (if /proc is
346 1.27 bgrayson * mounted) to grab as much information as we can.
347 1.27 bgrayson * The guts of emulating kvm_getprocs() is in
348 1.27 bgrayson * the file procfs_ops.c. */
349 1.29 jdolecek if (kd)
350 1.29 jdolecek warnx("%s.", kvm_geterr(kd));
351 1.28 bgrayson if (dontuseprocfs) {
352 1.28 bgrayson exit(1);
353 1.28 bgrayson }
354 1.27 bgrayson /* procfs_getprocs supports all but the
355 1.27 bgrayson * KERN_PROC_RUID flag. */
356 1.29 jdolecek kinfo = getkinfo_procfs(what, flag, &nentries);
357 1.29 jdolecek if (kinfo == 0) {
358 1.28 bgrayson errx(1, "fallback /proc-based lookup also failed. %s",
359 1.28 bgrayson "Giving up...");
360 1.27 bgrayson }
361 1.28 bgrayson fprintf(stderr, "%s%s",
362 1.28 bgrayson "Warning: /proc does not provide ",
363 1.28 bgrayson "valid data for all fields.\n");
364 1.31 jdolecek use_procfs = 1;
365 1.27 bgrayson }
366 1.29 jdolecek
367 1.1 cgd /*
368 1.1 cgd * print header
369 1.1 cgd */
370 1.1 cgd printheader();
371 1.24 mycroft if (nentries == 0)
372 1.1 cgd exit(0);
373 1.1 cgd /*
374 1.1 cgd * sort proc list
375 1.1 cgd */
376 1.11 cgd qsort(kinfo, nentries, sizeof(KINFO), pscomp);
377 1.1 cgd /*
378 1.1 cgd * for each proc, call each variable output function.
379 1.1 cgd */
380 1.1 cgd for (i = lineno = 0; i < nentries; i++) {
381 1.14 mycroft KINFO *ki = &kinfo[i];
382 1.14 mycroft
383 1.14 mycroft if (xflg == 0 && (KI_EPROC(ki)->e_tdev == NODEV ||
384 1.14 mycroft (KI_PROC(ki)->p_flag & P_CONTROLT ) == 0))
385 1.1 cgd continue;
386 1.1 cgd for (vent = vhead; vent; vent = vent->next) {
387 1.14 mycroft (vent->var->oproc)(ki, vent);
388 1.1 cgd if (vent->next != NULL)
389 1.11 cgd (void)putchar(' ');
390 1.1 cgd }
391 1.11 cgd (void)putchar('\n');
392 1.11 cgd if (prtheader && lineno++ == prtheader - 4) {
393 1.11 cgd (void)putchar('\n');
394 1.1 cgd printheader();
395 1.1 cgd lineno = 0;
396 1.1 cgd }
397 1.1 cgd }
398 1.1 cgd exit(eval);
399 1.23 mycroft /* NOTREACHED */
400 1.1 cgd }
401 1.1 cgd
402 1.29 jdolecek static KINFO *
403 1.29 jdolecek getkinfo_kvm(kd, what, flag, nentriesp, needuser)
404 1.29 jdolecek kvm_t *kd;
405 1.29 jdolecek int what, flag, *nentriesp, needuser;
406 1.29 jdolecek {
407 1.29 jdolecek struct kinfo_proc *kp;
408 1.29 jdolecek KINFO *kinfo=NULL;
409 1.29 jdolecek size_t i;
410 1.29 jdolecek
411 1.29 jdolecek if ((kp = kvm_getprocs(kd, what, flag, nentriesp)) != 0)
412 1.29 jdolecek {
413 1.29 jdolecek if ((kinfo = malloc((*nentriesp) * sizeof(*kinfo))) == NULL)
414 1.32 drochner err(1, NULL);
415 1.30 jdolecek for (i = (*nentriesp); i-- > 0; kp++) {
416 1.29 jdolecek kinfo[i].ki_p = kp;
417 1.29 jdolecek if (needuser)
418 1.29 jdolecek saveuser(&kinfo[i]);
419 1.29 jdolecek }
420 1.29 jdolecek }
421 1.29 jdolecek
422 1.29 jdolecek return (kinfo);
423 1.29 jdolecek }
424 1.29 jdolecek
425 1.11 cgd static void
426 1.1 cgd scanvars()
427 1.1 cgd {
428 1.11 cgd struct varent *vent;
429 1.11 cgd VAR *v;
430 1.11 cgd int i;
431 1.1 cgd
432 1.1 cgd for (vent = vhead; vent; vent = vent->next) {
433 1.1 cgd v = vent->var;
434 1.1 cgd i = strlen(v->header);
435 1.1 cgd if (v->width < i)
436 1.1 cgd v->width = i;
437 1.1 cgd totwidth += v->width + 1; /* +1 for space */
438 1.1 cgd if (v->flag & USER)
439 1.1 cgd needuser = 1;
440 1.1 cgd if (v->flag & COMM)
441 1.1 cgd needcomm = 1;
442 1.1 cgd }
443 1.1 cgd totwidth--;
444 1.11 cgd }
445 1.1 cgd
446 1.11 cgd static void
447 1.1 cgd saveuser(ki)
448 1.1 cgd KINFO *ki;
449 1.1 cgd {
450 1.11 cgd struct pstats pstats;
451 1.11 cgd struct usave *usp;
452 1.5 cgd
453 1.11 cgd usp = &ki->ki_u;
454 1.30 jdolecek if (kvm_read(kd, (u_long)&KI_PROC(ki)->p_addr->u_stats,
455 1.11 cgd (char *)&pstats, sizeof(pstats)) == sizeof(pstats)) {
456 1.1 cgd /*
457 1.11 cgd * The u-area might be swapped out, and we can't get
458 1.11 cgd * at it because we have a crashdump and no swap.
459 1.11 cgd * If it's here fill in these fields, otherwise, just
460 1.11 cgd * leave them 0.
461 1.1 cgd */
462 1.11 cgd usp->u_start = pstats.p_start;
463 1.11 cgd usp->u_ru = pstats.p_ru;
464 1.11 cgd usp->u_cru = pstats.p_cru;
465 1.11 cgd usp->u_valid = 1;
466 1.1 cgd } else
467 1.11 cgd usp->u_valid = 0;
468 1.1 cgd }
469 1.1 cgd
470 1.11 cgd static int
471 1.11 cgd pscomp(a, b)
472 1.11 cgd const void *a, *b;
473 1.1 cgd {
474 1.1 cgd int i;
475 1.1 cgd #ifdef NEWVM
476 1.11 cgd #define VSIZE(k) (KI_EPROC(k)->e_vm.vm_dsize + KI_EPROC(k)->e_vm.vm_ssize + \
477 1.11 cgd KI_EPROC(k)->e_vm.vm_tsize)
478 1.1 cgd #else
479 1.1 cgd #define VSIZE(k) ((k)->ki_p->p_dsize + (k)->ki_p->p_ssize + (k)->ki_e->e_xsize)
480 1.1 cgd #endif
481 1.1 cgd
482 1.1 cgd if (sortby == SORTCPU)
483 1.11 cgd return (getpcpu((KINFO *)b) - getpcpu((KINFO *)a));
484 1.1 cgd if (sortby == SORTMEM)
485 1.11 cgd return (VSIZE((KINFO *)b) - VSIZE((KINFO *)a));
486 1.11 cgd i = KI_EPROC((KINFO *)a)->e_tdev - KI_EPROC((KINFO *)b)->e_tdev;
487 1.1 cgd if (i == 0)
488 1.11 cgd i = KI_PROC((KINFO *)a)->p_pid - KI_PROC((KINFO *)b)->p_pid;
489 1.1 cgd return (i);
490 1.1 cgd }
491 1.1 cgd
492 1.1 cgd /*
493 1.1 cgd * ICK (all for getopt), would rather hide the ugliness
494 1.1 cgd * here than taint the main code.
495 1.1 cgd *
496 1.1 cgd * ps foo -> ps -foo
497 1.1 cgd * ps 34 -> ps -p34
498 1.1 cgd *
499 1.25 mycroft * The old convention that 't' with no trailing tty arg means the user's
500 1.1 cgd * tty, is only supported if argv[1] doesn't begin with a '-'. This same
501 1.1 cgd * feature is available with the option 'T', which takes no argument.
502 1.1 cgd */
503 1.11 cgd static char *
504 1.1 cgd kludge_oldps_options(s)
505 1.1 cgd char *s;
506 1.1 cgd {
507 1.1 cgd size_t len;
508 1.1 cgd char *newopts, *ns, *cp;
509 1.1 cgd
510 1.1 cgd len = strlen(s);
511 1.16 thorpej if ((newopts = ns = malloc(len + 3)) == NULL)
512 1.32 drochner err(1, NULL);
513 1.1 cgd /*
514 1.1 cgd * options begin with '-'
515 1.1 cgd */
516 1.1 cgd if (*s != '-')
517 1.1 cgd *ns++ = '-'; /* add option flag */
518 1.1 cgd /*
519 1.1 cgd * gaze to end of argv[1]
520 1.1 cgd */
521 1.1 cgd cp = s + len - 1;
522 1.1 cgd /*
523 1.1 cgd * if last letter is a 't' flag with no argument (in the context
524 1.1 cgd * of the oldps options -- option string NOT starting with a '-' --
525 1.1 cgd * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)).
526 1.1 cgd */
527 1.1 cgd if (*cp == 't' && *s != '-')
528 1.1 cgd *cp = 'T';
529 1.1 cgd else {
530 1.1 cgd /*
531 1.1 cgd * otherwise check for trailing number, which *may* be a
532 1.1 cgd * pid.
533 1.1 cgd */
534 1.1 cgd while (cp >= s && isdigit(*cp))
535 1.1 cgd --cp;
536 1.1 cgd }
537 1.1 cgd cp++;
538 1.11 cgd memmove(ns, s, (size_t)(cp - s)); /* copy up to trailing number */
539 1.1 cgd ns += cp - s;
540 1.1 cgd /*
541 1.1 cgd * if there's a trailing number, and not a preceding 'p' (pid) or
542 1.1 cgd * 't' (tty) flag, then assume it's a pid and insert a 'p' flag.
543 1.1 cgd */
544 1.25 mycroft if (isdigit(*cp) &&
545 1.25 mycroft (cp == s || (cp[-1] != 'U' && cp[-1] != 't' && cp[-1] != 'p' &&
546 1.25 mycroft (cp - 1 == s || cp[-2] != 't'))))
547 1.1 cgd *ns++ = 'p';
548 1.18 mrg /* and append the number */
549 1.18 mrg (void)strcpy(ns, cp); /* XXX strcpy is safe */
550 1.1 cgd
551 1.1 cgd return (newopts);
552 1.1 cgd }
553 1.1 cgd
554 1.11 cgd static void
555 1.11 cgd usage()
556 1.1 cgd {
557 1.1 cgd
558 1.11 cgd (void)fprintf(stderr,
559 1.11 cgd "usage:\t%s\n\t %s\n\t%s\n",
560 1.28 bgrayson "ps [-aChjKlmrSTuvwx] [-O|o fmt] [-p pid] [-t tty]",
561 1.11 cgd "[-M core] [-N system] [-W swap]",
562 1.11 cgd "ps [-L]");
563 1.1 cgd exit(1);
564 1.23 mycroft /* NOTREACHED */
565 1.1 cgd }
566