handling of many sysfs attributes using a single show function

George White georgewhite584 at yahoo.co.uk
Wed Aug 14 06:29:59 EDT 2013


Hi all,

I am creating a module that has a lot of configuration parameters (of _Bool type). These parameters are set to 0 or 1 using sysfs attributes registered in an attribute group of one kobject. Each attribute show just printfs the var and attrbute store just scanfs the corresponding variable of the same name.

I have just one show and one store function that is shared among all attributes, in an effort to minimise repetition.

My problem is that I don't know how to make this single 'show' function simple enough, so that it does not have to parse the name of each file read in order to find out which variable to work with.

To make this more clear, here is my code right now for the show function of the attribute group:

_Bool system_status = 1;
_Bool confoptionA= 1;
_Bool confoptionB = 0;
...
_Bool confoptionZ = 0;


static ssize_t option_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
size_t count = 0;
        if (strcmp(attr->attr.name, "system_status") == 0){
count = sprintf(buf, "%d\n", system_status);
}
        else if (strcmp(attr->attr.name, "confoptionA") == 0){
count = sprintf(buf, "%lu\n", confoptionA);
}
        else if (strcmp(attr->attr.name, "confoptionB") == 0){
count = sprintf(buf, "%d\n", confoptionB);
}
        else if (strcmp(attr->attr.name, "confoptionC") == 0){
count = sprintf(buf, "%d\n", confoptionC);
}
.....

        return count;
}

This repeats for all my 40something options. 

Surely this is not ideal and there has to be a better way to do it!

Any ideas?

Thanks,
GeorgeW
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20130814/407ebcad/attachment-0001.html 


More information about the Kernelnewbies mailing list