【问题标题】:How to add a custom Extended Attribute from Linux kernel space (i.e from a custom system call)如何从 Linux 内核空间添加自定义扩展属性(即从自定义系统调用)
【发布时间】:2018-09-17 16:14:46
【问题描述】:

如何添加像命令行函数setfattr -n user.custom_attrib -v 99 ex1.txt 这样的扩展属性,但在自定义系统调用中从内核内部完成。我查看了linux/xattrib.h,但我没有运气尝试从内核空间设置任何东西。每当我使用vfs_setxattr(struct dentry *, const char *, const void *, size_t, int); 时,它都会重新启动整个虚拟机。最后,我试图将一个新的整数类型作为扩展属性添加到文件中,并且我还需要检索该扩展属性。我需要使用内核空间中允许的功能。

【问题讨论】:

  • 根据setxattr 系统调用的implementation,它将对vfs_setxattr(通过setxattr())的调用包装到mnt_want_write()/mnt_drop_write() 守卫中。因此,您只需要获取与您的dentry 对应的vfsmount 对象,并使用给定的守卫。

标签: linux linux-kernel filesystems


【解决方案1】:

我能够使扩展属性适用于:vfs_setxattr(struct dentry *, const char *, const void *, size_t, int); 主要问题是const void * 想要通过char *。该代码看起来像这样:

char * buf = "test\0";
int size = 5;     //number of bytes needed for attribute
int flag = 0;     //0 allows for replacement or creation of attribute
int err;          //gets error code negative error and positive success

err = vfs_setxattr(path_struct.dentry, "user.custom_attrib", buf, size, flag);

我也能够让vfs_getxattr(struct dentry *, const char *, const void *, size_t); 正常工作。缓冲区和void * 再次成为我卡住的地方。我必须分配一个缓冲区来保存正在传递的扩展属性。所以我的代码看起来像这样:

char buf[1024];
int size_buf = 1024;
int err;

err = vfs_getxattr(path_struct.dentry, "user.custom_attrib",buf, size_buf);

所以现在 buf 将保存来自 dentry 的指定文件的值。错误代码对于弄清楚发生了什么非常有帮助。使用命令行工具也是如此。

安装命令行工具:

sudo apt-get install attr

从命令行手动设置属性:

setfattr -n user.custom_attrib -v "test_if working" test.txt

从命令行手动获取属性:

getfattr -n user.custom_attrib test.txt  

我无法弄清楚是否可以将 int 等不同类型传递到扩展属性中,并且我的试验导致我多次构建内核。希望这可以帮助一些人,或者如果有人有任何更正,请告诉我。

【讨论】:

    猜你喜欢
    • 2020-01-17
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    相关资源
    最近更新 更多