Linuxカーネルでユーザが編集可能なグローバル変数を使用したいと思います。それは可能ですか?私は、ソースコードで提供された例を使用して思い付いたものです :sysfsのkobjectをグローバル変数として使用するにはどうすればよいですか?
アーチ/ x86の/カーネル/ foo.cを
#include <linux/kobject.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include <linux/module.h>
#include <linux/init.h>
int foo = 12;
static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,
char *buf)
{
return sprintf(buf, "%d\n", foo);
}
static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,
const char *buf, size_t count)
{
sscanf(buf, "%du", &foo);
return count;
}
static struct kobj_attribute foo_attribute =
__ATTR(foo, 0666, foo_show, foo_store);
static struct attribute *attrs[] = {
&foo_attribute.attr,
NULL,
};
static struct attribute_group attr_group = {
.attrs = attrs,
};
static struct kobject *example_kobj;
static int __init example_init(void)
{
int retval;
example_kobj = kobject_create_and_add("kobject_example", kernel_kobj);
if (!example_kobj)
return -ENOMEM;
retval = sysfs_create_group(example_kobj, &attr_group);
if (retval)
kobject_put(example_kobj);
return retval;
}
static void __exit example_exit(void)
{
kobject_put(example_kobj);
}
module_init(example_init);
module_exit(example_exit);
は/ Linuxの/ foo.hという
が含まれます#ifndef FOO_H
#define FOO_H
extern unsigned int foo;
#endif
アーチ/ x86の/ randomfile.c
#include <linux/foo.h>
....
int foobar = (12 + foo);
....
は私が得るこの エラー:初期化子の要素が一定ではなく、私は本当に間違っているが、私は私が何かを見つけることができないと私は理解することはできません検索と同じくらい何かをしなければなら実現可能 カーネル内の他の実装を調べる方法...
誰かが正しい方向に、おそらく実用的な例を教えてくれますか?