ps.c revision 1.29 1 /* $NetBSD: ps.c,v 1.29 1999/10/15 19:31:25 jdolecek 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.29 1999/10/15 19:31:25 jdolecek 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 KINFO *getkinfo_kvm __P((kvm_t *, int, int, int *, int));
94 static char *kludge_oldps_options __P((char *));
95 static int pscomp __P((const void *, const void *));
96 static void saveuser __P((KINFO *));
97 static void scanvars __P((void));
98 static void usage __P((void));
99 int main __P((int, char *[]));
100
101 char dfmt[] = "pid tt state time command";
102 char jfmt[] = "user pid ppid pgid sess jobc state tt time command";
103 char lfmt[] = "uid pid ppid cpu pri nice vsz rss wchan state tt time command";
104 char o1[] = "pid";
105 char o2[] = "tt state time command";
106 char ufmt[] = "user pid %cpu %mem vsz rss tt state start time command";
107 char vfmt[] = "pid state time sl re pagein vsz rss lim tsiz %cpu %mem command";
108
109 kvm_t *kd;
110
111 int
112 main(argc, argv)
113 int argc;
114 char *argv[];
115 {
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 if (dontuseprocfs)
310 errx(1, "%s", errbuf);
311 else {
312 warnx("kvm_openfiles: %s", errbuf);
313 fprintf(stderr, "ps: falling back to /proc-based lookup\n");
314 }
315 }
316
317 if (nlistf == NULL && memf == NULL && swapf == NULL)
318 (void)setgid(getgid());
319
320 if (!fmt)
321 parsefmt(dfmt);
322
323 /*
324 * scan requested variables, noting what structures are needed,
325 * and adjusting header widths as appropiate.
326 */
327 scanvars();
328
329 /*
330 * select procs
331 */
332 if (!kd || !(kinfo = getkinfo_kvm(kd, what, flag, &nentries, needuser)))
333 {
334 /* If/when the /proc-based code is ripped out
335 * again, make sure all references to the -K
336 * option are also pulled (getopt(), usage(),
337 * man page). See the man page comments about
338 * this for more details. */
339 /* sysctl() ought to provide some sort of
340 * always-working-but-minimal-functionality
341 * method of providing at least some of the
342 * process information. Unfortunately, such a
343 * change will require too much work to be put
344 * into 1.4. For now, enable this experimental
345 * /proc-based support instead (if /proc is
346 * mounted) to grab as much information as we can.
347 * The guts of emulating kvm_getprocs() is in
348 * the file procfs_ops.c. */
349 if (kd)
350 warnx("%s.", kvm_geterr(kd));
351 if (dontuseprocfs) {
352 exit(1);
353 }
354 /* procfs_getprocs supports all but the
355 * KERN_PROC_RUID flag. */
356 kinfo = getkinfo_procfs(what, flag, &nentries);
357 if (kinfo == 0) {
358 errx(1, "fallback /proc-based lookup also failed. %s",
359 "Giving up...");
360 }
361 fprintf(stderr, "%s%s",
362 "Warning: /proc does not provide ",
363 "valid data for all fields.\n");
364 }
365
366 /*
367 * print header
368 */
369 printheader();
370 if (nentries == 0)
371 exit(0);
372 /*
373 * sort proc list
374 */
375 qsort(kinfo, nentries, sizeof(KINFO), pscomp);
376 /*
377 * for each proc, call each variable output function.
378 */
379 for (i = lineno = 0; i < nentries; i++) {
380 KINFO *ki = &kinfo[i];
381
382 if (xflg == 0 && (KI_EPROC(ki)->e_tdev == NODEV ||
383 (KI_PROC(ki)->p_flag & P_CONTROLT ) == 0))
384 continue;
385 for (vent = vhead; vent; vent = vent->next) {
386 (vent->var->oproc)(ki, vent);
387 if (vent->next != NULL)
388 (void)putchar(' ');
389 }
390 (void)putchar('\n');
391 if (prtheader && lineno++ == prtheader - 4) {
392 (void)putchar('\n');
393 printheader();
394 lineno = 0;
395 }
396 }
397 exit(eval);
398 /* NOTREACHED */
399 }
400
401 static KINFO *
402 getkinfo_kvm(kd, what, flag, nentriesp, needuser)
403 kvm_t *kd;
404 int what, flag, *nentriesp, needuser;
405 {
406 struct kinfo_proc *kp;
407 KINFO *kinfo=NULL;
408 size_t i;
409
410 if ((kp = kvm_getprocs(kd, what, flag, nentriesp)) != 0)
411 {
412 if ((kinfo = malloc((*nentriesp) * sizeof(*kinfo))) == NULL)
413 err(1, "%s", "");
414 for (i = *nentriesp; --i >= 0; ++kp) {
415 kinfo[i].ki_p = kp;
416 if (needuser)
417 saveuser(&kinfo[i]);
418 }
419 }
420
421 return (kinfo);
422 }
423
424 static void
425 scanvars()
426 {
427 struct varent *vent;
428 VAR *v;
429 int i;
430
431 for (vent = vhead; vent; vent = vent->next) {
432 v = vent->var;
433 i = strlen(v->header);
434 if (v->width < i)
435 v->width = i;
436 totwidth += v->width + 1; /* +1 for space */
437 if (v->flag & USER)
438 needuser = 1;
439 if (v->flag & COMM)
440 needcomm = 1;
441 }
442 totwidth--;
443 }
444
445 static void
446 saveuser(ki)
447 KINFO *ki;
448 {
449 struct pstats pstats;
450 struct usave *usp;
451
452 usp = &ki->ki_u;
453 if (kd && kvm_read(kd, (u_long)&KI_PROC(ki)->p_addr->u_stats,
454 (char *)&pstats, sizeof(pstats)) == sizeof(pstats)) {
455 /*
456 * The u-area might be swapped out, and we can't get
457 * at it because we have a crashdump and no swap.
458 * If it's here fill in these fields, otherwise, just
459 * leave them 0.
460 */
461 usp->u_start = pstats.p_start;
462 usp->u_ru = pstats.p_ru;
463 usp->u_cru = pstats.p_cru;
464 usp->u_valid = 1;
465 } else
466 usp->u_valid = 0;
467 }
468
469 static int
470 pscomp(a, b)
471 const void *a, *b;
472 {
473 int i;
474 #ifdef NEWVM
475 #define VSIZE(k) (KI_EPROC(k)->e_vm.vm_dsize + KI_EPROC(k)->e_vm.vm_ssize + \
476 KI_EPROC(k)->e_vm.vm_tsize)
477 #else
478 #define VSIZE(k) ((k)->ki_p->p_dsize + (k)->ki_p->p_ssize + (k)->ki_e->e_xsize)
479 #endif
480
481 if (sortby == SORTCPU)
482 return (getpcpu((KINFO *)b) - getpcpu((KINFO *)a));
483 if (sortby == SORTMEM)
484 return (VSIZE((KINFO *)b) - VSIZE((KINFO *)a));
485 i = KI_EPROC((KINFO *)a)->e_tdev - KI_EPROC((KINFO *)b)->e_tdev;
486 if (i == 0)
487 i = KI_PROC((KINFO *)a)->p_pid - KI_PROC((KINFO *)b)->p_pid;
488 return (i);
489 }
490
491 /*
492 * ICK (all for getopt), would rather hide the ugliness
493 * here than taint the main code.
494 *
495 * ps foo -> ps -foo
496 * ps 34 -> ps -p34
497 *
498 * The old convention that 't' with no trailing tty arg means the user's
499 * tty, is only supported if argv[1] doesn't begin with a '-'. This same
500 * feature is available with the option 'T', which takes no argument.
501 */
502 static char *
503 kludge_oldps_options(s)
504 char *s;
505 {
506 size_t len;
507 char *newopts, *ns, *cp;
508
509 len = strlen(s);
510 if ((newopts = ns = malloc(len + 3)) == NULL)
511 err(1, "%s", "");
512 /*
513 * options begin with '-'
514 */
515 if (*s != '-')
516 *ns++ = '-'; /* add option flag */
517 /*
518 * gaze to end of argv[1]
519 */
520 cp = s + len - 1;
521 /*
522 * if last letter is a 't' flag with no argument (in the context
523 * of the oldps options -- option string NOT starting with a '-' --
524 * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)).
525 */
526 if (*cp == 't' && *s != '-')
527 *cp = 'T';
528 else {
529 /*
530 * otherwise check for trailing number, which *may* be a
531 * pid.
532 */
533 while (cp >= s && isdigit(*cp))
534 --cp;
535 }
536 cp++;
537 memmove(ns, s, (size_t)(cp - s)); /* copy up to trailing number */
538 ns += cp - s;
539 /*
540 * if there's a trailing number, and not a preceding 'p' (pid) or
541 * 't' (tty) flag, then assume it's a pid and insert a 'p' flag.
542 */
543 if (isdigit(*cp) &&
544 (cp == s || (cp[-1] != 'U' && cp[-1] != 't' && cp[-1] != 'p' &&
545 (cp - 1 == s || cp[-2] != 't'))))
546 *ns++ = 'p';
547 /* and append the number */
548 (void)strcpy(ns, cp); /* XXX strcpy is safe */
549
550 return (newopts);
551 }
552
553 static void
554 usage()
555 {
556
557 (void)fprintf(stderr,
558 "usage:\t%s\n\t %s\n\t%s\n",
559 "ps [-aChjKlmrSTuvwx] [-O|o fmt] [-p pid] [-t tty]",
560 "[-M core] [-N system] [-W swap]",
561 "ps [-L]");
562 exit(1);
563 /* NOTREACHED */
564 }
565