-
Notifications
You must be signed in to change notification settings - Fork 11
/
procfs_proc_info.cc
366 lines (316 loc) · 10.6 KB
/
procfs_proc_info.cc
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
extern "C" {
#if __i386__
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "procfs_proc_info.h"
enum {
PROC_INFO_LISTPIDS = 1,
PROC_INFO_PIDINFO = 2,
PROC_INFO_PIDFDINFO = 3,
PROC_INFO_KERNMSGBUF = 4,
};
extern int proc_pidinfo(int32_t pid, uint32_t flavor, uint64_t arg,
caddr_t buffer, int32_t buffersize);
extern int proc_pidfdinfo(int32_t pid, uint32_t flavor, uint32_t arg,
caddr_t buffer, int32_t buffersize);
static const char *
fd_type_string(uint32_t proc_fdtype)
{
switch (proc_fdtype) {
case PROX_FDTYPE_ATALK:
return "AppleTalk";
break;
case PROX_FDTYPE_VNODE:
return "vnode";
break;
case PROX_FDTYPE_SOCKET:
return "socket";
break;
case PROX_FDTYPE_PSHM:
return "POSIX shm";
break;
case PROX_FDTYPE_PSEM:
return "POSIX sem";
break;
case PROX_FDTYPE_KQUEUE:
return "kqueue";
break;
case PROX_FDTYPE_PIPE:
return "pipe";
break;
case PROX_FDTYPE_FSEVENTS:
return "fsevents";
break;
default:
return "unknown descriptor type";
break;
}
/* NOTREACHED */
return "UNKNOWN";
}
static const char *socket_families[] = {
"AF_UNSPEC",
"AF_UNIX",
"AF_INET",
"AF_IMPLINK",
"AF_PUP",
"AF_CHAOS",
"AF_NS",
"AF_ISO",
"AF_ECMA",
"AF_DATAKIT",
"AF_CCITT",
"AF_SNA",
"AF_DECnet",
"AF_DLI",
"AF_LAT",
"AF_HYLINK",
"AF_APPLETALK",
"AF_ROUTE",
"AF_LINK",
"#define",
"AF_COIP",
"AF_CNT",
"pseudo_AF_RTIP",
"AF_IPX",
"AF_SIP",
"pseudo_AF_PIP",
"pseudo_AF_BLUE",
"AF_NDRV",
"AF_ISDN",
"pseudo_AF_KEY",
"AF_INET6",
"AF_NATM",
"AF_SYSTEM",
"AF_NETBIOS",
"AF_PPP",
"pseudo_AF_HDRCMPLT",
"AF_RESERVED_36",
};
#define SOCKET_FAMILY_MAX (int)(sizeof(socket_families)/sizeof(char *))
static const char *
socket_family_string(int soi_family)
{
if ((soi_family < 0) || (soi_family >= SOCKET_FAMILY_MAX)) {
return "unknown socket family";
}
return socket_families[soi_family];
}
static const char *
socket_type_string(int soi_type)
{
switch (soi_type) {
case SOCK_STREAM:
return "SOCK_STREAM";
break;
case SOCK_DGRAM:
return "SOCK_DGRAM";
break;
case SOCK_RAW:
return "SOCK_RAW";
break;
case SOCK_RDM:
return "SOCK_RDM";
break;
case SOCK_SEQPACKET:
return "SOCK_SEQPACKET";
break;
}
/* NOTREACHED */
return "unknown socket type";
}
int
procfs_proc_pidinfo(pid_t pid, char *buf, int *len)
{
int ret;
int orig_len = *len;
int used_len = 0;
ret = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, 0, 0);
if (ret == -1) {
return -1;
}
size_t buffersize = (size_t)ret;
char *buffer = (char *)malloc((size_t)buffersize);
if (!buffer) {
return -1;
}
ret = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, buffer,
(size_t)buffersize);
struct proc_fdinfo *infop;
int nfd = ret / sizeof(struct proc_fdinfo);
infop = (struct proc_fdinfo *)buffer;
int i;
for (i = 0; i < nfd; i++) {
if (used_len >= orig_len) {
goto out;
}
used_len += snprintf(buf + used_len, orig_len - used_len,
"%-4d %-10s ", infop[i].proc_fd,
fd_type_string(infop[i].proc_fdtype));
switch (infop[i].proc_fdtype) {
case PROX_FDTYPE_ATALK:
case PROX_FDTYPE_FSEVENTS:
case PROX_FDTYPE_KQUEUE:
case PROX_FDTYPE_PIPE:
if (used_len >= orig_len) {
goto out;
}
used_len += snprintf(buf + used_len, orig_len - used_len, "\n");
break;
case PROX_FDTYPE_PSEM:
{
int nb;
struct psem_fdinfo ps;
if (used_len >= orig_len) {
goto out;
}
nb = proc_pidinfo(pid, infop[i].proc_fd, PROC_PIDFDPSEMINFO,
(char *)&ps, sizeof(ps));
if (nb <= 0) {
used_len += snprintf(buf + used_len, orig_len - used_len, "\n");
} else if ((unsigned int)nb < sizeof(ps)) {
used_len += snprintf(buf + used_len, orig_len - used_len,
"(%s)\n", "incomplete info");
} else {
if (ps.pseminfo.psem_name[0]) {
used_len += snprintf(buf + used_len, orig_len - used_len,
"%s\n", ps.pseminfo.psem_name);
} else {
used_len += snprintf(buf + used_len, orig_len - used_len,
"\n");
}
}
break;
}
case PROX_FDTYPE_PSHM:
{
int nb;
struct pshm_fdinfo ps;
if (used_len >= orig_len) {
goto out;
}
nb = proc_pidinfo(pid, infop[i].proc_fd, PROC_PIDFDPSHMINFO,
(char *)&ps, sizeof(ps));
if (nb <= 0) {
used_len += snprintf(buf + used_len, orig_len - used_len, "\n");
} else if ((unsigned int)nb < sizeof(ps)) {
used_len += snprintf(buf + used_len, orig_len - used_len,
"(%s)\n", "incomplete info");
} else {
if (ps.pshminfo.pshm_name[0]) {
ps.pshminfo.pshm_name[sizeof(ps.pshminfo.pshm_name) - 1] = '\0';
used_len += snprintf(buf + used_len, orig_len - used_len,
"%s\n", ps.pshminfo.pshm_name);
} else if (ps.pshminfo.pshm_mappaddr) {
/* ick */
#define CAST_DOWN(type, addr) (((type)((uintptr_t)(addr))))
used_len += snprintf(buf + used_len, orig_len - used_len,
"obj=%p\n",
CAST_DOWN(void *, ps.pshminfo.pshm_mappaddr));
} else {
used_len += snprintf(buf + used_len, orig_len - used_len,
"\n");
}
}
break;
}
case PROX_FDTYPE_SOCKET:
{
int nb;
struct socket_fdinfo si;
if (used_len >= orig_len) {
goto out;
}
nb = proc_pidfdinfo(pid, infop[i].proc_fd, PROC_PIDFDSOCKETINFO,
(char *)&si, sizeof(si));
if (nb <= 0) {
used_len += snprintf(buf + used_len, orig_len - used_len, "\n");
} else if ((unsigned int)nb < sizeof(si)) {
used_len += snprintf(buf + used_len, orig_len - used_len,
"(%s)\n", "incomplete info");
} else {
struct protoent *proto = getprotobynumber(si.psi.soi_protocol);
unsigned char *la = NULL, *fa = NULL;
int lp = 0, fp = 0;
if ((si.psi.soi_family == AF_INET) &&
(si.psi.soi_kind == SOCKINFO_TCP)) {
la = (unsigned char *)&si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_laddr.ina_46.i46a_addr4;
lp = (int)ntohs(si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_lport);
fa = (unsigned char *)&si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_faddr.ina_46.i46a_addr4;
fp = (int)ntohs(si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_fport);
char lh[MAXHOSTNAMELEN + 1] = { 0 };
char fh[MAXHOSTNAMELEN + 1] = { 0 };
char *h;
h = inet_ntoa(*(struct in_addr *)la);
if (h) {
snprintf(lh, MAXHOSTNAMELEN, "%s", h);
}
h = inet_ntoa(*(struct in_addr *)fa);
if (h) {
snprintf(fh, MAXHOSTNAMELEN, "%s", h);
}
used_len += snprintf(buf + used_len, orig_len - used_len,
"%s, %s, %s, %s:%d --> %s:%d\n",
socket_type_string(si.psi.soi_type),
(proto) ? proto->p_name : "unknown protocol",
socket_family_string(si.psi.soi_family),
(lh[0]) ? lh : "unknown host", ntohs(lp),
(fh[0]) ? fh : "unknown host", ntohs(fp));
} else {
used_len += snprintf(buf + used_len, orig_len - used_len,
"%s, %s, %s\n",
socket_type_string(si.psi.soi_type),
(proto) ? proto->p_name : "unknown protocol",
socket_family_string(si.psi.soi_family));
}
}
break;
}
case PROX_FDTYPE_VNODE:
{
int nb;
struct vnode_fdinfowithpath vi;
nb = proc_pidfdinfo(pid, infop[i].proc_fd, PROC_PIDFDVNODEPATHINFO,
(char *)&vi, sizeof(vi));
if (used_len >= orig_len) {
goto out;
}
if (nb <= 0) {
if (errno == ENOENT) {
used_len += snprintf(buf + used_len, orig_len - used_len,
"(%s)\n", "revoked");
} else {
used_len += snprintf(buf + used_len, orig_len - used_len,
"\n");
}
} else if ((unsigned int)nb < sizeof(vi)) {
used_len += snprintf(buf + used_len, orig_len - used_len,
"(%s)\n", "incomplete info");
} else {
used_len += snprintf(buf + used_len, orig_len - used_len,
"%s\n", vi.pvip.vip_path);
}
break;
}
default:
used_len += snprintf(buf + used_len, orig_len - used_len,
"(%s)\n", "unknown descriptor type");
break;
}
}
out:
if (buffer) {
free(buffer);
}
*len = used_len;
return 0;
}
#endif /* __i386__ */
}