如何从Linux内核空间(即从自定义系统调用)添加自定义扩展属性
如何添加一个扩展属性,比如命令行函数setfattr-nuser.custom_attrib-v99 ex1.txt,而不是通过一个自定义系统调用在内核中进行添加。我已经看过如何从Linux内核空间(即从自定义系统调用)添加自定义扩展属性,linux,linux-kernel,filesystems,Linux,Linux Kernel,Filesystems,如何添加一个扩展属性,比如命令行函数setfattr-nuser.custom_attrib-v99 ex1.txt,而不是通过一个自定义系统调用在内核中进行添加。我已经看过linux/xattrib.h了,但是我没有从内核空间设置任何东西。只要我使用了vfs\u setxattr(struct dentry*、const char*、const void*、size\u t、int)它重新启动整个虚拟机。最后,我尝试添加一个新的整数类型作为文件的扩展属性,并且还需要检索该扩展属性。我需要使用内
linux/xattrib.h
了,但是我没有从内核空间设置任何东西。只要我使用了vfs\u setxattr(struct dentry*、const char*、const void*、size\u t、int)代码>它重新启动整个虚拟机。最后,我尝试添加一个新的整数类型作为文件的扩展属性,并且还需要检索该扩展属性。我需要使用内核空间中允许的函数 我能够让扩展属性工作:vfs\u setxattr(struct dentry*、const char*、const void*、size\u 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);
char buf[1024];
int size_buf = 1024;
int err;
err = vfs_getxattr(path_struct.dentry, "user.custom_attrib",buf, size_buf);
我还能够得到vfs\u getxattr(struct dentry*、const char*、const void*、size\t)代码>也可以工作。缓冲区和void*
又是我被卡住的地方。我必须分配一个缓冲区来保存正在传递的扩展属性。所以我的代码看起来像这样:
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);
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
要从命令行手动获取属性,请执行以下操作:
setfattr -n user.custom_attrib -v "test_if working" test.txt
getfattr -n user.custom_attrib test.txt
我无法确定是否可以将不同的类型(如int)传递到扩展的atributes中,我的尝试导致我多次构建内核。希望这能帮助一些人,或者如果有人有任何纠正让我知道 根据setxattr
syscall的定义,它将对vfs\u setxattr
(通过setxattr()
)的调用封装到mnt\u want\u write()
/mnt\u drop\u write()
保护中。因此,您只需要获得与您的dentry
对应的vfsmount
对象,并使用给定的防护。