title: Problem with chardev.c example from The Linux Kernel Module Programmers Guide
body: <p>I compiled and ran <a href="http://tldp.org/LDP/lkmpg/2.6/html/x569.html" rel="nofollow noreferrer">the chardev.c</a> example from <a href="http://tldp.org/LDP/lkmpg/2.6/html/index.html" rel="nofollow noreferrer">the lkmpg</a> and when writing to the device received an unexpected error:</p>
  
  <pre><code>anon@anon:~/lkmpg$ sudo echo "hi" &gt; /dev/chardev
  bash: /dev/chardev: Permission denied
  </code></pre>
  
  <p>The module write function looks like this:</p>
  
  <pre><code>/*  
   * Called when a process writes to dev file: echo "hi" &gt; /dev/chardev 
   */
  static ssize_t
  device_write(struct file *filp, const char *buff, size_t len, loff_t * off)
  {
      printk(KERN_ALERT "Sorry, this operation isn't supported.\n");
      return -EINVAL;
  }
  </code></pre>
  
  <p>I'm not getting the expected error of invalid operation and the error printed to /var/log/messages.</p>
  
  <p>I can read from the device with no problem, receiving the expected results:</p>
  
  <pre><code>anon@anon:~/lkmpg$ cat /dev/chardev 
  I already told you 6 times Hello world!
  </code></pre>
  
  <p>The device /dev/chardev is created manually using:</p>
  
  <pre><code>sudo mknod /dev/chardev c 252 0
  </code></pre>
  
  <p>What's the deal?</p>
  
  <p><strong>--Edit--</strong></p>
  
  <p>Apparently when I mknod to create the device it ends up with the following permissions: </p>
  
  <pre><code>crw-r--r-- 1 root root 252, 0 2009-10-30 09:27 /dev/chardev. 
  </code></pre>
  
  <p>Once I did <code>sudo chmod a+w /dev/chardev</code> the driver worked as expected.</p>
  
  <p>However, people have said it is not correct to do this.</p>
  
  <p><strong>What is the correct course of action and why?</strong></p>