Find the answer to your Linux question:
Results 1 to 4 of 4
How to open a file in a linux module? sys_open()? a simple test: Code: extern long sys_open(const char *filename,int flags,int mode); extern int sys_close(unsigned int fd); int fd; int init_module(void) ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2003
    Location
    china
    Posts
    8

    a problem about linux module


    How to open a file in a linux module?
    sys_open()?

    a simple test:
    Code:
    extern long sys_open(const char *filename,int flags,int mode);
    extern int sys_close(unsigned int fd);
    int fd;
    int init_module(void)
    {
      fd=sys_open("/dev/kmem",2,0);
      printk&#40;"<1>fd is&#58;%d\n",fd&#41;;
      return 0;
    &#125;
    void cleanup_module&#40;void&#41;
    &#123;
      if&#40;fd>0&#41;
           sys_close&#40;fd&#41;;
      printk&#40;"<1>Removed OK!\n"&#41;;
    &#125;
    insmod test.o
    But the output is:
    fd is:-14
    that is to say,the module open file error,and the errno is 14(EFAULT)
    (bad address),that is why?
    Could I use the system call in a module?

  2. #2
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    God! How can you even consider using a syscall in a module?! Those are only user-space interfaces, and they also operate on user-space addresses, which is why you get EFAULT; you are passing an address that is valid in the kernel's addressing space, but that probably isn't even mapped in the user-space process' space.
    The kernel doesn't use file descriptors!

    Why do you want to open /dev/kmem for? Just access the memory directly instead.
    You shouldn't even be opening files at all in the kernel. Why do you want to do that?

    Forgive me my harsh language, I'm just a bit shocked.

  3. #3
    Just Joined!
    Join Date
    Jul 2003
    Location
    china
    Posts
    8
    You are right !
    It is should access the kernel memory directly in the module
    Thank you very much for helping me!!

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Nov 2003
    Posts
    7
    It is something tricky to access RAM by physical addresses, not just kernel virtual memory. Implementation of /dev/mem and /dev/kmem fail to do it (the latter even fails to access highmem zone).

    If you want access to any memory (not just mapped in kernelspace) you can try something like that:
    struct page *p = mem_map+((phys_addr)>>PAGE_SHIFT);
    char * vaddr = kmap(p);
    ...
    kunmap (p);

    It has an implication that your computer has only one pgdat, but it is the case for many platforms (at least IA32).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •