- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想将大于 1024 个字符的字符串传递到我的模块(文件系统)。由于内核参数限制为 1024 个字符,someone recommended改为使用 sysfs。
我试图包括 this example在我的 super.c 类中为我的模块在 sysfs 中创建字符串“文件名”和字符串“代码”条目。
static decl_subsys(myfs, NULL, NULL);
struct myfs_attr {
struct attribute attr;
char *value;
};
static struct myfs_attr fname = {
.attr.name="filename",
.attr.owner = THIS_MODULE,
.attr.mode = 0644,
.value = "/my/test/path",
};
static struct myfs_attr code = {
.attr.name="code",
.attr.owner = THIS_MODULE,
.attr.mode = 0644,
.value = "0101",
};
在编译我的模块时出现了很多错误(第 41 行是 decl_subsys):
fs/myfs/super.c:41:26: error: expected ‘)’ before ‘(’ token
fs/myfs/super.c:50:2: error: unknown field ‘owner’ specified in initializer
fs/myfs/super.c:50:2: warning: initialization from incompatible pointer type [enabled by default]
fs/myfs/super.c:50:2: warning: (near initialization for ‘fname.attr.name’) [enabled by default]
...
fs/myfs/super.c: At top level:
fs/myfs/super.c:83:15: error: variable ‘myfsops’ has initializer but incomplete type
fs/myfs/super.c:84:2: error: unknown field ‘show’ specified in initializer
fs/myfs/super.c:84:2: warning: excess elements in struct initializer [enabled by default]
fs/myfs/super.c:84:2: warning: (near initialization for ‘myfsops’) [enabled by default]
fs/myfs/super.c:85:2: error: unknown field ‘store’ specified in initializer
fs/myfs/super.c:85:2: warning: excess elements in struct initializer [enabled by default]
fs/myfs/super.c:85:2: warning: (near initialization for ‘myfsops’) [enabled by default]
fs/myfs/super.c:89:2: error: unknown field ‘myfs_ops’ specified in initializer
fs/myfs/super.c:89:2: warning: initialization from incompatible pointer type [enabled by default]
fs/myfs/super.c:89:2: warning: (near initialization for ‘myfstype.release’) [enabled by default]
fs/myfs/super.c: In function ‘init_myfs_fs’:
fs/myfs/super.c:1554:2: error: implicit declaration of function ‘kobj_set_kset_s’ [-Werror=implicit-function-declaration]
fs/myfs/super.c:1554:19: error: ‘myfs_subsys’ undeclared (first use in this function)
fs/myfs/super.c:1554:19: note: each undeclared identifier is reported only once for each function it appears in
fs/myfs/super.c:1554:32: error: ‘fs_subsys’ undeclared (first use in this function)
fs/myfs/super.c:1557:2: error: implicit declaration of function ‘subsystem_register’ [-Werror=implicit-function-declaration]
fs/myfs/super.c: In function ‘exit_myfs_fs’:
fs/myfs/super.c:1579:2: error: implicit declaration of function ‘subsystem_unregister’ [-Werror=implicit-function-declaration]
fs/myfs/super.c:1579:24: error: ‘myfs_subsys’ undeclared (first use in this function)
最佳答案
这是将数据传输到“param_buf”字符串的源代码。根据要求,没有读取方法。仅存储。
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <asm/string.h>
static struct kobject *register_kobj;
static char *param_buf;
// function for many symbol data enter
static ssize_t __used store_value(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count){
printk(KERN_ALERT "you entered %s\n", buf);
strncpy(param_buf, buf, PAGE_SIZE - 1);
return count;
}
// register function to attribute
static struct kobj_attribute store_val_attribute = __ATTR( put_parameters, 0220, NULL, store_value);
// put attribute to attribute group
static struct attribute *register_attrs[] = {
&store_val_attribute.attr,
NULL, /* NULL terminate the list*/
};
static struct attribute_group reg_attr_group = {
.attrs = register_attrs
};
static int hello_init(void){
param_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
// create sysfs object ( /sys/kernel/test_1025_sym directory )
register_kobj = kobject_create_and_add("test_1025_sym", kernel_kobj);
if (!register_kobj)
return -ENOMEM;
//create attributes (files)
if(sysfs_create_group(register_kobj, ®_attr_group)){
kobject_put(register_kobj);
return -ENOMEM;
}
return 0;
}
static void hello_exit(void){
printk(KERN_ALERT "last value was %s\n", param_buf);
kfree(param_buf);
kobject_put(register_kobj);
}
MODULE_LICENSE("Dual BSD/GPL");
module_init(hello_init);
module_exit(hello_exit);
可以这样测试:
cat /etc/fstab > /sys/kernel/test_1025_sym/put_parameters
对于两个字符串条目:复制一个store_value函数,再注册一个store_val_attribute并放入属性列表。
关于从内核模块创建 sysfs 条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23280635/
我之前已经发布了查询,我正在重复同样的问题我想修改 kernel2.6 中内置的 igmpv3 (Linux)。-- 以便它从文件中读取一个值并适本地决定内部的保留 (res 1) 值主机发送的igm
在内核 4.0 上,当单步执行 sysfs_create_bin_file 的内核源代码时,我注意到它传递给了 sysfs_add_file(kobj->sd, &attr->attr, true);
我只是想为/sys 文件系统中的设备添加一个简单的属性。看起来很简单,但没有提到任何辅助方法 here或 here是从内核导出的,所以我不能从模块中使用它们?我一定遗漏了什么,任何人都可以提供一些见解
所以我一直在阅读 Linux API(系统调用和 libc)以及设备 ioctls,通过 create(dev_name) 和 ioctl(device_file_handle) 调用 ioctls
我想将大于 1024 个字符的字符串传递到我的模块(文件系统)。由于内核参数限制为 1024 个字符,someone recommended改为使用 sysfs。 我试图包括 this example
在用户空间中,我可以echo noop >/sys/block/sda/queue/scheduler。 如何在内核模块中做同样的事情? 我期待这样的事情(伪代码): struct dentry* e
我正在编写一个检查电池容量的守护进程。这是用于运行 Linux 的太阳能嵌入式设备。我读过使用 sleep() 是个坏主意在守护进程中,因此我正在尝试使用事件。所以我写了一些 PoC,但我没有收到任何
最近我正在查看 kobjects 和 sysfs 的内核。 我知道/理解以下.. 所有内核对象使用地址 > 0x80000000 kobjects 也不异常(exception) sysfs 只不过是
处理 sysfs阅读我需要创建一个 show添加到 kobj_attribute 的函数结构体。 the function is defined as的原型(prototype): ssize_t (
我已经成功地向我的设备驱动程序添加了一个设备属性,但在我发现的所有示例中,该属性都用于向内核注入(inject)数据,而我的目标是从内核导出一些数据以使其可见在用户空间。从内核空间写入该文件的正确方法
源自this question (和 my solution ),我已经意识到可能存在死锁,但我不明白为什么以及如何避免它。 简而言之,内核空间中有一个信号量,内核模块(它们实际上是在内核空间中运行的
我如何从用户应用程序读取 sysfs 属性。它与 cat 命令的内容类似。比如我有一个标签是亮度。如何获取亮度标签的值?因为我需要从我的应用程序读取 USB 信息,并且我看到它们被导出到 sysfs,
如何使用 sysfs 检测我的硬件模块(例如 MSR)是否执行事件或中断? 最佳答案 你的问题有点含糊。 对于中断,您可以查看 cat/proc/interrupts 的输出,看看您的驱动程序的中断计
我想知道..实际上什么是 sysfs 中的属性文件,..为什么我们需要在 sysfs 目录中为某些驱动程序而不是某些驱动程序创建属性文件.. 我们什么时候真正需要它? 我已经阅读了 LDD3 和其他一
我正在为多个定制硬件编写驱动程序。所有设备都通过 PCIe 连接到主机。为了方便起见,我想将所有这些自定义设备组合到一个 sysfs 类中(我认为这是可以接受的事情?)。不幸的是,LDD3 中的信息已
在我正在使用的设备上,我可以写入 /sys/class/leds 来打开/关闭 LED 灯。我可以在我的代码中保持文件打开并仅在进程退出时关闭,还是应该仅在需要写入文件时打开文件? 最佳答案 是的。这
我正在编写一个 linux 设备驱动程序,除其他外,它在 sysfs 中公开了一个只读二进制属性,其接口(interface)是 static ssize_t read_dump_fifo(struc
我正在尝试学习如何编写 sysfs 模块,但对基本的初始化感到困惑。在 this查看 kobject.h 文件的文档,有几个与创建 sysfs 条目相关的不同函数。 从外观上看,函数“kobject_
我正在使用 C 文件 IO 从 linux 中的 sysfs 接口(interface)读取值。寄存器的路径和样例值如下: cat /sys/class/powercap/intel-rapl/int
我的 FPGA 中有一个带有多个 I/O 寄存器的通用 AXI 从设备。我想使用 sysfs 接口(interface)访问我的寄存器。对于我想要访问的每个寄存器,我都创建了一个 _show 和一个
我是一名优秀的程序员,十分优秀!