Linux 内核模块 - 创建目录

Dan*_*oph 2 c linux kernel-module linux-kernel

我在 Linux 内核模块中创建目录时遇到问题。

我想要的:在内核模块中创建一个目录。

这是我的实际代码:

struct file *fp = (struct file *) NULL;
fp = filp_open("/home/testdir", O_DIRECTORY|O_CREAT, S_IRUSR);
Run Code Online (Sandbox Code Playgroud)

但它创建一个文件而不是目录。

我尝试使用与上面相同的代码而不使用标志“O_DIRECTORY”:

struct file *fp = (struct file *) NULL;
fp = filp_open("/home/testdir", O_CREAT, S_IRUSR);
Run Code Online (Sandbox Code Playgroud)

并且结果与之前的结果类似。

我不明白这种行为。我究竟做错了什么?

编辑 1:我正在 Raspberry PI、Raspbian 上编码,内核版本:4.4.43-v7

Dan*_*oph 5

我自己拿到的。解决办法是:

struct file *fp = (struct file *) NULL;
fp = filp_open("/home/testdir/", O_DIRECTORY|O_CREAT, S_IRUSR);
Run Code Online (Sandbox Code Playgroud)

请注意路径末尾的“/”。

感谢@all 的尝试!