char *action;
char *devpath_old;
char *physdevpath;
+ char *knodename;
char **envp;
char *monitor_buf;
size_t monitor_buf_len;
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);
}
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);
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;
int action_set = 0;
int maj = 0;
int min = 0;
+ int is_kernel = 0;
retry:
if (udev_monitor == NULL)
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;
}
}
} 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;
} 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);
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
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)),
(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));
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;
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));
/* 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;
}