Different between get_fs
and sget_fc
I tried to use it for read file in Linux kernel
BTW, How do I read file which the file type is txt file in Linux Module
I know that trying to read or write file in Linux Kernel is a stupid action.
Can someone answer the question ? thx
j0000@ubuntu:~/Desktop/file$ make
make -C /usr/src/linux-headers-5.11.0-37-generic/ M=/home/j0000/Desktop/file modules
make[1]: Entering directory '/usr/src/linux-headers-5.11.0-37-generic'
CC [M] /home/j0000/Desktop/file/reverse.o
/home/j0000/Desktop/file/reverse.c: In function ‘test_init’:
/home/j0000/Desktop/file/reverse.c:20:9: error: implicit declaration of function ‘get_fs’; did you mean ‘sget_fc’? [-Werror=implicit-function-declaration]
20 | fs =get_fs();
| ^~~~~~
| sget_fc
/home/j0000/Desktop/file/reverse.c:20:9: error: incompatible types when assigning to type ‘mm_segment_t’ {aka ‘struct <anonymous>’} from type ‘int’
/home/j0000/Desktop/file/reverse.c:21:5: error: implicit declaration of function ‘set_fs’; did you mean ‘sget_fc’? [-Werror=implicit-function-declaration]
21 | set_fs(KERNEL_DS);
| ^~~~~~
| sget_fc
/home/j0000/Desktop/file/reverse.c:21:12: error: ‘KERNEL_DS’ undeclared (first use in this function); did you mean ‘KERNFS_NS’?
21 | set_fs(KERNEL_DS);
| ^~~~~~~~~
| KERNFS_NS
/home/j0000/Desktop/file/reverse.c:21:12: note: each undeclared identifier is reported only once for each function it appears in
cc1: some warnings being treated as errors
make[2]: *** [scripts/Makefile.build:288: /home/j0000/Desktop/file/reverse.o] Error 1
make[1]: *** [Makefile:1849: /home/j0000/Desktop/file] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-5.11.0-37-generic'
make: *** [Makefile:8: default] Error 2
Updated my code :
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Reversing text from Jackson");
MODULE_VERSION("0.1");
void *in_buf;
const void *out_buf = "add" ;
static int __init test_start(void){
struct file *i_fp, *o_fp;
loff_t pos;
size_t count1, count2;
printk(KERN_INFO "start converting...\n");
i_fp = filp_open("/home/j0000/Desktop/file/input.txt",O_RDWR | O_CREAT, 0644);
o_fp = filp_open("/home/j0000/Desktop/file/output.txt",O_RDWR | O_CREAT, 0644);
if (IS_ERR(i_fp)){
printk(KERN_INFO "intput file open error/n");
return -1;
}
if (IS_ERR(o_fp)){
printk(KERN_INFO "output file open error/n");
return -1;
}
pos = 0;
kernel_read(i_fp, in_buf, count1, &pos);
pos = 0;
kernel_write(o_fp, out_buf, count2, &pos);
filp_close(i_fp, NULL);
filp_close(o_fp, NULL);
return 0;
}
static void __exit test_end(void){
printk(KERN_INFO "Ending~\n");
}
module_init(test_start);
module_exit(test_end);