Linux カーネルでユーザーが編集可能なグローバル変数を使用したいと考えています。それは可能ですか?ソースコードで提供されている例を使用して思いついたのは次のとおりです。
arch/x86/kernel/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
arch/x86/randomfile.c
#include <linux/foo.h>
....
int foobar = ( 12 + foo );
....
私はこの エラーを受け取ります: 初期化子要素は定数 ではありません。これは、私が何か本当に間違ったことをしているに違いないことを認識させますが、検索しても何も見つからず、他の実装を見てそれを行う方法を理解できませんカーネルで...
おそらく実用的な例で、誰かが私を正しい方向に向けることができますか?