LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-20-2006, 08:37 AM   #1
sailershen
LQ Newbie
 
Registered: Mar 2005
Posts: 27

Rep: Reputation: 15
Read /dev/kmem failed


Code:
struct descriptor_idt {
        unsigned short offset_low, seg_selector;
        unsigned char reserved, flag;
        unsigned short offset_high;
};

       .......

        struct descriptor_idt *descriptor;
       .......

        fd_kmem = open("/dev/kmem", O_RDWR);
        ptr_idt = get_addr_idt();
        descriptor = (struct descriptor_idt *) malloc(sizeof(struct descriptor_idt));
        ......
        readkmem(descriptor, ptr_idt + 8 * x, sizeof(struct descriptor_idt));

        ......

void readkmem(void *m, unsigned off, int size)
{
        int i;
        if (lseek(fd_kmem, off, SEEK_SET) != off) {
                fprintf(stderr, "Error lseek. Are you root? \n");
                exit(-1);
        }
        if (read(fd_kmem, m, size) != size) {
                fprintf(stderr, "Error read kmem\n");
                exit(-1);
        }
}

unsigned long get_addr_idt(void)
{
        unsigned char idtr[6];
        unsigned long idt;
        __asm__ volatile ("sidt %0":"=m" (idtr));
        idt = *((unsigned long *) &idtr[2]);
        return (idt);
}
When it's run, it also said "Error read kmem", and I found that the "read(fd_kmem, m, size)" always return 0, I don't know why?

Thanks for help.
 
Old 03-20-2006, 08:46 AM   #2
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
add
#include <errno.h>
and replace your printf by perror("read");

and see what it gives. perror is very uselfull.

"permission denied" ?
 
Old 03-20-2006, 07:48 PM   #3
sailershen
LQ Newbie
 
Registered: Mar 2005
Posts: 27

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by nx5000
add
#include <errno.h>
and replace your printf by perror("read");

and see what it gives. perror is very uselfull.

"permission denied" ?
Thanks for help.

I change it:
Code:
#include <errno.h>

        ......

        if ((i = read(fd_kmem, m, size)) != size) {
                fprintf(stderr, "Error read kmem\n");
                perror("read");
                exit(-1);
        }
When it run in SLES9 and RHEL3, it said:
Code:
Error read kmem
read: Success
 
Old 03-21-2006, 02:49 AM   #4
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
Well, your test doesn't really show an error.

I would change

fprintf(stderr, "Error read kmem\n");

->

fprintf(stderr, "Error read kmem, only read %d bytes\n",i);

because read:Success means what it means
 
Old 03-21-2006, 04:21 AM   #5
sailershen
LQ Newbie
 
Registered: Mar 2005
Posts: 27

Original Poster
Rep: Reputation: 15
I change the code:
Code:
void readkmem(void *m, unsigned off, int size)
{
        int i;
        if (lseek(fd_kmem, off, SEEK_SET) != off) {
                fprintf(stderr, "Error lseek. Are you root? \n");
                exit(-1);
        }
        if ((i = read(fd_kmem, m, size)) != size) {
                fprintf(stderr, "Error read kmem, only read %d bytes\n",i);
                perror("read");
                exit(-1);
        }
}
When run, it said:
Code:
Error read kmem, only read 0 bytes
read: Success
 
Old 03-21-2006, 05:33 AM   #6
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
Look at the kernel source
Maybe you will have more answers on other forums...
like kerneltrap.org
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mmap() of /dev/kmem - can it work? wwc Programming 1 02-04-2006 01:47 PM
Missing file /dev/kmem in Fedora 3 deardron Fedora 0 04-16-2005 12:12 PM
open() failed on /dev/tty ekko37 Linux - Software 7 01-15-2005 02:47 AM
unpacking of archive failed: cpio: read failed-input/output error rafc Linux - Newbie 0 04-21-2004 09:03 AM
growisofs -Z /dev/dvd=/dev/zero failed penguin123 Linux - Hardware 1 02-19-2004 03:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration