xref: /illumos-gate/usr/src/cmd/ipf/lib/kmem.c (revision f3ac678143127d4c6c1793fadabb5ded04e127b6)
1*f3ac6781SToomas Soome /*
2*f3ac6781SToomas Soome  * Copyright (C) 1993-2001 by Darren Reed.
3*f3ac6781SToomas Soome  *
4*f3ac6781SToomas Soome  * See the IPFILTER.LICENCE file for details on licencing.
5*f3ac6781SToomas Soome  */
6*f3ac6781SToomas Soome /*
7*f3ac6781SToomas Soome  * kmemcpy() - copies n bytes from kernel memory into user buffer.
8*f3ac6781SToomas Soome  * returns 0 on success, -1 on error.
9*f3ac6781SToomas Soome  */
10*f3ac6781SToomas Soome 
11*f3ac6781SToomas Soome /*
12*f3ac6781SToomas Soome  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
13*f3ac6781SToomas Soome  * Use is subject to license terms.
14*f3ac6781SToomas Soome  */
15*f3ac6781SToomas Soome 
16*f3ac6781SToomas Soome #include <stdio.h>
17*f3ac6781SToomas Soome #include <sys/param.h>
18*f3ac6781SToomas Soome #include <sys/types.h>
19*f3ac6781SToomas Soome #include <sys/uio.h>
20*f3ac6781SToomas Soome #include <unistd.h>
21*f3ac6781SToomas Soome #include <string.h>
22*f3ac6781SToomas Soome #include <fcntl.h>
23*f3ac6781SToomas Soome #include <sys/file.h>
24*f3ac6781SToomas Soome #if !defined(__sgi) && !defined(__hpux) && !defined(__osf__) && !defined(linux) && !defined(_AIX51)
25*f3ac6781SToomas Soome #include <kvm.h>
26*f3ac6781SToomas Soome #endif
27*f3ac6781SToomas Soome #include <fcntl.h>
28*f3ac6781SToomas Soome #include <sys/socket.h>
29*f3ac6781SToomas Soome #include <sys/ioctl.h>
30*f3ac6781SToomas Soome #include <netinet/in.h>
31*f3ac6781SToomas Soome #include <arpa/inet.h>
32*f3ac6781SToomas Soome #include <netinet/in_systm.h>
33*f3ac6781SToomas Soome #include <netinet/ip.h>
34*f3ac6781SToomas Soome #include <net/if.h>
35*f3ac6781SToomas Soome #if __FreeBSD_version >= 300000
36*f3ac6781SToomas Soome # include <net/if_var.h>
37*f3ac6781SToomas Soome #endif
38*f3ac6781SToomas Soome #if defined(linux) || defined(__osf__) || defined(__sgi) || defined(__hpux)
39*f3ac6781SToomas Soome # include <stdlib.h>
40*f3ac6781SToomas Soome #endif
41*f3ac6781SToomas Soome 
42*f3ac6781SToomas Soome #include "kmem.h"
43*f3ac6781SToomas Soome 
44*f3ac6781SToomas Soome #ifndef __STDC__
45*f3ac6781SToomas Soome # define	const
46*f3ac6781SToomas Soome #endif
47*f3ac6781SToomas Soome 
48*f3ac6781SToomas Soome #if !defined(lint)
49*f3ac6781SToomas Soome static const char sccsid[] = "@(#)kmem.c	1.4 1/12/96 (C) 1992 Darren Reed";
50*f3ac6781SToomas Soome static const char rcsid[] = "@(#)$Id: kmem.c,v 1.16.2.2 2005/06/12 07:18:41 darrenr Exp $";
51*f3ac6781SToomas Soome #endif
52*f3ac6781SToomas Soome 
53*f3ac6781SToomas Soome 
54*f3ac6781SToomas Soome 
55*f3ac6781SToomas Soome #if !defined(__sgi) && !defined(__hpux) && !defined(__osf__) && \
56*f3ac6781SToomas Soome     !defined(linux) && !defined(_AIX51)
57*f3ac6781SToomas Soome /*
58*f3ac6781SToomas Soome  * For all platforms where there is a libkvm and a kvm_t, we use that...
59*f3ac6781SToomas Soome  */
60*f3ac6781SToomas Soome static	kvm_t	*kvm_f = NULL;
61*f3ac6781SToomas Soome 
62*f3ac6781SToomas Soome #else
63*f3ac6781SToomas Soome /*
64*f3ac6781SToomas Soome  *...and for the others (HP-UX, IRIX, Tru64), we have to provide our own.
65*f3ac6781SToomas Soome  */
66*f3ac6781SToomas Soome 
67*f3ac6781SToomas Soome typedef	int *	kvm_t;
68*f3ac6781SToomas Soome 
69*f3ac6781SToomas Soome static	kvm_t	kvm_f = NULL;
70*f3ac6781SToomas Soome static	char	*kvm_errstr = NULL;
71*f3ac6781SToomas Soome 
72*f3ac6781SToomas Soome kvm_t kvm_open __P((char *, char *, char *, int, char *));
73*f3ac6781SToomas Soome int kvm_read __P((kvm_t, u_long, char *, size_t));
74*f3ac6781SToomas Soome 
kvm_open(kernel,core,swap,mode,errstr)75*f3ac6781SToomas Soome kvm_t kvm_open(kernel, core, swap, mode, errstr)
76*f3ac6781SToomas Soome char *kernel, *core, *swap;
77*f3ac6781SToomas Soome int mode;
78*f3ac6781SToomas Soome char *errstr;
79*f3ac6781SToomas Soome {
80*f3ac6781SToomas Soome 	kvm_t k;
81*f3ac6781SToomas Soome 	int fd;
82*f3ac6781SToomas Soome 
83*f3ac6781SToomas Soome 	kvm_errstr = errstr;
84*f3ac6781SToomas Soome 
85*f3ac6781SToomas Soome 	if (core == NULL)
86*f3ac6781SToomas Soome 		core = "/dev/kmem";
87*f3ac6781SToomas Soome 
88*f3ac6781SToomas Soome 	fd = open(core, mode);
89*f3ac6781SToomas Soome 	if (fd == -1)
90*f3ac6781SToomas Soome 		return NULL;
91*f3ac6781SToomas Soome 	k = malloc(sizeof(*k));
92*f3ac6781SToomas Soome 	if (k == NULL) {
93*f3ac6781SToomas Soome 		close(fd);
94*f3ac6781SToomas Soome 		return NULL;
95*f3ac6781SToomas Soome 	}
96*f3ac6781SToomas Soome 	*k = fd;
97*f3ac6781SToomas Soome 	return k;
98*f3ac6781SToomas Soome }
99*f3ac6781SToomas Soome 
kvm_read(kvm,pos,buffer,size)100*f3ac6781SToomas Soome int kvm_read(kvm, pos, buffer, size)
101*f3ac6781SToomas Soome kvm_t kvm;
102*f3ac6781SToomas Soome u_long pos;
103*f3ac6781SToomas Soome char *buffer;
104*f3ac6781SToomas Soome size_t size;
105*f3ac6781SToomas Soome {
106*f3ac6781SToomas Soome 	int r = 0, left;
107*f3ac6781SToomas Soome 	char *bufp;
108*f3ac6781SToomas Soome 
109*f3ac6781SToomas Soome 	if (lseek(*kvm, pos, 0) == -1) {
110*f3ac6781SToomas Soome 		if (kvm_errstr != NULL) {
111*f3ac6781SToomas Soome 			fprintf(stderr, "%s", kvm_errstr);
112*f3ac6781SToomas Soome 			perror("lseek");
113*f3ac6781SToomas Soome 		}
114*f3ac6781SToomas Soome 		return -1;
115*f3ac6781SToomas Soome 	}
116*f3ac6781SToomas Soome 
117*f3ac6781SToomas Soome 	for (bufp = buffer, left = size; left > 0; bufp += r, left -= r) {
118*f3ac6781SToomas Soome 		r = read(*kvm, bufp, left);
119*f3ac6781SToomas Soome #ifdef	__osf__
120*f3ac6781SToomas Soome 		/*
121*f3ac6781SToomas Soome 		 * Tru64 returns "0" for successful operation, not the number
122*f3ac6781SToomas Soome 		 * of bytes read.
123*f3ac6781SToomas Soome 		 */
124*f3ac6781SToomas Soome 		if (r == 0)
125*f3ac6781SToomas Soome 			r = left;
126*f3ac6781SToomas Soome #endif
127*f3ac6781SToomas Soome 		if (r <= 0)
128*f3ac6781SToomas Soome 			return -1;
129*f3ac6781SToomas Soome 	}
130*f3ac6781SToomas Soome 	return r;
131*f3ac6781SToomas Soome }
132*f3ac6781SToomas Soome #endif /* !defined(__sgi) && !defined(__hpux) && !defined(__osf__) */
133*f3ac6781SToomas Soome 
openkmem(kern,core)134*f3ac6781SToomas Soome int	openkmem(kern, core)
135*f3ac6781SToomas Soome char	*kern, *core;
136*f3ac6781SToomas Soome {
137*f3ac6781SToomas Soome 	kvm_f = kvm_open(kern, core, NULL, O_RDONLY, NULL);
138*f3ac6781SToomas Soome 	if (kvm_f == NULL)
139*f3ac6781SToomas Soome 	    {
140*f3ac6781SToomas Soome 		perror("openkmem:open");
141*f3ac6781SToomas Soome 		return -1;
142*f3ac6781SToomas Soome 	    }
143*f3ac6781SToomas Soome 	return kvm_f != NULL;
144*f3ac6781SToomas Soome }
145*f3ac6781SToomas Soome 
kmemcpy(buf,pos,n)146*f3ac6781SToomas Soome int	kmemcpy(buf, pos, n)
147*f3ac6781SToomas Soome register char	*buf;
148*f3ac6781SToomas Soome long	pos;
149*f3ac6781SToomas Soome register int	n;
150*f3ac6781SToomas Soome {
151*f3ac6781SToomas Soome 	register int	r;
152*f3ac6781SToomas Soome 
153*f3ac6781SToomas Soome 	if (!n)
154*f3ac6781SToomas Soome 		return 0;
155*f3ac6781SToomas Soome 
156*f3ac6781SToomas Soome 	if (kvm_f == NULL)
157*f3ac6781SToomas Soome 		if (openkmem(NULL, NULL) == -1)
158*f3ac6781SToomas Soome 			return -1;
159*f3ac6781SToomas Soome 
160*f3ac6781SToomas Soome 	while ((r = kvm_read(kvm_f, pos, buf, n)) < n)
161*f3ac6781SToomas Soome 		if (r <= 0)
162*f3ac6781SToomas Soome 		    {
163*f3ac6781SToomas Soome 			fprintf(stderr, "pos=0x%lx ", (u_long)pos);
164*f3ac6781SToomas Soome 			perror("kmemcpy:read");
165*f3ac6781SToomas Soome 			return -1;
166*f3ac6781SToomas Soome 		    }
167*f3ac6781SToomas Soome 		else
168*f3ac6781SToomas Soome 		    {
169*f3ac6781SToomas Soome 			buf += r;
170*f3ac6781SToomas Soome 			pos += r;
171*f3ac6781SToomas Soome 			n -= r;
172*f3ac6781SToomas Soome 		    }
173*f3ac6781SToomas Soome 	return 0;
174*f3ac6781SToomas Soome }
175*f3ac6781SToomas Soome 
kstrncpy(buf,pos,n)176*f3ac6781SToomas Soome int	kstrncpy(buf, pos, n)
177*f3ac6781SToomas Soome register char	*buf;
178*f3ac6781SToomas Soome long	pos;
179*f3ac6781SToomas Soome register int	n;
180*f3ac6781SToomas Soome {
181*f3ac6781SToomas Soome 	register int	r;
182*f3ac6781SToomas Soome 
183*f3ac6781SToomas Soome 	if (!n)
184*f3ac6781SToomas Soome 		return 0;
185*f3ac6781SToomas Soome 
186*f3ac6781SToomas Soome 	if (kvm_f == NULL)
187*f3ac6781SToomas Soome 		if (openkmem(NULL, NULL) == -1)
188*f3ac6781SToomas Soome 			return -1;
189*f3ac6781SToomas Soome 
190*f3ac6781SToomas Soome 	while (n > 0)
191*f3ac6781SToomas Soome 	    {
192*f3ac6781SToomas Soome 		r = kvm_read(kvm_f, pos, buf, 1);
193*f3ac6781SToomas Soome 		if (r <= 0)
194*f3ac6781SToomas Soome 		    {
195*f3ac6781SToomas Soome 			fprintf(stderr, "pos=0x%lx ", (u_long)pos);
196*f3ac6781SToomas Soome 			perror("kmemcpy:read");
197*f3ac6781SToomas Soome 			return -1;
198*f3ac6781SToomas Soome 		    }
199*f3ac6781SToomas Soome 		else
200*f3ac6781SToomas Soome 		    {
201*f3ac6781SToomas Soome 			if (*buf == '\0')
202*f3ac6781SToomas Soome 				break;
203*f3ac6781SToomas Soome 			buf++;
204*f3ac6781SToomas Soome 			pos++;
205*f3ac6781SToomas Soome 			n--;
206*f3ac6781SToomas Soome 		    }
207*f3ac6781SToomas Soome 	    }
208*f3ac6781SToomas Soome 	return 0;
209*f3ac6781SToomas Soome }
210