1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
--- machdep/bsd-44.c.orig Sun Feb 20 07:37:39 2000
+++ machdep/bsd-44.c Tue Jun 27 00:03:39 2000
@@ -23,11 +23,54 @@
#endif
#endif
+#if defined(BSD4_4) && (BSD < 199306)
+#include <sys/kinfo.h>
+#include <sys/kinfo_proc.h>
+#define _ALLPROCFLAG KINFO_PROC_ALL
+#else
+#include <sys/sysctl.h>
+#include <limits.h>
+#define _ALLPROCFLAG KERN_PROC_ALL
+#endif
+
#include <sys/resource.h>
+#include <err.h>
+#include <signal.h>
#include <stdio.h>
-#include <unistd.h>
+#include <stdlib.h>
#include <string.h>
-#include <signal.h>
+#include <unistd.h>
+
+#ifndef P_WEXIT /* backward compatibility */
+#define P_WEXIT SWEXIT
+#endif
+
+static char *pidmap[] = { "swapper", "init", "pagedaemon" };
+static int pidmapsiz = sizeof(pidmap) / sizeof(pidmap[0]);
+
+#ifdef KERN_PROC2
+#define CMDLEN KI_MAXCOMLEN
+#define PROCTYPE struct kinfo_proc2
+#define SYSCTL_TO_USE KERN_PROC2
+#define SYSCTL_MIBS 6
+#define PROC_COMM(kprocp) kprocp->p_comm
+#define PROC_FLAG(kprocp) kprocp->p_flag
+#define PROC_PID(kprocp) kprocp->p_pid
+#define PROC_STAT(kprocp) kprocp->p_stat
+#define PROC_TDEV(kprocp) kprocp->p_tdev
+#define PROC_UID(kprocp) kprocp->p_uid
+#else
+#define PROCTYPE struct kinfo_proc
+#define SYSCTL_TO_USE KERN_PROC
+#define SYSCTL_MIBS 4
+#define PROC_COMM(kprocp) kprocp->kp_proc.p_comm
+#define PROC_FLAG(kprocp) kprocp->kp_proc.p_flag
+#define PROC_PID(kprocp) kprocp->kp_proc.p_pid
+#define PROC_STAT(kprocp) kprocp->kp_proc.p_stat
+#define PROC_TDEV(kprocp) kprocp->kp_eproc.e_tdev
+#define PROC_UID(kprocp) kprocp->kp_eproc.e_ucred.cr_uid
+#endif
+
/*
* Define SigNames, NSig, and TtyDevDir here; they are used by other
@@ -132,33 +175,6 @@
}
/*
- * Now, set up everything we need to write a GetProc() routine.
- */
-
-#include <kvm.h>
-#include <fcntl.h>
-
-#if defined(BSD4_4) && (BSD < 199306)
-#include <sys/kinfo.h>
-#include <sys/kinfo_proc.h>
-#define _ALLPROCFLAG KINFO_PROC_ALL
-#else
-#include <sys/sysctl.h>
-#include <limits.h>
-#define _ALLPROCFLAG KERN_PROC_ALL
-#endif
-
-#ifndef P_WEXIT /* backward compatibility */
-#define P_WEXIT SWEXIT
-#endif
-
-static char *pidmap[] = { "swapper", "init", "pagedaemon" };
-static int pidmapsiz = sizeof(pidmap) / sizeof(pidmap[0]);
-
-#define PROC(kprocp) kprocp->kp_proc
-#define EPROC(kprocp) kprocp->kp_eproc
-
-/*
* GetProc()
*
* Fill in and return a `struct ProcInfo' with information about the
@@ -169,54 +185,54 @@
{
static struct ProcInfo procinfo;
static int nproc = -1;
- static struct kinfo_proc *aproc;
- static kvm_t *kd = NULL;
-#ifdef _POSIX2_LINE_MAX
- char errbuf[_POSIX2_LINE_MAX];
-#else
- char errbuf[2048];
-#endif
+ static PROCTYPE *aproc;
+ int mib[6];
+ size_t size;
/*
* If this is our first time here, prepare to read procs from kernel.
*/
if (nproc == -1) {
- kd = kvm_openfiles((char *)NULL, (char *)NULL, (char *)NULL,
- O_RDONLY, errbuf);
- if (kd == NULL) {
- fprintf(stderr, "%s: %s\n", ProgName, errbuf);
- exit(EX_SERR);
- }
+ mib[0] = CTL_KERN;
+ mib[1] = SYSCTL_TO_USE;
+ mib[2] = _ALLPROCFLAG;
+ mib[3] = 0;
+#ifdef KERN_PROC2
+ mib[4] = sizeof(PROCTYPE);
+ mib[5] = 0;
+#endif
+ if (sysctl(mib, SYSCTL_MIBS, NULL, &size, NULL, 0) < 0)
+ err(1, "sysctl kern.proc2");
+ aproc = (PROCTYPE *)malloc(size);
+ if (aproc == NULL)
+ errx(1, "out of memory");
- if ((aproc=kvm_getprocs(kd, _ALLPROCFLAG, 0, &nproc)) == NULL) {
- fprintf(stderr, "%s: %s\n", ProgName, kvm_geterr(kd));
- exit(EX_SERR);
- }
+#ifdef KERN_PROC2
+ mib[5] = size / sizeof(PROCTYPE);
+#endif
+ if (sysctl(mib, SYSCTL_MIBS, aproc, &size, NULL, 0) < 0)
+ err(1, "sysctl kern.proc2");
+ nproc = size / sizeof(PROCTYPE);
}
- if (nproc == 0) {
- if (kd != NULL) {
- kvm_close(kd);
- kd = NULL;
- }
- return((struct ProcInfo *)NULL);
- }
+ if (nproc == 0)
+ return(NULL);
do {
- if (PROC(aproc).p_stat != 0) {
+ if (PROC_STAT(aproc) != 0) {
/*
* Make sure this isn't a "zombie" or "exiting"
* process. If it is, fill in procinfo and return.
*/
procinfo.pi_flags = 0;
- procinfo.pi_pid = (pid_T) PROC(aproc).p_pid;
- procinfo.pi_uid = (uid_T) EPROC(aproc).e_ucred.cr_uid;
+ procinfo.pi_pid = (pid_T) PROC_PID(aproc);
+ procinfo.pi_uid = (uid_T) PROC_UID(aproc);
- if (PROC(aproc).p_stat == SZOMB) { /* zombie */
+ if (PROC_STAT(aproc) == SZOMB) { /* zombie */
static char *zombie = "<defunct>";
procinfo.pi_flags |= PI_ZOMBIE;
procinfo.pi_cmd = zombie;
- } else if (PROC(aproc).p_flag & P_WEXIT) { /* exiting */
+ } else if (PROC_FLAG(aproc) & P_WEXIT) { /* exiting */
static char *exiting = "<exiting>";
procinfo.pi_flags |= PI_SWEXIT;
procinfo.pi_cmd = exiting;
@@ -228,22 +244,22 @@
return(&procinfo);
}
}
- } while (PROC(aproc).p_stat == 0);
+ } while (PROC_STAT(aproc) == 0);
/*
* We now have a process (`aproc').
* Fill in the rest of `procinfo'.
*/
- if (EPROC(aproc).e_tdev != NODEV) { /* controlling tty */
+ if (PROC_TDEV(aproc) != NODEV) { /* controlling tty */
procinfo.pi_flags |= PI_CTLTTY;
- procinfo.pi_tty = (tty_T) EPROC(aproc).e_tdev;
+ procinfo.pi_tty = (tty_T) PROC_TDEV(aproc);
}
- if (PROC(aproc).p_pid < pidmapsiz) { /* special */
- procinfo.pi_cmd = pidmap[PROC(aproc).p_pid];
+ if (PROC_PID(aproc) < pidmapsiz) { /* special */
+ procinfo.pi_cmd = pidmap[PROC_PID(aproc)];
procinfo.pi_flags |= PI_ASKUSR;
} else /* set path-stripped command name */
- SETCMD(procinfo.pi_cmd, PROC(aproc).p_comm, MAXCOMLEN)
+ SETCMD(procinfo.pi_cmd, PROC_COMM(aproc), MAXCOMLEN)
nproc--;
aproc++;
|