1997-02-09 22:50:16 +00:00
|
|
|
/*
|
2001-07-28 11:59:33 +00:00
|
|
|
* Copyright (C) 1993-2001 by Darren Reed.
|
1997-02-09 22:50:16 +00:00
|
|
|
*
|
2001-07-28 11:59:33 +00:00
|
|
|
* See the IPFILTER.LICENCE file for details on licencing.
|
1997-02-09 22:50:16 +00:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* kmemcpy() - copies n bytes from kernel memory into user buffer.
|
|
|
|
* returns 0 on success, -1 on error.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/file.h>
|
1997-04-03 10:22:02 +00:00
|
|
|
#include "kmem.h"
|
1997-02-09 22:50:16 +00:00
|
|
|
|
2001-07-28 11:59:33 +00:00
|
|
|
#ifndef __STDC__
|
|
|
|
# define const
|
|
|
|
#endif
|
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
#if !defined(lint)
|
|
|
|
static const char sccsid[] = "@(#)kmem.c 1.4 1/12/96 (C) 1992 Darren Reed";
|
2001-07-28 11:59:33 +00:00
|
|
|
static const char rcsid[] = "@(#)$Id: kmem.c,v 2.2.2.3 2001/07/15 22:06:16 darrenr Exp $";
|
1997-02-09 22:50:16 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static int kmemfd = -1;
|
|
|
|
|
2001-07-28 11:59:33 +00:00
|
|
|
int openkmem(core)
|
|
|
|
char *core;
|
1997-02-09 22:50:16 +00:00
|
|
|
{
|
2001-07-28 11:59:33 +00:00
|
|
|
if (core == NULL)
|
|
|
|
core = KMEM;
|
|
|
|
|
|
|
|
if ((kmemfd = open(core, O_RDONLY)) == -1)
|
1997-02-09 22:50:16 +00:00
|
|
|
{
|
|
|
|
perror("kmeminit:open");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return kmemfd;
|
|
|
|
}
|
|
|
|
|
|
|
|
int kmemcpy(buf, pos, n)
|
|
|
|
register char *buf;
|
|
|
|
long pos;
|
|
|
|
register int n;
|
|
|
|
{
|
|
|
|
register int r;
|
|
|
|
|
|
|
|
if (!n)
|
|
|
|
return 0;
|
|
|
|
if (kmemfd == -1)
|
2001-07-28 11:59:33 +00:00
|
|
|
if (openkmem(NULL) == -1)
|
1997-02-09 22:50:16 +00:00
|
|
|
return -1;
|
|
|
|
if (lseek(kmemfd, pos, 0) == -1)
|
|
|
|
{
|
|
|
|
perror("kmemcpy:lseek");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
while ((r = read(kmemfd, buf, n)) < n)
|
|
|
|
if (r <= 0)
|
|
|
|
{
|
|
|
|
perror("kmemcpy:read");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf += r;
|
|
|
|
n -= r;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
1999-11-08 20:51:23 +00:00
|
|
|
|
|
|
|
int kstrncpy(buf, pos, n)
|
|
|
|
register char *buf;
|
|
|
|
long pos;
|
|
|
|
register int n;
|
|
|
|
{
|
|
|
|
register int r;
|
|
|
|
|
|
|
|
if (!n)
|
|
|
|
return 0;
|
|
|
|
if (kmemfd == -1)
|
2001-07-28 11:59:33 +00:00
|
|
|
if (openkmem(NULL) == -1)
|
1999-11-08 20:51:23 +00:00
|
|
|
return -1;
|
|
|
|
if (lseek(kmemfd, pos, 0) == -1)
|
|
|
|
{
|
|
|
|
perror("kmemcpy:lseek");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
while (n > 0) {
|
|
|
|
r = read(kmemfd, buf, 1);
|
|
|
|
if (r <= 0)
|
|
|
|
{
|
|
|
|
perror("kmemcpy:read");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (*buf == '\0')
|
|
|
|
break;
|
|
|
|
buf++;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|