gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:43:24 24 4
gpt4 key购买 nike

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

最佳答案

我能够让扩展属性为:vfs_setxattr(struct dentry *, const char *, const void *, size_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);

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

关于linux - 如何从 Linux 内核空间添加自定义扩展属性(即从自定义系统调用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49930779/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com