Linux 内核模块 read/write 写入文件
Linux Kernel module read/write into a file
我试图 read/write 我的内核模块中的一个文件(我知道这很危险,根本不建议这样做,但出于各种原因我需要这样做)
我遵循了这个答案 How to read/write files within a Linux kernel module?,它工作正常。
这是我执行的代码,用于测试基本功能是否正常工作:
void test_file(){
struct file * f = file_open("./test.txt", O_CREAT | O_RDWR |
O_APPEND, S_IRWXU | S_IRWXG | S_IRWXO);
if(f != NULL){
char arr[100];
char * str = "I just wrote something";
file_write(f,0, str, strlen(str));
memset(arr, '[=12=]', 100);
file_read(f, 0, arr, 20);
printk(KERN_INFO "Read %s\n",arr);
file_close(f);
}else{
printk(KERN_ERR "Error! Cannot write into file\n");
}
}
如果我在 __init
函数中执行此代码,test.txt
是 created/updated 在 .ko 文件所在的当前文件夹中。
但是,我注意到如果我在新的 kthread 中执行此代码,文件会在 /
文件夹中创建,我需要提供绝对路径才能在当前位置获取它。
void test_function(){
test_file(); // creates test.txt in /
}
static int __init file_init(void) {
struct task_struct * test_thread = kthread_run((void *)test_function, NULL, "Test");
test_file(); // creates test.txt in .
}
module_init(file_init)
file_write
、file_read
、file_close
和 file_open
的定义在链接的 Whosebug 答案中给出
有人知道如何在 kthread 中也给出相对路径吗?
这是我所做的:
struct file * f;
void test_file(){
if(f != NULL){
char arr[100];
char * str = "I just wrote something";
file_write(f,0, str, strlen(str));
memset(arr, '[=10=]', 100);
file_read(f, 0, arr, 20);
printk(KERN_INFO "Read %s\n",arr);
file_close(f);
}else{
printk(KERN_ERR "Error! Cannot open file\n");
}
}
void test_function(){
test_file(); // access the file from the kthread
}
static int __init file_init(void) {
// Create and open the file in user space
f = file_open("./test.txt", O_CREAT | O_RDWR | O_APPEND, \
S_IRWXU | S_IRWXG | S_IRWXO);
struct task_struct * test_thread = kthread_run((void *)test_function, \
NULL, "Test");
}
module_init(file_init)
我试图 read/write 我的内核模块中的一个文件(我知道这很危险,根本不建议这样做,但出于各种原因我需要这样做)
我遵循了这个答案 How to read/write files within a Linux kernel module?,它工作正常。
这是我执行的代码,用于测试基本功能是否正常工作:
void test_file(){
struct file * f = file_open("./test.txt", O_CREAT | O_RDWR |
O_APPEND, S_IRWXU | S_IRWXG | S_IRWXO);
if(f != NULL){
char arr[100];
char * str = "I just wrote something";
file_write(f,0, str, strlen(str));
memset(arr, '[=12=]', 100);
file_read(f, 0, arr, 20);
printk(KERN_INFO "Read %s\n",arr);
file_close(f);
}else{
printk(KERN_ERR "Error! Cannot write into file\n");
}
}
如果我在 __init
函数中执行此代码,test.txt
是 created/updated 在 .ko 文件所在的当前文件夹中。
但是,我注意到如果我在新的 kthread 中执行此代码,文件会在 /
文件夹中创建,我需要提供绝对路径才能在当前位置获取它。
void test_function(){
test_file(); // creates test.txt in /
}
static int __init file_init(void) {
struct task_struct * test_thread = kthread_run((void *)test_function, NULL, "Test");
test_file(); // creates test.txt in .
}
module_init(file_init)
file_write
、file_read
、file_close
和 file_open
的定义在链接的 Whosebug 答案中给出
有人知道如何在 kthread 中也给出相对路径吗?
这是我所做的:
struct file * f;
void test_file(){
if(f != NULL){
char arr[100];
char * str = "I just wrote something";
file_write(f,0, str, strlen(str));
memset(arr, '[=10=]', 100);
file_read(f, 0, arr, 20);
printk(KERN_INFO "Read %s\n",arr);
file_close(f);
}else{
printk(KERN_ERR "Error! Cannot open file\n");
}
}
void test_function(){
test_file(); // access the file from the kthread
}
static int __init file_init(void) {
// Create and open the file in user space
f = file_open("./test.txt", O_CREAT | O_RDWR | O_APPEND, \
S_IRWXU | S_IRWXG | S_IRWXO);
struct task_struct * test_thread = kthread_run((void *)test_function, \
NULL, "Test");
}
module_init(file_init)