admin管理员组

文章数量:1302384

I am wondering if it is possible to write a Linux module along the following lines:

fops_impl.h

  • Declare the function prototypes of various functions that can be used to initialize members of struct file_operations

fops_impl.c

  • Define the various methods that are used to initialize members of struct file_operations

foo_module.c

  • #include fops_impl.h
  • Define and initialize static instance of struct file_operations
  • Use the symbol of struct file_operations to create and register a char device

This code base builds a module i.e. one can load the module as determined by lsmod. However there is no char device and neither are printk statements in the kernel log.

Questions:

  • Is it necessary for struct file_operations to be static?
  • Is there a working example of a char device where:
  • Device registration is one file
  • Its file operations is defined in yet another file

I am wondering if it is possible to write a Linux module along the following lines:

fops_impl.h

  • Declare the function prototypes of various functions that can be used to initialize members of struct file_operations

fops_impl.c

  • Define the various methods that are used to initialize members of struct file_operations

foo_module.c

  • #include fops_impl.h
  • Define and initialize static instance of struct file_operations
  • Use the symbol of struct file_operations to create and register a char device

This code base builds a module i.e. one can load the module as determined by lsmod. However there is no char device and neither are printk statements in the kernel log.

Questions:

  • Is it necessary for struct file_operations to be static?
  • Is there a working example of a char device where:
  • Device registration is one file
  • Its file operations is defined in yet another file
Share asked Feb 10 at 19:28 saidasasaidasa 114 bronze badges 1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Bot Commented Feb 13 at 23:53
Add a comment  | 

1 Answer 1

Reset to default 0

I was able to build a "hello world" module with following properties:

file_module.c: Implements the following

  • module_init
  • Calls method to register char device
  • module_exit
  • Calls methods to deregister char device

file_chardev.c: Implements the following

  • extern struct file_operations
  • Method to register a char device
  • Method to deregister a char device

file_fops_impl.h: Implements the following

  • Declares prototypes of methods used to initialize members of struct file_operations

file_fops.c: Implements the following

  • include header file: file_fops_impl.h
  • struct file_operation fops = { - - - }

file_fops_impl.c: Implements the following

  • include header file: file_fops_impl.h
  • Methods that are used to initialize members of struct file_operations

My earlier question became moot when I tried again the same code after a reboot. What is strange is the call to module_init() didn't print any kernel log messages even though I had print statement with error mode.

本文标签: file ioCan someone use nonstatic instance of struct fileoperationsStack Overflow