2 * Copyright (C) 1993-2001 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
7 * kmemcpy() - copies n bytes from kernel memory into user buffer.
8 * returns 0 on success, -1 on error.
12 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
13 * Use is subject to license terms.
16 #pragma ident "%Z%%M% %I% %E% SMI"
19 #include <sys/param.h>
20 #include <sys/types.h>
26 #if !defined(__sgi) && !defined(__hpux) && !defined(__osf__) && !defined(linux) && !defined(_AIX51)
30 #include <sys/socket.h>
31 #include <sys/ioctl.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <netinet/in_systm.h>
35 #include <netinet/ip.h>
37 #if __FreeBSD_version >= 300000
38 # include <net/if_var.h>
40 #if defined(linux) || defined(__osf__) || defined(__sgi) || defined(__hpux)
50 static const char sccsid
[] = "@(#)kmem.c 1.4 1/12/96 (C) 1992 Darren Reed";
51 static const char rcsid
[] = "@(#)$Id: kmem.c,v 1.16.2.2 2005/06/12 07:18:41 darrenr Exp $";
55 #if !defined(__sgi) && !defined(__hpux) && !defined(__osf__) && \
56 !defined(linux) && !defined(_AIX51)
58 * For all platforms where there is a libkvm and a kvm_t, we use that...
60 static kvm_t
*kvm_f
= NULL
;
64 *...and for the others (HP-UX, IRIX, Tru64), we have to provide our own.
69 static kvm_t kvm_f
= NULL
;
70 static char *kvm_errstr
= NULL
;
72 kvm_t kvm_open
__P((char *, char *, char *, int, char *));
73 int kvm_read
__P((kvm_t
, u_long
, char *, size_t));
75 kvm_t
kvm_open(kernel
, core
, swap
, mode
, errstr
)
76 char *kernel
, *core
, *swap
;
88 fd
= open(core
, mode
);
91 k
= malloc(sizeof(*k
));
100 int kvm_read(kvm
, pos
, buffer
, size
)
109 if (lseek(*kvm
, pos
, 0) == -1) {
110 if (kvm_errstr
!= NULL
) {
111 fprintf(stderr
, "%s", kvm_errstr
);
117 for (bufp
= buffer
, left
= size
; left
> 0; bufp
+= r
, left
-= r
) {
118 r
= read(*kvm
, bufp
, left
);
121 * Tru64 returns "0" for successful operation, not the number
132 #endif /* !defined(__sgi) && !defined(__hpux) && !defined(__osf__) */
134 int openkmem(kern
, core
)
137 kvm_f
= kvm_open(kern
, core
, NULL
, O_RDONLY
, NULL
);
140 perror("openkmem:open");
143 return kvm_f
!= NULL
;
146 int kmemcpy(buf
, pos
, n
)
157 if (openkmem(NULL
, NULL
) == -1)
160 while ((r
= kvm_read(kvm_f
, pos
, buf
, n
)) < n
)
163 fprintf(stderr
, "pos=0x%lx ", (u_long
)pos
);
164 perror("kmemcpy:read");
176 int kstrncpy(buf
, pos
, n
)
187 if (openkmem(NULL
, NULL
) == -1)
192 r
= kvm_read(kvm_f
, pos
, buf
, 1);
195 fprintf(stderr
, "pos=0x%lx ", (u_long
)pos
);
196 perror("kmemcpy:read");