How Does Linux Kernel Creates Sysfs

How does linux kernel creates sysfs?

You should not use that kind of functions directly. You should also avoid the use of kobject (unless you are touching the kernel core).

Usually, sysfs attribute are associated to the device structure. So, when you register a device the sysfs attribute are created.

Take a look at the device structure in device.h line 689. At the end of the structure you will find the following field

const struct attribute_group **groups;

You have to create your own attribute, insert them in an attribute group and assigns your groups to your device before invoking device_register()

If you follow the device_register() function, you will see what it does to create the associated sysfs attribute

How do I make my kernel module's sysfs entry be owned by a non-root user?

Owner's and group's identifiers of a sysfs attribute are determined by the the kobject for which this attribute is created.

Specifically, it is get_ownership field of the struct kobj_type which may contain a callback for set owner's and group's identifiers. The field has following signature:

struct kobj_type {
...
void (*get_ownership)(struct kobject *kobj, kuid_t *uid, kgid_t *gid);
};

When the callback is called, both *uid and *gid contain 0 (which corresponds to the root user). The callback may set these output parameters to other values.



Related Topics



Leave a reply



Submit