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

查看:260
本文介绍了如何从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\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.

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

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