Following is a simple sysfs code that creates the kobject test_test and adds a new attribute attr1 to it.
Code:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>
static ssize_t attr1_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
printk("goldeneagle: attr1_show\n");
return 0;
}
static ssize_t attr1_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t cnt)
{
printk("goldeneagle: attr1_store\n");
return 0;
}
struct kobject *kobj;
const struct kobj_attribute kobj_attr =
__ATTR(attr1, 0666, attr1_show, attr1_store);
static int test_char_init(void)
{
int ret = 0;
printk("\ngoldeneagle:%s\n", __func__);
kobj = kobject_create_and_add("test_test", kernel_kobj);
if (!kobj) {
printk("goldeneagle: kobj could not be created and added\n");
ret = -ENOMEM;
goto fail;
}
ret = sysfs_create_file(kobj, &kobj_attr.attr);
if (ret) {
printk("goldeneagle: Failed to add new attribute\n");
goto err;
}
return 0;
err:
sysfs_remove_file(kobj, &kobj_attr.attr);
kobject_put(kobj);
fail:
return ret;
}
static void test_char_exit(void)
{
printk("\ngoldeneagle:%s\n", __func__);
sysfs_remove_file(kobj, &kobj_attr.attr);
kobject_put(kobj);
}
module_init(test_char_init);
module_exit(test_char_exit);
MODULE_LICENSE("GPL");
When I try to write something to attr1 via "echo 0 > attr1"
I see attr1_store getting called infinite no. of time.
Please suggest what might be causing this issue !!!