如何从 Linux 内核 space 添加自定义扩展属性(即从自定义系统调用)
How to add a custom Extended Attribute from Linux kernel space (i.e from a custom system call)
如何添加扩展属性,如命令行函数 setfattr -n user.custom_attrib -v 99 ex1.txt
,但在自定义系统调用中从内核中进行。我查看了 linux/xattrib.h
,但我没有运气尝试从内核 space 设置任何内容。每当我使用 vfs_setxattr(struct dentry *, const char *, const void *, size_t, int);
时,它都会重新启动整个虚拟机。最后,我尝试添加一个新的整数类型作为文件的扩展属性,我还需要检索该扩展属性。我需要使用内核 space 允许的功能。
我能够获得适用于以下项目的扩展属性:vfs_setxattr(struct dentry *, const char *, const void *, size_t, int);
主要问题是 const void *
想要通过 char *
。该代码看起来像这样:
char * buf = "test[=10=]";
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)传递到扩展属性中,我的试验导致我多次破坏内核构建。希望这可以帮助一些人,或者如果有人有任何更正,请告诉我。
如何添加扩展属性,如命令行函数 setfattr -n user.custom_attrib -v 99 ex1.txt
,但在自定义系统调用中从内核中进行。我查看了 linux/xattrib.h
,但我没有运气尝试从内核 space 设置任何内容。每当我使用 vfs_setxattr(struct dentry *, const char *, const void *, size_t, int);
时,它都会重新启动整个虚拟机。最后,我尝试添加一个新的整数类型作为文件的扩展属性,我还需要检索该扩展属性。我需要使用内核 space 允许的功能。
我能够获得适用于以下项目的扩展属性:vfs_setxattr(struct dentry *, const char *, const void *, size_t, int);
主要问题是 const void *
想要通过 char *
。该代码看起来像这样:
char * buf = "test[=10=]";
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)传递到扩展属性中,我的试验导致我多次破坏内核构建。希望这可以帮助一些人,或者如果有人有任何更正,请告诉我。