[Pommed-commits] r416 - in trunk: . pommed

jblache at alioth.debian.org jblache at alioth.debian.org
Thu Dec 6 20:29:09 UTC 2007


Author: jblache
Date: 2007-12-06 20:29:09 +0000 (Thu, 06 Dec 2007)
New Revision: 416

Modified:
   trunk/ChangeLog
   trunk/pommed/evdev.c
Log:
Handle several inotify events at once.

Also handle inotify events with len > sizeof(struct inotify_event), otherwise we end up busy-looping.


Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2007-12-06 19:59:22 UTC (rev 415)
+++ trunk/ChangeLog	2007-12-06 20:29:09 UTC (rev 416)
@@ -8,6 +8,10 @@
 	- gpomme: add video switch support.
 	- pommed: add support for LCD backlight control on the Intel 965GM
 	found in the MacBook3,1.
+	- pommed: rework the inotify code to handle several events at once
+	and to handle events with long filenames (longer than
+	sizeof(struct inotify_event)); pommed could enter a busy-loop when
+	receiving an inotify event with ie->len > sizeof(struct inotify_event).
 
 version 1.12:
 	- pommed: do not expect at least 3 evdevs at startup; there are

Modified: trunk/pommed/evdev.c
===================================================================
--- trunk/pommed/evdev.c	2007-12-06 19:59:22 UTC (rev 415)
+++ trunk/pommed/evdev.c	2007-12-06 20:29:09 UTC (rev 416)
@@ -294,52 +294,83 @@
 {
   int ret;
   int fd;
+  int qsize;
 
-  struct inotify_event ie[2];
+  struct inotify_event *all_ie;
+  struct inotify_event *ie;
   char evdev[32];
 
-  ret = read(ifd, ie, sizeof(ie));
+  /* Determine the size of the inotify queue */
+  ret = ioctl(ifd, FIONREAD, &qsize);
   if (ret < 0)
     {
-      logdebug("inotify read failed: %s\n", strerror(errno));
+      logdebug("Could not determine inotify queue size: %s\n", strerror(errno));
 
       return;
     }
 
-  /* ie[0] contains the inotify event information
-   * the memory space for ie[1] contains the name of the file
-   * see the inotify documentation
-   */
-
-  if ((ie[0].len == 0) || (ie[0].name == NULL))
+  all_ie = (struct inotify_event *) malloc(qsize);
+  if (all_ie == NULL)
     {
-      logdebug("inotify event with no name\n");
+      logmsg(LOG_ERR, "Could not allocate %d bytes for inotify events");
 
       return;
     }
 
-  logdebug("Found new event device %s/%s\n", EVDEV_DIR, ie[0].name);
-
-  if (strncmp("event", ie[0].name, 5))
+  ret = read(ifd, all_ie, qsize);
+  if (ret < 0)
     {
-      logdebug("Discarding %s/%s\n", EVDEV_DIR, ie[0].name);
+      logmsg(LOG_WARNING, "inotify read failed: %s", strerror(errno));
 
+      free(all_ie);
       return;
     }
 
-  ret = snprintf(evdev, 32, "%s/%s", EVDEV_DIR, ie[0].name);
+  /* ioctl(FIONREAD) returns the number of bytes, now we need the number of elements */
+  qsize /= sizeof(struct inotify_event);
 
-  if ((ret <= 0) || (ret > 31))
-    return;
+  /* Loop through all the events we got */
+  for (ie = all_ie; (ie - all_ie) < qsize; ie += (1 + (ie->len / sizeof(struct inotify_event))))
+    {
+      /* ie[0] contains the inotify event information
+       * the memory space for ie[1+] contains the name of the file
+       * see the inotify documentation
+       */
 
-  fd = open(evdev, O_RDWR);
-  if (fd < 0)
-    {
-      if (errno != ENOENT)
-	logmsg(LOG_WARNING, "Could not open %s: %s", evdev, strerror(errno));
+      if ((ie->len == 0) || (ie->name == NULL))
+	{
+	  logdebug("inotify event with no name\n");
+
+	  continue;
+	}
+
+      logdebug("Found new event device %s/%s\n", EVDEV_DIR, ie->name);
+
+      if (strncmp("event", ie->name, 5))
+	{
+	  logdebug("Discarding %s/%s\n", EVDEV_DIR, ie->name);
+
+	  continue;
+	}
+
+      ret = snprintf(evdev, sizeof(evdev), "%s/%s", EVDEV_DIR, ie->name);
+
+      if ((ret <= 0) || (ret >= sizeof(evdev)))
+	continue;
+
+      fd = open(evdev, O_RDWR);
+      if (fd < 0)
+	{
+	  if (errno != ENOENT)
+	    logmsg(LOG_WARNING, "Could not open %s: %s", evdev, strerror(errno));
+
+	  continue;
+	}
+
+      evdev_try_add(fd);
     }
 
-  evdev_try_add(fd);
+  free(all_ie);
 }
 
 




More information about the Pommed-commits mailing list