Linux内核:获得符号链接背后的真实路径 [英] Linux Kernel: Get real path behind a symlink
问题描述
我正在处理一些Linux内核,并且我有一个伪造的路径/dev/blah/,它指向/dev/block/real_device
I'm working on some linux kernel stuff and I have a fake path called /dev/blah/whatever that points to /dev/block/real_device
问题是lookup_bdev将无法遵循符号链接,因此我想通过获取真实路径(/dev/block/real_device)预先按摩路径,因此我可以将其传递给lookup_bdev,以使其成功返回错误.
The issue is that lookup_bdev will fail to follow the symlink so I'd like to massage the path upfront by getting the real path (/dev/block/real_device) so I can hand that off to lookup_bdev so it returns successfully instead of an error.
或者在给定初始路径的情况下,可以正确检索block_device信息的任何其他内核调用.
Or any other kernel call that would correctly retrieve the block_device information given the initial path.
谢谢
推荐答案
为此使用VFS层(尤其是dcache/nameidata).
Use VFS layer for this (dcache/nameidata in particular).
#include <linux/namei.h>
#include <linux/dcache.h>
...
struct path path;
char buf[256];
char* ptr;
int err = kern_path("/dev/disk/by-id/dm-name-lkdevel-root",
LOOKUP_FOLLOW, &path);
if(!err) {
ptr = d_path(&path, buf, 256);
if(!IS_ERR(ptr)) {
/* ptr contains real path */
}
}
这已在香草Linux 3.12上进行了测试
This was tested on vanilla Linux 3.12
请注意,对于特殊的文件系统,d_path()
可能会返回奇怪的结果,并将(deleted)
后缀附加到已删除的文件中.
Note that d_path()
may return weird results for special filesystems and append (deleted)
suffix to deleted files.
这篇关于Linux内核:获得符号链接背后的真实路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!