title: Segmentation fault in linux driver
body: <p>I'm trying to write a linux driver. The kernel version is 2.4.18 and the distribution is Red Hat linux 8.0.</p>
  
  <p>The code of my driver is:</p>
  
  <pre><code>#define LINUX
  
  #include &lt;linux/kernel.h&gt; /* We're doing kernel work */
  #include &lt;linux/module.h&gt; /* Specifically, a module */
  #include &lt;linux/fs.h&gt;
  #include &lt;asm-i386/semaphore.h&gt;
  #include "rng.h"
  #include &lt;linux/random.h&gt;
  #include &lt;linux/slab.h&gt;
  
  #define DEVICE_NAME "rng"
  #define BUF_LEN 80
  
  static int major;
  int init_module();
  void cleanup_module();
  static int device_open(struct inode *, struct file *);
  static int device_release(struct inode *, struct file *);
  
  struct file_operations my_fops = {
    open: device_open,
    release: device_release,
  };
  
  
  /* Init and Cleanup */
  
  int init_module() {
     SET_MODULE_OWNER(&amp;my_fops);
     major = register_chrdev(0, DEVICE_NAME, &amp;my_fops);
     return 0;
  }
  
  void cleanup_module() {
  
     int ret = unregister_chrdev(major, DEVICE_NAME);
     if (ret &lt; 0)
         printk("Error in unregister_chrdev: %d\n", ret);
  
  }
  
  static int device_open(struct inode *inode, struct file *file) {
     file-&gt;f_op=&amp;my_fops;
     return 1;
  }
  
  static int device_release(struct inode *inode, struct file *file) {
     return 0;
  }
  </code></pre>
  
  <p>And the code I'm using in order to test my driver is:</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;errno.h&gt; 
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  #include &lt;fcntl.h&gt;
  
  int openTest() {
      int game1 = open("/dev/game1", O_RDONLY); // SEGMENTATION FAULT
      int retValue=1;
  
      close(game1);
      return retValue;
  }
  
  int main() {
      int res;
      if (openTest() &lt; 1) {
         fprintf(stderr, "open didnt work\n");
      return -1;
      }
      fprintf(stderr, "everything works :)\n");
      return 0;
  }
  </code></pre>
  
  <p>In the code above, I'm getting a segmentation fault when I'm trying to open the device. Can somebody explain to me why I'm getting this segmentation fault? I really don't understand.</p>
  
  <p>Thanks a lot!</p>