Muni Sekhar
Muni Sekhar

Reputation: 1

mandatory functions of ‘struct file_operations’

I'm trying to understand the concepts behind character device driver in Linux kernel. Basically I want to expose an IOCtl to user mode program. I implemented an 'struct file_operations->unlocked_ioctl' and dummy 'struct file_operations->open, ->close' callbacks(I mean these ->open, ->close callbacks always return true without any other logic). So my doubt is do I really need to keep these API's? I tried removing these ->open->close callbacks in kernel mode driver and still I'm able to access IOCtl function from user mode. I'd like to know what are all the mandatory functions of ‘struct file_operations’ to be exposed in this scenario. Can someone please clarify me on this?

Upvotes: 0

Views: 1264

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 66268

In the struct file_operations object none of callback functions is mandatory.

When VFS (Virtual FileSystem) finds some function to be NULL, it may interpret it as default implementation (e.g. .open() and .close() simply returns 0 by default) or as "for given file, given functionality is not supported" (e.g., absence of .write() and some other fields means that given file doesn't support writing into it).

Fill only those function(s), which are needed to implement a functionality which you want.


The only field in struct file_operations which is recommended to fill, is the owner field. In most cases it should be set to THIS_MODULE:

.owner = THIS_MODULE

Such a way you will protect other callback functions from the module's unloading.

Upvotes: 1

Related Questions