簡體   English   中英

內核空間寫入文件

[英]Kernel space write a file

我正在嘗試在內核 5.10 C 代碼中編寫文件\/proc\/test\/enable<\/strong> 。
我可以通過調用 fwrite 將其寫入用戶空間。

當我在內核 C 代碼中使用kernel_write<\/strong>時,我會得到錯誤kernel write not supported for file test\/enable 和錯誤代碼 22<\/strong> 。

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);

為了支持“正常”寫入(來自用戶空間),文件可以定義.write<\/code>或.write_iter<\/code>操作(在其struct file_operations<\/code>中)。

但是從內核寫入只能<\/strong>使用.write_iter<\/code>操作。 為了支持從內核寫入文件,您需要為文件定義.write_iter<\/code>操作而不是<\/strong>.write<\/code>操作。


您的案例的失敗檢查是

    /*
     * 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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM