Commit | Line | Data |
---|---|---|
f700a7ee | 1 | static char *sccsid = "@(#)ps.c 4.7 (Berkeley) %G%"; |
aafc2e2f BJ |
2 | /* |
3 | * ps; VAX 4BSD version | |
4 | */ | |
5 | ||
6 | #include <stdio.h> | |
7 | #include <ctype.h> | |
8 | #include <nlist.h> | |
9 | #include <pwd.h> | |
10 | #include <sys/param.h> | |
11 | #include <sys/tty.h> | |
12 | #include <sys/dir.h> | |
13 | #include <sys/user.h> | |
14 | #include <sys/proc.h> | |
15 | #include <sys/pte.h> | |
16 | #include <sys/vm.h> | |
17 | #include <sys/text.h> | |
18 | #include <sys/stat.h> | |
7f0fa372 | 19 | #include <math.h> |
aafc2e2f BJ |
20 | |
21 | struct nlist nl[] = { | |
22 | { "_proc" }, | |
23 | #define X_PROC 0 | |
24 | { "_Usrptmap" }, | |
25 | #define X_USRPTMA 1 | |
26 | { "_usrpt" }, | |
27 | #define X_USRPT 2 | |
28 | { "_text" }, | |
29 | #define X_TEXT 3 | |
30 | { "_nswap" }, | |
31 | #define X_NSWAP 4 | |
c67277e8 BJ |
32 | { "_maxslp" }, |
33 | #define X_MAXSLP 5 | |
7f0fa372 BJ |
34 | { "_ccpu" }, |
35 | #define X_CCPU 6 | |
0f0e0052 BJ |
36 | { "_ecmx" }, |
37 | #define X_ECMX 7 | |
aafc2e2f BJ |
38 | { 0 }, |
39 | }; | |
40 | ||
41 | struct savcom { | |
42 | union { | |
43 | struct lsav *lp; | |
44 | float u_pctcpu; | |
45 | struct vsav *vp; | |
46 | int s_ssiz; | |
47 | } sun; | |
48 | struct asav *ap; | |
49 | } savcom[NPROC]; | |
50 | ||
51 | struct asav { | |
52 | char *a_cmdp; | |
53 | int a_flag; | |
c67277e8 | 54 | short a_stat, a_uid, a_pid, a_nice, a_pri, a_slptime, a_time; |
0f0e0052 BJ |
55 | size_t a_size, a_rss, a_tsiz, a_txtrss; |
56 | short a_xccount; | |
aafc2e2f BJ |
57 | char a_tty[DIRSIZ+1]; |
58 | dev_t a_ttyd; | |
c67277e8 | 59 | time_t a_cpu; |
aafc2e2f BJ |
60 | }; |
61 | ||
62 | char *lhdr; | |
aafc2e2f BJ |
63 | struct lsav { |
64 | short l_ppid; | |
c67277e8 | 65 | char l_cpu; |
aafc2e2f BJ |
66 | int l_addr; |
67 | caddr_t l_wchan; | |
68 | }; | |
69 | ||
70 | char *uhdr; | |
aafc2e2f | 71 | char *shdr; |
aafc2e2f BJ |
72 | |
73 | char *vhdr; | |
aafc2e2f | 74 | struct vsav { |
0f0e0052 BJ |
75 | u_int v_majflt; |
76 | size_t v_swrss, v_txtswrss; | |
77 | float v_pctcpu; | |
aafc2e2f BJ |
78 | }; |
79 | ||
80 | struct proc proc[8]; /* 8 = a few, for less syscalls */ | |
81 | struct proc *mproc; | |
82 | struct text *text; | |
83 | ||
f700a7ee | 84 | int paduser1; /* avoid hardware mem clobbering botch */ |
aafc2e2f BJ |
85 | union { |
86 | struct user user; | |
87 | char upages[UPAGES][NBPG]; | |
88 | } user; | |
89 | #define u user.user | |
f700a7ee | 90 | int paduser2; /* avoid hardware mem clobbering botch */ |
aafc2e2f BJ |
91 | |
92 | #define clear(x) ((int)x & 0x7fffffff) | |
93 | ||
94 | int chkpid; | |
95 | int aflg, cflg, eflg, gflg, kflg, lflg, sflg, uflg, vflg, xflg; | |
96 | char *tptr; | |
c67277e8 | 97 | char *gettty(), *getcmd(), *getname(), *savestr(), *alloc(), *state(); |
0f0e0052 | 98 | double pcpu(), pmem(); |
aafc2e2f | 99 | int pscomp(); |
c67277e8 | 100 | int nswap, maxslp; |
7f0fa372 | 101 | double ccpu; |
0f0e0052 | 102 | int ecmx; |
aafc2e2f BJ |
103 | struct pte *Usrptma, *usrpt; |
104 | ||
105 | struct ttys { | |
106 | char name[DIRSIZ+1]; | |
107 | dev_t ttyd; | |
108 | struct ttys *next; | |
109 | struct ttys *cand; | |
110 | } *allttys, *cand[16]; | |
111 | ||
112 | struct savcom savcom[NPROC]; | |
113 | int npr; | |
114 | ||
115 | int cmdstart; | |
116 | int twidth; | |
117 | char *kmemf, *memf, *swapf, *nlistf; | |
118 | int kmem, mem, swap; | |
1fb6a666 | 119 | int rawcpu, sumcpu; |
aafc2e2f BJ |
120 | |
121 | int pcbpf; | |
122 | int argaddr; | |
123 | extern char _sobuf[]; | |
124 | ||
125 | main(argc, argv) | |
126 | char **argv; | |
127 | { | |
128 | register int i, j; | |
129 | register char *ap; | |
130 | int uid; | |
131 | off_t procp; | |
132 | ||
133 | if (chdir("/dev") < 0) { | |
134 | perror("/dev"); | |
135 | exit(1); | |
136 | } | |
137 | twidth = 80; | |
138 | setbuf(stdout, _sobuf); | |
139 | argc--, argv++; | |
140 | if (argc > 0) { | |
141 | ap = argv[0]; | |
142 | while (*ap) switch (*ap++) { | |
143 | ||
0f0e0052 BJ |
144 | case 'C': |
145 | rawcpu++; | |
146 | break; | |
1fb6a666 BJ |
147 | case 'S': |
148 | sumcpu++; | |
149 | break; | |
aafc2e2f BJ |
150 | case 'a': |
151 | aflg++; | |
152 | break; | |
153 | case 'c': | |
154 | cflg = !cflg; | |
155 | break; | |
156 | case 'e': | |
157 | eflg++; | |
158 | break; | |
159 | case 'g': | |
160 | gflg++; | |
161 | break; | |
162 | case 'k': | |
163 | kflg++; | |
164 | break; | |
165 | case 'l': | |
166 | lflg++; | |
167 | break; | |
168 | case 's': | |
169 | sflg++; | |
170 | break; | |
171 | case 't': | |
172 | if (*ap) | |
173 | tptr = ap; | |
174 | aflg++; | |
175 | gflg++; | |
176 | if (*tptr == '?') | |
177 | xflg++; | |
178 | while (*ap) | |
179 | ap++; | |
180 | break; | |
181 | case 'u': | |
182 | uflg++; | |
183 | break; | |
184 | case 'v': | |
185 | cflg = 1; | |
186 | vflg++; | |
187 | break; | |
188 | case 'w': | |
189 | if (twidth == 80) | |
190 | twidth = 132; | |
191 | else | |
192 | twidth = BUFSIZ; | |
193 | break; | |
194 | case 'x': | |
195 | xflg++; | |
196 | break; | |
197 | default: | |
198 | if (!isdigit(ap[-1])) | |
199 | break; | |
200 | chkpid = atoi(--ap); | |
201 | *ap = 0; | |
202 | aflg++; | |
203 | xflg++; | |
204 | break; | |
205 | } | |
206 | } | |
207 | openfiles(argc, argv); | |
208 | getkvars(argc, argv); | |
209 | getdev(); | |
210 | uid = getuid(); | |
211 | printhdr(); | |
212 | procp = nl[X_PROC].n_value; | |
213 | for (i=0; i<NPROC; i += 8) { | |
214 | lseek(kmem, (char *)procp, 0); | |
215 | j = NPROC - i; | |
216 | if (j > 8) | |
217 | j = 8; | |
218 | j *= sizeof (struct proc); | |
219 | if (read(kmem, (char *)proc, j) != j) | |
220 | cantread("proc table", kmemf); | |
221 | procp += j; | |
222 | for (j = j / sizeof (struct proc) - 1; j >= 0; j--) { | |
223 | mproc = &proc[j]; | |
224 | if (mproc->p_stat == 0 || | |
225 | mproc->p_pgrp == 0 && xflg == 0) | |
c67277e8 | 226 | continue; |
aafc2e2f BJ |
227 | if (tptr == 0 && gflg == 0 && xflg == 0 && |
228 | mproc->p_ppid == 1 && (mproc->p_flag&SDETACH) == 0) | |
229 | continue; | |
230 | if (uid != mproc->p_uid && aflg==0 || | |
231 | chkpid != 0 && chkpid != mproc->p_pid) | |
232 | continue; | |
233 | if (vflg && gflg == 0 && xflg == 0) { | |
234 | if (mproc->p_stat == SZOMB || | |
235 | mproc->p_flag&SWEXIT) | |
236 | continue; | |
237 | if (mproc->p_slptime > MAXSLP && | |
238 | (mproc->p_stat == SSLEEP || | |
239 | mproc->p_stat == SSTOP)) | |
240 | continue; | |
241 | } | |
242 | save(); | |
243 | } | |
244 | } | |
245 | qsort(savcom, npr, sizeof(savcom[0]), pscomp); | |
246 | for (i=0; i<npr; i++) { | |
247 | register struct savcom *sp = &savcom[i]; | |
248 | if (lflg) | |
249 | lpr(sp); | |
250 | else if (vflg) | |
251 | vpr(sp); | |
252 | else if (uflg) | |
253 | upr(sp); | |
254 | else | |
255 | spr(sp); | |
256 | if (sp->ap->a_flag & SWEXIT) | |
257 | printf(" <exiting>"); | |
258 | else if (sp->ap->a_stat == SZOMB) | |
259 | printf(" <defunct>"); | |
260 | else if (sp->ap->a_pid == 0) | |
261 | printf(" swapper"); | |
262 | else if (sp->ap->a_pid == 2) | |
263 | printf(" pagedaemon"); | |
264 | else | |
265 | printf(" %.*s", twidth - cmdstart - 2, sp->ap->a_cmdp); | |
266 | printf("\n"); | |
267 | } | |
268 | exit(npr == 0); | |
269 | } | |
270 | ||
271 | openfiles(argc, argv) | |
272 | char **argv; | |
273 | { | |
274 | ||
275 | kmemf = "kmem"; | |
276 | if (kflg) | |
277 | kmemf = argc > 1 ? argv[1] : "/vmcore"; | |
278 | kmem = open(kmemf, 0); | |
279 | if (kmem < 0) { | |
280 | perror(kmemf); | |
281 | exit(1); | |
282 | } | |
283 | if (kflg) { | |
284 | mem = kmem; | |
285 | memf = kmemf; | |
286 | } else { | |
287 | memf = "mem"; | |
288 | mem = open(memf, 0); | |
289 | if (mem < 0) { | |
290 | perror(memf); | |
291 | exit(1); | |
292 | } | |
293 | } | |
294 | swapf = argc>2 ? argv[2]: "drum"; | |
295 | swap = open(swapf, 0); | |
296 | if (swap < 0) { | |
297 | perror(swapf); | |
298 | exit(1); | |
299 | } | |
300 | } | |
301 | ||
302 | getkvars(argc, argv) | |
303 | char **argv; | |
304 | { | |
305 | register struct nlist *nlp; | |
306 | ||
307 | nlistf = argc > 3 ? argv[3] : "/vmunix"; | |
308 | nlist(nlistf, nl); | |
309 | if (nl[0].n_type == 0) { | |
310 | fprintf(stderr, "%s: No namelist\n", nlistf); | |
311 | exit(1); | |
312 | } | |
313 | if (kflg) | |
314 | for (nlp = nl; nlp < &nl[sizeof (nl)/sizeof (nl[0])]; nlp++) | |
315 | nlp->n_value = clear(nlp->n_value); | |
316 | Usrptma = (struct pte *)nl[X_USRPTMA].n_value; | |
317 | usrpt = (struct pte *)nl[X_USRPT].n_value; | |
318 | lseek(kmem, (long)nl[X_NSWAP].n_value, 0); | |
319 | if (read(kmem, &nswap, sizeof (nswap)) != sizeof (nswap)) { | |
320 | cantread("nswap", kmemf); | |
321 | exit(1); | |
322 | } | |
c67277e8 BJ |
323 | lseek(kmem, (long)nl[X_MAXSLP].n_value, 0); |
324 | if (read(kmem, &maxslp, sizeof (maxslp)) != sizeof (maxslp)) { | |
325 | cantread("maxslp", kmemf); | |
326 | exit(1); | |
327 | } | |
7f0fa372 BJ |
328 | lseek(kmem, (long)nl[X_CCPU].n_value, 0); |
329 | if (read(kmem, &ccpu, sizeof (ccpu)) != sizeof (ccpu)) { | |
330 | cantread("ccpu", kmemf); | |
331 | exit(1); | |
332 | } | |
0f0e0052 BJ |
333 | lseek(kmem, (long)nl[X_ECMX].n_value, 0); |
334 | if (read(kmem, &ecmx, sizeof (ecmx)) != sizeof (ecmx)) { | |
335 | cantread("ecmx", kmemf); | |
336 | exit(1); | |
337 | } | |
338 | if (uflg || vflg) { | |
aafc2e2f BJ |
339 | text = (struct text *)alloc(NTEXT * sizeof (struct text)); |
340 | if (text == 0) { | |
341 | fprintf(stderr, "no room for text table\n"); | |
342 | exit(1); | |
343 | } | |
344 | lseek(kmem, (long)nl[X_TEXT].n_value, 0); | |
345 | if (read(kmem, (char *)text, NTEXT * sizeof (struct text)) | |
346 | != NTEXT * sizeof (struct text)) { | |
347 | cantread("text table", kmemf); | |
348 | exit(1); | |
349 | } | |
350 | } | |
351 | } | |
352 | ||
353 | printhdr() | |
354 | { | |
355 | char *hdr; | |
356 | ||
357 | if (sflg+lflg+vflg+uflg > 1) { | |
358 | fprintf(stderr, "ps: specify only one of s,l,v and u\n"); | |
359 | exit(1); | |
360 | } | |
361 | hdr = lflg ? lhdr : (vflg ? vhdr : (uflg ? uhdr : shdr)); | |
362 | if (lflg+vflg+uflg+sflg == 0) | |
c67277e8 | 363 | hdr += strlen("SSIZ "); |
aafc2e2f BJ |
364 | cmdstart = strlen(hdr); |
365 | printf("%s COMMAND\n", hdr); | |
366 | fflush(stdout); | |
367 | } | |
368 | ||
369 | cantread(what, fromwhat) | |
370 | char *what, *fromwhat; | |
371 | { | |
372 | ||
373 | fprintf(stderr, "ps: error reading %s from %s", what, fromwhat); | |
374 | } | |
375 | ||
376 | struct direct dbuf; | |
377 | int dialbase; | |
378 | ||
379 | getdev() | |
380 | { | |
381 | register FILE *df; | |
382 | register struct ttys *dp; | |
383 | ||
384 | dialbase = -1; | |
385 | if ((df = fopen(".", "r")) == NULL) { | |
386 | fprintf(stderr, "Can't open . in /dev\n"); | |
387 | exit(1); | |
388 | } | |
389 | while (fread((char *)&dbuf, sizeof(dbuf), 1, df) == 1) { | |
390 | if (dbuf.d_ino == 0) | |
391 | continue; | |
392 | maybetty(dp); | |
393 | } | |
394 | fclose(df); | |
395 | } | |
396 | ||
397 | /* | |
398 | * Attempt to avoid stats by guessing minor device | |
399 | * numbers from tty names. Console is known, | |
400 | * know that r(hp|up|mt) are unlikely as are different mem's, | |
401 | * floppy, null, tty, etc. | |
402 | */ | |
403 | maybetty() | |
404 | { | |
405 | register char *cp = dbuf.d_name; | |
406 | register struct ttys *dp; | |
407 | int x; | |
408 | struct stat stb; | |
409 | ||
410 | switch (cp[0]) { | |
411 | ||
412 | case 'c': | |
413 | if (!strcmp(cp, "console")) { | |
414 | x = 0; | |
415 | goto donecand; | |
416 | } | |
417 | /* cu[la]? are possible!?! don't rule them out */ | |
418 | break; | |
419 | ||
420 | case 'd': | |
421 | if (!strcmp(cp, "drum")) | |
422 | return (0); | |
423 | break; | |
424 | ||
425 | case 'f': | |
426 | if (!strcmp(cp, "floppy")) | |
427 | return (0); | |
428 | break; | |
429 | ||
430 | case 'k': | |
431 | cp++; | |
432 | if (*cp == 'U') | |
433 | cp++; | |
434 | goto trymem; | |
435 | ||
436 | case 'r': | |
437 | cp++; | |
438 | if (*cp == 'r' || *cp == 'u' || *cp == 'h') | |
439 | cp++; | |
440 | #define is(a,b) cp[0] == 'a' && cp[1] == 'b' | |
441 | if (is(r,p) || is(u,p) || is(r,k) || is(r,m) || is(m,t)) { | |
442 | cp += 2; | |
443 | if (isdigit(*cp) && cp[2] == 0) | |
444 | return (0); | |
445 | } | |
446 | break; | |
447 | ||
448 | case 'm': | |
449 | trymem: | |
450 | if (cp[0] == 'm' && cp[1] == 'e' && cp[2] == 'm' && cp[3] == 0) | |
451 | return (0); | |
452 | break; | |
453 | ||
454 | case 'n': | |
455 | if (!strcmp(cp, "null")) | |
456 | return (0); | |
457 | break; | |
458 | ||
459 | case 'v': | |
460 | if ((cp[1] == 'a' || cp[1] == 'p') && isdigit(cp[2]) && | |
461 | cp[3] == 0) | |
462 | return (0); | |
463 | break; | |
464 | } | |
465 | mightbe: | |
466 | cp = dbuf.d_name; | |
467 | while (cp < &dbuf.d_name[DIRSIZ] && *cp) | |
468 | cp++; | |
469 | --cp; | |
470 | x = 0; | |
471 | if (cp[-1] == 'd') { | |
472 | if (dialbase == -1) { | |
473 | if (stat("ttyd0", &stb) == 0) | |
474 | dialbase = stb.st_rdev & 017; | |
475 | else | |
476 | dialbase = -2; | |
477 | } | |
478 | if (dialbase == -2) | |
479 | x = 0; | |
480 | else | |
481 | x = 11; | |
482 | } | |
483 | if (cp > dbuf.d_name && isdigit(cp[-1]) && isdigit(*cp)) | |
484 | x += 10 * (cp[-1] - ' ') + cp[0] - '0'; | |
485 | else if (*cp >= 'a' && *cp <= 'f') | |
486 | x += 10 + *cp - 'a'; | |
487 | else if (isdigit(*cp)) | |
488 | x += *cp - '0'; | |
489 | else | |
490 | x = -1; | |
491 | donecand: | |
492 | dp = (struct ttys *)alloc(sizeof (struct ttys)); | |
493 | strncpy(dp->name, dbuf.d_name, DIRSIZ); | |
494 | dp->next = allttys; | |
495 | dp->ttyd = -1; | |
496 | allttys = dp; | |
497 | if (x == -1) | |
498 | return; | |
499 | x &= 017; | |
500 | dp->cand = cand[x]; | |
501 | cand[x] = dp; | |
502 | } | |
503 | ||
504 | char * | |
505 | gettty() | |
506 | { | |
507 | register char *p; | |
508 | register struct ttys *dp; | |
509 | struct stat stb; | |
510 | int x; | |
511 | ||
512 | if (u.u_ttyp == 0) | |
513 | return("?"); | |
514 | x = u.u_ttyd & 017; | |
515 | for (dp = cand[x]; dp; dp = dp->cand) { | |
516 | if (dp->ttyd == -1) { | |
c67277e8 BJ |
517 | if (stat(dp->name, &stb) == 0 && |
518 | (stb.st_mode&S_IFMT)==S_IFCHR) | |
aafc2e2f BJ |
519 | dp->ttyd = stb.st_rdev; |
520 | else | |
521 | dp->ttyd = -2; | |
522 | } | |
523 | if (dp->ttyd == u.u_ttyd) | |
524 | goto found; | |
525 | } | |
526 | /* ick */ | |
527 | for (dp = allttys; dp; dp = dp->next) { | |
528 | if (dp->ttyd == -1) { | |
529 | if (stat(dp->name, &stb) == 0) | |
530 | dp->ttyd = stb.st_rdev; | |
531 | else | |
532 | dp->ttyd = -2; | |
533 | } | |
534 | if (dp->ttyd == u.u_ttyd) | |
535 | goto found; | |
536 | } | |
537 | return ("?"); | |
538 | found: | |
539 | p = dp->name; | |
540 | if (p[0]=='t' && p[1]=='t' && p[2]=='y') | |
541 | p += 3; | |
542 | return (p); | |
543 | } | |
544 | ||
545 | save() | |
546 | { | |
547 | register struct savcom *sp; | |
548 | register struct asav *ap; | |
549 | register char *cp; | |
0f0e0052 | 550 | register struct text *xp; |
aafc2e2f BJ |
551 | char *ttyp, *cmdp; |
552 | ||
553 | if (mproc->p_stat != SZOMB && getu() == 0) | |
554 | return; | |
555 | ttyp = gettty(); | |
556 | if (xflg == 0 && ttyp[0] == '?' || tptr && strcmpn(tptr, ttyp, 2)) | |
557 | return; | |
558 | sp = &savcom[npr]; | |
559 | cmdp = getcmd(); | |
560 | if (cmdp == 0) | |
561 | return; | |
562 | sp->ap = ap = (struct asav *)alloc(sizeof (struct asav)); | |
563 | sp->ap->a_cmdp = cmdp; | |
564 | #define e(a,b) ap->a = mproc->b | |
565 | e(a_flag, p_flag); e(a_stat, p_stat); e(a_nice, p_nice); | |
0f0e0052 | 566 | e(a_uid, p_uid); e(a_pid, p_pid); e(a_pri, p_pri); |
c67277e8 | 567 | e(a_slptime, p_slptime); e(a_time, p_time); |
aafc2e2f BJ |
568 | ap->a_tty[0] = ttyp[0]; |
569 | ap->a_tty[1] = ttyp[1] ? ttyp[1] : ' '; | |
570 | if (ap->a_stat == SZOMB) { | |
571 | register struct xproc *xp = (struct xproc *)mproc; | |
572 | ||
c67277e8 | 573 | ap->a_cpu = xp->xp_vm.vm_utime + xp->xp_vm.vm_stime; |
aafc2e2f BJ |
574 | } else { |
575 | ap->a_size = mproc->p_dsize + mproc->p_ssize; | |
0f0e0052 | 576 | e(a_rss, p_rssize); |
aafc2e2f | 577 | ap->a_ttyd = u.u_ttyd; |
c67277e8 | 578 | ap->a_cpu = u.u_vm.vm_utime + u.u_vm.vm_stime; |
1fb6a666 BJ |
579 | if (sumcpu) |
580 | ap->a_cpu += u.u_cvm.vm_utime + u.u_cvm.vm_stime; | |
b2867a35 | 581 | if (mproc->p_textp && text) { |
0f0e0052 BJ |
582 | xp = &text[mproc->p_textp - |
583 | (struct text *)nl[X_TEXT].n_value]; | |
584 | ap->a_tsiz = xp->x_size; | |
585 | ap->a_txtrss = xp->x_rssize; | |
586 | ap->a_xccount = xp->x_ccount; | |
587 | } | |
aafc2e2f | 588 | } |
0f0e0052 | 589 | #undef e |
c67277e8 | 590 | ap->a_cpu /= HZ; |
aafc2e2f BJ |
591 | if (lflg) { |
592 | register struct lsav *lp; | |
593 | ||
594 | sp->sun.lp = lp = (struct lsav *)alloc(sizeof (struct lsav)); | |
595 | #define e(a,b) lp->a = mproc->b | |
c67277e8 | 596 | e(l_ppid, p_ppid); e(l_cpu, p_cpu); |
aafc2e2f BJ |
597 | if (ap->a_stat != SZOMB) |
598 | e(l_wchan, p_wchan); | |
599 | #undef e | |
600 | lp->l_addr = pcbpf; | |
601 | } else if (vflg) { | |
602 | register struct vsav *vp; | |
aafc2e2f BJ |
603 | |
604 | sp->sun.vp = vp = (struct vsav *)alloc(sizeof (struct vsav)); | |
605 | #define e(a,b) vp->a = mproc->b | |
aafc2e2f | 606 | if (ap->a_stat != SZOMB) { |
7f0fa372 | 607 | e(v_swrss, p_swrss); |
aafc2e2f | 608 | vp->v_majflt = u.u_vm.vm_majflt; |
0f0e0052 | 609 | if (mproc->p_textp) |
aafc2e2f | 610 | vp->v_txtswrss = xp->x_swrss; |
aafc2e2f | 611 | } |
7f0fa372 | 612 | vp->v_pctcpu = pcpu(); |
aafc2e2f BJ |
613 | #undef e |
614 | } else if (uflg) | |
7f0fa372 | 615 | sp->sun.u_pctcpu = pcpu(); |
aafc2e2f BJ |
616 | else if (sflg) { |
617 | if (ap->a_stat != SZOMB) { | |
618 | for (cp = (char *)u.u_stack; | |
619 | cp < &user.upages[UPAGES][NBPG]; ) | |
620 | if (*cp++) | |
621 | break; | |
622 | sp->sun.s_ssiz = (&user.upages[UPAGES][NBPG] - cp); | |
623 | } | |
624 | } | |
625 | npr++; | |
626 | } | |
627 | ||
0f0e0052 BJ |
628 | double |
629 | pmem(ap) | |
630 | register struct asav *ap; | |
631 | { | |
632 | double fracmem; | |
633 | int szptudot; | |
634 | ||
635 | if ((ap->a_flag&SLOAD) == 0) | |
636 | fracmem = 0.0; | |
637 | else { | |
638 | szptudot = UPAGES + clrnd(ctopt(ap->a_size+ap->a_tsiz)); | |
639 | fracmem = ((float)ap->a_rss+szptudot)/CLSIZE/ecmx; | |
640 | if (ap->a_xccount) | |
641 | fracmem += ((float)ap->a_txtrss)/CLSIZE/ | |
642 | ap->a_xccount/ecmx; | |
643 | } | |
644 | return (100.0 * fracmem); | |
645 | } | |
646 | ||
7f0fa372 BJ |
647 | double |
648 | pcpu() | |
649 | { | |
0f0e0052 | 650 | time_t time; |
7f0fa372 | 651 | |
0f0e0052 BJ |
652 | time = mproc->p_time; |
653 | if (time == 0 || (mproc->p_flag&SLOAD) == 0) | |
7f0fa372 | 654 | return (0.0); |
0f0e0052 BJ |
655 | if (rawcpu) |
656 | return (100.0 * mproc->p_pctcpu); | |
657 | return (100.0 * mproc->p_pctcpu / (1.0 - exp(time * log(ccpu)))); | |
7f0fa372 BJ |
658 | } |
659 | ||
aafc2e2f BJ |
660 | getu() |
661 | { | |
f700a7ee BJ |
662 | struct pte *pteaddr, apte; |
663 | int pad1; /* avoid hardware botch */ | |
664 | struct pte arguutl[UPAGES+CLSIZE]; | |
665 | int pad2; /* avoid hardware botch */ | |
aafc2e2f BJ |
666 | register int i; |
667 | int ncl, size; | |
668 | ||
669 | size = sflg ? ctob(UPAGES) : sizeof (struct user); | |
670 | if ((mproc->p_flag & SLOAD) == 0) { | |
671 | lseek(swap, ctob(mproc->p_swaddr), 0); | |
672 | if (read(swap, (char *)&user.user, size) != size) { | |
673 | fprintf(stderr, "ps: cant read u for pid %d from %s\n", | |
674 | mproc->p_pid, swapf); | |
675 | return (0); | |
676 | } | |
677 | pcbpf = 0; | |
678 | argaddr = 0; | |
679 | return (1); | |
680 | } | |
681 | pteaddr = &Usrptma[btokmx(mproc->p_p0br) + mproc->p_szpt - 1]; | |
682 | lseek(kmem, kflg ? clear(pteaddr) : (int)pteaddr, 0); | |
683 | if (read(kmem, (char *)&apte, sizeof(apte)) != sizeof(apte)) { | |
684 | printf("ps: cant read indir pte to get u for pid %d from %s\n", | |
685 | mproc->p_pid, swapf); | |
686 | return (0); | |
687 | } | |
688 | lseek(mem, | |
689 | ctob(apte.pg_pfnum+1) - (UPAGES+CLSIZE) * sizeof (struct pte), 0); | |
690 | if (read(mem, (char *)arguutl, sizeof(arguutl)) != sizeof(arguutl)) { | |
691 | printf("ps: cant read page table for u of pid %d from %s\n", | |
692 | mproc->p_pid, swapf); | |
693 | return (0); | |
694 | } | |
695 | if (arguutl[0].pg_fod == 0 && arguutl[0].pg_pfnum) | |
696 | argaddr = ctob(arguutl[0].pg_pfnum); | |
697 | else | |
698 | argaddr = 0; | |
699 | pcbpf = arguutl[CLSIZE].pg_pfnum; | |
700 | ncl = (size + NBPG*CLSIZE - 1) / (NBPG*CLSIZE); | |
701 | while (--ncl >= 0) { | |
702 | i = ncl * CLSIZE; | |
703 | lseek(mem, ctob(arguutl[CLSIZE+i].pg_pfnum), 0); | |
704 | if (read(mem, user.upages[i], CLSIZE*NBPG) != CLSIZE*NBPG) { | |
705 | printf("ps: cant read page %d of u of pid %d from %s\n", | |
706 | arguutl[CLSIZE+i].pg_pfnum, mproc->p_pid, memf); | |
707 | return(0); | |
708 | } | |
709 | } | |
710 | return (1); | |
711 | } | |
712 | ||
713 | char * | |
714 | getcmd() | |
715 | { | |
716 | char cmdbuf[BUFSIZ]; | |
f700a7ee | 717 | int pad1; /* avoid hardware botch */ |
aafc2e2f BJ |
718 | union { |
719 | char argc[CLSIZE*NBPG]; | |
720 | int argi[CLSIZE*NBPG/sizeof (int)]; | |
721 | } argspac; | |
f700a7ee | 722 | int pad2; /* avoid hardware botch */ |
aafc2e2f BJ |
723 | register char *cp; |
724 | register int *ip; | |
725 | char c; | |
726 | int nbad; | |
727 | struct dblock db; | |
728 | ||
729 | if (mproc->p_stat == SZOMB || mproc->p_flag&(SSYS|SWEXIT)) | |
730 | return (""); | |
731 | if (cflg) { | |
732 | strncpy(cmdbuf, u.u_comm, sizeof (u.u_comm)); | |
733 | return (savestr(cmdbuf)); | |
734 | } | |
735 | if ((mproc->p_flag & SLOAD) == 0 || argaddr == 0) { | |
736 | vstodb(0, CLSIZE, &u.u_smap, &db, 1); | |
737 | lseek(swap, ctob(db.db_base), 0); | |
738 | if (read(swap, (char *)&argspac, sizeof(argspac)) | |
739 | != sizeof(argspac)) | |
740 | goto bad; | |
741 | } else { | |
742 | lseek(mem, argaddr, 0); | |
743 | if (read(mem, (char *)&argspac, sizeof (argspac)) | |
744 | != sizeof (argspac)) | |
745 | goto bad; | |
746 | } | |
747 | ip = &argspac.argi[CLSIZE*NBPG/sizeof (int)]; | |
748 | ip -= 2; /* last arg word and .long 0 */ | |
749 | while (*--ip) | |
750 | if (ip == argspac.argi) | |
751 | goto retucomm; | |
752 | *(char *)ip = ' '; | |
753 | ip++; | |
754 | nbad = 0; | |
755 | for (cp = (char *)ip; cp < &argspac.argc[CLSIZE*NBPG]; cp++) { | |
756 | c = *cp & 0177; | |
757 | if (c == 0) | |
758 | *cp = ' '; | |
759 | else if (c < ' ' || c > 0176) { | |
760 | if (++nbad >= 5*(eflg+1)) { | |
761 | *cp++ = ' '; | |
762 | break; | |
763 | } | |
764 | *cp = '?'; | |
765 | } else if (eflg == 0 && c == '=') { | |
766 | while (*--cp != ' ') | |
767 | if (cp <= (char *)ip) | |
768 | break; | |
769 | break; | |
770 | } | |
771 | } | |
772 | *cp = 0; | |
773 | while (*--cp == ' ') | |
774 | *cp = 0; | |
775 | cp = (char *)ip; | |
776 | strncpy(cmdbuf, cp, &argspac.argc[CLSIZE*NBPG] - cp); | |
777 | if (cp[0] == '-' || cp[0] == '?' || cp[0] <= ' ') { | |
778 | strcat(cmdbuf, " ("); | |
779 | strncat(cmdbuf, u.u_comm, sizeof(u.u_comm)); | |
780 | strcat(cmdbuf, ")"); | |
781 | } | |
c67277e8 | 782 | /* |
aafc2e2f BJ |
783 | if (xflg == 0 && gflg == 0 && tptr == 0 && cp[0] == '-') |
784 | return (0); | |
c67277e8 | 785 | */ |
aafc2e2f BJ |
786 | return (savestr(cmdbuf)); |
787 | ||
788 | bad: | |
789 | fprintf(stderr, "ps: error locating command name for pid %d\n", | |
790 | mproc->p_pid); | |
791 | retucomm: | |
792 | strcpy(cmdbuf, " ("); | |
793 | strncat(cmdbuf, u.u_comm, sizeof (u.u_comm)); | |
794 | strcat(cmdbuf, ")"); | |
795 | return (savestr(cmdbuf)); | |
796 | } | |
797 | ||
798 | char *lhdr = | |
0f0e0052 | 799 | " F UID PID PPID CP PRI NI ADDR SZ RSS WCHAN STAT TT TIME"; |
aafc2e2f BJ |
800 | lpr(sp) |
801 | struct savcom *sp; | |
802 | { | |
803 | register struct asav *ap = sp->ap; | |
804 | register struct lsav *lp = sp->sun.lp; | |
805 | ||
c67277e8 BJ |
806 | printf("%6x%4d%6u%6u%3d%4d%3d%5x%4d%5d", |
807 | ap->a_flag, ap->a_uid, | |
808 | ap->a_pid, lp->l_ppid, lp->l_cpu&0377, ap->a_pri-PZERO, | |
809 | ap->a_nice-NZERO, lp->l_addr, ap->a_size/2, ap->a_rss/2); | |
aafc2e2f | 810 | printf(lp->l_wchan ? " %5x" : " ", (int)lp->l_wchan&0xfffff); |
0f0e0052 | 811 | printf(" %4.4s ", state(ap)); |
aafc2e2f BJ |
812 | ptty(ap->a_tty); |
813 | ptime(ap); | |
814 | } | |
815 | ||
816 | ptty(tp) | |
817 | char *tp; | |
818 | { | |
819 | ||
c67277e8 | 820 | printf("%-2.2s", tp); |
aafc2e2f BJ |
821 | } |
822 | ||
823 | ptime(ap) | |
824 | struct asav *ap; | |
825 | { | |
826 | ||
c67277e8 | 827 | printf("%3ld:%02ld", ap->a_cpu / HZ, ap->a_cpu % HZ); |
aafc2e2f BJ |
828 | } |
829 | ||
830 | char *uhdr = | |
0f0e0052 | 831 | "USER PID %CPU %MEM SZ RSS TT STAT TIME"; |
aafc2e2f BJ |
832 | upr(sp) |
833 | struct savcom *sp; | |
834 | { | |
835 | register struct asav *ap = sp->ap; | |
0f0e0052 BJ |
836 | int vmsize, rmsize; |
837 | ||
838 | vmsize = (ap->a_size + ap->a_tsiz)/2; | |
839 | rmsize = ap->a_rss/2; | |
840 | if (ap->a_xccount) | |
841 | rmsize += ap->a_txtrss/ap->a_xccount/2; | |
842 | printf("%-8.8s %5d%5.1f%5.1f%5d%5d", | |
843 | getname(ap->a_uid), ap->a_pid, sp->sun.u_pctcpu, pmem(ap), | |
844 | vmsize, rmsize); | |
c67277e8 | 845 | putchar(' '); |
aafc2e2f | 846 | ptty(ap->a_tty); |
0f0e0052 | 847 | printf(" %4.4s", state(ap)); |
aafc2e2f BJ |
848 | ptime(ap); |
849 | } | |
850 | ||
851 | char *vhdr = | |
0f0e0052 | 852 | " PID TT STAT TIME SL RE PAGEIN SIZE RSS SRS TSIZ TRS %CPU %MEM"; |
aafc2e2f BJ |
853 | vpr(sp) |
854 | struct savcom *sp; | |
855 | { | |
856 | register struct vsav *vp = sp->sun.vp; | |
857 | register struct asav *ap = sp->ap; | |
c67277e8 | 858 | |
0f0e0052 | 859 | printf("%5u ", ap->a_pid); |
c67277e8 | 860 | ptty(ap->a_tty); |
0f0e0052 | 861 | printf(" %4.4s", state(ap)); |
c67277e8 | 862 | ptime(ap); |
0f0e0052 BJ |
863 | printf("%3d%3d%7d%5d%5d%5d%5d%4d%5.1f%5.1f", |
864 | ap->a_slptime, ap->a_time > 99 ? 99 : ap->a_time, vp->v_majflt, | |
c67277e8 | 865 | ap->a_size/2, ap->a_rss/2, vp->v_swrss/2, |
0f0e0052 | 866 | ap->a_tsiz/2, ap->a_txtrss/2, vp->v_pctcpu, pmem(ap)); |
c67277e8 BJ |
867 | } |
868 | ||
869 | char *shdr = | |
0f0e0052 | 870 | "SSIZ PID TT STAT TIME"; |
c67277e8 BJ |
871 | spr(sp) |
872 | struct savcom *sp; | |
873 | { | |
874 | register struct asav *ap = sp->ap; | |
875 | ||
876 | if (sflg) | |
877 | printf("%4d ", sp->sun.s_ssiz); | |
878 | printf("%5u", ap->a_pid); | |
879 | putchar(' '); | |
880 | ptty(ap->a_tty); | |
0f0e0052 | 881 | printf(" %4.4s", state(ap)); |
c67277e8 BJ |
882 | ptime(ap); |
883 | } | |
884 | ||
885 | char * | |
886 | state(ap) | |
887 | register struct asav *ap; | |
888 | { | |
889 | char stat, load, nice, anom; | |
890 | static char res[5]; | |
aafc2e2f BJ |
891 | |
892 | switch (ap->a_stat) { | |
893 | ||
aafc2e2f | 894 | case SSTOP: |
c67277e8 BJ |
895 | stat = 'T'; |
896 | break; | |
897 | ||
898 | case SSLEEP: | |
899 | if (ap->a_pri >= PZERO) | |
900 | if (ap->a_slptime >= MAXSLP) | |
901 | stat = 'I'; | |
902 | else | |
903 | stat = 'S'; | |
aafc2e2f BJ |
904 | else if (ap->a_flag & SPAGE) |
905 | stat = 'P'; | |
906 | else | |
907 | stat = 'D'; | |
908 | break; | |
909 | ||
c67277e8 | 910 | case SWAIT: |
aafc2e2f BJ |
911 | case SRUN: |
912 | case SIDL: | |
c67277e8 BJ |
913 | stat = 'R'; |
914 | break; | |
915 | ||
916 | case SZOMB: | |
917 | stat = 'Z'; | |
aafc2e2f | 918 | break; |
c67277e8 BJ |
919 | |
920 | default: | |
921 | stat = '?'; | |
aafc2e2f | 922 | } |
c67277e8 | 923 | load = ap->a_flag & SLOAD ? ' ' : 'W'; |
0f0e0052 BJ |
924 | if (ap->a_nice < NZERO) |
925 | nice = '<'; | |
926 | else if (ap->a_nice > NZERO) | |
927 | nice = 'N'; | |
928 | else | |
929 | nice = ' '; | |
aafc2e2f | 930 | anom = ap->a_flag & (SANOM|SUANOM) ? 'A' : ' '; |
c67277e8 BJ |
931 | res[0] = stat; res[1] = load; res[2] = nice; res[3] = anom; |
932 | return (res); | |
aafc2e2f BJ |
933 | } |
934 | ||
935 | /* | |
936 | * Given a base/size pair in virtual swap area, | |
937 | * return a physical base/size pair which is the | |
938 | * (largest) initial, physically contiguous block. | |
939 | */ | |
940 | vstodb(vsbase, vssize, dmp, dbp, rev) | |
941 | register int vsbase; | |
942 | int vssize; | |
943 | struct dmap *dmp; | |
944 | register struct dblock *dbp; | |
945 | { | |
946 | register int blk = DMMIN; | |
947 | register swblk_t *ip = dmp->dm_map; | |
948 | ||
949 | if (vsbase < 0 || vsbase + vssize > dmp->dm_size) | |
950 | panic("vstodb"); | |
951 | while (vsbase >= blk) { | |
952 | vsbase -= blk; | |
953 | if (blk < DMMAX) | |
954 | blk *= 2; | |
955 | ip++; | |
956 | } | |
957 | if (*ip <= 0 || *ip + blk > nswap) | |
958 | panic("vstodb *ip"); | |
959 | dbp->db_size = min(vssize, blk - vsbase); | |
960 | dbp->db_base = *ip + (rev ? blk - (vsbase + dbp->db_size) : vsbase); | |
961 | } | |
962 | ||
963 | /*ARGSUSED*/ | |
964 | panic(cp) | |
965 | char *cp; | |
966 | { | |
967 | ||
968 | #ifdef DEBUG | |
969 | printf("%s\n", cp); | |
970 | #endif | |
971 | } | |
972 | ||
973 | min(a, b) | |
974 | { | |
975 | ||
976 | return (a < b ? a : b); | |
977 | } | |
978 | ||
979 | pscomp(s1, s2) | |
980 | struct savcom *s1, *s2; | |
981 | { | |
982 | register int i; | |
983 | ||
7f0fa372 BJ |
984 | if (uflg) |
985 | return (s2->sun.u_pctcpu > s1->sun.u_pctcpu ? 1 : -1); | |
aafc2e2f BJ |
986 | if (vflg) |
987 | return (vsize(s2) - vsize(s1)); | |
988 | i = s1->ap->a_ttyd - s2->ap->a_ttyd; | |
989 | if (i == 0) | |
990 | i = s1->ap->a_pid - s2->ap->a_pid; | |
991 | return (i); | |
992 | } | |
993 | ||
994 | vsize(sp) | |
995 | struct savcom *sp; | |
996 | { | |
997 | register struct asav *ap = sp->ap; | |
998 | register struct vsav *vp = sp->sun.vp; | |
999 | ||
1000 | if (ap->a_flag & SLOAD) | |
1001 | return (ap->a_rss + | |
0f0e0052 BJ |
1002 | ap->a_txtrss / (ap->a_xccount ? ap->a_xccount : 1)); |
1003 | return (vp->v_swrss + (ap->a_xccount ? 0 : vp->v_txtswrss)); | |
aafc2e2f BJ |
1004 | } |
1005 | ||
1006 | #define NMAX 8 | |
1007 | #define NUID 2048 | |
1008 | ||
1009 | char names[NUID][NMAX+1]; | |
1010 | ||
1011 | /* | |
1012 | * Stolen from ls... | |
1013 | */ | |
1014 | char * | |
1015 | getname(uid) | |
1016 | { | |
1017 | register struct passwd *pw; | |
1018 | static init; | |
1019 | struct passwd *getpwent(); | |
1020 | ||
0f0e0052 | 1021 | if (uid >= 0 && uid < NUID && names[uid][0]) |
aafc2e2f BJ |
1022 | return (&names[uid][0]); |
1023 | if (init == 2) | |
1024 | return (0); | |
1025 | if (init == 0) | |
1026 | setpwent(), init = 1; | |
1027 | while (pw = getpwent()) { | |
1028 | if (pw->pw_uid >= NUID) | |
1029 | continue; | |
1030 | if (names[pw->pw_uid][0]) | |
1031 | continue; | |
1032 | strncpy(names[pw->pw_uid], pw->pw_name, NMAX); | |
1033 | if (pw->pw_uid == uid) | |
1034 | return (&names[uid][0]); | |
1035 | } | |
1036 | init = 2; | |
1037 | endpwent(); | |
1038 | return (0); | |
1039 | } | |
1040 | ||
1041 | char *freebase; | |
1042 | int nleft; | |
1043 | ||
1044 | char * | |
1045 | alloc(size) | |
1046 | int size; | |
1047 | { | |
1048 | register char *cp; | |
1049 | register int i; | |
1050 | ||
1051 | if (size > nleft) { | |
1052 | freebase = (char *)sbrk(i = size > 2048 ? size : 2048); | |
1053 | if (freebase == 0) { | |
1054 | fprintf(stderr, "ps: ran out of memory\n"); | |
1055 | exit(1); | |
1056 | } | |
1057 | nleft = i - size; | |
1058 | } else | |
1059 | nleft -= size; | |
1060 | cp = freebase; | |
1061 | for (i = size; --i >= 0; ) | |
1062 | *cp++ = 0; | |
1063 | freebase = cp; | |
1064 | return (cp - size); | |
1065 | } | |
1066 | ||
1067 | char * | |
1068 | savestr(cp) | |
1069 | char *cp; | |
1070 | { | |
1071 | register int len; | |
1072 | register char *dp; | |
1073 | ||
1074 | len = strlen(cp); | |
1075 | dp = (char *)alloc(len+1); | |
1076 | strcpy(dp, cp); | |
1077 | return (dp); | |
1078 | } |