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