简体   繁体   中英

Kernel space write a file

I'm trying to write a file \/proc\/test\/enable<\/strong> in kernel 5.10 C code.
I can write it in user space by calling fwrite.


struct file *filp;
char context[] = "test";
filp = filp_open("/proc/test/enable", O_WRONLY, 0);
int err = kernel_write(filp, (void *)context, sizeof(context), &filp->f_pos);
dev_info(afe->dev, "%s(), DONE, code = %d", __func__, err);

For support "normal" write (from userspace), a file may define .write<\/code> or .write_iter<\/code> operations (in its struct file_operations<\/code> ).

For support writing your file from the kernel, you need to define .write_iter<\/code> operation for the file instead<\/strong> of .write<\/code> one.


    /*
     * Also fail if ->write_iter and ->write are both wired up as that
     * implies very convoluted semantics.
     */
    if (unlikely(!file->f_op->write_iter || file->f_op->write))
        return warn_unsupported(file, "write");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM