linux howto - fabio de lorenzo personal informations and experiences Forum Index linux howto - fabio de lorenzo personal informations and experiences
kernel modules and software
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

read write userspace file from kernel

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    linux howto - fabio de lorenzo personal informations and experiences Forum Index -> kernel and modules
View previous topic :: View next topic  
Author Message
root
Site Admin


Joined: 27 Apr 2004
Posts: 156

PostPosted: Fri Nov 13, 2009 7:45 pm    Post subject: read write userspace file from kernel Reply with quote

#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
View user's profile Send private message Send e-mail
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    linux howto - fabio de lorenzo personal informations and experiences Forum Index -> kernel and modules All times are GMT
Page 1 of 1

 
Jump to:  
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