show and store methods of a Kobject Attribute in Linux- Explained

Embedded linux






In this article, we explain the show and store methods of a kobject attribute in linux.

show and store methods are very important when we are dealing with kobject attributes, because they allow us to either view the value of a kobject attribute (in the case of the show method) or update the value of a kobject attribute (in the case of the store method).

The show method shows the value of a kobject attribute.

The store method updates the value of a kobject attribute.

Although practically all attributes will incorporate the show method, so that a user can view its value, not all attributes will incorporate the store method. The store method will only be incorporated for an attribute where a value needs updating. If a value is fixed, for example like the serial number of a device, it will not use of a store method.

So the show method is used to export an attribute value to the user space, while the store method is used to receive a new value from the user space for an attribute.

Let's now explore the show method and store methods, as they appear in code.

Below are the show and store methods, which can be found at /include/linux/device.h

These methods are found under struct device_attribute.


show and store methods of a kobject attribute in linux




show method

So let's first go over the show method.

This the line, ssize_t(*show)(struct device *dev, struct device_attribute *attr,char *buf);

The show method gets invoked when a user space program tires to read the value of the attribute.

This is normally done when a user is using the linux terminal and uses the cat method to read the value of the attribute. This method will be called by the kernel whenever there is an attempt from the user space to read the attribute value.

The value of the attribute is copied to the buffer, which is then used to show to the user space.

Note that buf is not a user level pointer. It is provided by the kernel buffer.

The size of buf is limited to PAGE_SIZE, which for ARM architecture is 4KB (4096 bytes) long. So this size must not be exceeded when copying data to the buf pointer.

To copy data into buf, you can either use sprintf() or scnprintf()

An important distinction to make in linux is that there is a read method and there is a show method.

The read method is used by the user space to read large amounts of data from the driver.

The show method is used for reading a single value data (such as a number) or an array of similar values or data (such as a serial number, e.g. ABCD1234) whose length is less than PAGE_SIZE.

Thus, the show method would not be appropriate for reading an entire file of data. Rather, it would be used for the reading of a single value of data short in length (less than PAGE_SIZE).

The show method should return the number of bytes copied into the buffer or an error code.

So this is the show method.

store method

The next method is the store method.

This is the line, ssize_t (*store)(struct device *dev, struct device_attribute *attr, const char *buf,size_t count);

The store method is invoked when a user tries to modify the value of the sysfs file.

In this method, buf points to user data. This is not a user level buffer, but a kernel level buffer, which holds the user level data.

The count parameter is the amount of user data being passed in.

The maximum amount of data which the buf pointer can carry is limited to PAGE_SIZE.

The data carried by buf is null terminated, which means for a total size of 4096 bytes, the maximum useful data can be 4095 bytes.

The store method should return the number of bytes from the buffer. If the entire buffer has been used, the count argument can be returned.

So this is the store and show methods explained in terms of their use in displaying the value or updating the value of kobject attributes in linux.



Related Resources





HTML Comment Box is loading comments...