. You can also test whether a user-space pointer is valid through a call to access_ok
. These functions are defined in linux/include/asm/uaccess.h.
You use the access_ok
macro to validate a user-space pointer for a given operation. This function takes the type of access (VERIFY_READ
or VERIFY_WRITE
), the pointer to the user-space memory block, and the size of the block (in bytes). The function returns zero on success:
int access_ok( type, address, size );
|
Moving simple types between the kernel and user-space (such as ints or longs) is accomplished easily with get_user
and put_user
. These macros each take a value and a pointer to a variable. The get_user
function moves the value that the user-space address specifies (ptr
) into the kernel variable specified (var
). The put_user
function moves the value that the kernel variable (var
) specifies into the user-space address (ptr
). The functions return zero on success:
int get_user( var, ptr );
int put_user( var, ptr );
|
To move larger objects, such as structures or arrays, you can use the copy_from_user
and copy_to_user
functions. These functions move an entire block of data between user-space and the kernel. The copy_from_user
function moves a block of data from user-space into kernel-space, and copy_to_user
moves a block of data from the kernel into user-space:
unsigned long copy_from_user( void *to, const void __user *from, unsigned long n );
unsigned long copy_to_user( void *to, const void __user *from, unsigned long n );
|
Finally, you can copy a NULL-terminated string from user-space to the kernel by using the strncpy_from_user
function. Before calling this function, you can get the size of the user-space string with a call to the strlen_user
macro:
long strncpy_from_user( char *dst, const char __user *src, long count );
strlen_user( str );
|
These functions provide the basics for memory movement between the kernel and user-space. Some additional functions exist (such as those that reduce the amount of checking performed). You can find these functions in uaccess.h.
Using the system call
Now that kernel is updated with a few new system calls, let's look at what's necessary to use them from a user-space application. There are two ways that you can use new kernel system calls. The first is a convenience method (not something that you'd probably want to do in production code), and the second is the traditional method that requires a bit more work.
共8页: 上一页 [1] [2] [3] 4 [5] [6] [7] [8] 下一页