xref: /csrg-svn/usr.bin/systat/fetch.c (revision 15146)
115131Ssam #ifndef lint
2*15146Ssam static char sccsid[] = "@(#)fetch.c	1.2 (Lucasfilm) 10/02/83";
315131Ssam #endif
415131Ssam 
515131Ssam #include "systat.h"
615131Ssam 
715131Ssam long
815131Ssam getw(loc)
915131Ssam         int loc;
1015131Ssam {
1115131Ssam         long word;
1215131Ssam 
1315131Ssam         lseek(kmem, loc, L_SET);
1415131Ssam         if (read(kmem, &word, sizeof (word)) != sizeof (word))
1515131Ssam                 printf("Error reading kmem at %x\n", loc);
1615131Ssam         return (word);
1715131Ssam }
1815131Ssam 
1915131Ssam char *
2015131Ssam getpname(pid, mproc)
2115131Ssam         int pid;
2215131Ssam         register struct proc *mproc;
2315131Ssam {
2415131Ssam         register struct procs *pp;
2515131Ssam         register char *namp;
2615131Ssam         register int j;
2715131Ssam         char *getcmd();
2815131Ssam 
2915131Ssam         pp = procs;
3015131Ssam         for (j = numprocs - 1; j >= 0; j--) {
3115131Ssam                 if (pp->pid == pid)
3215131Ssam                         return (pp->cmd);
3315131Ssam                 pp++;
3415131Ssam         }
3515131Ssam         if (j < 0) {
3615131Ssam                 if (numprocs < 200) {
3715131Ssam                         pp = &procs[numprocs];
3815131Ssam                         namp = strncpy(pp->cmd, getcmd(pid, mproc), 15);
3915131Ssam                         pp->cmd[15] = 0;
4015131Ssam                         pp->pid = pid;
4115131Ssam                         numprocs++;
4215131Ssam                 } else
4315131Ssam                         namp = getcmd(pid, mproc);
4415131Ssam         }
4515131Ssam         return (namp);
4615131Ssam }
4715131Ssam 
4815131Ssam union {
4915131Ssam         struct  user user;
5015131Ssam         char    upages[UPAGES][NBPG];
5115131Ssam } user;
5215131Ssam #define u       user.user
5315131Ssam 
5415131Ssam char *
5515131Ssam getcmd(pid, mproc)
5615131Ssam         int pid;
5715131Ssam         register struct proc *mproc;
5815131Ssam {
5915131Ssam         static char cmd[30];
6015131Ssam 
61*15146Ssam         if (mproc == NULL || mproc->p_stat == SZOMB)
62*15146Ssam 		return ("");
63*15146Ssam 	if (pid == 1)
64*15146Ssam 		return ("swapper");
65*15146Ssam 	if (pid == 2)
66*15146Ssam 		return ("pagedaemon");
67*15146Ssam         if (mproc->p_flag&(SSYS|SWEXIT))
6815131Ssam                 return ("");
69*15146Ssam         if (getu(mproc) == 0)
70*15146Ssam 		return ("???");
7115131Ssam         (void) strncpy(cmd, u.u_comm, sizeof (cmd));
7215131Ssam         return (cmd);
7315131Ssam }
7415131Ssam 
7515131Ssam getu(mproc)
7615131Ssam         register struct proc *mproc;
7715131Ssam {
7815131Ssam         struct pte *pteaddr, apte;
7915131Ssam         struct pte arguutl[UPAGES+CLSIZE];
8015131Ssam         register int i;
8115131Ssam         int ncl, size;
8215131Ssam 
8315131Ssam         size = sizeof (struct user);
8415131Ssam         if ((mproc->p_flag & SLOAD) == 0) {
8515131Ssam                 if (swap < 0)
8615131Ssam                         return (0);
8715131Ssam                 (void) lseek(swap, (long)dtob(mproc->p_swaddr), L_SET);
8815131Ssam                 if (read(swap, (char *)&user.user, size) != size) {
89*15146Ssam 			error("cant read u for pid %d", mproc->p_pid);
9015131Ssam                         return (0);
9115131Ssam                 }
9215131Ssam                 pcbpf = 0;
9315131Ssam                 argaddr = 0;
9415131Ssam                 return (1);
9515131Ssam         }
9615131Ssam         pteaddr = &Usrptma[btokmx(mproc->p_p0br) + mproc->p_szpt - 1];
9715131Ssam         klseek(kmem, (long)pteaddr, L_SET);
9815131Ssam         if (read(kmem, (char *)&apte, sizeof (apte)) != sizeof (apte)) {
99*15146Ssam                 error("cant read indir pte to get u for pid %d", mproc->p_pid);
10015131Ssam                 return (0);
10115131Ssam         }
10215131Ssam         klseek(mem,
10315131Ssam             (long)ctob(apte.pg_pfnum+1) - (UPAGES+CLSIZE) * sizeof (struct pte),
10415131Ssam                 L_SET);
10515131Ssam         if (read(mem, (char *)arguutl, sizeof (arguutl)) != sizeof (arguutl)) {
106*15146Ssam                 error("cant read page table for u of pid %d", mproc->p_pid);
10715131Ssam                 return (0);
10815131Ssam         }
10915131Ssam         if (arguutl[0].pg_fod == 0 && arguutl[0].pg_pfnum)
11015131Ssam                 argaddr = ctob(arguutl[0].pg_pfnum);
11115131Ssam         else
11215131Ssam                 argaddr = 0;
11315131Ssam         pcbpf = arguutl[CLSIZE].pg_pfnum;
11415131Ssam         ncl = (size + NBPG*CLSIZE - 1) / (NBPG*CLSIZE);
11515131Ssam         while (--ncl >= 0) {
11615131Ssam                 i = ncl * CLSIZE;
11715131Ssam                 klseek(mem, (long)ctob(arguutl[CLSIZE+i].pg_pfnum), L_SET);
11815131Ssam                 if (read(mem, user.upages[i], CLSIZE*NBPG) != CLSIZE*NBPG) {
119*15146Ssam                         error("cant read page %d of u of pid %d\n",
120*15146Ssam                             arguutl[CLSIZE+i].pg_pfnum, mproc->p_pid);
121*15146Ssam                         return (0);
12215131Ssam                 }
12315131Ssam         }
12415131Ssam         return (1);
12515131Ssam }
12615131Ssam 
12715131Ssam klseek(fd, loc, off)
12815131Ssam         int fd;
12915131Ssam         long loc;
13015131Ssam         int off;
13115131Ssam {
13215131Ssam 
13315131Ssam         (void) lseek(fd, (long)loc, off);
13415131Ssam }
135