Linux Kernel: Get real path behind a symlink

霸气de小男生 提交于 2019-12-12 12:02:59

问题


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

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.

Or any other kernel call that would correctly retrieve the block_device information given the initial path.

Thanks


回答1:


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 */
    }
}

This was tested on vanilla Linux 3.12

Note that d_path() may return weird results for special filesystems and append (deleted) suffix to deleted files.




回答2:


Try to use sys_readlink() system call.



来源:https://stackoverflow.com/questions/28221166/linux-kernel-get-real-path-behind-a-symlink

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