HaloKiller
HaloKiller

Reputation: 136

Linux: Using module functions

Let's say I have a module which has this function my_open:

int my_open( struct inode *inode, struct file *filp ) {

   filp->private_data = //allocate private data 

   if( filp->f_mode & FMODE_READ )
            //handle read opening
   if( filp->f_mode & FMODE_WRITE )
            //handle write opening

   if (MINOR( inode->i_rdev )==2){
      filp->f_op = &my_fops2;
   }

   return 0;
   }

how do I use this function from the shell/terminal?

Upvotes: 1

Views: 168

Answers (1)

This is either the open function for a device driver, or it’s a sheep in wolf’s clothing [sic].  In the unlikely event that this is ordinary, vanilla user-level code, compile it into an executable and use that.  But if it is a device driver’s open function,

  • First of all, determine whether it is compiled/linked into the currently running kernel.  If it isn’t, make it so (compile/link it into the kernel, and reboot).
    • Alternatively, you may have the ability to load it into the kernel dynamically.  Exactly how to do that depends on your particular operating system and is out of scope for this question.
  • Find out whether it is a block device or a character device, and what its major device number is.  I can’t tell you specifically how to do that, either; consult your local resources.
  • OK, let’s suppose that it is a character device with major number 42.  Look through /dev (with ls -l) for entries that begin with c (for “character”) and contain 42, something where the size should be, like this:

    drwxr-xr-x  1 root    root        512 Feb 10  2015 .
    drwxr-xr-x  1 root    root       1024 Feb 10  2015 ..
    crw-rw-rw-  1 root    root    42,   0 Aug 15 18:31 foo
    crw-rw-rw-  1 root    root    42,   2 Aug 15 18:31 fu
    crw-rw-rw-  1 root    root    42,  17 Aug 15 18:31 fubar
    

    If you can’t find any, create some.  See man mknod for details.  You should probably create one with minor device number 2 and at least one with a different number (because the code treats 2 as a special case).

  • Do to the /dev/whatever files whatever you want to, based on the intended function of the driver.  (Determining the intended function of the driver is out of scope.)  For instance, you might try things like

    od -cb /dev/foo
    echo "Hello, world." > /dev/fu
    
  • Naturally, if it’s a block device, replace c in the above instructions with b.

Upvotes: 3

Related Questions