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