1

I want to read input.txt's content & write it into output.txt

This is my code, and it can makefile after all.

Linux Kernel 15.11 version

So it can't use vfs_read & vfs_write

I think that I can store the content from input.txt with ret

And use ret to write the content with kernel_write into output.txt

But the type of ret is ssize_t & in_buf is size_t So, it can't fit into the kernel_write function.

The function for kernel_write & kernel_read format is from the link

Read/write files within a Linux kernel module

#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <asm/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;

static int __init test_start(void){
        struct file *i_fp, *o_fp;
        loff_t pos;
        size_t count1;
        ssize_t ret;
        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;
        ret = kernel_read(i_fp, in_buf, count1, &pos);
        pos = 0;
        kernel_write(o_fp, ret, count1, &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);
jackson
  • 11
  • 3
  • 9
  • Not seeing where in_buf is initialized to a buffer in user space. – stark Oct 19 '21 at 15:40
  • @stark you're right. Maybe it will take a lot of time to write code for initalize void *in_buf & const void *out_buf in Linux kernel module :( – jackson Oct 19 '21 at 16:02
  • "But the type of ret is ssize_t & in_buf is size_t So, it can't fit into the kernel_write function." - Eh? After checking that `ret` is non-negative (otherwise it denotes the error), it can be perfectly represented in type `size_t`. – Tsyvarev Oct 19 '21 at 23:42
  • @Tsyvarev OK, I got it ,thanks. – jackson Oct 20 '21 at 00:31

0 Answers0