0

I want to read a txt file and write another txt file in kernel module How should I do ? I have seen many website. It seems like it has many ways to solve this question ex : filp_read() or kernel_read() or vfs_read() [Sloved] But I have tried many times, it couldn't makefile.

[Answer] Use mv make Makefile

reverse.c

#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/uaccess.h>

static char buf[] ="It's from linux kernel\n";
static char buf1[32];
 
int __init test_init(void)
{
    struct file *fp;
    mm_segment_t fs;
    loff_t pos;
    printk("test enter\n");
    fp =filp_open("/home/j0000/Desktop/file/kernel_file",O_RDWR | O_CREAT,0644);
    if (IS_ERR(fp)){
        printk("create file error\n");
        return -1;
    }
    fs =get_fs();
    set_fs(KERNEL_DS);
    pos =0;
    vfs_write(fp,buf, sizeof(buf), &pos);
    pos =0;
    vfs_read(fp,buf1, sizeof(buf), &pos);
    printk("Write contet=%s\n",buf1);
    filp_close(fp,NULL);
    set_fs(fs);
    return 0;
}
void __exit test_exit(void)
{
    printk("test exit\n");
}
 
module_init(test_init);
module_exit(test_exit);
 
MODULE_LICENSE("GPL");

makefile

obj-m :=reverse.o                          
read_userspace-objs:= read_userspace_file.o     
KVERSION := $(shell uname -r)
KDIR := /usr/src/linux-headers-$(KVERSION)/
PWD := $(shell pwd)

default:
        make -C $(KDIR) M=$(PWD) modules

clean:
        rm -rf *.o *.cmd *.ko *.mod.c .tmp_versions Module.symvers modules.order

error message

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'
scripts/Makefile.build:44: /home/j0000/Desktop/file/Makefile: No such file or directory
make[2]: *** No rule to make target '/home/j0000/Desktop/file/Makefile'.  Stop.
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
j0000@ubuntu:~/Desktop/file$ 

updated error message

I have seen these error many times~ So may I ask why it can't use get_fs & set_fs in this situation

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

Actually I use this website for tutorial https://www.cnblogs.com/arnoldlu/p/8879800.html

After all, I will use kernel_read & kernel_write to fix my bug.

Thanks for answering my question :)

jackson
  • 11
  • 3
  • 9

0 Answers0