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
|
$NetBSD: patch-ah,v 1.5 2011/03/21 00:22:02 jym Exp $
--- dialects/n+obsd/dproc.c.orig 2005-05-11 14:54:00.000000000 +0200
+++ dialects/n+obsd/dproc.c
@@ -37,6 +37,33 @@ static char *rcsid = "$Id: dproc.c,v 1.1
#include "lsof.h"
+#if __NetBSD_Version__ >= 499006200
+ /*
+ * In NetBSD-4.99.62, struct fdfile was added, struct filedesc::fd_ofiles
+ * changed type from struct file ** to struct fdfile **, and
+ * fd_ofileflags disappeared from struct filedesc, being
+ * replaced by fields in struct fdfile.
+ */
+# define HAVE_STRUCT_FDFILE 1
+# define FILESTRUCT struct fdfile
+#else
+# undef HAVE_STRUCT_FDFILE
+# define FILESTRUCT struct file
+#endif
+#if __NetBSD_Version__ >= 599001400
+ /*
+ * Between NetBSD-5.99.13 and 5.99.14, struct fdtab was added, and
+ * struct filedesc::fd_ofiles and fd_nfiles were replaced by
+ * struct filedesc::fd_dt (a pointer to struct fdtab).
+ */
+# define HAVE_STRUCT_FDTAB 1
+# define NFILES(fd,dt) ((dt).dt_nfiles)
+# define OFILES(fd,dt) ((fd).fd_dt->dt_ff)
+#else
+# undef HAVE_STRUCT_FDTAB
+# define NFILES(fd,dt) ((fd).fd_nfiles)
+# define OFILES(fd,dt) ((fd).fd_ofiles)
+#endif
_PROTOTYPE(static void enter_vn_text,(KA_T va, int *n));
_PROTOTYPE(static void get_kernel_access,(void));
@@ -152,7 +179,7 @@ gather_proc_info()
struct filedesc fd;
int i, nf;
MALLOC_S nb;
- static struct file **ofb = NULL;
+ static FILESTRUCT **ofb = NULL;
static int ofbb = 0;
short pss, sf;
int px;
@@ -179,6 +206,10 @@ gather_proc_info()
struct kinfo_proc *p;
#endif /* defined(HASKVMGETPROC2) */
+#if HAVE_STRUCT_FDTAB
+ struct fdtab dt;
+#endif /* HAVE_STRUCT_FDTAB */
+
/*
* Read the process table.
*/
@@ -218,7 +249,14 @@ gather_proc_info()
if (!p->P_FD
|| kread((KA_T)p->P_FD, (char *)&fd, sizeof(fd)))
continue;
- if (!fd.fd_refcnt || fd.fd_lastfile > fd.fd_nfiles)
+ if (!fd.fd_refcnt)
+ continue;
+#if HAVE_STRUCT_FDTAB
+ if (!fd.fd_dt
+ || kread((KA_T)fd.fd_dt, (char *)&dt, sizeof(dt)))
+ continue;
+#endif /* ! HAVE_STRUCT_FDTAB */
+ if (fd.fd_lastfile > NFILES(fd,dt))
continue;
#if defined(HASCWDINFO)
@@ -278,14 +316,14 @@ gather_proc_info()
/*
* Read open file structure pointers.
*/
- if (!fd.fd_ofiles || (nf = fd.fd_nfiles) <= 0)
+ if (!OFILES(fd,dt) || (nf = NFILES(fd,dt)) <= 0)
continue;
- nb = (MALLOC_S)(sizeof(struct file *) * nf);
+ nb = (MALLOC_S)(sizeof(FILESTRUCT *) * nf);
if (nb > ofbb) {
if (!ofb)
- ofb = (struct file **)malloc(nb);
+ ofb = (FILESTRUCT **)malloc(nb);
else
- ofb = (struct file **)realloc((MALLOC_P *)ofb, nb);
+ ofb = (FILESTRUCT **)realloc((MALLOC_P *)ofb, nb);
if (!ofb) {
(void) fprintf(stderr, "%s: PID %d, no file * space\n",
Pn, p->P_PID);
@@ -293,7 +331,7 @@ gather_proc_info()
}
ofbb = nb;
}
- if (kread((KA_T)fd.fd_ofiles, (char *)ofb, nb))
+ if (kread((KA_T)OFILES(fd,dt), (char *)ofb, nb))
continue;
#if defined(HASFSTRUCT)
@@ -311,8 +349,10 @@ gather_proc_info()
}
pofb = nb;
}
+#if ! HAVE_STRUCT_FDFILE
if (!fd.fd_ofileflags || kread((KA_T)fd.fd_ofileflags, pof, nb))
zeromem(pof, nb);
+#endif /* ! HAVE_STRUCT_FDFILE */
}
#endif /* defined(HASFSTRUCT) */
@@ -321,8 +361,20 @@ gather_proc_info()
*/
for (i = 0; i < nf; i++) {
if (ofb[i]) {
+#if HAVE_STRUCT_FDFILE
+ struct fdfile fdf;
+ if (kread((KA_T)ofb[i], (char *)&fdf, sizeof(fdf)))
+ continue;
+ Cfp = fdf.ff_file;
+ if (Cfp == NULL)
+ continue;
+ if (pof)
+ pof[i] = fdf.ff_exclose;
+#else /* ! HAVE_STRUCT_FDFILE */
+ Cfp = ofb[i];
+#endif /* ! HAVE_STRUCT_FDFILE */
alloc_lfile(NULL, i);
- process_file((KA_T)(Cfp = ofb[i]));
+ process_file((KA_T)Cfp);
if (Lf->sf) {
#if defined(HASFSTRUCT)
|