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