title: Using the Linux sysfs_notify call
body: <p>I am trying to communicate asynchronously between a kernel driver and a user-space program (I know there are lots of questions here that ask for similar information, but I could find none that deal with sysfs_notify).</p>
  
  <p>I am leaving Vilhelm's edit here, but adding the source to both a simple driver utilizing sysfs and a user-space program to poll it.  The driver works fine (I got most of it from the net; it is missing the credits, but I couldn't find them when I went back to add them).
  Unfortunately, the polling program does not work.  It always returns success immediately.  Interestingly, if I don't perform the two reads prior to the poll, the revents members are set to POLLERR | POLLIN instead of just POLLIN as seen in the program output.</p>
  
  <p>Program output:</p>
  
  <blockquote>
    <p>root@ubuntu:/home/wmulcahy/demo# ./readhello<br>
    triggered<br>
    Attribute file value: 74 (t) [0]<br>
    revents[0]: 00000001<br>
    revents[1]: 00000001<br></p>
  </blockquote>
  
  <p>Here is the driver: hello.c (you can see where I started out...)</p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;linux/module.h&gt;
  #include &lt;linux/kernel.h&gt;
  #include &lt;linux/init.h&gt;
  #include &lt;linux/fs.h&gt;
  #include &lt;linux/slab.h&gt;
  
  struct my_attr {
      struct attribute attr;
      int value;
  };
  
  static struct my_attr notify = {
      .attr.name="notify",
      .attr.mode = 0644,
      .value = 0,
  };
  
  static struct my_attr trigger = {
      .attr.name="trigger",
      .attr.mode = 0644,
      .value = 0,
  };
  
  static struct attribute * myattr[] = {
      &amp;notify.attr,
      &amp;trigger.attr,
      NULL
  };
  
  static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
  {
      struct my_attr *a = container_of(attr, struct my_attr, attr);
      printk( "hello: show called (%s)\n", a-&gt;attr.name );
      return scnprintf(buf, PAGE_SIZE, "%s: %d\n", a-&gt;attr.name, a-&gt;value);
  }
  static struct kobject *mykobj;
  
  static ssize_t store(struct kobject *kobj, struct attribute *attr, const char *buf, size_t len)
  {
      struct my_attr *a = container_of(attr, struct my_attr, attr);
  
      sscanf(buf, "%d", &amp;a-&gt;value);
      notify.value = a-&gt;value;
      printk("sysfs_notify store %s = %d\n", a-&gt;attr.name, a-&gt;value);
      sysfs_notify(mykobj, NULL, "notify");
      return sizeof(int);
  }
  
  static struct sysfs_ops myops = {
      .show = show,
      .store = store,
  };
  
  static struct kobj_type mytype = {
      .sysfs_ops = &amp;myops,
      .default_attrs = myattr,
  };
  
  static struct kobject *mykobj;
  static int __init hello_module_init(void)
  {
      int err = -1;
      printk("Hello: init\n");
      mykobj = kzalloc(sizeof(*mykobj), GFP_KERNEL);
      if (mykobj) {
          kobject_init(mykobj, &amp;mytype);
          if (kobject_add(mykobj, NULL, "%s", "hello")) {
               err = -1;
               printk("Hello: kobject_add() failed\n");
               kobject_put(mykobj);
               mykobj = NULL;
          }
          err = 0;
      }
      return err;
  }
  
  static void __exit hello_module_exit(void)
  {
      if (mykobj) {
          kobject_put(mykobj);
          kfree(mykobj);
      }
      printk("Hello: exit\n");
  }
  
  module_init(hello_module_init);
  module_exit(hello_module_exit);
  MODULE_LICENSE("GPL");
  </code></pre>
  
  <p>And here is the poll program:  readhello.c</p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdint.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;sys/types.h&gt; 
  #include &lt;sys/stat.h&gt; 
  #include &lt;poll.h&gt;
  
  #define TEST_SYSFS_TRIGGER  "/sys/hello/trigger"
  #define TEST_SYSFS_NOTIFY   "/sys/hello/notify"
  
  int main(int argc, char **argv)
  {
      int cnt, notifyFd, triggerFd, rv;
      char attrData[100];
      struct pollfd ufds[2];
  
      // Open a connection to the attribute file.
      if ((notifyFd = open(TEST_SYSFS_NOTIFY, O_RDWR)) &lt; 0)
      {
          perror("Unable to open notify");
          exit(1);
      }
      // Open a connection to the attribute file.
      if ((triggerFd = open(TEST_SYSFS_TRIGGER, O_RDWR)) &lt; 0)
      {
          perror("Unable to open trigger");
          exit(1);
      }
  
      ufds[0].fd = notifyFd;
      ufds[0].events = POLLIN;
      ufds[1].fd = triggerFd;
      ufds[1].events = POLLIN;
  
      // Someone suggested dummy reads before the poll() call
      cnt = read( notifyFd, attrData, 100 );
      cnt = read( triggerFd, attrData, 100 );
      ufds[0].revents = 0;
      ufds[1].revents = 0;
      if (( rv = poll( ufds, 2, 10000)) &lt; 0 )
      {
          perror("poll error");
      }
      else if (rv == 0)
      {
          printf("Timeout occurred!\n");
      }
      else if (ufds[0].revents &amp; POLLIN)
      {
          printf("triggered\n");
          cnt = read( notifyFd, attrData, 1 );
          printf( "Attribute file value: %02X (%c) [%d]\n", attrData[0], attrData[0], cnt );
      }
      printf( "revents[0]: %08X\n", ufds[0].revents );
      printf( "revents[1]: %08X\n", ufds[1].revents );
  
      close( triggerFd );
      close( notifyFd );
  }
  </code></pre>
  
  <p><a href="http://lwn.net/Articles/174660/" rel="noreferrer">Some upcoming sysfs enhancements</a>.</p>
  
  <blockquote>
    <p>Internally, the patch adds a wait queue head to every kobject on the
    system; that queue is inserted into a poll table in response to a
    poll() call. The sysfs code has no way of knowing, however, when the
    value of any given sysfs attribute has changed, so the subsystem
    implementing a pollable attribute must make explicit calls to:</p>
  
  <pre><code>void sysfs_notify(struct kobject *kobj, char *dir, char *attr);
  </code></pre>
  </blockquote>
  
  <p>Thanks,
  Lee</p>