int getkmemdata(void *buf, int bufsize, caddr_t address)
{
int n;
/*
* Do stuff we only need to do once per invocation, like opening
* the kmem file and fetching the parts of the symbol table.
*/
if (kmemfd < 0)
{
if ((kmemfd = open("/dev/kmem", O_RDONLY)) < 0)
{
//perror("kmem");
return -3;
}
/*
* We only need to be root for getting access to kmem, so give up
* root permissions now!
*/
setuid(getuid());
setgid(getgid());
/*
* Set the close on exec bit for the kmem file descriptor.
*/
if (fcntl(kmemfd, F_SETFD, FD_CLOEXEC) == -1)
{
//perror("fcntl kmem");
close(kmemfd);
return -1;
}
}
/*
* Get the structure from the running kernel.
*/
lseek(kmemfd, (off_t) address, SEEK_SET);
n = read(kmemfd, buf, bufsize);