xref: /csrg-svn/usr.bin/systat/fetch.c (revision 21448)
1*21448Smckusick /*
2*21448Smckusick  * Copyright (c) 1980 Regents of the University of California.
3*21448Smckusick  * All rights reserved.  The Berkeley software License Agreement
4*21448Smckusick  * specifies the terms and conditions for redistribution.
5*21448Smckusick  */
6*21448Smckusick 
715131Ssam #ifndef lint
8*21448Smckusick static char sccsid[] = "@(#)fetch.c	5.1 (Berkeley) 05/30/85";
9*21448Smckusick #endif not lint
1015131Ssam 
1115131Ssam #include "systat.h"
1216868Smckusick #include <sys/dir.h>
1316868Smckusick #include <sys/user.h>
1416868Smckusick #include <sys/proc.h>
1516868Smckusick #include <sys/vmmac.h>
1616868Smckusick #include <machine/pte.h>
1716868Smckusick #include <pwd.h>
1815131Ssam 
1915131Ssam long
2015131Ssam getw(loc)
2115131Ssam         int loc;
2215131Ssam {
2315131Ssam         long word;
2415131Ssam 
2515131Ssam         lseek(kmem, loc, L_SET);
2615131Ssam         if (read(kmem, &word, sizeof (word)) != sizeof (word))
2715131Ssam                 printf("Error reading kmem at %x\n", loc);
2815131Ssam         return (word);
2915131Ssam }
3015131Ssam 
3115131Ssam char *
3215131Ssam getpname(pid, mproc)
3315131Ssam         int pid;
3415131Ssam         register struct proc *mproc;
3515131Ssam {
3615131Ssam         register struct procs *pp;
3715131Ssam         register char *namp;
3815131Ssam         register int j;
3915131Ssam         char *getcmd();
4015131Ssam 
4115131Ssam         pp = procs;
4215131Ssam         for (j = numprocs - 1; j >= 0; j--) {
4315131Ssam                 if (pp->pid == pid)
4415131Ssam                         return (pp->cmd);
4515131Ssam                 pp++;
4615131Ssam         }
4715131Ssam         if (j < 0) {
4815131Ssam                 if (numprocs < 200) {
4915131Ssam                         pp = &procs[numprocs];
5015131Ssam                         namp = strncpy(pp->cmd, getcmd(pid, mproc), 15);
5115131Ssam                         pp->cmd[15] = 0;
5215131Ssam                         pp->pid = pid;
5315131Ssam                         numprocs++;
5415131Ssam                 } else
5515131Ssam                         namp = getcmd(pid, mproc);
5615131Ssam         }
5715131Ssam         return (namp);
5815131Ssam }
5915131Ssam 
6015131Ssam union {
6115131Ssam         struct  user user;
6215131Ssam         char    upages[UPAGES][NBPG];
6315131Ssam } user;
6415131Ssam #define u       user.user
6515131Ssam 
6615131Ssam char *
6715131Ssam getcmd(pid, mproc)
6815131Ssam         int pid;
6915131Ssam         register struct proc *mproc;
7015131Ssam {
7115131Ssam         static char cmd[30];
7215131Ssam 
7315146Ssam         if (mproc == NULL || mproc->p_stat == SZOMB)
7415146Ssam 		return ("");
7515146Ssam 	if (pid == 1)
7615146Ssam 		return ("swapper");
7715146Ssam 	if (pid == 2)
7815146Ssam 		return ("pagedaemon");
7915146Ssam         if (mproc->p_flag&(SSYS|SWEXIT))
8015131Ssam                 return ("");
8115146Ssam         if (getu(mproc) == 0)
8215146Ssam 		return ("???");
8315131Ssam         (void) strncpy(cmd, u.u_comm, sizeof (cmd));
8415131Ssam         return (cmd);
8515131Ssam }
8615131Ssam 
8715151Ssam static	int argaddr;
8815151Ssam static	int pcbpf;
8915151Ssam 
9015131Ssam getu(mproc)
9115131Ssam         register struct proc *mproc;
9215131Ssam {
9315131Ssam         struct pte *pteaddr, apte;
9415131Ssam         struct pte arguutl[UPAGES+CLSIZE];
9515131Ssam         register int i;
9615131Ssam         int ncl, size;
9715131Ssam 
9815131Ssam         size = sizeof (struct user);
9915131Ssam         if ((mproc->p_flag & SLOAD) == 0) {
10015131Ssam                 if (swap < 0)
10115131Ssam                         return (0);
10215131Ssam                 (void) lseek(swap, (long)dtob(mproc->p_swaddr), L_SET);
10315131Ssam                 if (read(swap, (char *)&user.user, size) != size) {
10415146Ssam 			error("cant read u for pid %d", mproc->p_pid);
10515131Ssam                         return (0);
10615131Ssam                 }
10715131Ssam                 pcbpf = 0;
10815131Ssam                 argaddr = 0;
10915131Ssam                 return (1);
11015131Ssam         }
11115131Ssam         pteaddr = &Usrptma[btokmx(mproc->p_p0br) + mproc->p_szpt - 1];
11215131Ssam         klseek(kmem, (long)pteaddr, L_SET);
11315131Ssam         if (read(kmem, (char *)&apte, sizeof (apte)) != sizeof (apte)) {
11415146Ssam                 error("cant read indir pte to get u for pid %d", mproc->p_pid);
11515131Ssam                 return (0);
11615131Ssam         }
11715131Ssam         klseek(mem,
11815131Ssam             (long)ctob(apte.pg_pfnum+1) - (UPAGES+CLSIZE) * sizeof (struct pte),
11915131Ssam                 L_SET);
12015131Ssam         if (read(mem, (char *)arguutl, sizeof (arguutl)) != sizeof (arguutl)) {
12115146Ssam                 error("cant read page table for u of pid %d", mproc->p_pid);
12215131Ssam                 return (0);
12315131Ssam         }
12415131Ssam         if (arguutl[0].pg_fod == 0 && arguutl[0].pg_pfnum)
12515131Ssam                 argaddr = ctob(arguutl[0].pg_pfnum);
12615131Ssam         else
12715131Ssam                 argaddr = 0;
12815131Ssam         pcbpf = arguutl[CLSIZE].pg_pfnum;
12915131Ssam         ncl = (size + NBPG*CLSIZE - 1) / (NBPG*CLSIZE);
13015131Ssam         while (--ncl >= 0) {
13115131Ssam                 i = ncl * CLSIZE;
13215131Ssam                 klseek(mem, (long)ctob(arguutl[CLSIZE+i].pg_pfnum), L_SET);
13315131Ssam                 if (read(mem, user.upages[i], CLSIZE*NBPG) != CLSIZE*NBPG) {
13415146Ssam                         error("cant read page %d of u of pid %d\n",
13515146Ssam                             arguutl[CLSIZE+i].pg_pfnum, mproc->p_pid);
13615146Ssam                         return (0);
13715131Ssam                 }
13815131Ssam         }
13915131Ssam         return (1);
14015131Ssam }
14115131Ssam 
14215131Ssam klseek(fd, loc, off)
14315131Ssam         int fd;
14415131Ssam         long loc;
14515131Ssam         int off;
14615131Ssam {
14715131Ssam 
14815131Ssam         (void) lseek(fd, (long)loc, off);
14915131Ssam }
150