void udev_node_add(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid) { struct udev *udev = udev_device_get_udev(dev); char filename[UTIL_PATH_SIZE]; struct udev_list_entry *list_entry; int err = 0; info(udev, "handling device node '%s', devnum=%s, mode=%#o, uid=%d, gid=%d\n", udev_device_get_devnode(dev), udev_device_get_id_filename(dev), mode, uid, gid); if (node_fixup(dev, mode, uid, gid) < 0) return; /* always add /dev/{block,char}/$major:$minor */ snprintf(filename, sizeof(filename), "%s/%s/%u:%u", udev_get_dev_path(udev), strcmp(udev_device_get_subsystem(dev), "block") == 0 ? "block" : "char", major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev))); node_symlink(udev, udev_device_get_devnode(dev), filename); /* create/update symlinks, add symlinks to name index */ udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) { if (udev_list_entry_get_num(list_entry)) /* simple unmanaged link name */ node_symlink(udev, udev_device_get_devnode(dev), udev_list_entry_get_name(list_entry)); else link_update(dev, udev_list_entry_get_name(list_entry), 1); } }
/* manage "stack of names" with possibly specified device priorities */ static void link_update(struct udev_device *dev, const char *slink, bool add) { struct udev *udev = udev_device_get_udev(dev); char name_enc[UTIL_PATH_SIZE]; char filename[UTIL_PATH_SIZE * 2]; char dirname[UTIL_PATH_SIZE]; const char *target; char buf[UTIL_PATH_SIZE]; dbg(udev, "update symlink '%s' of '%s'\n", slink, udev_device_get_syspath(dev)); util_path_encode(&slink[strlen(udev_get_dev_path(udev))+1], name_enc, sizeof(name_enc)); util_strscpyl(dirname, sizeof(dirname), udev_get_run_path(udev), "/links/", name_enc, NULL); util_strscpyl(filename, sizeof(filename), dirname, "/", udev_device_get_id_filename(dev), NULL); if (!add) { dbg(udev, "removing index: '%s'\n", filename); if (unlink(filename) == 0) rmdir(dirname); } target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf)); if (target == NULL) { info(udev, "no reference left, remove '%s'\n", slink); if (unlink(slink) == 0) util_delete_path(udev, slink); } else { info(udev, "creating link '%s' to '%s'\n", slink, target); node_symlink(udev, target, slink); } if (add) { int err; dbg(udev, "creating index: '%s'\n", filename); do { int fd; err = util_create_path(udev, filename); if (err != 0 && err != -ENOENT) break; fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444); if (fd >= 0) close(fd); else err = -errno; } while (err == -ENOENT); } }
/* manage "stack of names" with possibly specified device priorities */ static void link_update(struct udev_device *dev, const char *slink, bool add) { struct udev *udev = udev_device_get_udev(dev); char name_enc[UTIL_PATH_SIZE]; char filename[UTIL_PATH_SIZE * 2]; char dirname[UTIL_PATH_SIZE]; const char *target; char buf[UTIL_PATH_SIZE]; dbg(udev, "update symlink '%s' of '%s'\n", slink, udev_device_get_syspath(dev)); util_path_encode(&slink[strlen(udev_get_dev_path(udev))+1], name_enc, sizeof(name_enc)); snprintf(dirname, sizeof(dirname), "%s/.udev/links/%s", udev_get_dev_path(udev), name_enc); snprintf(filename, sizeof(filename), "%s/%c%u:%u", dirname, strcmp(udev_device_get_subsystem(dev), "block") == 0 ? 'b' : 'c', major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev))); if (!add) { dbg(udev, "removing index: '%s'\n", filename); unlink(filename); util_delete_path(udev, filename); } target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf)); if (target == NULL) { info(udev, "no reference left, remove '%s'\n", slink); unlink(slink); util_delete_path(udev, slink); } else { info(udev, "creating link '%s' to '%s'\n", slink, target); node_symlink(udev, target, slink); } if (add) { int err; dbg(udev, "creating index: '%s'\n", filename); do { err = util_create_path(udev, filename); if (err != 0 && err != -ENOENT) break; err = symlink(udev_device_get_devpath(dev), filename); if (err != 0) err = -errno; } while (err == -ENOENT); } }
/* manage "stack of names" with possibly specified device priorities */ static int link_update(sd_device *dev, const char *slink, bool add) { _cleanup_free_ char *target = NULL, *filename = NULL, *dirname = NULL; char name_enc[PATH_MAX]; const char *id_filename; int r; assert(dev); assert(slink); r = device_get_id_filename(dev, &id_filename); if (r < 0) return log_device_debug_errno(dev, r, "Failed to get id_filename: %m"); util_path_encode(slink + STRLEN("/dev"), name_enc, sizeof(name_enc)); dirname = path_join("/run/udev/links/", name_enc); if (!dirname) return log_oom(); filename = path_join(dirname, id_filename); if (!filename) return log_oom(); if (!add && unlink(filename) == 0) (void) rmdir(dirname); r = link_find_prioritized(dev, add, dirname, &target); if (r < 0) { log_device_debug(dev, "No reference left, removing '%s'", slink); if (unlink(slink) == 0) (void) rmdir_parents(slink, "/"); } else (void) node_symlink(dev, target, slink); if (add) do { _cleanup_close_ int fd = -1; r = mkdir_parents(filename, 0755); if (!IN_SET(r, 0, -ENOENT)) break; fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444); if (fd < 0) r = -errno; } while (r == -ENOENT); return r; }
int udev_node_add(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid) { struct udev *udev = udev_device_get_udev(dev); int i; int num; struct udev_list_entry *list_entry; int err = 0; info(udev, "creating device node '%s', devnum=%d:%d, mode=%#o, uid=%d, gid=%d\n", udev_device_get_devnode(dev), major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)), mode, uid, gid); if (udev_node_mknod(dev, NULL, makedev(0,0), mode, uid, gid) != 0) { err = -1; goto exit; } /* create all_partitions if requested */ num = udev_device_get_num_fake_partitions(dev); if (num > 0) { info(udev, "creating device partition nodes '%s[1-%i]'\n", udev_device_get_devnode(dev), num); for (i = 1; i <= num; i++) { char partitionname[UTIL_PATH_SIZE]; dev_t part_devnum; snprintf(partitionname, sizeof(partitionname), "%s%d", udev_device_get_devnode(dev), i); partitionname[sizeof(partitionname)-1] = '\0'; part_devnum = makedev(major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)) + i); udev_node_mknod(dev, partitionname, part_devnum, mode, uid, gid); } } /* create/update symlinks, add symlinks to name index */ udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) { if (udev_list_entry_get_flag(list_entry)) /* simple unmanaged link name */ node_symlink(udev, udev_device_get_devnode(dev), udev_list_entry_get_name(list_entry)); else link_update(dev, udev_list_entry_get_name(list_entry), 1); } exit: return err; }
/* manage "stack of names" with possibly specified device priorities */ static void link_update(struct udev_device *dev, const char *slink, bool add) { struct udev *udev = udev_device_get_udev(dev); char name_enc[UTIL_PATH_SIZE]; char filename[UTIL_PATH_SIZE * 2]; char dirname[UTIL_PATH_SIZE]; const char *target; char buf[UTIL_PATH_SIZE]; util_path_encode(slink + strlen("/dev"), name_enc, sizeof(name_enc)); strscpyl(dirname, sizeof(dirname), "/run/udev/links/", name_enc, NULL); strscpyl(filename, sizeof(filename), dirname, "/", udev_device_get_id_filename(dev), NULL); if (!add && unlink(filename) == 0) rmdir(dirname); target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf)); if (target == NULL) { log_debug("no reference left, remove '%s'", slink); if (unlink(slink) == 0) util_delete_path(udev, slink); } else { log_debug("creating link '%s' to '%s'", slink, target); node_symlink(dev, target, slink); } if (add) { int err; do { int fd; err = mkdir_parents(filename, 0755); if (err != 0 && err != -ENOENT) break; fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444); if (fd >= 0) close(fd); else err = -errno; } while (err == -ENOENT); } }
void udev_node_add(struct udev_device *dev, bool apply, mode_t mode, uid_t uid, gid_t gid) { char filename[UTIL_PATH_SIZE]; struct udev_list_entry *list_entry; log_debug("handling device node '%s', devnum=%s, mode=%#o, uid=%d, gid=%d\n", udev_device_get_devnode(dev), udev_device_get_id_filename(dev), mode, uid, gid); if (node_permissions_apply(dev, apply, mode, uid, gid) < 0) return; /* always add /dev/{block,char}/$major:$minor */ snprintf(filename, sizeof(filename), "/dev/%s/%u:%u", streq(udev_device_get_subsystem(dev), "block") ? "block" : "char", major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev))); node_symlink(dev, udev_device_get_devnode(dev), filename); /* create/update symlinks, add symlinks to name index */ udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) link_update(dev, udev_list_entry_get_name(list_entry), true); }
void udev_node_add(struct udev_device *dev, bool apply, mode_t mode, uid_t uid, gid_t gid, struct udev_list *seclabel_list) { char filename[DEV_NUM_PATH_MAX]; struct udev_list_entry *list_entry; log_debug("handling device node '%s', devnum=%s, mode=%#o, uid="UID_FMT", gid="GID_FMT, udev_device_get_devnode(dev), udev_device_get_id_filename(dev), mode, uid, gid); if (node_permissions_apply(dev, apply, mode, uid, gid, seclabel_list) < 0) return; /* always add /dev/{block,char}/$major:$minor */ xsprintf_dev_num_path(filename, streq(udev_device_get_subsystem(dev), "block") ? "block" : "char", udev_device_get_devnum(dev)); node_symlink(dev, udev_device_get_devnode(dev), filename); /* create/update symlinks, add symlinks to name index */ udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) link_update(dev, udev_list_entry_get_name(list_entry), true); }
static int update_link(struct udev_device *dev, const char *slink, int test) { struct udev *udev = udev_device_get_udev(dev); struct udev_list_node dev_list; struct udev_list_entry *dev_entry; char target[UTIL_PATH_SIZE]; int count; int priority = 0; int rc = 0; dbg(udev, "update symlink '%s' of '%s'\n", slink, udev_device_get_syspath(dev)); udev_list_init(&dev_list); count = name_index_get_devices(udev, slink, &dev_list); if (count > 1) info(udev, "found %i devices with name '%s'\n", count, slink); /* if we don't have a reference, delete it */ if (count <= 0) { info(udev, "no reference left, remove '%s'\n", slink); if (!test) { unlink(slink); util_delete_path(udev, slink); } goto out; } /* find the device with the highest priority */ target[0] = '\0'; udev_list_entry_foreach(dev_entry, udev_list_get_entry(&dev_list)) { const char *syspath; struct udev_device *dev_db; const char *devnode; syspath = udev_list_entry_get_name(dev_entry); dbg(udev, "found '%s' for '%s'\n", syspath, slink); /* did we find ourself? we win, if we have the same priority */ if (strcmp(udev_device_get_syspath(dev), syspath) == 0) { dbg(udev, "compare (our own) priority of '%s' %i >= %i\n", udev_device_get_devpath(dev), udev_device_get_devlink_priority(dev), priority); if (strcmp(udev_device_get_devnode(dev), slink) == 0) { info(udev, "'%s' is our device node, database inconsistent, skip link update\n", udev_device_get_devnode(dev)); } else if (target[0] == '\0' || udev_device_get_devlink_priority(dev) >= priority) { priority = udev_device_get_devlink_priority(dev); util_strlcpy(target, udev_device_get_devnode(dev), sizeof(target)); } continue; } /* another device, read priority from database */ dev_db = udev_device_new_from_syspath(udev, syspath); if (dev_db == NULL) continue; devnode = udev_device_get_devnode(dev_db); if (devnode != NULL) { if (strcmp(devnode, slink) == 0) { info(udev, "'%s' is a device node of '%s', skip link update\n", devnode, syspath); } else { dbg(udev, "compare priority of '%s' %i > %i\n", udev_device_get_devpath(dev_db), udev_device_get_devlink_priority(dev_db), priority); if (target[0] == '\0' || udev_device_get_devlink_priority(dev_db) > priority) { priority = udev_device_get_devlink_priority(dev_db); util_strlcpy(target, devnode, sizeof(target)); } } } udev_device_unref(dev_db); } udev_list_cleanup_entries(udev, &dev_list); if (target[0] == '\0') { info(udev, "no current target for '%s' found\n", slink); rc = 1; goto out; } /* create symlink to the target with the highest priority */ info(udev, "'%s' with target '%s' has the highest priority %i, create it\n", slink, target, priority); if (!test) { util_create_path(udev, slink); node_symlink(udev, target, slink); } out: return rc; }