chiark / gitweb /
Merge keymap building in the top-level Makefile.am.
[elogind.git] / libudev / libudev-enumerate.c
index db8c514..e30a406 100644 (file)
@@ -49,6 +49,7 @@ struct udev_enumerate {
        struct udev_list_node sysattr_nomatch_list;
        struct udev_list_node subsystem_match_list;
        struct udev_list_node subsystem_nomatch_list;
+       struct udev_list_node sysname_match_list;
        struct udev_list_node properties_match_list;
        struct udev_list_node devices_list;
        struct syspath *devices;
@@ -76,6 +77,7 @@ struct udev_enumerate *udev_enumerate_new(struct udev *udev)
        udev_list_init(&udev_enumerate->sysattr_nomatch_list);
        udev_list_init(&udev_enumerate->subsystem_match_list);
        udev_list_init(&udev_enumerate->subsystem_nomatch_list);
+       udev_list_init(&udev_enumerate->sysname_match_list);
        udev_list_init(&udev_enumerate->properties_match_list);
        udev_list_init(&udev_enumerate->devices_list);
        return udev_enumerate;
@@ -117,6 +119,7 @@ void udev_enumerate_unref(struct udev_enumerate *udev_enumerate)
        udev_list_cleanup_entries(udev_enumerate->udev, &udev_enumerate->sysattr_nomatch_list);
        udev_list_cleanup_entries(udev_enumerate->udev, &udev_enumerate->subsystem_match_list);
        udev_list_cleanup_entries(udev_enumerate->udev, &udev_enumerate->subsystem_nomatch_list);
+       udev_list_cleanup_entries(udev_enumerate->udev, &udev_enumerate->sysname_match_list);
        udev_list_cleanup_entries(udev_enumerate->udev, &udev_enumerate->properties_match_list);
        udev_list_cleanup_entries(udev_enumerate->udev, &udev_enumerate->devices_list);
        for (i = 0; i < udev_enumerate->devices_cur; i++)
@@ -187,7 +190,8 @@ static int syspath_cmp(const void *p1, const void *p2)
        return ret;
 }
 
-static int devices_delay(struct udev *udev, const char *syspath)
+/* For devices that should be moved to the absolute end of the list */
+static int devices_delay_end(struct udev *udev, const char *syspath)
 {
        static const char *delay_device_list[] = {
                "/block/md",
@@ -207,6 +211,32 @@ static int devices_delay(struct udev *udev, const char *syspath)
        return 0;
 }
 
+/* For devices that should just be moved a little bit later, just
+ * before the point where some common path prefix changes. Returns the
+ * number of characters that make up that common prefix */
+static size_t devices_delay_later(struct udev *udev, const char *syspath)
+{
+       const char *c;
+
+       /* For sound cards the control device must be enumerated last
+        * to make sure it's the final device node that gets ACLs
+        * applied. Applications rely on this fact and use ACL changes
+        * on the control node as an indicator that the ACL change of
+        * the entire sound card completed. The kernel makes this
+        * guarantee when creating those devices, and hence we should
+        * too when enumerating them. */
+
+       if ((c = strstr(syspath, "/sound/card"))) {
+               c += 11;
+               c += strcspn(c, "/");
+
+               if (strncmp(c, "/controlC", 9) == 0)
+                       return c - syspath + 1;
+       }
+
+       return 0;
+}
+
 /**
  * udev_enumerate_get_list_entry:
  * @udev_enumerate: context
@@ -220,7 +250,7 @@ struct udev_list_entry *udev_enumerate_get_list_entry(struct udev_enumerate *ude
        if (!udev_enumerate->devices_uptodate) {
                unsigned int i;
                unsigned int max;
-               struct syspath *prev = NULL;
+               struct syspath *prev = NULL, *move_later = NULL;
 
                udev_list_cleanup_entries(udev_enumerate->udev, &udev_enumerate->devices_list);
                qsort(udev_enumerate->devices, udev_enumerate->devices_cur, sizeof(struct syspath), syspath_cmp);
@@ -228,6 +258,7 @@ struct udev_list_entry *udev_enumerate_get_list_entry(struct udev_enumerate *ude
                max = udev_enumerate->devices_cur;
                for (i = 0; i < max; i++) {
                        struct syspath *entry = &udev_enumerate->devices[i];
+                       size_t move_later_prefix = 0;
 
                        /* skip duplicated entries */
                        if (prev != NULL &&
@@ -237,14 +268,39 @@ struct udev_list_entry *udev_enumerate_get_list_entry(struct udev_enumerate *ude
                        prev = entry;
 
                        /* skip to be delayed devices, and add them to the end of the list */
-                       if (devices_delay(udev_enumerate->udev, entry->syspath)) {
+                       if (devices_delay_end(udev_enumerate->udev, entry->syspath)) {
                                syspath_add(udev_enumerate, entry->syspath);
                                continue;
                        }
 
+                       /* skip to be delayed devices, and move the to
+                        * the point where the prefix changes. We can
+                        * only move one item at a time. */
+                       if (!move_later) {
+                               move_later_prefix = devices_delay_later(udev_enumerate->udev, entry->syspath);
+
+                               if (move_later_prefix > 0) {
+                                       move_later = entry;
+                                       continue;
+                               }
+                       }
+
+                       if (move_later &&
+                           strncmp(entry->syspath, move_later->syspath, move_later_prefix) != 0) {
+
+                               udev_list_entry_add(udev_enumerate->udev, &udev_enumerate->devices_list,
+                                           move_later->syspath, NULL, 0, 0);
+                               move_later = NULL;
+                       }
+
                        udev_list_entry_add(udev_enumerate->udev, &udev_enumerate->devices_list,
                                            entry->syspath, NULL, 0, 0);
                }
+
+               if (move_later)
+                       udev_list_entry_add(udev_enumerate->udev, &udev_enumerate->devices_list,
+                                           move_later->syspath, NULL, 0, 0);
+
                /* add and cleanup delayed devices from end of list */
                for (i = max; i < udev_enumerate->devices_cur; i++) {
                        struct syspath *entry = &udev_enumerate->devices[i];
@@ -383,6 +439,25 @@ int udev_enumerate_add_match_property(struct udev_enumerate *udev_enumerate, con
        return 0;
 }
 
+/**
+ * udev_enumerate_add_match_sysname:
+ * @udev_enumerate: context
+ * @sysname: filter for the name of the device to include in the list
+ *
+ * Returns: 0 on success, otherwise a negative error value.
+ */
+int udev_enumerate_add_match_sysname(struct udev_enumerate *udev_enumerate, const char *sysname)
+{
+       if (udev_enumerate == NULL)
+               return -EINVAL;
+       if (sysname == NULL)
+               return 0;
+       if (udev_list_entry_add(udev_enumerate_get_udev(udev_enumerate),
+                               &udev_enumerate->sysname_match_list, sysname, NULL, 1, 0) == NULL)
+               return -ENOMEM;
+       return 0;
+}
+
 static int match_sysattr(struct udev_enumerate *udev_enumerate, const char *syspath)
 {
        struct udev *udev = udev_enumerate_get_udev(udev_enumerate);
@@ -454,6 +529,21 @@ out:
        return match;
 }
 
+static int match_sysname(struct udev_enumerate *udev_enumerate, const char *sysname)
+{
+       struct udev_list_entry *list_entry;
+
+       if (udev_list_get_entry(&udev_enumerate->sysname_match_list) == NULL)
+               return 1;
+
+       udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->sysname_match_list)) {
+               if (fnmatch(udev_list_entry_get_name(list_entry), sysname, 0) != 0)
+                       continue;
+               return 1;
+       }
+       return 0;
+}
+
 static int scan_dir_and_add_devices(struct udev_enumerate *udev_enumerate,
                                    const char *basedir, const char *subdir1, const char *subdir2)
 {
@@ -480,6 +570,9 @@ static int scan_dir_and_add_devices(struct udev_enumerate *udev_enumerate,
 
                if (dent->d_name[0] == '.')
                        continue;
+               if (!match_sysname(udev_enumerate, dent->d_name))
+                       continue;
+
                util_strscpyl(syspath, sizeof(syspath), path, "/", dent->d_name, NULL);
                if (lstat(syspath, &statbuf) != 0)
                        continue;