sys 파일 중 하나가 변경되었을 때 모듈에 알릴 수 있습니까? 내 임무는 모듈 내부의 버퍼 크기를 제어하는 파일을 수행하는 것인데, 파일의 값이 변경되면 버퍼의 크기를 조정하려고합니다. 다른 생각 (모듈에 알릴 수없는 경우)은 모듈을 사용할 때마다 이전 값을 확인한 다음 버퍼 크기를 조정하는 것이 었습니다.sysfs의 커널 모듈 매개 변수 - 변경 사항에 대한 빠른 대응
4
A
답변
9
Sysfs의 목적이 아닌가요?
kobject
을 만들고이를 Sysfs (디렉토리)에 표시하면 해당 디렉토리의 파일이 될 해당 객체의 속성을 만듭니다. kobject
에 store
및 show
콜백을 제공합니다. 이들은 기본적으로 resp와 같습니다. write
및 read
.
store
여기에 원하는 내용이 있습니다. 그것은 다음과 같습니다 : 당신은 즉시 가상 파일은 사용자 땅에서 기록 된대로 buffer
내 size
바이트를받을
ssize_t (*store)(struct kobject *kobj, struct attribute *attr,
const char *buffer, size_t size);
.
또한#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>
struct my_attr {
struct attribute attr;
int value;
};
static struct my_attr my_first = {
.attr.name="first",
.attr.mode = 0644,
.value = 1,
};
static struct my_attr my_second = {
.attr.name="second",
.attr.mode = 0644,
.value = 2,
};
static struct attribute * myattr[] = {
&my_first.attr,
&my_second.attr,
NULL
};
static ssize_t default_show(struct kobject *kobj, struct attribute *attr,
char *buf)
{
struct my_attr *a = container_of(attr, struct my_attr, attr);
return scnprintf(buf, PAGE_SIZE, "%d\n", a->value);
}
static ssize_t default_store(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t len)
{
struct my_attr *a = container_of(attr, struct my_attr, attr);
sscanf(buf, "%d", &a->value);
return sizeof(int);
}
static struct sysfs_ops myops = {
.show = default_show,
.store = default_store,
};
static struct kobj_type mytype = {
.sysfs_ops = &myops,
.default_attrs = myattr,
};
struct kobject *mykobj;
static int __init sysfsexample_module_init(void)
{
int err = -1;
mykobj = kzalloc(sizeof(*mykobj), GFP_KERNEL);
if (mykobj) {
kobject_init(mykobj, &mytype);
if (kobject_add(mykobj, NULL, "%s", "sysfs_sample")) {
err = -1;
printk("Sysfs creation failed\n");
kobject_put(mykobj);
mykobj = NULL;
}
err = 0;
}
return err;
}
static void __exit sysfsexample_module_exit(void)
{
if (mykobj) {
kobject_put(mykobj);
kfree(mykobj);
}
}
module_init(sysfsexample_module_init);
module_exit(sysfsexample_module_exit);
MODULE_LICENSE("GPL");
가 : 항목을 읽을 때 출력에 사용자에게 버퍼 크기를 할 수 있습니다
은 (here에서 촬영)를 수행이 모듈에서보세요. 이것은 보통 그것을하는 방법입니다. 또한 유닉스 철학을 따라 잡을 수있는 정보 (읽기 및 쓰기)가 사람이 읽을 수있는 형식인지 확인하십시오.업데이트 : 그렉 크로아 - 하트만, 상단 커널 개발자 중 하나에 의해 작성 sysfs를 파일 생성에 대한 this recent interesting article를 참조하십시오.
유용한 정보입니다. default_store()가 len을 반환해야합니다. 그렇지 않으면 쓰기 오류가 발생합니다. – pmod
@pmod : 쓰기 오류 또는 불완전한 쓰기? 당신은 [doc] (https://www.kernel.org/doc/Documentation/filesystems/sysfs.txt)가 store 함수가 반환해야 할 것이기 때문에 여기에'sizeof (int)'를 반환하는 것은 잘못된 것이라고 당신이 맞습니다. 사용 된 바이트 수. 여기서 'sizeof (int)'와는 분명히 관련이 없습니다. 전체 버퍼를 사용할 때, 당신이 제안한 것처럼'len'을 반환하는 것은 간단합니다. – eepp