Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use dentry_path_raw()

I'm writing a kernel module for Linux whose purpose necessitates that absolute paths be derived from dentry structs. I know that the function char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen) can be used to retrieve absolute paths from dentry structs. My problem is is that I do not know how to use it.

The buffer is supposedly where the path will be stored, but it wants a length for the buffer. How am I supposed to know what the length should be without having the full path to begin with?

I'm going to need to compare the path it gets with several hardcoded pathnames, but how do I do that with a buffer? Would this work:

char *path_of_file = dentry_path_raw(my_dentry, my_buffer, buflen);
char *test_path = "/root/file";
if (path_of_file == test_path) {
    return 0;
} else {
    return 1;
}
like image 552
Melab Avatar asked Oct 20 '22 00:10

Melab


1 Answers

path = dentry_path_raw(dentry, buffer, buflen);

builds path from the end buffer to the beginning. That is, buffer[buflen - 1] is set to '\0' and resulted path pointer will be greater or equal to buffer.

How am I supposed to know what the length should be without having the full path to begin with?

Even function itself doesn't know length of the path until it fully writes it. Just pick buffer with some lenght and call function. Good guess for path length would be PATH_MAX. If function found length to be insufficient, it returns error code ERR_PTR(-ENAMETOOLONG).

As for path comparision, simple strcmp(path, test_path) can be used. If you know length of the test_path, you can compare it with length of path, which can be simply calculated, and then use memcmp:

if(test_path_len == ((buffer + buflen - 1) - path)
    && !memcmp(path, test_path, test_path_len)) // paths are equal

Note, that dentry_path_raw return path of the dentry relative to mount point of filesystem, contained this dentry. So, it will be absolute path only when dentry is contained by root filesystem.

like image 58
Tsyvarev Avatar answered Oct 22 '22 00:10

Tsyvarev