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