문제

I have a linked list of mailboxes and I'm trying to copy their ids to the userspace variable mbxList but it is not printing out correctly.

asmlinkage long sys_listMailboxes(unsigned long * mbxList,
                    unsigned long K)
{
  int counter = 0;
  MBOX * currentBox;
  unsigned long * toUser;

  list_for_each_entry(currentBox, &mailbox_list, list)
    {
      if(counter != K)
    {
      printk("The id is: %lu\n", currentBox->id);
      toUser = &currentBox->id;
      copy_to_user(mbxList, toUser, sizeof toUser);
      mbxList++;
      counter++;
    }
    }
  return counter;
}

When I check prints in the kernel using dmesg I see the correct output: The id is: 1111 but in the user space when I try to print it out I get the output: The id is: 1474660693 which is incorrect.

The C code snippet implementing this system call is the following:

#include <stdio.h>
#include <unistd.h>
#include <syscall.h>

long listMbox(unsigned long * mbxList, unsigned long K)
{
  return syscall(__NR_listMailboxes, mbxList, K);
}

int main(void)
{
   unsigned long * mbxlist;
   unsigned long K = 2;

   listMailboxes(mbxlist, K);       

   int i;
   for(i = 0; i < K; i++)
   {
      printf("Mailbox id is: %lu\n", *mbxList);
      mbxList++;
   }
}

I get the same 1474660693 number everytime so I don't think it is a memory address. I thought I was giving the size argument in copy_to_user too big but that doesnt seem to be the case since. I have no idea what is wrong, I would appreciate any help. Thank you!

도움이 되었습니까?

해결책

You seem to be using sizeof toUser in the kernel part, but actually copying data that is pointed to by toUser.

So that is wrong. You are copying from an int the size of a pointer bytes.

I suggest using sizeof *toUser although you might need parenthesis like sizeof(*toUser)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top