I have a structure I am declaring/defining in the linux kernel (3.2), and I am currently trying to allocate one of these structures inside a syscall, and return a pointer to it for the process calling the syscall.
How can I
#include
this file in a program outside the kernel (the question might be which file should I include)? Currently, I am declaring the structure ininclude/linux/syscalls.h
and defining it in a file I created myself inkernel/mysystemcall.c
. If I try and use the structure in a program, I geterror: dereferencing pointer to incomplete type
.How can I actually read from this memory, given that if I dereference it, I get a segmentation fault? Currently, I am using
kmalloc
to allocate the memory; is there a flag I need to turn on to access the memory, or should I be using something else to allocate this memory?
Thanks for any help provided!
Current syscall implementation:
#include <linux/linkage.h>
#include <linux/sched.h>
#include <linux/slab.h>
struct threadinfo_struct {
int pid;
int nthreads;
int *tid;
};
asmlinkage struct threadinfo_struct *sys_threadinfo(void) {
struct threadinfo_struct *info = kmalloc(sizeof(struct threadinfo_struct), GFP_KERNEL);
info->pid = current->pid;
info->nthreads = -1;
info->tid = NULL;
return info;
}
Current test code (outsider kernel):
#include <stdio.h>
#include <linux/unistd.h>
#include <sys/syscall.h>
#define sys_threadinfo 349
int main(void) {
int *ti = (int*) syscall(sys_threadinfo);
printf("Thread id: %d\n", *ti); // Causes a segfault
return 0;
}
EDIT: I realize I can have my syscall take a pointer to already allocated memory, and just to fill in the values for the user, but it is preferred (teacher preference) to do it this way for the assignment.