且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从Linux内核空间(即通过自定义系统调用)添加自定义扩展属性

更新时间:2022-12-16 19:04:41

我能够获得适用于以下环境的扩展属性:vfs_setxattr(struct dentry *, const char *, const void *, size_t, int); 主要问题是const void *希望传递char *.该代码看起来像这样:

I was able to get the extended attributes working for: vfs_setxattr(struct dentry *, const char *, const void *, size_t, int); The main problem was the const void * wanted a char * to be passed. That code looked something like this:

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 *再次卡住了.我必须分配一个缓冲区来保存正在传递的扩展属性.所以我的代码看起来像这样:

I also was able to get vfs_getxattr(struct dentry *, const char *, const void *, size_t); working as well. The buffer and void * was once again where I got stuck. I had to allocate a buffer to hold the extended attribute that was being passed. So my code looked something like this:

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

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

因此,现在buf将保存来自dentry的指定文件中的值.错误代码对于找出正在发生的事情非常有帮助.使用命令行工具也是如此.

So now buf will hold the value from the specified file from dentry. The error codes are extremely helpful in figuring out what is going on. So is using the command line tools.

要安装命令行工具,请执行以下操作:

To install the command line tool:

sudo apt-get install attr

要从命令行手动设置属性,请执行以下操作:

To set an attribute manually from the command line:

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

要从命令行手动获取属性,请执行以下操作:

To get an attribute manually from the command line:

getfattr -n user.custom_attrib test.txt  

我无法确定是否可以将诸如int的其他类型传递给扩展的atrributes,而我的尝试使我无数次地构建了内核.希望这对某些人有所帮助,或者如果有人有任何更正,请告诉我.

I wasn't able to figure out if you could pass different types like int's into the extended atrributes and my trials caused me to brick the kernel builds numerous times. Hope this helps some people out, or if anyone has any corrections let me know.