chiark / gitweb /
handle devtmpfs nodes
authorKay Sievers <kay.sievers@vrfy.org>
Mon, 4 May 2009 02:52:31 +0000 (04:52 +0200)
committerKay Sievers <kay.sievers@vrfy.org>
Mon, 4 May 2009 02:52:31 +0000 (04:52 +0200)
UDev follows the kernel given name, and re-uses the kernel created
device node. If the kernel and spcecified udev rules disagree, the
udev specified node node is created and the kernel-created on is
deleted.

TODO
rules/rules.d/50-udev-default.rules
udev/lib/libudev-device.c
udev/lib/libudev-monitor.c
udev/lib/libudev-private.h
udev/udev-event.c
udev/udev-node.c

diff --git a/TODO b/TODO
index 6e35953..fe7eb17 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,4 +1,5 @@
 
+  o add tests for kernel provided DEVNAME logic
   o drop modprobe floppy alias (SUSE), it will be in the module (2.6.30)
   o remove MMC rules, they got a modalias now (2.6.30)
   o add scsi:t-0x09* to "ch" and remove modprobe rule (2.6.30)
index 5f73866..782db40 100644 (file)
@@ -13,7 +13,6 @@ KERNEL=="vcs|vcs[0-9]*|vcsa|vcsa[0-9]*", GROUP="tty"
 
 # serial
 KERNEL=="tty[A-Z]*[0-9]|pppox[0-9]*|ircomm[0-9]*|noz[0-9]*|rfcomm[0-9]*", GROUP="dialout"
-KERNEL=="ppp",                 MODE="0600"
 KERNEL=="mwave",               NAME="modems/mwave", GROUP="dialout"
 KERNEL=="hvc*|hvsi*",          GROUP="dialout"
 
index 92b2d18..8e29eff 100644 (file)
@@ -37,6 +37,7 @@ struct udev_device {
        char *action;
        char *devpath_old;
        char *physdevpath;
+       char *knodename;
        char **envp;
        char *monitor_buf;
        size_t monitor_buf_len;
@@ -208,6 +209,8 @@ int udev_device_read_uevent_file(struct udev_device *udev_device)
                        maj = strtoull(&line[6], NULL, 10);
                else if (strncmp(line, "MINOR=", 6) == 0)
                        min = strtoull(&line[6], NULL, 10);
+               else if (strncmp(line, "DEVNAME=", 8) == 0)
+                       udev_device_set_knodename(udev_device, &line[8]);
 
                udev_device_add_property_from_string(udev_device, line);
        }
@@ -621,6 +624,7 @@ void udev_device_unref(struct udev_device *udev_device)
        free(udev_device->action);
        free(udev_device->driver);
        free(udev_device->devpath_old);
+       free(udev_device->knodename);
        free(udev_device->physdevpath);
        udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
        free(udev_device->envp);
@@ -1175,6 +1179,19 @@ int udev_device_set_devpath_old(struct udev_device *udev_device, const char *dev
        return 0;
 }
 
+const char *udev_device_get_knodename(struct udev_device *udev_device)
+{
+       return udev_device->knodename;
+}
+
+int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
+{
+       udev_device->knodename = strdup(knodename);
+       if (udev_device->knodename == NULL)
+               return -ENOMEM;
+       return 0;
+}
+
 const char *udev_device_get_physdevpath(struct udev_device *udev_device)
 {
        return udev_device->physdevpath;
index c87ff74..fc904c4 100644 (file)
@@ -445,6 +445,7 @@ struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monito
        int action_set = 0;
        int maj = 0;
        int min = 0;
+       int is_kernel = 0;
 
 retry:
        if (udev_monitor == NULL)
@@ -480,9 +481,12 @@ retry:
                        info(udev_monitor->udev, "unicast netlink message ignored\n");
                        return NULL;
                }
-               if ((snl.nl_groups == UDEV_MONITOR_KERNEL) && (snl.nl_pid > 0)) {
-                       info(udev_monitor->udev, "multicast kernel netlink message from pid %d ignored\n", snl.nl_pid);
-                       return NULL;
+               if (snl.nl_groups == UDEV_MONITOR_KERNEL) {
+                       if (snl.nl_pid > 0) {
+                               info(udev_monitor->udev, "multicast kernel netlink message from pid %d ignored\n", snl.nl_pid);
+                               return NULL;
+                       }
+                       is_kernel = 1;
                }
        }
 
@@ -551,7 +555,10 @@ retry:
                } else if (strncmp(key, "DEVTYPE=", 8) == 0) {
                        udev_device_set_devtype(udev_device, &key[8]);
                } else if (strncmp(key, "DEVNAME=", 8) == 0) {
-                       udev_device_set_devnode(udev_device, &key[8]);
+                       if (is_kernel)
+                               udev_device_set_knodename(udev_device, &key[8]);
+                       else
+                               udev_device_set_devnode(udev_device, &key[8]);
                } else if (strncmp(key, "DEVLINKS=", 9) == 0) {
                        char devlinks[UTIL_PATH_SIZE];
                        char *slink;
@@ -586,7 +593,7 @@ retry:
                } else if (strncmp(key, "TIMEOUT=", 8) == 0) {
                        udev_device_set_timeout(udev_device, strtoull(&key[8], NULL, 10));
                } else if (strncmp(key, "PHYSDEV", 7) == 0) {
-                       /* skip deprecated values */
+                       /* suppress deprecated values */
                        continue;
                } else {
                        udev_device_add_property_from_string(udev_device, key);
index b283ff4..8723cd7 100644 (file)
@@ -62,6 +62,8 @@ int udev_device_set_action(struct udev_device *udev_device, const char *action);
 int udev_device_set_driver(struct udev_device *udev_device, const char *driver);
 const char *udev_device_get_devpath_old(struct udev_device *udev_device);
 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old);
+const char *udev_device_get_knodename(struct udev_device *udev_device);
+int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename);
 const char *udev_device_get_physdevpath(struct udev_device *udev_device);
 int udev_device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath);
 int udev_device_get_timeout(struct udev_device *udev_device);
index b7cdbf3..345fb20 100644 (file)
@@ -354,12 +354,24 @@ found:
 
                                if (event->tmp_node != NULL) {
                                        util_strlcat(string, event->tmp_node, maxsize);
-                                       dbg(event->udev, "return existing temporary node\n");
+                                       dbg(event->udev, "tempnode: return earlier created one\n");
                                        break;
                                }
                                devnum = udev_device_get_devnum(dev);
-                               if (major(udev_device_get_devnum(dev) == 0))
+                               if (major(devnum) == 0)
                                        break;
+                               /* lookup kernel provided node */
+                               if (udev_device_get_knodename(dev) != NULL) {
+                                       util_strlcpy(filename, udev_get_dev_path(event->udev), sizeof(filename));
+                                       util_strlcat(filename, "/", sizeof(filename));
+                                       util_strlcat(filename, udev_device_get_knodename(dev), sizeof(filename));
+                                       if (stat(filename, &statbuf) == 0 && statbuf.st_rdev == devnum) {
+                                               util_strlcat(string, filename, maxsize);
+                                               dbg(event->udev, "tempnode: return kernel node\n");
+                                               break;
+                                       }
+                               }
+                               /* lookup /dev/{char,block}/<maj>:<min> */
                                if (strcmp(udev_device_get_subsystem(dev), "block") == 0)
                                        devtype = "block";
                                else
@@ -370,10 +382,11 @@ found:
                                         minor(udev_device_get_devnum(dev)));
                                if (stat(filename, &statbuf) == 0 && statbuf.st_rdev == devnum) {
                                        util_strlcat(string, filename, maxsize);
-                                       dbg(event->udev, "return existing temporary node\n");
+                                       dbg(event->udev, "tempnode: return maj:min node\n");
                                        break;
                                }
-                               dbg(event->udev, "create temporary node\n");
+                               /* create temporary node */
+                               dbg(event->udev, "tempnode: create temp node\n");
                                asprintf(&event->tmp_node, "%s/.tmp-%s-%u:%u",
                                         udev_get_dev_path(event->udev), devtype,
                                         major(udev_device_get_devnum(dev)),
@@ -547,6 +560,7 @@ int udev_event_execute_rules(struct udev_event *event, struct udev_rules *rules)
            (strcmp(udev_device_get_action(dev), "add") == 0 || strcmp(udev_device_get_action(dev), "change") == 0)) {
                char filename[UTIL_PATH_SIZE];
                struct udev_device *dev_old;
+               int delete_kdevnode = 0;
 
                dbg(event->udev, "device node add '%s'\n", udev_device_get_devpath(dev));
 
@@ -562,7 +576,7 @@ int udev_event_execute_rules(struct udev_event *event, struct udev_rules *rules)
 
                udev_rules_apply_to_event(rules, event);
                if (event->tmp_node != NULL) {
-                       dbg(event->udev, "removing temporary device node\n");
+                       dbg(event->udev, "cleanup temporary device node\n");
                        util_unlink_secure(event->udev, event->tmp_node);
                        free(event->tmp_node);
                        event->tmp_node = NULL;
@@ -570,33 +584,38 @@ int udev_event_execute_rules(struct udev_event *event, struct udev_rules *rules)
 
                if (event->ignore_device) {
                        info(event->udev, "device event will be ignored\n");
+                       delete_kdevnode = 1;
                        goto exit_add;
                }
 
                if (event->name != NULL && event->name[0] == '\0') {
                        info(event->udev, "device node creation supressed\n");
+                       delete_kdevnode = 1;
                        goto exit_add;
                }
 
+               /* if rule given name disagrees with kernel node name, delete kernel node */
+               if (event->name != NULL && udev_device_get_knodename(dev) != NULL) {
+                       if (strcmp(event->name, udev_device_get_knodename(dev)) != 0)
+                               delete_kdevnode = 1;
+               }
+
+               /* no rule, use kernel provided name */
                if (event->name == NULL) {
-                       const char *devname;
-
-                       devname = udev_device_get_property_value(event->dev, "DEVNAME");
-                       if (devname != NULL) {
-                               info(event->udev, "no node name set, will use "
-                                    "kernel supplied name '%s'\n", devname);
-                               event->name = strdup(devname);
-                               if (event->name == NULL)
-                                       goto exit_add;
+                       if (udev_device_get_knodename(dev) != NULL) {
+                               event->name = strdup(udev_device_get_knodename(dev));
+                               info(event->udev, "no node name set, will use kernel supplied name '%s'\n", event->name);
+                       } else {
+                               event->name = strdup(udev_device_get_sysname(event->dev));
+                               info(event->udev, "no node name set, will use device name '%s'\n", event->name);
                        }
                }
+
+               /* something went wrong */
                if (event->name == NULL) {
-                       info(event->udev, "no node name set, will use device name '%s'\n",
-                            udev_device_get_sysname(event->dev));
-                       event->name = strdup(udev_device_get_sysname(event->dev));
-               }
-               if (event->name == NULL)
+                       err(event->udev, "no node name for '%s'\n", udev_device_get_sysname(event->dev));
                        goto exit_add;
+               }
 
                /* set device node name */
                util_strlcpy(filename, udev_get_dev_path(event->udev), sizeof(filename));
@@ -614,6 +633,18 @@ int udev_event_execute_rules(struct udev_event *event, struct udev_rules *rules)
                /* create new node and symlinks */
                err = udev_node_add(dev, event->mode, event->uid, event->gid);
 exit_add:
+               if (delete_kdevnode && udev_device_get_knodename(dev) != NULL) {
+                       struct stat stats;
+
+                       util_strlcpy(filename, udev_get_dev_path(event->udev), sizeof(filename));
+                       util_strlcat(filename, "/", sizeof(filename));
+                       util_strlcat(filename, udev_device_get_knodename(dev), sizeof(filename));
+                       if (stat(filename, &stats) == 0 && stats.st_rdev == udev_device_get_devnum(dev)) {
+                               unlink(filename);
+                               util_delete_path(event->udev, filename);
+                               info(event->udev, "removed kernel created node '%s'\n", filename);
+                       }
+               }
                udev_device_unref(dev_old);
                goto exit;
        }
index 322e0b8..2f9cf4d 100644 (file)
@@ -68,7 +68,6 @@ static int name_index(struct udev *udev, const char *devpath, const char *name,
 int udev_node_mknod(struct udev_device *dev, const char *file, dev_t devnum, mode_t mode, uid_t uid, gid_t gid)
 {
        struct udev *udev = udev_device_get_udev(dev);
-       char filename[UTIL_PATH_SIZE];
        struct stat stats;
        int preserve = 0;
        int err = 0;
@@ -81,14 +80,8 @@ int udev_node_mknod(struct udev_device *dev, const char *file, dev_t devnum, mod
        else
                mode |= S_IFCHR;
 
-       if (file == NULL) {
+       if (file == NULL)
                file = udev_device_get_devnode(dev);
-       } else if (file[0] != '/') {
-               util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
-               util_strlcat(filename, "/", sizeof(filename));
-               util_strlcat(filename, file, sizeof(filename));
-               file = filename;
-       }
 
        if (lstat(file, &stats) == 0) {
                if (((stats.st_mode & S_IFMT) == (mode & S_IFMT)) && (stats.st_rdev == devnum)) {