Commit | Line | Data |
---|---|---|
f42904bc DF |
1 | /* |
2 | * Copyright (c) 1980 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
7 | #ifndef lint | |
8 | char copyright[] = | |
9 | "@(#) Copyright (c) 1980 Regents of the University of California.\n\ | |
10 | All rights reserved.\n"; | |
11 | #endif not lint | |
12 | ||
68d13ee3 | 13 | #ifndef lint |
af08d75a | 14 | static char sccsid[] = "@(#)w.c 5.7 (Berkeley) %G%"; |
f42904bc DF |
15 | #endif not lint |
16 | ||
76238d88 BJ |
17 | /* |
18 | * w - print system status (who and what) | |
19 | * | |
20 | * This program is similar to the systat command on Tenex/Tops 10/20 | |
21 | * It needs read permission on /dev/mem, /dev/kmem, and /dev/drum. | |
22 | */ | |
23 | #include <sys/param.h> | |
24 | #include <nlist.h> | |
25 | #include <stdio.h> | |
26 | #include <ctype.h> | |
27 | #include <utmp.h> | |
76238d88 BJ |
28 | #include <sys/stat.h> |
29 | #include <sys/dir.h> | |
30 | #include <sys/user.h> | |
31 | #include <sys/proc.h> | |
3b8dff21 | 32 | #include <sys/ioctl.h> |
57ee57ff | 33 | #include <machine/pte.h> |
76238d88 | 34 | #include <sys/vm.h> |
af08d75a | 35 | #include <sys/tty.h> |
76238d88 BJ |
36 | |
37 | #define NMAX sizeof(utmp.ut_name) | |
38 | #define LMAX sizeof(utmp.ut_line) | |
3b8dff21 | 39 | #define HMAX sizeof(utmp.ut_host) |
76238d88 BJ |
40 | |
41 | #define ARGWIDTH 33 /* # chars left on 80 col crt for args */ | |
42 | ||
bdb1f95f | 43 | struct pr { |
76238d88 BJ |
44 | short w_pid; /* proc.p_pid */ |
45 | char w_flag; /* proc.p_flag */ | |
46 | short w_size; /* proc.p_size */ | |
47 | long w_seekaddr; /* where to find args */ | |
48 | long w_lastpg; /* disk address of stack */ | |
49 | int w_igintr; /* INTR+3*QUIT, 0=die, 1=ign, 2=catch */ | |
50 | time_t w_time; /* CPU time used by this process */ | |
51 | time_t w_ctime; /* CPU time used by children */ | |
52 | dev_t w_tty; /* tty device of process */ | |
af9a91c9 | 53 | int w_uid; /* uid of process */ |
76238d88 BJ |
54 | char w_comm[15]; /* user.u_comm, null terminated */ |
55 | char w_args[ARGWIDTH+1]; /* args if interesting process */ | |
bdb1f95f BJ |
56 | } *pr; |
57 | int nproc; | |
76238d88 BJ |
58 | |
59 | struct nlist nl[] = { | |
60 | { "_proc" }, | |
61 | #define X_PROC 0 | |
62 | { "_swapdev" }, | |
63 | #define X_SWAPDEV 1 | |
64 | { "_Usrptmap" }, | |
65 | #define X_USRPTMA 2 | |
66 | { "_usrpt" }, | |
67 | #define X_USRPT 3 | |
68 | { "_nswap" }, | |
69 | #define X_NSWAP 4 | |
70 | { "_avenrun" }, | |
71 | #define X_AVENRUN 5 | |
a43be079 SL |
72 | { "_boottime" }, |
73 | #define X_BOOTTIME 6 | |
bdb1f95f BJ |
74 | { "_nproc" }, |
75 | #define X_NPROC 7 | |
68d13ee3 SL |
76 | { "_dmmin" }, |
77 | #define X_DMMIN 8 | |
78 | { "_dmmax" }, | |
79 | #define X_DMMAX 9 | |
20932249 | 80 | { "" }, |
76238d88 BJ |
81 | }; |
82 | ||
83 | FILE *ps; | |
84 | FILE *ut; | |
85 | FILE *bootfd; | |
86 | int kmem; | |
87 | int mem; | |
88 | int swap; /* /dev/kmem, mem, and swap */ | |
89 | int nswap; | |
68d13ee3 | 90 | int dmmin, dmmax; |
76238d88 | 91 | dev_t tty; |
af9a91c9 | 92 | int uid; |
76238d88 BJ |
93 | char doing[520]; /* process attached to terminal */ |
94 | time_t proctime; /* cpu time of process in doing */ | |
95 | double avenrun[3]; | |
bdb1f95f | 96 | struct proc *aproc; |
af08d75a | 97 | struct tty ttyent; |
76238d88 | 98 | |
68d13ee3 | 99 | #define DIV60(t) ((t+30)/60) /* x/60 rounded */ |
af08d75a | 100 | #define TTYEQ (tty == pr[i].w_tty) |
76238d88 BJ |
101 | #define IGINT (1+3*1) /* ignoring both SIGINT & SIGQUIT */ |
102 | ||
103 | char *getargs(); | |
76238d88 BJ |
104 | char *ctime(); |
105 | char *rindex(); | |
106 | FILE *popen(); | |
107 | struct tm *localtime(); | |
80b96391 | 108 | time_t findidle(); |
76238d88 BJ |
109 | |
110 | int debug; /* true if -d flag: debugging output */ | |
3b8dff21 | 111 | int ttywidth = 80; /* width of tty */ |
76238d88 BJ |
112 | int header = 1; /* true if -h flag: don't print heading */ |
113 | int lflag = 1; /* true if -l flag: long style output */ | |
3b8dff21 | 114 | int prfrom = 1; /* true if not -f flag: print host from */ |
76238d88 | 115 | int login; /* true if invoked as login shell */ |
80b96391 | 116 | time_t idle; /* number of minutes user is idle */ |
76238d88 BJ |
117 | int nusers; /* number of users logged in now */ |
118 | char * sel_user; /* login of particular user selected */ | |
119 | char firstchar; /* first char of name of prog invoked as */ | |
120 | time_t jobtime; /* total cpu time visible */ | |
121 | time_t now; /* the current time of day */ | |
a43be079 SL |
122 | struct timeval boottime; |
123 | time_t uptime; /* time of last reboot & elapsed time since */ | |
76238d88 BJ |
124 | int np; /* number of processes currently active */ |
125 | struct utmp utmp; | |
126 | struct proc mproc; | |
20932249 SL |
127 | union { |
128 | struct user U_up; | |
129 | char pad[NBPG][UPAGES]; | |
130 | } Up; | |
131 | #define up Up.U_up | |
76238d88 BJ |
132 | |
133 | main(argc, argv) | |
134 | char **argv; | |
135 | { | |
136 | int days, hrs, mins; | |
137 | register int i, j; | |
138 | char *cp; | |
139 | register int curpid, empty; | |
3b8dff21 | 140 | struct winsize win; |
76238d88 | 141 | |
76238d88 BJ |
142 | login = (argv[0][0] == '-'); |
143 | cp = rindex(argv[0], '/'); | |
144 | firstchar = login ? argv[0][1] : (cp==0) ? argv[0][0] : cp[1]; | |
145 | cp = argv[0]; /* for Usage */ | |
146 | ||
147 | while (argc > 1) { | |
148 | if (argv[1][0] == '-') { | |
149 | for (i=1; argv[1][i]; i++) { | |
150 | switch(argv[1][i]) { | |
151 | ||
152 | case 'd': | |
153 | debug++; | |
154 | break; | |
155 | ||
3b8dff21 MK |
156 | case 'f': |
157 | prfrom = !prfrom; | |
158 | break; | |
159 | ||
76238d88 BJ |
160 | case 'h': |
161 | header = 0; | |
162 | break; | |
163 | ||
164 | case 'l': | |
165 | lflag++; | |
166 | break; | |
167 | ||
168 | case 's': | |
169 | lflag = 0; | |
170 | break; | |
171 | ||
172 | case 'u': | |
173 | case 'w': | |
635aa49f | 174 | firstchar = argv[1][i]; |
76238d88 BJ |
175 | break; |
176 | ||
177 | default: | |
178 | printf("Bad flag %s\n", argv[1]); | |
179 | exit(1); | |
180 | } | |
181 | } | |
182 | } else { | |
183 | if (!isalnum(argv[1][0]) || argc > 2) { | |
3b8dff21 | 184 | printf("Usage: %s [ -hlsfuw ] [ user ]\n", cp); |
76238d88 BJ |
185 | exit(1); |
186 | } else | |
187 | sel_user = argv[1]; | |
188 | } | |
189 | argc--; argv++; | |
190 | } | |
191 | ||
f04cce90 | 192 | if (ioctl(1, TIOCGWINSZ, &win) != -1 && win.ws_col > 70) |
3b8dff21 | 193 | ttywidth = win.ws_col; |
76238d88 BJ |
194 | if ((kmem = open("/dev/kmem", 0)) < 0) { |
195 | fprintf(stderr, "No kmem\n"); | |
196 | exit(1); | |
197 | } | |
198 | nlist("/vmunix", nl); | |
199 | if (nl[0].n_type==0) { | |
200 | fprintf(stderr, "No namelist\n"); | |
201 | exit(1); | |
202 | } | |
203 | ||
af08d75a MT |
204 | if (firstchar != 'u') /* if this program is not "uptime(1)" */ |
205 | readpr(); /* then read in procs */ | |
76238d88 BJ |
206 | |
207 | ut = fopen("/etc/utmp","r"); | |
80b96391 | 208 | time(&now); |
76238d88 BJ |
209 | if (header) { |
210 | /* Print time of day */ | |
80b96391 | 211 | prtat(&now); |
76238d88 BJ |
212 | |
213 | /* | |
214 | * Print how long system has been up. | |
a43be079 | 215 | * (Found by looking for "boottime" in kernel) |
76238d88 | 216 | */ |
a43be079 SL |
217 | lseek(kmem, (long)nl[X_BOOTTIME].n_value, 0); |
218 | read(kmem, &boottime, sizeof (boottime)); | |
76238d88 | 219 | |
a43be079 | 220 | uptime = now - boottime.tv_sec; |
baaa0078 | 221 | uptime += 30; |
76238d88 BJ |
222 | days = uptime / (60*60*24); |
223 | uptime %= (60*60*24); | |
224 | hrs = uptime / (60*60); | |
225 | uptime %= (60*60); | |
baaa0078 | 226 | mins = uptime / 60; |
76238d88 BJ |
227 | |
228 | printf(" up"); | |
229 | if (days > 0) | |
230 | printf(" %d day%s,", days, days>1?"s":""); | |
231 | if (hrs > 0 && mins > 0) { | |
232 | printf(" %2d:%02d,", hrs, mins); | |
233 | } else { | |
234 | if (hrs > 0) | |
235 | printf(" %d hr%s,", hrs, hrs>1?"s":""); | |
236 | if (mins > 0) | |
237 | printf(" %d min%s,", mins, mins>1?"s":""); | |
238 | } | |
239 | ||
240 | /* Print number of users logged in to system */ | |
241 | while (fread(&utmp, sizeof(utmp), 1, ut)) { | |
242 | if (utmp.ut_name[0] != '\0') | |
243 | nusers++; | |
244 | } | |
245 | rewind(ut); | |
fddf7972 | 246 | printf(" %d user%s", nusers, nusers>1?"s":""); |
76238d88 BJ |
247 | |
248 | /* | |
249 | * Print 1, 5, and 15 minute load averages. | |
250 | * (Found by looking in kernel for avenrun). | |
251 | */ | |
252 | printf(", load average:"); | |
253 | lseek(kmem, (long)nl[X_AVENRUN].n_value, 0); | |
254 | read(kmem, avenrun, sizeof(avenrun)); | |
255 | for (i = 0; i < (sizeof(avenrun)/sizeof(avenrun[0])); i++) { | |
256 | if (i > 0) | |
257 | printf(","); | |
258 | printf(" %.2f", avenrun[i]); | |
259 | } | |
260 | printf("\n"); | |
af08d75a | 261 | if (firstchar == 'u') /* if this was uptime(1), finished */ |
76238d88 BJ |
262 | exit(0); |
263 | ||
264 | /* Headers for rest of output */ | |
3b8dff21 MK |
265 | if (lflag && prfrom) |
266 | printf("User tty from login@ idle JCPU PCPU what\n"); | |
267 | else if (lflag) | |
76238d88 | 268 | printf("User tty login@ idle JCPU PCPU what\n"); |
3b8dff21 MK |
269 | else if (prfrom) |
270 | printf("User tty from idle what\n"); | |
76238d88 BJ |
271 | else |
272 | printf("User tty idle what\n"); | |
273 | fflush(stdout); | |
274 | } | |
275 | ||
276 | ||
277 | for (;;) { /* for each entry in utmp */ | |
278 | if (fread(&utmp, sizeof(utmp), 1, ut) == NULL) { | |
279 | fclose(ut); | |
280 | exit(0); | |
281 | } | |
282 | if (utmp.ut_name[0] == '\0') | |
283 | continue; /* that tty is free */ | |
284 | if (sel_user && strcmpn(utmp.ut_name, sel_user, NMAX) != 0) | |
285 | continue; /* we wanted only somebody else */ | |
286 | ||
287 | gettty(); | |
288 | jobtime = 0; | |
289 | proctime = 0; | |
290 | strcpy(doing, "-"); /* default act: normally never prints */ | |
291 | empty = 1; | |
292 | curpid = -1; | |
293 | idle = findidle(); | |
294 | for (i=0; i<np; i++) { /* for each process on this tty */ | |
295 | if (!(TTYEQ)) | |
296 | continue; | |
297 | jobtime += pr[i].w_time + pr[i].w_ctime; | |
298 | proctime += pr[i].w_time; | |
f8217b43 JL |
299 | /* |
300 | * Meaning of debug fields following proc name is: | |
301 | * & by itself: ignoring both SIGINT and QUIT. | |
302 | * (==> this proc is not a candidate.) | |
303 | * & <i> <q>: i is SIGINT status, q is quit. | |
304 | * 0 == DFL, 1 == IGN, 2 == caught. | |
305 | * *: proc pgrp == tty pgrp. | |
306 | */ | |
307 | if (debug) { | |
76238d88 BJ |
308 | printf("\t\t%d\t%s", pr[i].w_pid, pr[i].w_args); |
309 | if ((j=pr[i].w_igintr) > 0) | |
310 | if (j==IGINT) | |
311 | printf(" &"); | |
312 | else | |
313 | printf(" & %d %d", j%3, j/3); | |
314 | printf("\n"); | |
315 | } | |
316 | if (empty && pr[i].w_igintr!=IGINT) { | |
317 | empty = 0; | |
318 | curpid = -1; | |
319 | } | |
320 | if(pr[i].w_pid>curpid && (pr[i].w_igintr!=IGINT || empty)){ | |
321 | curpid = pr[i].w_pid; | |
322 | strcpy(doing, lflag ? pr[i].w_args : pr[i].w_comm); | |
323 | #ifdef notdef | |
324 | if (doing[0]==0 || doing[0]=='-' && doing[1]<=' ' || doing[0] == '?') { | |
325 | strcat(doing, " ("); | |
326 | strcat(doing, pr[i].w_comm); | |
327 | strcat(doing, ")"); | |
328 | } | |
329 | #endif | |
330 | } | |
331 | } | |
332 | putline(); | |
333 | } | |
334 | } | |
335 | ||
336 | /* figure out the major/minor device # pair for this tty */ | |
337 | gettty() | |
338 | { | |
339 | char ttybuf[20]; | |
340 | struct stat statbuf; | |
341 | ||
342 | ttybuf[0] = 0; | |
343 | strcpy(ttybuf, "/dev/"); | |
344 | strcat(ttybuf, utmp.ut_line); | |
345 | stat(ttybuf, &statbuf); | |
346 | tty = statbuf.st_rdev; | |
af9a91c9 | 347 | uid = statbuf.st_uid; |
76238d88 BJ |
348 | } |
349 | ||
350 | /* | |
351 | * putline: print out the accumulated line of info about one user. | |
352 | */ | |
353 | putline() | |
354 | { | |
355 | register int tm; | |
3b8dff21 | 356 | int width = ttywidth - 1; |
76238d88 BJ |
357 | |
358 | /* print login name of the user */ | |
359 | printf("%-*.*s ", NMAX, NMAX, utmp.ut_name); | |
3b8dff21 | 360 | width -= NMAX + 1; |
76238d88 BJ |
361 | |
362 | /* print tty user is on */ | |
3b8dff21 | 363 | if (lflag && !prfrom) { |
76238d88 BJ |
364 | /* long form: all (up to) LMAX chars */ |
365 | printf("%-*.*s", LMAX, LMAX, utmp.ut_line); | |
3b8dff21 MK |
366 | width -= LMAX; |
367 | } else { | |
76238d88 BJ |
368 | /* short form: 2 chars, skipping 'tty' if there */ |
369 | if (utmp.ut_line[0]=='t' && utmp.ut_line[1]=='t' && utmp.ut_line[2]=='y') | |
370 | printf("%-2.2s", &utmp.ut_line[3]); | |
371 | else | |
372 | printf("%-2.2s", utmp.ut_line); | |
3b8dff21 MK |
373 | width -= 2; |
374 | } | |
375 | ||
376 | if (prfrom) { | |
377 | printf(" %-14.14s", utmp.ut_host); | |
378 | width -= 15; | |
76238d88 BJ |
379 | } |
380 | ||
3b8dff21 | 381 | if (lflag) { |
76238d88 | 382 | /* print when the user logged in */ |
80b96391 | 383 | prtat(&utmp.ut_time); |
3b8dff21 MK |
384 | width -= 8; |
385 | } | |
76238d88 BJ |
386 | |
387 | /* print idle time */ | |
80b96391 MK |
388 | if (idle >= 36 * 60) |
389 | printf("%2ddays ", (idle + 12 * 60) / (24 * 60)); | |
390 | else | |
391 | prttime(idle," "); | |
3b8dff21 | 392 | width -= 7; |
76238d88 BJ |
393 | |
394 | if (lflag) { | |
395 | /* print CPU time for all processes & children */ | |
a591da4b | 396 | prttime(jobtime," "); |
3b8dff21 | 397 | width -= 7; |
76238d88 | 398 | /* print cpu time for interesting process */ |
a591da4b | 399 | prttime(proctime," "); |
3b8dff21 | 400 | width -= 7; |
76238d88 BJ |
401 | } |
402 | ||
403 | /* what user is doing, either command tail or args */ | |
3b8dff21 | 404 | printf(" %-.*s\n", width-1, doing); |
76238d88 BJ |
405 | fflush(stdout); |
406 | } | |
407 | ||
408 | /* find & return number of minutes current tty has been idle */ | |
409 | findidle() | |
410 | { | |
411 | struct stat stbuf; | |
412 | long lastaction, diff; | |
413 | char ttyname[20]; | |
414 | ||
415 | strcpy(ttyname, "/dev/"); | |
416 | strcatn(ttyname, utmp.ut_line, LMAX); | |
417 | stat(ttyname, &stbuf); | |
418 | time(&now); | |
419 | lastaction = stbuf.st_atime; | |
420 | diff = now - lastaction; | |
421 | diff = DIV60(diff); | |
422 | if (diff < 0) diff = 0; | |
423 | return(diff); | |
424 | } | |
425 | ||
80b96391 MK |
426 | #define HR (60 * 60) |
427 | #define DAY (24 * HR) | |
428 | #define MON (30 * DAY) | |
429 | ||
76238d88 | 430 | /* |
80b96391 | 431 | * prttime prints a time in hours and minutes or minutes and seconds. |
76238d88 BJ |
432 | * The character string tail is printed at the end, obvious |
433 | * strings to pass are "", " ", or "am". | |
434 | */ | |
435 | prttime(tim, tail) | |
436 | time_t tim; | |
437 | char *tail; | |
438 | { | |
76238d88 BJ |
439 | |
440 | if (tim >= 60) { | |
441 | printf("%3d:", tim/60); | |
80b96391 MK |
442 | tim %= 60; |
443 | printf("%02d", tim); | |
444 | } else if (tim > 0) | |
445 | printf(" %2d", tim); | |
446 | else | |
447 | printf(" "); | |
76238d88 BJ |
448 | printf("%s", tail); |
449 | } | |
450 | ||
80b96391 MK |
451 | char *weekday[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
452 | char *month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
453 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; | |
454 | ||
76238d88 | 455 | /* prtat prints a 12 hour time given a pointer to a time of day */ |
80b96391 MK |
456 | prtat(time) |
457 | long *time; | |
76238d88 | 458 | { |
80b96391 MK |
459 | struct tm *p; |
460 | register int hr, pm; | |
461 | ||
462 | p = localtime(time); | |
463 | hr = p->tm_hour; | |
464 | pm = (hr > 11); | |
465 | if (hr > 11) | |
466 | hr -= 12; | |
467 | if (hr == 0) | |
468 | hr = 12; | |
469 | if (now - *time <= 18 * HR) | |
470 | prttime(hr * 60 + p->tm_min, pm ? "pm" : "am"); | |
471 | else if (now - *time <= 7 * DAY) | |
472 | printf(" %s%2d%s", weekday[p->tm_wday], hr, pm ? "pm" : "am"); | |
473 | else | |
474 | printf(" %2d%s%2d", p->tm_mday, month[p->tm_mon], p->tm_year); | |
76238d88 BJ |
475 | } |
476 | ||
477 | /* | |
478 | * readpr finds and reads in the array pr, containing the interesting | |
479 | * parts of the proc and user tables for each live process. | |
af08d75a MT |
480 | * We only accept procs whos controlling tty has a pgrp equal to the |
481 | * pgrp of the proc. This accurately defines the notion of the current | |
482 | * process(s), but because of time skew, we always read in the tty struct | |
483 | * after reading the proc, even though the same tty struct may have been | |
484 | * read earlier on. | |
76238d88 BJ |
485 | */ |
486 | readpr() | |
487 | { | |
488 | int pn, mf, addr, c; | |
489 | int szpt, pfnum, i; | |
490 | struct pte *Usrptma, *usrpt, *pte, apte; | |
491 | struct dblock db; | |
492 | ||
493 | Usrptma = (struct pte *) nl[X_USRPTMA].n_value; | |
494 | usrpt = (struct pte *) nl[X_USRPT].n_value; | |
495 | if((mem = open("/dev/mem", 0)) < 0) { | |
496 | fprintf(stderr, "No mem\n"); | |
497 | exit(1); | |
498 | } | |
499 | if ((swap = open("/dev/drum", 0)) < 0) { | |
500 | fprintf(stderr, "No drum\n"); | |
501 | exit(1); | |
502 | } | |
503 | /* | |
504 | * read mem to find swap dev. | |
505 | */ | |
506 | lseek(kmem, (long)nl[X_SWAPDEV].n_value, 0); | |
507 | read(kmem, &nl[X_SWAPDEV].n_value, sizeof(nl[X_SWAPDEV].n_value)); | |
508 | /* | |
68d13ee3 | 509 | * Find base of and parameters of swap |
76238d88 BJ |
510 | */ |
511 | lseek(kmem, (long)nl[X_NSWAP].n_value, 0); | |
512 | read(kmem, &nswap, sizeof(nswap)); | |
68d13ee3 SL |
513 | lseek(kmem, (long)nl[X_DMMIN].n_value, 0); |
514 | read(kmem, &dmmin, sizeof(dmmin)); | |
515 | lseek(kmem, (long)nl[X_DMMAX].n_value, 0); | |
516 | read(kmem, &dmmax, sizeof(dmmax)); | |
76238d88 BJ |
517 | /* |
518 | * Locate proc table | |
519 | */ | |
bdb1f95f BJ |
520 | lseek(kmem, (long)nl[X_NPROC].n_value, 0); |
521 | read(kmem, &nproc, sizeof(nproc)); | |
522 | pr = (struct pr *)calloc(nproc, sizeof (struct pr)); | |
76238d88 | 523 | np = 0; |
bdb1f95f BJ |
524 | lseek(kmem, (long)nl[X_PROC].n_value, 0); |
525 | read(kmem, &aproc, sizeof(aproc)); | |
526 | for (pn=0; pn<nproc; pn++) { | |
527 | lseek(kmem, (int)(aproc + pn), 0); | |
76238d88 BJ |
528 | read(kmem, &mproc, sizeof mproc); |
529 | /* decide if it's an interesting process */ | |
af08d75a MT |
530 | if (mproc.p_stat==0 || mproc.p_stat==SZOMB |
531 | || mproc.p_stat==SSTOP || mproc.p_pgrp==0) | |
76238d88 | 532 | continue; |
76238d88 BJ |
533 | /* find & read in the user structure */ |
534 | if ((mproc.p_flag & SLOAD) == 0) { | |
535 | /* not in memory - get from swap device */ | |
20932249 | 536 | addr = dtob(mproc.p_swaddr); |
76238d88 BJ |
537 | lseek(swap, (long)addr, 0); |
538 | if (read(swap, &up, sizeof(up)) != sizeof(up)) { | |
539 | continue; | |
540 | } | |
541 | } else { | |
542 | int p0br, cc; | |
543 | #define INTPPG (NBPG / sizeof (int)) | |
544 | struct pte pagetbl[NBPG / sizeof (struct pte)]; | |
545 | /* loaded, get each page from memory separately */ | |
546 | szpt = mproc.p_szpt; | |
547 | p0br = (int)mproc.p_p0br; | |
548 | pte = &Usrptma[btokmx(mproc.p_p0br) + szpt-1]; | |
549 | lseek(kmem, (long)pte, 0); | |
550 | if (read(kmem, &apte, sizeof(apte)) != sizeof(apte)) | |
551 | continue; | |
552 | lseek(mem, ctob(apte.pg_pfnum), 0); | |
553 | if (read(mem,pagetbl,sizeof(pagetbl)) != sizeof(pagetbl)) | |
554 | cont: | |
555 | continue; | |
556 | for(cc=0; cc<UPAGES; cc++) { /* get u area */ | |
557 | int upage = pagetbl[NPTEPG-UPAGES+cc].pg_pfnum; | |
558 | lseek(mem,ctob(upage),0); | |
559 | if (read(mem,((int *)&up)+INTPPG*cc,NBPG) != NBPG) | |
560 | goto cont; | |
561 | } | |
562 | szpt = up.u_pcb.pcb_szpt; | |
563 | pr[np].w_seekaddr = ctob(apte.pg_pfnum); | |
564 | } | |
e941b445 | 565 | vstodb(0, CLSIZE, &up.u_smap, &db, 1); |
20932249 | 566 | pr[np].w_lastpg = dtob(db.db_base); |
76238d88 BJ |
567 | if (up.u_ttyp == NULL) |
568 | continue; | |
569 | ||
af08d75a MT |
570 | /* only include a process whose tty has a pgrp which matchs its own */ |
571 | lseek(kmem, (long)up.u_ttyp, 0); | |
572 | if (read(kmem, &ttyent, sizeof(ttyent)) != sizeof(ttyent)) | |
573 | continue; | |
574 | if (ttyent.t_pgrp != mproc.p_pgrp) | |
575 | continue; | |
576 | ||
76238d88 BJ |
577 | /* save the interesting parts */ |
578 | pr[np].w_pid = mproc.p_pid; | |
579 | pr[np].w_flag = mproc.p_flag; | |
580 | pr[np].w_size = mproc.p_dsize + mproc.p_ssize; | |
20932249 SL |
581 | pr[np].w_igintr = (((int)up.u_signal[2]==1) + |
582 | 2*((int)up.u_signal[2]>1) + 3*((int)up.u_signal[3]==1)) + | |
583 | 6*((int)up.u_signal[3]>1); | |
a43be079 SL |
584 | pr[np].w_time = |
585 | up.u_ru.ru_utime.tv_sec + up.u_ru.ru_stime.tv_sec; | |
586 | pr[np].w_ctime = | |
587 | up.u_cru.ru_utime.tv_sec + up.u_cru.ru_stime.tv_sec; | |
76238d88 | 588 | pr[np].w_tty = up.u_ttyd; |
af9a91c9 | 589 | pr[np].w_uid = mproc.p_uid; |
76238d88 BJ |
590 | up.u_comm[14] = 0; /* Bug: This bombs next field. */ |
591 | strcpy(pr[np].w_comm, up.u_comm); | |
592 | /* | |
593 | * Get args if there's a chance we'll print it. | |
594 | * Cant just save pointer: getargs returns static place. | |
595 | * Cant use strcpyn: that crock blank pads. | |
596 | */ | |
597 | pr[np].w_args[0] = 0; | |
598 | strcatn(pr[np].w_args,getargs(&pr[np]),ARGWIDTH); | |
599 | if (pr[np].w_args[0]==0 || pr[np].w_args[0]=='-' && pr[np].w_args[1]<=' ' || pr[np].w_args[0] == '?') { | |
600 | strcat(pr[np].w_args, " ("); | |
601 | strcat(pr[np].w_args, pr[np].w_comm); | |
602 | strcat(pr[np].w_args, ")"); | |
603 | } | |
604 | np++; | |
605 | } | |
606 | } | |
607 | ||
608 | /* | |
609 | * getargs: given a pointer to a proc structure, this looks at the swap area | |
610 | * and tries to reconstruct the arguments. This is straight out of ps. | |
611 | */ | |
612 | char * | |
613 | getargs(p) | |
bdb1f95f | 614 | struct pr *p; |
76238d88 BJ |
615 | { |
616 | int c, addr, nbad; | |
e941b445 | 617 | static int abuf[CLSIZE*NBPG/sizeof(int)]; |
76238d88 BJ |
618 | struct pte pagetbl[NPTEPG]; |
619 | register int *ip; | |
620 | register char *cp, *cp1; | |
621 | ||
622 | if ((p->w_flag & SLOAD) == 0) { | |
623 | lseek(swap, p->w_lastpg, 0); | |
624 | if (read(swap, abuf, sizeof(abuf)) != sizeof(abuf)) | |
625 | return(p->w_comm); | |
626 | } else { | |
627 | c = p->w_seekaddr; | |
628 | lseek(mem,c,0); | |
629 | if (read(mem,pagetbl,NBPG) != NBPG) | |
630 | return(p->w_comm); | |
e941b445 BJ |
631 | if (pagetbl[NPTEPG-CLSIZE-UPAGES].pg_fod==0 && pagetbl[NPTEPG-CLSIZE-UPAGES].pg_pfnum) { |
632 | lseek(mem,ctob(pagetbl[NPTEPG-CLSIZE-UPAGES].pg_pfnum),0); | |
76238d88 BJ |
633 | if (read(mem,abuf,sizeof(abuf)) != sizeof(abuf)) |
634 | return(p->w_comm); | |
635 | } else { | |
636 | lseek(swap, p->w_lastpg, 0); | |
637 | if (read(swap, abuf, sizeof(abuf)) != sizeof(abuf)) | |
638 | return(p->w_comm); | |
639 | } | |
640 | } | |
e941b445 BJ |
641 | abuf[sizeof(abuf)/sizeof(abuf[0])-1] = 0; |
642 | for (ip = &abuf[sizeof(abuf)/sizeof(abuf[0])-2]; ip > abuf;) { | |
76238d88 BJ |
643 | /* Look from top for -1 or 0 as terminator flag. */ |
644 | if (*--ip == -1 || *ip == 0) { | |
645 | cp = (char *)(ip+1); | |
646 | if (*cp==0) | |
647 | cp++; | |
648 | nbad = 0; /* up to 5 funny chars as ?'s */ | |
e941b445 | 649 | for (cp1 = cp; cp1 < (char *)&abuf[sizeof(abuf)/sizeof(abuf[0])]; cp1++) { |
76238d88 BJ |
650 | c = *cp1&0177; |
651 | if (c==0) /* nulls between args => spaces */ | |
652 | *cp1 = ' '; | |
653 | else if (c < ' ' || c > 0176) { | |
654 | if (++nbad >= 5) { | |
655 | *cp1++ = ' '; | |
656 | break; | |
657 | } | |
658 | *cp1 = '?'; | |
659 | } else if (c=='=') { /* Oops - found an | |
660 | * environment var, back | |
661 | * over & erase it. */ | |
662 | *cp1 = 0; | |
663 | while (cp1>cp && *--cp1!=' ') | |
664 | *cp1 = 0; | |
665 | break; | |
666 | } | |
667 | } | |
668 | while (*--cp1==' ') /* strip trailing spaces */ | |
669 | *cp1 = 0; | |
670 | return(cp); | |
671 | } | |
672 | } | |
673 | return (p->w_comm); | |
674 | } | |
675 | ||
676 | /* | |
677 | * Given a base/size pair in virtual swap area, | |
678 | * return a physical base/size pair which is the | |
679 | * (largest) initial, physically contiguous block. | |
680 | */ | |
681 | vstodb(vsbase, vssize, dmp, dbp, rev) | |
682 | register int vsbase; | |
683 | int vssize; | |
684 | struct dmap *dmp; | |
685 | register struct dblock *dbp; | |
686 | { | |
68d13ee3 | 687 | register int blk = dmmin; |
76238d88 BJ |
688 | register swblk_t *ip = dmp->dm_map; |
689 | ||
20932249 SL |
690 | vsbase = ctod(vsbase); |
691 | vssize = ctod(vssize); | |
76238d88 BJ |
692 | if (vsbase < 0 || vsbase + vssize > dmp->dm_size) |
693 | panic("vstodb"); | |
694 | while (vsbase >= blk) { | |
695 | vsbase -= blk; | |
68d13ee3 | 696 | if (blk < dmmax) |
76238d88 BJ |
697 | blk *= 2; |
698 | ip++; | |
699 | } | |
700 | if (*ip <= 0 || *ip + blk > nswap) | |
701 | panic("vstodb *ip"); | |
702 | dbp->db_size = min(vssize, blk - vsbase); | |
703 | dbp->db_base = *ip + (rev ? blk - (vsbase + dbp->db_size) : vsbase); | |
704 | } | |
705 | ||
706 | panic(cp) | |
707 | char *cp; | |
708 | { | |
709 | ||
710 | /* printf("%s\n", cp); */ | |
711 | } | |
712 | ||
713 | min(a, b) | |
714 | { | |
715 | ||
716 | return (a < b ? a : b); | |
717 | } |