Commit | Line | Data |
---|---|---|
87b3cf93 BJ |
1 | /* |
2 | * ps; VAX 4BSD version | |
3 | */ | |
4 | ||
5 | #include <stdio.h> | |
6 | #include <ctype.h> | |
7 | #include <olda.out.h> | |
8 | #include <pwd.h> | |
9 | #include <sys/param.h> | |
10 | #include <sys/tty.h> | |
11 | #include <sys/dir.h> | |
12 | #include <sys/user.h> | |
13 | #include <sys/proc.h> | |
14 | #include <sys/pte.h> | |
15 | #include <sys/vm.h> | |
16 | #include <sys/text.h> | |
17 | #include <sys/stat.h> | |
18 | ||
19 | struct nlist nl[] = { | |
20 | { "_proc" }, | |
21 | #define X_PROC 0 | |
22 | { "_Usrptma" }, | |
23 | #define X_USRPTMA 1 | |
24 | { "_usrpt" }, | |
25 | #define X_USRPT 2 | |
26 | { "_text" }, | |
27 | #define X_TEXT 3 | |
28 | { "_nswap" }, | |
29 | #define X_NSWAP 4 | |
30 | { 0 }, | |
31 | }; | |
32 | ||
33 | struct savcom { | |
34 | union { | |
35 | struct lsav *lp; | |
36 | float u_pctcpu; | |
37 | struct vsav *vp; | |
38 | int s_ssiz; | |
39 | } sun; | |
40 | struct asav *ap; | |
41 | } savcom[NPROC]; | |
42 | ||
43 | struct asav { | |
44 | char *a_cmdp; | |
45 | int a_flag; | |
46 | short a_stat, a_uid, a_pid, a_nice; | |
47 | size_t a_size, a_rss; | |
48 | char a_tty[4]; | |
49 | dev_t a_ttyd; | |
50 | time_t a_time; | |
51 | }; | |
52 | ||
53 | char *lhdr; | |
54 | /* F S UID PID PPID CP PRI NICE ADDR SZ RSS WCHAN TTY TIME */ | |
55 | struct lsav { | |
56 | short l_ppid; | |
57 | char l_cpu, l_pri; | |
58 | int l_addr; | |
59 | caddr_t l_wchan; | |
60 | }; | |
61 | ||
62 | char *uhdr; | |
63 | /* USER PID %%CPU NICE SZ RSS TTY TIME */ | |
64 | ||
65 | char *shdr; | |
66 | /* SSIZ PID TTY TIME */ | |
67 | ||
68 | char *vhdr; | |
69 | /* F PID TT TIME TIM SL MINFLT MAJFLT SIZE RSS SRS TSIZ TRS PF*/ | |
70 | struct vsav { | |
71 | short v_slptime, v_pri; | |
72 | u_int v_minflt, v_majflt; | |
73 | size_t v_swrss, v_tsiz, v_txtrss, v_txtswrss; | |
74 | short v_xccount; | |
75 | short v_aveflt; | |
76 | }; | |
77 | ||
78 | struct proc mproc; | |
79 | struct text *text; | |
80 | ||
81 | union { | |
82 | struct user user; | |
83 | char upages[UPAGES][NBPG]; | |
84 | } user; | |
85 | #define u user.user | |
86 | ||
87 | #define clear(x) ((int)x & 0x7fffffff) | |
88 | ||
89 | int chkpid; | |
90 | int aflg, cflg, gflg, kflg, lflg, sflg, uflg, vflg, xflg; | |
91 | char *tptr; | |
92 | char *gettty(), *getcmd(), *getname(), *savestr(), *alloc(); | |
93 | int pscomp(); | |
94 | int nswap; | |
95 | struct pte *Usrptma, *usrpt; | |
96 | ||
97 | int ndev; | |
98 | struct devl { | |
99 | char dname[DIRSIZ]; | |
100 | dev_t dev; | |
101 | struct devl *next; | |
102 | struct devl *cand; | |
103 | } devl[256], *cand[16], *cons; | |
104 | ||
105 | struct savcom savcom[NPROC]; | |
106 | int npr; | |
107 | ||
108 | int cmdstart; | |
109 | int twidth; | |
110 | char *kmemf, *memf, *swapf, *nlistf; | |
111 | int kmem, mem, swap; | |
112 | ||
113 | int pcbpf; | |
114 | int argaddr; | |
115 | extern char _sobuf[]; | |
116 | ||
117 | main(argc, argv) | |
118 | char **argv; | |
119 | { | |
120 | register int i; | |
121 | register char *ap; | |
122 | int uid; | |
123 | off_t procp; | |
124 | ||
125 | setbuf(stdout, _sobuf); | |
126 | argc--, argv++; | |
127 | if (argc > 0) { | |
128 | ap = argv[0]; | |
129 | while (*ap) switch (*ap++) { | |
130 | ||
131 | case 'a': | |
132 | aflg++; | |
133 | break; | |
134 | case 'c': | |
135 | cflg = !cflg; | |
136 | break; | |
137 | case 'g': | |
138 | gflg++; | |
139 | break; | |
140 | case 'k': | |
141 | kflg++; | |
142 | break; | |
143 | case 'l': | |
144 | lflg++; | |
145 | break; | |
146 | case 's': | |
147 | sflg++; | |
148 | break; | |
149 | case 't': | |
150 | if (*ap) | |
151 | tptr = ap; | |
152 | aflg++; | |
153 | gflg++; | |
154 | if (*tptr == '?') | |
155 | xflg++; | |
156 | while (*ap) | |
157 | ap++; | |
158 | break; | |
159 | case 'u': | |
160 | uflg++; | |
161 | break; | |
162 | case 'v': | |
163 | cflg = 1; | |
164 | vflg++; | |
165 | break; | |
166 | case 'w': | |
167 | if (twidth == 80) | |
168 | twidth = 132; | |
169 | else | |
170 | twidth = BUFSIZ; | |
171 | break; | |
172 | case 'x': | |
173 | xflg++; | |
174 | break; | |
175 | default: | |
176 | if (!isdigit(ap[-1])) | |
177 | break; | |
178 | chkpid = atoi(--ap); | |
179 | *ap = 0; | |
180 | aflg++; | |
181 | xflg++; | |
182 | break; | |
183 | } | |
184 | } | |
185 | openfiles(argc, argv); | |
186 | getkvars(argc, argv); | |
187 | getdev(); | |
188 | uid = getuid(); | |
189 | printhdr(); | |
190 | procp = nl[X_PROC].n_value; | |
191 | for (i=0; i<NPROC; i++) { | |
192 | lseek(kmem, (char *)procp, 0); | |
193 | if (read(kmem, (char *)&mproc, sizeof mproc) != sizeof mproc) | |
194 | cantread("proc table", kmemf); | |
195 | procp += sizeof (mproc); | |
196 | if (mproc.p_stat == 0 || mproc.p_pgrp == 0 && xflg == 0) | |
197 | continue; | |
198 | if (tptr == 0 && gflg == 0 && xflg == 0 && | |
199 | mproc.p_ppid == 1 && (mproc.p_flag&SDETACH) == 0) | |
200 | continue; | |
201 | if (uid != mproc.p_uid && aflg==0 || | |
202 | chkpid != 0 && chkpid != mproc.p_pid) | |
203 | continue; | |
204 | if (vflg && gflg == 0 && xflg == 0) { | |
205 | if (mproc.p_stat == SZOMB) | |
206 | continue; | |
207 | if (mproc.p_slptime > MAXSLP && | |
208 | (mproc.p_stat == SSLEEP || mproc.p_stat == SSTOP)) | |
209 | continue; | |
210 | } | |
211 | save(); | |
212 | } | |
213 | qsort(savcom, npr, sizeof(savcom[0]), pscomp); | |
214 | for (i=0; i<npr; i++) { | |
215 | register struct savcom *sp = &savcom[i]; | |
216 | if (lflg) | |
217 | lpr(sp); | |
218 | else if (vflg) | |
219 | vpr(sp); | |
220 | else if (uflg) | |
221 | upr(sp); | |
222 | else | |
223 | spr(sp); | |
224 | if (sp->ap->a_pid == 0) | |
225 | printf(" swapper"); | |
226 | else if (sp->ap->a_pid == 2) | |
227 | printf(" pagedaemon"); | |
228 | else | |
229 | printf(" %.*s", twidth - cmdstart - 2, sp->ap->a_cmdp); | |
230 | printf("\n"); | |
231 | } | |
232 | exit(npr == 0); | |
233 | } | |
234 | ||
235 | openfiles(argc, argv) | |
236 | char **argv; | |
237 | { | |
238 | ||
239 | kmemf = "/dev/kmem"; | |
240 | if (kflg) | |
241 | kmemf = argc > 1 ? argv[1] : "/vmcore"; | |
242 | kmem = open(kmemf, 0); | |
243 | if (kmem < 0) { | |
244 | perror(kmemf); | |
245 | exit(1); | |
246 | } | |
247 | if (kflg) { | |
248 | mem = kmem; | |
249 | memf = kmemf; | |
250 | } else { | |
251 | memf = "/dev/mem"; | |
252 | mem = open(memf, 0); | |
253 | if (mem < 0) { | |
254 | perror(memf); | |
255 | exit(1); | |
256 | } | |
257 | } | |
258 | swapf = argc>2 ? argv[2]: "/dev/drum"; | |
259 | swap = open(swapf, 0); | |
260 | if (swap < 0) { | |
261 | perror(swapf); | |
262 | exit(1); | |
263 | } | |
264 | } | |
265 | ||
266 | getkvars(argc, argv) | |
267 | char **argv; | |
268 | { | |
269 | register struct nlist *nlp; | |
270 | ||
271 | nlistf = argc > 3 ? argv[3] : "/vmunix"; | |
272 | nlist(nlistf, nl); | |
273 | if (nl[0].n_type == 0) { | |
274 | fprintf(stderr, "%s: No namelist\n", nlistf); | |
275 | exit(1); | |
276 | } | |
277 | if (chdir("/dev") < 0) { | |
278 | perror("/dev"); | |
279 | exit(1); | |
280 | } | |
281 | if (kflg) | |
282 | for (nlp = nl; nlp < &nl[sizeof (nl)/sizeof (nl[0])]; nlp++) | |
283 | nlp->n_value = clear(nlp->n_value); | |
284 | Usrptma = (struct pte *)nl[X_USRPTMA].n_value; | |
285 | usrpt = (struct pte *)nl[X_USRPT].n_value; | |
286 | lseek(kmem, (long)nl[X_NSWAP].n_value, 0); | |
287 | if (read(kmem, &nswap, sizeof (nswap)) != sizeof (nswap)) { | |
288 | /*###287 [lint] read arg. 2 used inconsistently ps.c(41) :: ps.c(287)%%%*/ | |
289 | cantread("nswap", kmemf); | |
290 | /*###288 [lint] cantread arg. 1 used inconsistently ps.c(322) :: ps.c(288)%%%*/ | |
291 | /*###288 [lint] cantread arg. 2 used inconsistently ps.c(322) :: ps.c(288)%%%*/ | |
292 | exit(1); | |
293 | } | |
294 | if (vflg) { | |
295 | text = (struct text *)alloc(NTEXT * sizeof (struct text)); | |
296 | if (text == 0) { | |
297 | fprintf(stderr, "no room for text table\n"); | |
298 | exit(1); | |
299 | } | |
300 | lseek(kmem, (long)nl[X_TEXT].n_value, 0); | |
301 | if (read(kmem, text, sizeof (text)) != sizeof (text)) { | |
302 | /*###298 [lint] read arg. 2 used inconsistently ps.c(41) :: ps.c(298)%%%*/ | |
303 | cantread("text table", kmemf); | |
304 | /*###299 [lint] cantread arg. 1 used inconsistently ps.c(322) :: ps.c(299)%%%*/ | |
305 | /*###299 [lint] cantread arg. 2 used inconsistently ps.c(322) :: ps.c(299)%%%*/ | |
306 | exit(1); | |
307 | } | |
308 | } | |
309 | } | |
310 | ||
311 | printhdr() | |
312 | { | |
313 | char *hdr; | |
314 | ||
315 | if (sflg+lflg+vflg+uflg > 1) { | |
316 | fprintf(stderr, "ps: specify only one of s,l,v and u\n"); | |
317 | exit(1); | |
318 | } | |
319 | hdr = lflg ? lhdr : (vflg ? vhdr : (uflg ? uhdr : shdr)); | |
320 | if (lflg+vflg+uflg+sflg == 0) | |
321 | hdr += strlen(" SSIZ"); | |
322 | cmdstart = strlen(hdr); | |
323 | printf("%s COMMAND\n", hdr); | |
324 | fflush(stdout); | |
325 | } | |
326 | ||
327 | cantread(what, fromwhat) | |
328 | char *what, *fromwhat; | |
329 | { | |
330 | ||
331 | fprintf(stderr, "ps: error reading %s from %s", what, fromwhat); | |
332 | } | |
333 | ||
334 | getdev() | |
335 | { | |
336 | register FILE *df; | |
337 | struct stat sbuf; | |
338 | struct direct dbuf; | |
339 | ||
340 | if ((df = fopen("/dev", "r")) == NULL) { | |
341 | fprintf(stderr, "Can't open /dev\n"); | |
342 | exit(1); | |
343 | } | |
344 | ndev = 0; | |
345 | while (fread((char *)&dbuf, sizeof(dbuf), 1, df) == 1) { | |
346 | if (dbuf.d_ino == 0) | |
347 | continue; | |
348 | if (stat(dbuf.d_name, &sbuf) < 0) | |
349 | continue; | |
350 | if ((sbuf.st_mode&S_IFMT) != S_IFCHR) | |
351 | continue; | |
352 | strcpy(devl[ndev].dname, dbuf.d_name); | |
353 | devl[ndev].dev = sbuf.st_rdev; | |
354 | ndev++; | |
355 | } | |
356 | fclose(df); | |
357 | } | |
358 | ||
359 | save() | |
360 | { | |
361 | register struct savcom *sp; | |
362 | register struct asav *ap; | |
363 | register char *cp; | |
364 | char *ttyp, *cmdp; | |
365 | ||
366 | if (mproc.p_stat != SZOMB && getu() == 0) | |
367 | return; | |
368 | ttyp = gettty(); | |
369 | if (tptr && strcmpn(tptr, ttyp, 2)) | |
370 | return; | |
371 | sp = &savcom[npr]; | |
372 | cmdp = getcmd(); | |
373 | if (cmdp == 0) | |
374 | return; | |
375 | sp->ap = ap = (struct asav *)alloc(sizeof (struct asav)); | |
376 | sp->ap->a_cmdp = cmdp; | |
377 | #define e(a,b) ap->a = mproc.b | |
378 | e(a_flag, p_flag); e(a_stat, p_stat); e(a_nice, p_nice); | |
379 | e(a_uid, p_uid); e(a_pid, p_pid); e(a_rss, p_rssize); | |
380 | #undef e | |
381 | ap->a_size = mproc.p_dsize + mproc.p_ssize; | |
382 | ap->a_tty[0] = ttyp[0]; | |
383 | ap->a_tty[1] = ttyp[1] ? ttyp[1] : ' '; | |
384 | ap->a_ttyd = u.u_ttyd; | |
385 | ap->a_time = u.u_vm.vm_utime + u.u_vm.vm_stime; | |
386 | if (lflg) { | |
387 | register struct lsav *lp; | |
388 | ||
389 | sp->sun.lp = lp = (struct lsav *)alloc(sizeof (struct lsav)); | |
390 | #define e(a,b) lp->a = mproc.b | |
391 | e(l_ppid, p_ppid); e(l_cpu, p_cpu); | |
392 | e(l_pri, p_pri); e(l_wchan, p_wchan); | |
393 | #undef e | |
394 | lp->l_addr = pcbpf; | |
395 | } else if (vflg) { | |
396 | register struct vsav *vp; | |
397 | register struct text *xp; | |
398 | ||
399 | sp->sun.vp = vp = (struct vsav *)alloc(sizeof (struct vsav)); | |
400 | #define e(a,b) vp->a = mproc.b | |
401 | e(v_slptime, p_slptime); e(v_pri, p_pri); | |
402 | e(v_swrss, p_swrss); e(v_aveflt, p_aveflt); | |
403 | #undef e | |
404 | vp->v_minflt = u.u_vm.vm_minflt; | |
405 | vp->v_majflt = u.u_vm.vm_majflt; | |
406 | if (mproc.p_textp) { | |
407 | xp = &text[mproc.p_textp - | |
408 | (struct text *)nl[X_TEXT].n_value]; | |
409 | vp->v_tsiz = xp->x_size; | |
410 | vp->v_txtrss = xp->x_rssize; | |
411 | vp->v_txtswrss = xp->x_swrss; | |
412 | vp->v_xccount = xp->x_ccount; | |
413 | } else { | |
414 | vp->v_tsiz = 0; | |
415 | vp->v_txtrss = 0; | |
416 | vp->v_xccount = 0; | |
417 | vp->v_txtswrss = 0; | |
418 | } | |
419 | } else if (uflg) | |
420 | sp->sun.u_pctcpu = 0.0; | |
421 | else if (sflg) { | |
422 | for (cp = (char *)u.u_stack; | |
423 | cp < (char *)&u + ctob(UPAGES); ) | |
424 | if (*cp++) | |
425 | break; | |
426 | sp->sun.s_ssiz = (int) ((char *)&u + ctob(UPAGES) - cp); | |
427 | } | |
428 | npr++; | |
429 | } | |
430 | ||
431 | getu() | |
432 | { | |
433 | struct pte *pteaddr, apte, arguutl[UPAGES+CLSIZE]; | |
434 | register int i; | |
435 | int ncl, size; | |
436 | ||
437 | size = sflg ? ctob(UPAGES) : sizeof (struct user); | |
438 | if ((mproc.p_flag & SLOAD) == 0) { | |
439 | lseek(swap, ctob(mproc.p_swaddr), 0); | |
440 | if (read(swap, (char *)&u, size) != size) { | |
441 | fprintf(stderr, "ps: cant read u for pid %d from %s\n", | |
442 | mproc.p_pid, swapf); | |
443 | return (0); | |
444 | } | |
445 | pcbpf = 0; | |
446 | argaddr = 0; | |
447 | return (1); | |
448 | } | |
449 | pteaddr = &Usrptma[btokmx(mproc.p_p0br) + mproc.p_szpt - 1]; | |
450 | lseek(kmem, kflg ? clear(pteaddr) : (int)pteaddr, 0); | |
451 | if (read(kmem, (char *)&apte, sizeof(apte)) != sizeof(apte)) { | |
452 | printf("ps: cant read indir pte to get u for pid %d from %s\n", | |
453 | mproc.p_pid, swapf); | |
454 | return (0); | |
455 | } | |
456 | lseek(mem, | |
457 | ctob(apte.pg_pfnum+1) - (UPAGES+CLSIZE) * sizeof (struct pte), 0); | |
458 | if (read(mem, (char *)arguutl, sizeof(arguutl)) != sizeof(arguutl)) { | |
459 | printf("ps: cant read page table for u of pid %d from %s\n", | |
460 | mproc.p_pid, swapf); | |
461 | return (0); | |
462 | } | |
463 | if (arguutl[0].pg_fod == 0 && arguutl[0].pg_pfnum) | |
464 | argaddr = ctob(arguutl[0].pg_pfnum); | |
465 | else | |
466 | argaddr = 0; | |
467 | pcbpf = arguutl[CLSIZE].pg_pfnum; | |
468 | ncl = (size + NBPG*CLSIZE - 1) / (NBPG*CLSIZE); | |
469 | while (--ncl >= 0) { | |
470 | i = ncl * CLSIZE; | |
471 | lseek(mem, ctob(arguutl[CLSIZE+i].pg_pfnum), 0); | |
472 | if (read(mem, user.upages[CLSIZE+i], CLSIZE*NBPG) | |
473 | != CLSIZE*NBPG) { | |
474 | printf("ps: cant read page %d of u of pid %d from %s\n", | |
475 | arguutl[CLSIZE+i].pg_pfnum, mproc.p_pid, memf); | |
476 | return(0); | |
477 | } | |
478 | } | |
479 | return (1); | |
480 | } | |
481 | ||
482 | char * | |
483 | getcmd() | |
484 | { | |
485 | char cmdbuf[BUFSIZ]; | |
486 | union { | |
487 | char argc[CLSIZE*NBPG]; | |
488 | int argi[CLSIZE*NBPG/sizeof (int)]; | |
489 | } argspac; | |
490 | register char *cp; | |
491 | register int *ip; | |
492 | char c; | |
493 | int nbad; | |
494 | struct dblock db; | |
495 | ||
496 | if (mproc.p_stat == SZOMB) | |
497 | return (""); | |
498 | if (cflg) | |
499 | goto retucomm; | |
500 | if ((mproc.p_flag & SLOAD) == 0 || argaddr == 0) { | |
501 | vstodb(0, CLSIZE, &u.u_smap, &db, 1); | |
502 | lseek(swap, ctob(db.db_base), 0); | |
503 | if (read(swap, (char *)&argspac, sizeof(argspac)) | |
504 | != sizeof(argspac)) | |
505 | goto bad; | |
506 | } else { | |
507 | lseek(mem, argaddr, 0); | |
508 | if (read(mem, (char *)&argspac, sizeof (argspac)) | |
509 | != sizeof (argspac)) | |
510 | goto bad; | |
511 | } | |
512 | ip = &argspac.argi[CLSIZE*NBPG/sizeof (int)]; | |
513 | *--ip = 0; | |
514 | while (*--ip) | |
515 | if (ip == argspac.argi) | |
516 | goto retucomm; | |
517 | *(char *)ip = ' '; | |
518 | ip++; | |
519 | nbad = 0; | |
520 | for (cp = (char *)ip; cp < &argspac.argc[CLSIZE*NBPG]; cp++) { | |
521 | c = *cp & 0177; | |
522 | if (c == 0) | |
523 | *cp = ' '; | |
524 | else if (c < ' ' || c > 0176) { | |
525 | if (++nbad >= 5) { | |
526 | *cp++ = ' '; | |
527 | break; | |
528 | } | |
529 | *cp = '?'; | |
530 | } else if (c == '=') { | |
531 | while (*--cp != ' ') | |
532 | if (cp <= (char *)ip) | |
533 | break; | |
534 | break; | |
535 | } | |
536 | } | |
537 | *cp = 0; | |
538 | while (*--cp == ' ') | |
539 | *cp = 0; | |
540 | cp = (char *)ip; | |
541 | strncpy(cmdbuf, cp, &argspac.argc[CLSIZE*NBPG] - cp); | |
542 | if (cp[0] == '-' || cp[0] == '?' || cp[0] <= ' ') { | |
543 | strcat(cp, " ("); | |
544 | strncat(cp, u.u_comm, sizeof(u.u_comm)); | |
545 | strcat(cp, ")"); | |
546 | } | |
547 | if (xflg == 0 && gflg == 0 && tptr == 0 && cp[0] == '-') | |
548 | return (0); | |
549 | return (savestr(cmdbuf)); | |
550 | ||
551 | bad: | |
552 | fprintf(stderr, "ps: error locating command name for pid %d\n", | |
553 | mproc.p_pid); | |
554 | retucomm: | |
555 | strcat(cp, " ("); | |
556 | strncpy(cmdbuf, u.u_comm, sizeof (u.u_comm)); | |
557 | strcat(cp, ")"); | |
558 | return (savestr(cmdbuf)); | |
559 | } | |
560 | ||
561 | char *lhdr = | |
562 | " F S UID PID PPID CP PRI NICE ADDR SZ RSS WCHAN TTY TIME"; | |
563 | lpr(sp) | |
564 | struct savcom *sp; | |
565 | { | |
566 | register struct asav *ap = sp->ap; | |
567 | register struct lsav *lp = sp->sun.lp; | |
568 | ||
569 | printf("%5x %c %4d%5u%6u%3d%4d%4d%6x%4d%5d", | |
570 | ap->a_flag, "0SWRIZT"[ap->a_stat], ap->a_uid, | |
571 | ap->a_pid, lp->l_ppid, lp->l_cpu&0377, lp->l_pri-PZERO, | |
572 | ap->a_nice-NZERO, lp->l_addr, ap->a_size, ap->a_rss); | |
573 | printf(lp->l_wchan ? "%6x" : " ", lp->l_wchan); | |
574 | ptty(ap->a_tty); | |
575 | ptime(ap); | |
576 | } | |
577 | ||
578 | ptty(tp) | |
579 | char *tp; | |
580 | { | |
581 | ||
582 | printf(" %-2.2s", tp); | |
583 | } | |
584 | ||
585 | ptime(ap) | |
586 | struct asav *ap; | |
587 | { | |
588 | ||
589 | if (ap->a_stat == SZOMB) | |
590 | printf(" <defunct>"); | |
591 | else | |
592 | printf("%3ld:%02ld", ap->a_time / HZ, ap->a_time % HZ); | |
593 | } | |
594 | ||
595 | char *uhdr = | |
596 | "USER PID %%CPU NICE SZ RSS TTY TIME"; | |
597 | upr(sp) | |
598 | struct savcom *sp; | |
599 | { | |
600 | register struct asav *ap = sp->ap; | |
601 | ||
602 | printf("%-8.8s%5u%5.1f%4d%4d%5d", | |
603 | getname(ap->a_uid), ap->a_pid, sp->sun.u_pctcpu, ap->a_nice, | |
604 | ap->a_size, ap->a_rss); | |
605 | ptty(ap->a_tty); | |
606 | ptime(ap); | |
607 | } | |
608 | ||
609 | char *vhdr = | |
610 | "F PID TT TIME TIM SL MINFLT MAJFLT SIZE RSS SRS TSIZ TRS PF"; | |
611 | vpr(sp) | |
612 | struct savcom *sp; | |
613 | { | |
614 | register struct vsav *vp = sp->sun.vp; | |
615 | register struct asav *ap = sp->ap; | |
616 | char stat, nice, anom; | |
617 | ||
618 | switch (ap->a_stat) { | |
619 | ||
620 | case SSLEEP: | |
621 | case SSTOP: | |
622 | if ((ap->a_flag & SLOAD) == 0) | |
623 | stat = 'W'; | |
624 | else if (vp->v_pri >= PZERO) | |
625 | stat = 'S'; | |
626 | else if (ap->a_flag & SPAGE) | |
627 | stat = 'P'; | |
628 | else | |
629 | stat = 'D'; | |
630 | break; | |
631 | ||
632 | case SRUN: | |
633 | case SIDL: | |
634 | stat = ap->a_flag & SLOAD ? 'R' : 'W'; | |
635 | break; | |
636 | } | |
637 | nice = ap->a_nice > NZERO ? 'N' : ' '; | |
638 | anom = ap->a_flag & (SANOM|SUANOM) ? 'A' : ' '; | |
639 | printf("%c%c%c%5u", stat, nice, anom, ap->a_pid); | |
640 | ptty(ap->a_tty); | |
641 | ptime(ap); | |
642 | printf("%3d%3d%8d%8d%5d%5d%5d%5d%4d%3d", | |
643 | ap->a_time, vp->v_slptime, vp->v_majflt, vp->v_minflt, | |
644 | ap->a_size, ap->a_rss, vp->v_swrss, vp->v_tsiz, vp->v_txtrss, | |
645 | vp->v_aveflt); | |
646 | } | |
647 | ||
648 | char *shdr = | |
649 | " SSIZ PID TTY TIME"; | |
650 | spr(sp) | |
651 | struct savcom *sp; | |
652 | { | |
653 | register struct asav *ap = sp->ap; | |
654 | ||
655 | if (sflg) | |
656 | printf("%5d", sp->sun.s_ssiz); | |
657 | printf(" %5u", ap->a_pid); | |
658 | ptty(ap->a_tty); | |
659 | ptime(ap); | |
660 | } | |
661 | ||
662 | char * | |
663 | gettty() | |
664 | { | |
665 | register i; | |
666 | register char *p; | |
667 | ||
668 | if (u.u_ttyp == 0) | |
669 | return("?"); | |
670 | for (i=0; i<ndev; i++) { | |
671 | if (devl[i].dev == u.u_ttyd) { | |
672 | p = devl[i].dname; | |
673 | if (p[0]=='t' && p[1]=='t' && p[2]=='y') | |
674 | p += 3; | |
675 | return(p); | |
676 | } | |
677 | } | |
678 | return("?"); | |
679 | } | |
680 | ||
681 | /* | |
682 | * Given a base/size pair in virtual swap area, | |
683 | * return a physical base/size pair which is the | |
684 | * (largest) initial, physically contiguous block. | |
685 | */ | |
686 | vstodb(vsbase, vssize, dmp, dbp, rev) | |
687 | register int vsbase; | |
688 | int vssize; | |
689 | struct dmap *dmp; | |
690 | register struct dblock *dbp; | |
691 | { | |
692 | register int blk = DMMIN; | |
693 | register swblk_t *ip = dmp->dm_map; | |
694 | ||
695 | if (vsbase < 0 || vsbase + vssize > dmp->dm_size) | |
696 | panic("vstodb"); | |
697 | while (vsbase >= blk) { | |
698 | vsbase -= blk; | |
699 | if (blk < DMMAX) | |
700 | blk *= 2; | |
701 | ip++; | |
702 | } | |
703 | if (*ip <= 0 || *ip + blk > nswap) | |
704 | panic("vstodb *ip"); | |
705 | dbp->db_size = min(vssize, blk - vsbase); | |
706 | dbp->db_base = *ip + (rev ? blk - (vsbase + dbp->db_size) : vsbase); | |
707 | } | |
708 | ||
709 | /*ARGSUSED*/ | |
710 | panic(cp) | |
711 | char *cp; | |
712 | { | |
713 | ||
714 | #ifdef DEBUG | |
715 | printf("%s\n", cp); | |
716 | #endif | |
717 | } | |
718 | ||
719 | min(a, b) | |
720 | { | |
721 | ||
722 | return (a < b ? a : b); | |
723 | } | |
724 | ||
725 | pscomp(s1, s2) | |
726 | struct savcom *s1, *s2; | |
727 | { | |
728 | register int i; | |
729 | ||
730 | if (vflg) | |
731 | return (vsize(s2) - vsize(s1)); | |
732 | i = s1->ap->a_ttyd - s2->ap->a_ttyd; | |
733 | if (i == 0) | |
734 | i = s1->ap->a_pid - s2->ap->a_pid; | |
735 | return (i); | |
736 | } | |
737 | ||
738 | vsize(sp) | |
739 | struct savcom *sp; | |
740 | { | |
741 | register struct asav *ap = sp->ap; | |
742 | register struct vsav *vp = sp->sun.vp; | |
743 | ||
744 | if (ap->a_flag & SLOAD) | |
745 | return (ap->a_rss + | |
746 | vp->v_txtrss / (vp->v_xccount ? vp->v_xccount : 1)); | |
747 | return (vp->v_swrss + (vp->v_xccount ? 0 : vp->v_txtswrss)); | |
748 | } | |
749 | ||
750 | #define NMAX 8 | |
751 | #define NUID 2048 | |
752 | ||
753 | char names[NUID][NMAX+1]; | |
754 | ||
755 | /* | |
756 | * Stolen from ls... | |
757 | */ | |
758 | char * | |
759 | getname(uid) | |
760 | { | |
761 | register struct passwd *pw; | |
762 | static init; | |
763 | struct passwd *getpwent(); | |
764 | ||
765 | if (names[uid][0]) | |
766 | return (&names[uid][0]); | |
767 | if (init == 2) | |
768 | return (0); | |
769 | if (init == 0) | |
770 | setpwent(), init = 1; | |
771 | while (pw = getpwent()) { | |
772 | if (pw->pw_uid >= NUID) | |
773 | continue; | |
774 | if (names[pw->pw_uid][0]) | |
775 | continue; | |
776 | strncpy(names[pw->pw_uid], pw->pw_name, NMAX); | |
777 | if (pw->pw_uid == uid) | |
778 | return (&names[uid][0]); | |
779 | } | |
780 | init = 2; | |
781 | endpwent(); | |
782 | return (0); | |
783 | } | |
784 | ||
785 | char *freebase; | |
786 | int nleft; | |
787 | ||
788 | char * | |
789 | alloc(size) | |
790 | int size; | |
791 | { | |
792 | register char *cp; | |
793 | register int i; | |
794 | ||
795 | if (size > nleft) { | |
796 | freebase = (char *)sbrk(size > 2048 ? size : 2048); | |
797 | if (freebase == 0) { | |
798 | fprintf(stderr, "ps: ran out of memory\n"); | |
799 | exit(1); | |
800 | } | |
801 | } | |
802 | cp = freebase; | |
803 | for (i = size; --i >= 0; ) | |
804 | *cp++ = 0; | |
805 | freebase = cp; | |
806 | return (cp - size); | |
807 | } | |
808 | ||
809 | char * | |
810 | savestr(cp) | |
811 | char *cp; | |
812 | { | |
813 | register int len; | |
814 | register char *dp; | |
815 | ||
816 | len = strlen(cp); | |
817 | dp = (char *)alloc(len+1); | |
818 | strcpy(dp, cp); | |
819 | return (dp); | |
820 | } |