views:

184

answers:

3

Hello,

So let's I have a struct that I want to read from user-level space that is defined in the kernel-space, but the user-level space has multiple processes.

Example:

In a kernel module, I have a global struct. struct { int a; int b; } test;

In a user-level module, I have "externed" that global struct

extern struct { int a; int b; } test;

Compiler doesn't complain, and linkage editor doesn't complain. However, if the user has multiple processes, then is that struct cloned for each process? If I use shared memory along with extern, then I could access the kernel's struct, and if I have n processes, then there's only 1 struct since its shared. I can access a kernel-level variable with 1 user-level process, but if I have more processes, then I get clones for each struct that is "externed"

My question is, Can multiple user-level processes read a kernel-level variable?

A: 

On most operating systems you cannot access kernel space variables from user space.
You will need to expose your data via the mechanisms that your os provides. This could be a custom system call, a file exposed via the vfs or any other form of IPC.

Martijn
A: 

On Unix this is usually done by mmap-ing some special device file like /dev/kmem.

Nikolai N Fetissov
A: 

Userspace cannot see kernel ram directly in any case - and mmap'ing /dev/kmem isn't a good solution either (it is really ugly in my opinion and should only be used for kernel debugging).

I think the nicest way is to expose it either through a file in /proc (which is pretty easy) or a character-device with an IOCTL (which is only slightly more complicated).

(NB: this is Linux / Unix specific)

MarkR