4 * Copyright (C) 2000-2005 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
9 * kmemcpy() - copies n bytes from kernel memory into user buffer.
10 * returns 0 on success, -1 on error.
14 #include <sys/param.h>
15 #include <sys/types.h>
21 #if !defined(__sgi) && !defined(__hpux) && !defined(__osf__) && !defined(linux) && !defined(_AIX51)
25 #include <sys/socket.h>
26 #include <sys/ioctl.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <netinet/in_systm.h>
30 #include <netinet/ip.h>
32 #if __FreeBSD_version >= 300000
33 # include <net/if_var.h>
35 #if defined(linux) || defined(__osf__) || defined(__sgi) || defined(__hpux)
46 static const char sccsid
[] = "@(#)kmem.c 1.4 1/12/96 (C) 1992 Darren Reed";
47 static const char rcsid
[] = "@(#)Id: kmem.c,v 1.16.2.3 2006/06/16 17:21:04 darrenr Exp";
52 #if !defined(__sgi) && !defined(__hpux) && !defined(__osf__) && \
53 !defined(linux) && !defined(_AIX51)
55 * For all platforms where there is a libkvm and a kvm_t, we use that...
57 static kvm_t
*kvm_f
= NULL
;
61 *...and for the others (HP-UX, IRIX, Tru64), we have to provide our own.
66 static kvm_t kvm_f
= NULL
;
67 static char *kvm_errstr
= NULL
;
69 kvm_t kvm_open
__P((char *, char *, char *, int, char *));
70 int kvm_read
__P((kvm_t
, u_long
, char *, size_t));
72 kvm_t
kvm_open(kernel
, core
, swap
, mode
, errstr
)
73 char *kernel
, *core
, *swap
;
85 fd
= open(core
, mode
);
88 k
= malloc(sizeof(*k
));
95 int kvm_read(kvm
, pos
, buffer
, size
)
104 if (lseek(*kvm
, pos
, 0) == -1) {
105 if (kvm_errstr
!= NULL
) {
106 fprintf(stderr
, "%s", kvm_errstr
);
112 for (bufp
= buffer
, left
= size
; left
> 0; bufp
+= r
, left
-= r
) {
113 r
= read(*kvm
, bufp
, left
);
116 * Tru64 returns "0" for successful operation, not the number
127 #endif /* !defined(__sgi) && !defined(__hpux) && !defined(__osf__) */
129 int openkmem(kern
, core
)
132 kvm_f
= kvm_open(kern
, core
, NULL
, O_RDONLY
, NULL
);
135 perror("openkmem:open");
138 return kvm_f
!= NULL
;
141 int kmemcpy(buf
, pos
, n
)
152 if (openkmem(NULL
, NULL
) == -1)
155 while ((r
= kvm_read(kvm_f
, pos
, buf
, n
)) < n
)
158 fprintf(stderr
, "pos=0x%lx ", (u_long
)pos
);
159 perror("kmemcpy:read");
171 int kstrncpy(buf
, pos
, n
)
182 if (openkmem(NULL
, NULL
) == -1)
187 r
= kvm_read(kvm_f
, pos
, buf
, 1);
190 fprintf(stderr
, "pos=0x%lx ", (u_long
)pos
);
191 perror("kmemcpy:read");