PCI device driver exporting information to /proc file system
I was asked this question in an interview. You are writing a PCI driver and you want to export the hardware-related information to the /proc filesystem. The interesting thing is that I searched the driver code and I couldn't find any call related to /proc filesystem though 开发者_如何学编程actually the information is exported. Is it done automatically? What is the mechanism? Can anyone please explain?
Creating entries in the /proc pseudo-filesystem is explained in Linux Device Drivers [3rd ed], chapter 4.
Nowadays you probably want to consider using sysfs
instead; it's covered in LDD3 chapter 14.
One way to do it is for your driver to
implement a function that will get called whenever a process reads the corresponding /proc entry with the following signature:
int (*read_proc)(char *page, char **start, off_t offset, int count, int *eof, void *data);
register your function by passing its pointer to
create_proc_read_entry()
, which accepts the name of the /proc entry as a string among other things:create_proc_read_entry("foobar", 0, NULL, your_read_func_ptr, NULL);
When your driver unloads, it should remove the entry with remove_proc_entry()
精彩评论