From cb14f4543a941ff8a22ef2725df86b3b0190a189 Mon Sep 17 00:00:00 2001 From: Kay Sievers Date: Mon, 4 May 2009 04:52:31 +0200 Subject: [PATCH] handle devtmpfs nodes 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 | 1 + rules/rules.d/50-udev-default.rules | 1 - udev/lib/libudev-device.c | 17 +++++++ udev/lib/libudev-monitor.c | 17 ++++--- udev/lib/libudev-private.h | 2 + udev/udev-event.c | 69 +++++++++++++++++++++-------- udev/udev-node.c | 9 +--- 7 files changed, 83 insertions(+), 33 deletions(-) diff --git a/TODO b/TODO index 6e359537..fe7eb170 100644 --- 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) diff --git a/rules/rules.d/50-udev-default.rules b/rules/rules.d/50-udev-default.rules index 5f73866a..782db401 100644 --- a/rules/rules.d/50-udev-default.rules +++ b/rules/rules.d/50-udev-default.rules @@ -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" diff --git a/udev/lib/libudev-device.c b/udev/lib/libudev-device.c index 92b2d18b..8e29efff 100644 --- a/udev/lib/libudev-device.c +++ b/udev/lib/libudev-device.c @@ -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; diff --git a/udev/lib/libudev-monitor.c b/udev/lib/libudev-monitor.c index c87ff743..fc904c4a 100644 --- a/udev/lib/libudev-monitor.c +++ b/udev/lib/libudev-monitor.c @@ -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); diff --git a/udev/lib/libudev-private.h b/udev/lib/libudev-private.h index b283ff4a..8723cd79 100644 --- a/udev/lib/libudev-private.h +++ b/udev/lib/libudev-private.h @@ -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); diff --git a/udev/udev-event.c b/udev/udev-event.c index b7cdbf34..345fb20e 100644 --- a/udev/udev-event.c +++ b/udev/udev-event.c @@ -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}/: */ 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; } diff --git a/udev/udev-node.c b/udev/udev-node.c index 322e0b88..2f9cf4d7 100644 --- a/udev/udev-node.c +++ b/udev/udev-node.c @@ -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)) { -- 2.39.5