I'm trying to make a custom system call.
my system call takes 2 parameters struct buffer **mybuffer
& int size
.
it's imposed any change that happens to **mybuffer
should reflect in the user-space, but it seems it doesn't work.
so I've seen somewhere else that i can use copy_to_user(void *dest, void *src, int size)
to copy data from kernel space to user space.
in user-space i have a struct called buffer, also this struct appears the same in the system call.
typedef struct buffer {
int n;
}buffer;
int main(void)
{
buffer **buf = malloc(sizeof(buffer *));
int i = 0
for(;i<8;i++)
buf[i] = malloc(sizeof(buffer));
long int sys = systemcall(801,buf,8)
//print out buf
return 0;
}
In system call i have
asmlinkage long sys_something(buffer **buf,int size)
{
//allocate buffer same as it appears in int main
//fill buf with some data
for(i = 0; i<size,i++)
copy_to_user(buf[i],buf[i],sizeof(buffer));
I'm pretty sure that i'm doing something wrong. how actually to copy data from kernel space to user space ?
P.s. I'm using linux kernel 3.16.0
dest
andsrc
equal tobuf[i]
is definitely wrong. What kernel's data you want to copy? Pointer to these data should be used as the second argument tocopy_to_user
. Note also, because the first syscall's parameter is actually an array of pointers, you need to read these pointers to the kernel's (temporary) memory usingcopy_from_user
, and then use pointers from that kernel's memory incopy_to_user
call. – Tsyvarev