如何从 Linux 内核空间(即从自定义系统调用)添加自定义扩展属性 [英] How to add a custom Extended Attribute from Linux kernel space (i.e from a custom system call)

查看:35
本文介绍了如何从 Linux 内核空间(即从自定义系统调用)添加自定义扩展属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How would one go about adding an extended attribute like the command line function setfattr -n user.custom_attrib -v 99 ex1.txt, but doing it from within the kernel in a custom system call. I've looked at linux/xattrib.h and I've had no luck trying to set anything from kernel space. Anytime I've used vfs_setxattr(struct dentry *, const char *, const void *, size_t, int); it reboots the whole VM. In the end I'm trying to add a new integer type as an extended attribute to files and I also will need to retrieve that extended attribute. I need to use the functions allowed within kernel space.

推荐答案

我能够获得适用于以下的扩展属性: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";
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.

安装命令行工具:

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)传递到扩展属性中,而我的试验使我多次构建内核.希望这可以帮助一些人,或者如果有人有任何更正,请告诉我.

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.

这篇关于如何从 Linux 内核空间(即从自定义系统调用)添加自定义扩展属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆