'Linux Kernel Module - Create Directory
I've got a problem about creating a directory within a linux kernel module.
What I want: Creating a directory within a kernel module.
Here is my actual code:
struct file *fp = (struct file *) NULL;
fp = filp_open("/home/testdir", O_DIRECTORY|O_CREAT, S_IRUSR);
But it creates a file instead of directory.
I tried to same code as above without the flag "O_DIRECTORY":
struct file *fp = (struct file *) NULL;
fp = filp_open("/home/testdir", O_CREAT, S_IRUSR);
And the result is similar to the previous result.
I don't understand the behaviour. What am I doing wrong?
Edit 1: I am coding on a Raspberry PI, Raspbian, kernel version: 4.4.43-v7
Solution 1:[1]
I got it by myself. The solution is:
struct file *fp = (struct file *) NULL;
fp = filp_open("/home/testdir/", O_DIRECTORY|O_CREAT, S_IRUSR);
Note the "/" at the end of the path.
Thanks @all for the try!
Solution 2:[2]
This doesn't create a directory but opens a directory like opendir
does (the directory should exists).
Solution 3:[3]
something that worked for me: its also similar to how they create a dir in devtmpfs
int mkdir_(const char *dirPath, umode_t mode) {
struct path path;
struct dentry *dentry = kern_path_create(AT_FDCWD, dirPath, &path, mode);
int err = PTR_ERR(dentry);
if (IS_ERR(dentry)) {
return -1;
}
err = vfs_mkdir(path.dentry->d_inode, dentry, mode);
done_path_create(&path, dentry);
return err;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Daniel Christoph |
Solution 2 | Jean-Baptiste Yunès |
Solution 3 |