pkgsrc/sysutils/lsof/patches/patch-ah
jym 1373123a11 When we moved from fd_ofiles to a struct fdtab, the fd_ofiles became
an array of pointer (in struct fdtab) rather than a pointer of pointers.

Sadly for us, no, arrays and pointers are not equivalent from a memory
perspective: while pointers from/to another address space can
be consumed by kvm(3) to query for data in kernel space, arrays
are more tricky, especially when their content is copied in userland:
they are part of the copied struct.

Address of array members are only valid in their own address space,
in our case userland, which is (fortunately?) different from kernel space.
This breaks the various kvm_read() calls that query for file descriptor
information. Consequence: lsof(1) cannot print filedescriptor information
(starting from 5.99.14), and silently ignores the errors, as using
the userland fdtab (``dt'' variable) is not valid for kernel.

Fix that by using the ``fd_dt'' member of struct filedes, which stores
the address of the fdtab struct in kernel address space. Took a few hours
to understand what was going on with lsof(1), hmmm.

Luckily, fstat(1) uses the proper model (checked about 5min ago). Why
lsof(1) decided not to log an error on kvm_read() is... a good question.

Bump rev.
2011-03-21 00:22:02 +00:00

135 lines
3.8 KiB
Text

$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)