xref: /original-bsd/usr.bin/systat/fetch.c (revision acfb0788)
1 #ifndef lint
2 static char sccsid[] = "@(#)fetch.c	1.4 (Lucasfilm) 08/09/84";
3 #endif
4 
5 #include "systat.h"
6 #include <sys/param.h>
7 #include <sys/dir.h>
8 #include <sys/user.h>
9 #include <sys/proc.h>
10 #include <sys/file.h>
11 #include <sys/vmmac.h>
12 #include <machine/pte.h>
13 #include <pwd.h>
14 
15 long
16 getw(loc)
17         int loc;
18 {
19         long word;
20 
21         lseek(kmem, loc, L_SET);
22         if (read(kmem, &word, sizeof (word)) != sizeof (word))
23                 printf("Error reading kmem at %x\n", loc);
24         return (word);
25 }
26 
27 char *
28 getpname(pid, mproc)
29         int pid;
30         register struct proc *mproc;
31 {
32         register struct procs *pp;
33         register char *namp;
34         register int j;
35         char *getcmd();
36 
37         pp = procs;
38         for (j = numprocs - 1; j >= 0; j--) {
39                 if (pp->pid == pid)
40                         return (pp->cmd);
41                 pp++;
42         }
43         if (j < 0) {
44                 if (numprocs < 200) {
45                         pp = &procs[numprocs];
46                         namp = strncpy(pp->cmd, getcmd(pid, mproc), 15);
47                         pp->cmd[15] = 0;
48                         pp->pid = pid;
49                         numprocs++;
50                 } else
51                         namp = getcmd(pid, mproc);
52         }
53         return (namp);
54 }
55 
56 union {
57         struct  user user;
58         char    upages[UPAGES][NBPG];
59 } user;
60 #define u       user.user
61 
62 char *
63 getcmd(pid, mproc)
64         int pid;
65         register struct proc *mproc;
66 {
67         static char cmd[30];
68 
69         if (mproc == NULL || mproc->p_stat == SZOMB)
70 		return ("");
71 	if (pid == 1)
72 		return ("swapper");
73 	if (pid == 2)
74 		return ("pagedaemon");
75         if (mproc->p_flag&(SSYS|SWEXIT))
76                 return ("");
77         if (getu(mproc) == 0)
78 		return ("???");
79         (void) strncpy(cmd, u.u_comm, sizeof (cmd));
80         return (cmd);
81 }
82 
83 static	int argaddr;
84 static	int pcbpf;
85 
86 getu(mproc)
87         register struct proc *mproc;
88 {
89         struct pte *pteaddr, apte;
90         struct pte arguutl[UPAGES+CLSIZE];
91         register int i;
92         int ncl, size;
93 
94         size = sizeof (struct user);
95         if ((mproc->p_flag & SLOAD) == 0) {
96                 if (swap < 0)
97                         return (0);
98                 (void) lseek(swap, (long)dtob(mproc->p_swaddr), L_SET);
99                 if (read(swap, (char *)&user.user, size) != size) {
100 			error("cant read u for pid %d", mproc->p_pid);
101                         return (0);
102                 }
103                 pcbpf = 0;
104                 argaddr = 0;
105                 return (1);
106         }
107         pteaddr = &Usrptma[btokmx(mproc->p_p0br) + mproc->p_szpt - 1];
108         klseek(kmem, (long)pteaddr, L_SET);
109         if (read(kmem, (char *)&apte, sizeof (apte)) != sizeof (apte)) {
110                 error("cant read indir pte to get u for pid %d", mproc->p_pid);
111                 return (0);
112         }
113         klseek(mem,
114             (long)ctob(apte.pg_pfnum+1) - (UPAGES+CLSIZE) * sizeof (struct pte),
115                 L_SET);
116         if (read(mem, (char *)arguutl, sizeof (arguutl)) != sizeof (arguutl)) {
117                 error("cant read page table for u of pid %d", mproc->p_pid);
118                 return (0);
119         }
120         if (arguutl[0].pg_fod == 0 && arguutl[0].pg_pfnum)
121                 argaddr = ctob(arguutl[0].pg_pfnum);
122         else
123                 argaddr = 0;
124         pcbpf = arguutl[CLSIZE].pg_pfnum;
125         ncl = (size + NBPG*CLSIZE - 1) / (NBPG*CLSIZE);
126         while (--ncl >= 0) {
127                 i = ncl * CLSIZE;
128                 klseek(mem, (long)ctob(arguutl[CLSIZE+i].pg_pfnum), L_SET);
129                 if (read(mem, user.upages[i], CLSIZE*NBPG) != CLSIZE*NBPG) {
130                         error("cant read page %d of u of pid %d\n",
131                             arguutl[CLSIZE+i].pg_pfnum, mproc->p_pid);
132                         return (0);
133                 }
134         }
135         return (1);
136 }
137 
138 klseek(fd, loc, off)
139         int fd;
140         long loc;
141         int off;
142 {
143 
144         (void) lseek(fd, (long)loc, off);
145 }
146