title: How to add poll function to the kernel module code?
body: <p>As I know, to inform the user space from kernel space, one way is to using poll. That means kernel driver should provide poll method first.
  Below code is found from internet, and it really works!</p>
  <pre class="lang-c prettyprint-override"><code>#include &lt;linux/module.h&gt;
  #include &lt;linux/kernel.h&gt;
  #include &lt;linux/proc_fs.h&gt;
  #include &lt;linux/string.h&gt;
  #include &lt;linux/vmalloc.h&gt;
  #include &lt;asm/uaccess.h&gt;
   
  MODULE_LICENSE(&quot;GPL&quot;);
  MODULE_DESCRIPTION(&quot;Fortune Cookie Kernel Module&quot;);
  MODULE_AUTHOR(&quot;M. Tim Jones&quot;);
   
  #define MAX_COOKIE_LENGTH       PAGE_SIZE
   
  static struct proc_dir_entry *proc_entry;
  static char *cookie_buf;  // Space for fortune strings
  static int write_index;   // Index to write next fortune
  static int read_index;    // Index to read next fortune
   
  ssize_t fortune_write( struct file *filp, const char __user *buff,
                          unsigned long len, void *data )
  // Refer to: ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
  {
    int space_available = (MAX_COOKIE_LENGTH-write_index);
   
    if (len &gt; space_available) {
      printk(KERN_INFO &quot;fortune: cookie buffer is full!\n&quot;);
      return -ENOSPC;
    }
   
    if (copy_from_user( &amp;cookie_buf[write_index], buff, len )) {
      return -EFAULT;
    }
   
    write_index += len;
    cookie_buf[write_index-1] = 0;
   
    return len;
  }
   
  ssize_t fortune_read(struct file *file, char *buf, size_t count, loff_t *f_pos){
  // Refer to: ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
      int len;
   
      //there's no fortune or a fortune has already been read
      //the *f_pos &gt; 0 hack is needed because `cat /proc/fortune` would otherwise
      //display every thing in the cookie_buf
      if(write_index == 0 || *f_pos &gt; 0){
          return 0;
      }
   
      // cicle through fortunes
      if(read_index &gt;= write_index){
          read_index = 0;
      }
   
      len = sprintf(buf, &quot;%s\n&quot;, &amp;cookie_buf[read_index]);
   
      read_index += len;
      *f_pos += len;
   
      return len;
  }
   
  static const struct file_operations proc_test_fops = {
     .owner        = THIS_MODULE,
  //    .open        = led_proc_open,
     .read        = fortune_read,
  //    .llseek        = seq_lseek,
  //    .release    = single_release,
     .write        = fortune_write,
  //    unsigned int (*poll) (struct file *, struct poll_table_struct *);
  //    int (*fasync) (int, struct file *, int);
  };
   
  int __init init_fortune_module( void )
  {
      int ret = 0;
      cookie_buf = (char *)vmalloc( MAX_COOKIE_LENGTH );
      if (!cookie_buf) {
          ret = -ENOMEM;
      } else {
          memset( cookie_buf, 0, MAX_COOKIE_LENGTH );
  //        proc_entry = create_proc_entry( &quot;fortune&quot;, 0644, NULL );
          proc_entry = proc_create( &quot;fortune&quot;, 0644, NULL, &amp;proc_test_fops );
   
          if (proc_entry == NULL) {
              ret = -ENOMEM;
              vfree(cookie_buf);
              printk(KERN_INFO &quot;fortune: Couldn't create proc entry\n&quot;);
          } else {
              write_index = 0;
              read_index = 0;
              printk(KERN_INFO &quot;fortune: Module loaded.\n&quot;);
          }
      }
   
      return ret;
  }
   
  void __exit exit_fortune_module( void )
  {
  //    remove_proc_entry(&quot;fortune&quot;, &amp;proc_entry);
      proc_remove(proc_entry);
      vfree(cookie_buf);
      printk(KERN_INFO &quot;fortune: Module unloaded.\n&quot;);
  }
   
  module_init( init_fortune_module );
  module_exit( exit_fortune_module );
  </code></pre>
  <p>I can do like this to make it work:</p>
  <pre class="lang-sh prettyprint-override"><code>echo &quot;hello&quot; &gt; /proc/fortune
  </code></pre>
  <p>And then</p>
  <pre class="lang-sh prettyprint-override"><code>cat /proc/fortune
  </code></pre>
  <p>to see the result.</p>
  <p>But how to add poll method to it? I tried some times, but still failed.</p>