 |
linux howto - fabio de lorenzo personal informations and experiences kernel modules and software
|
| View previous topic :: View next topic |
| Author |
Message |
root Site Admin
Joined: 27 Apr 2004 Posts: 156
|
Posted: Fri Nov 13, 2009 7:45 pm Post subject: read write userspace file from kernel |
|
|
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/vmalloc.h>
#include <linux/syscalls.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/fcntl.h>
#include <asm/uaccess.h>
MODULE_LICENSE("GPL");
/**
* Reads give buffer to file.
*
* GPL'd code.
* FOR DEBUGGING PURPOSES ONLY.
*/
ssize_t fabio_gpl_read_file(char *filename, unsigned char *data, int data_size)
{
struct file *file;
loff_t pos = 0;
int fd;
ssize_t ret = -EBADF;
mm_segment_t old_fs = get_fs();
set_fs(KERNEL_DS);
fd = sys_open(filename, O_RDONLY, 0644);
if (fd >= 0) {
ret = __sys_read(fd, data, data_size);
file = fget(fd);
if (file) {
ret = vfs_read(file, data, data_size, &pos);
fput(file);
}
sys_close(fd);
}
set_fs(old_fs);
return ret;
}
/**
* Writes give buffer to file.
*
* GPL'd code.
* FOR DEBUGGING PURPOSES ONLY.
*/
ssize_t fabio_gpl_write_file(char *filename, unsigned char *data, int data_size, bool append)
{
struct file *file;
loff_t pos = 0;
int fd;
ssize_t ret = -EBADF;
mm_segment_t old_fs = get_fs();
set_fs(KERNEL_DS);
fd = sys_open(filename, O_WRONLY|O_CREAT|(append ? O_APPEND : 0), 0644);
if (fd >= 0) {
ret = __sys_write(fd, data, data_size);
file = fget(fd);
if (file) {
ret = vfs_write(file, data, data_size, &pos);
fput(file);
}
sys_close(fd);
}
set_fs(old_fs);
return ret;
} |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|