How to obtain a pathname or dentry or struct file from a given inode?

人盡茶涼 提交于 2019-12-01 21:23:51

This sample code will work well in Linux kernel version 2.6.xx

struct dentry *sample_dentry = NULL;
struct inode *tmp_inode = &inode_need_to_get;
struct list_head *tmp_list = NULL;
list_for_each(tmp_list, &(tmp_inode->i_dentry))
{
    sample_dentry = list_entry(tmp_list, struct dentry, d_alias);
    printk(KERN_EMERG, "name of file is %s\n", sample_dentry->d_iname);
}

Each inode object will have one or more dentries object in case this file have a hard link.

This is, in general, extremely difficult to do.

An inode may have thousands of pathnames. All names are equally "valid". Even on filesystems that do not support multiple links, the file could be bind-mounted thousands of times to anywhere else in the system.

Both the AppArmor and TOMOYO mandatory access control systems rely upon pathnames -- but with a gigantic difference: access controls are performed on a specific file descriptor, which was opened with a specific name, and both tools use that specific name.

Look into the security/apparmor/path.c function aa_get_name() or security/tomoyo/file.c function tomoyo_get_realpath() for details on finding pathnames from an inode -- given additional supporting information. From just the plain inode object, I think you're probably out of luck.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!