chiark / gitweb /
libudev: also import "uevent" file when reading udev database
[elogind.git] / udev / lib / libudev-device.c
index 76ef298a3420aec4b0f9fbb3ae4973c6b83aa02c..4a9c815064cad3478bbba11a5acba9e54ecf4bc4 100644 (file)
@@ -17,8 +17,6 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "config.h"
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <stddef.h>
@@ -67,6 +65,32 @@ static size_t syspath_to_db_path(struct udev_device *udev_device, char *filename
        return util_path_encode(&filename[start], len - start);
 }
 
+static int device_read_uevent_file(struct udev_device *udev_device)
+{
+       char filename[UTIL_PATH_SIZE];
+       FILE *f;
+       char line[UTIL_LINE_SIZE];
+
+       util_strlcpy(filename, udev_device->syspath, sizeof(filename));
+       util_strlcat(filename, "/uevent", sizeof(filename));
+       f = fopen(filename, "r");
+       if (f == NULL)
+               return -1;
+
+       while (fgets(line, sizeof(line), f)) {
+               char *pos;
+
+               pos = strchr(line, '\n');
+               if (pos == NULL)
+                       continue;
+               pos[0] = '\0';
+               device_add_property_from_string(udev_device, line);
+       }
+
+       fclose(f);
+       return 0;
+}
+
 static int device_read_db(struct udev_device *udev_device)
 {
        struct stat stats;
@@ -213,11 +237,32 @@ struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *
        device_set_syspath(udev_device, path);
        info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
 
+       device_read_uevent_file(udev_device);
        if (device_read_db(udev_device) >= 0)
                info(udev, "device %p filled with udev database data\n", udev_device);
        return udev_device;
 }
 
+struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
+{
+       char path[UTIL_PATH_SIZE];
+       const char *type_str;
+
+       if (type == 'b')
+               type_str = "block";
+       else if (type == 'c')
+               type_str = "char";
+       else
+               return NULL;
+
+       snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
+                type_str, major(devnum), minor(devnum));
+       if (util_resolve_sys_link(udev, path, sizeof(path)) < 0)
+               return NULL;
+
+       return udev_device_new_from_syspath(udev, path);
+}
+
 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
 {
        struct udev_device *udev_device_parent = NULL;
@@ -363,7 +408,7 @@ const char *udev_device_get_sysname(struct udev_device *udev_device)
 }
 
 /**
- * udev_device_get_devname:
+ * udev_device_get_devnode:
  * @udev_device: udev device
  *
  * Retrieve the device node file name belonging to the udev device.
@@ -371,7 +416,7 @@ const char *udev_device_get_sysname(struct udev_device *udev_device)
  *
  * Returns: the device node file name of the udev device, or #NULL if no device node exists
  **/
-const char *udev_device_get_devname(struct udev_device *udev_device)
+const char *udev_device_get_devnode(struct udev_device *udev_device)
 {
        if (udev_device == NULL)
                return NULL;