Reputation: 55
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 = ¤tBox->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!
Upvotes: 0
Views: 535
Reputation: 54363
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)
Upvotes: 1