title: I need help in understanding the following kernel module written in C
body: <p>As an assignment I need to complete the following C code in order to produce a kernel module able to act as a memory, but from how it's written I can't understand how it works and why many variables are not used but just declared. I have already tried looking on the teaching material they gave me, and it's even more confusing, plus I can't find on the web a good site where to find documentation about these functions. </p>
  
  <p>The code is the following:</p>
  
  <pre><code>#include &lt;linux/kernel.h&gt;
  #include &lt;linux/module.h&gt;
  #include &lt;linux/init.h&gt;
  #include &lt;linux/fs.h&gt;
  #include &lt;asm/uaccess.h&gt;
  
  #define DEVICE_NAME             "my_device"
  #define MAJOR_DEVICE_NUMBER     60
  #define MINOR_DEVICE_NUMBER     0
  #define BUF_LEN                 1024
  
  static char msg[BUF_LEN];
  static char *msg_ptr; // I'm pretty sure this should become msg_reading_offset
  static int major;
  
  MODULE_AUTHOR("&lt;YOUR NAME&gt;");
  MODULE_LICENSE("GPL");
  
  static ssize_t my_read (
      struct file *filp, char __user *buf,
      size_t length, loff_t *offset);
  static ssize_t my_write (
      struct file *filp, const char __user *buf,
      size_t length, loff_t *offset);
  static int my_open (struct inode *inode,
      struct file *filp);
  static int my_close (struct inode *inode,
      struct file *filp);
  static int __init my_init (void);
  static void __exit my_cleanup (void);
  static struct file_operations fops = {
      .read = my_read,
      .write = my_write,
      .open = my_open,
      .release = my_close,
  };
  
  // I need to implement this function
  static int my_open (struct inode *inode,
     struct file *filp)
  {
      return 0;
  }
  // and this function
  static int my_close (struct inode *inode,
      struct file *filp)
  {
      return 0;
  }
  
  static ssize_t my_read (
      struct file *filp, char __user *buf,
      size_t length, loff_t *offset)
  {
     int nc = 0;
     // if no more "valid" bytes can be read, stop
     if (*msg_reading_offset == 0) return 0;
     // no-negative values allowed
     if (length &lt; 0)
        return -EINVAL;
     // read the whole msg, nothing more
     if (length &gt; strlen(msg)) {
         length = strlen(msg);
     }
     nc = copy_to_user(buf, msg_reading_offset, length);
     /*
     updates the current reading offset pointer so that a
     recursive call due to not original
     full length will get a 0 (nothing to read)
     */
     msg_reading_offset += sizeof(char) * (length-nc);
     // returns the number of REAL bytes read.
     return length - nc;
  }
  
  static ssize_t my_write (
      struct file *filp, const char __user *buf,
      size_t length, loff_t *offset)
  {
     int nc = 0;
     if (length &gt; BUF_LEN)
        return BUF_LEN-length;
     nc = copy_from_user(msg,buf,length);
     msg_ptr = msg;
     return length - nc;
  }
  
  static int __init my_init (void)
  {
     register_chrdev (MAJOR_DEVICE_NUMBER,
        DEVICE_NAME,
        &amp;fops);
  
  }
  module_init(my_init);
  static void __exit my_cleanup (void)
  {
     unregister_chrdev (major, DEVICE_NAME);
  }
  module_exit(my_cleanup);
  </code></pre>
  
  <p>At the moment these are my biggest problems:</p>
  
  <ul>
  <li>Where are all the *inode, *filp variables going? Am I supposed to use them?</li>
  <li>How is this program even working? I know I need to compile it with a makefile I've been give, but then how am I supposed to access these functions? </li>
  <li>Is this supposed to be a real program executed by the kernel or is it just a collections of functions I should use in another C program?</li>
  </ul>
  
  <p>I'm sorry if the questions may seem stupid, but I am at a loss to know how the hell I'm supposed to approach this.</p>