/** * udev_device_get_sysattr_list_entry: * @udev_device: udev device * * Retrieve the list of available sysattrs, with value being empty; * This just return all available sysfs attributes for a particular * device without reading their values. * * Returns: the first entry of the property list **/ _public_ struct udev_list_entry *udev_device_get_sysattr_list_entry(struct udev_device *udev_device) { assert_return_errno(udev_device, NULL, EINVAL); if (!udev_device->sysattrs_read) { const char *sysattr; udev_list_cleanup(&udev_device->sysattrs); FOREACH_DEVICE_SYSATTR(udev_device->device, sysattr) udev_list_entry_add(&udev_device->sysattrs, sysattr, NULL); udev_device->sysattrs_read = true; } return udev_list_get_entry(&udev_device->sysattrs); }
/** * udev_device_get_tags_list_entry: * @udev_device: udev device * * Retrieve the list of tags attached to the udev device. The next * list entry can be retrieved with udev_list_entry_get_next(), * which returns #NULL if no more entries exist. The tag string * can be retrieved from the list entry by udev_list_entry_get_name(). * * Returns: the first entry of the tag list **/ _public_ struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device) { assert_return_errno(udev_device, NULL, EINVAL); if (device_get_tags_generation(udev_device->device) != udev_device->tags_generation || !udev_device->tags_read) { const char *tag; udev_list_cleanup(&udev_device->tags); FOREACH_DEVICE_TAG(udev_device->device, tag) udev_list_entry_add(&udev_device->tags, tag, NULL); udev_device->tags_read = true; udev_device->tags_generation = device_get_tags_generation(udev_device->device); } return udev_list_get_entry(&udev_device->tags); }
/** * udev_device_get_event_properties_entry: * @udev_device: udev device * * Retrieve the list of key/value device properties of the udev * device. The next list entry can be retrieved with udev_list_entry_get_next(), * which returns #NULL if no more entries exist. The property name * can be retrieved from the list entry by udev_list_entry_get_name(), * the property value by udev_list_entry_get_value(). * * Returns: the first entry of the property list **/ _public_ struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device) { assert_return_errno(udev_device, NULL, EINVAL); if (device_get_properties_generation(udev_device->device) != udev_device->properties_generation || !udev_device->properties_read) { const char *key, *value; udev_list_cleanup(&udev_device->properties); FOREACH_DEVICE_PROPERTY(udev_device->device, key, value) udev_list_entry_add(&udev_device->properties, key, value); udev_device->properties_read = true; udev_device->properties_generation = device_get_properties_generation(udev_device->device); } return udev_list_get_entry(&udev_device->properties); }
/** * udev_device_get_devlinks_list_entry: * @udev_device: udev device * * Retrieve the list of device links pointing to the device file of * the udev device. The next list entry can be retrieved with * udev_list_entry_get_next(), which returns #NULL if no more entries exist. * The devlink path can be retrieved from the list entry by * udev_list_entry_get_name(). The path is an absolute path, and starts with * the device directory. * * Returns: the first entry of the device node link list **/ _public_ struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device) { assert_return_errno(udev_device, NULL, EINVAL); if (device_get_devlinks_generation(udev_device->device) != udev_device->devlinks_generation || !udev_device->devlinks_read) { const char *devlink; udev_list_cleanup(&udev_device->devlinks); FOREACH_DEVICE_DEVLINK(udev_device->device, devlink) udev_list_entry_add(&udev_device->devlinks, devlink, NULL); udev_device->devlinks_read = true; udev_device->devlinks_generation = device_get_devlinks_generation(udev_device->device); } return udev_list_get_entry(&udev_device->devlinks); }
static int64_t trie_store_nodes(struct trie_f *trie, struct trie_node *node) { uint64_t i; struct trie_node_f n = { .prefix_off = htole64(trie->strings_off + node->prefix_off), .children_count = node->children_count, .values_count = htole64(node->values_count), }; struct trie_child_entry_f *children = NULL; int64_t node_off; if (node->children_count) { children = new0(struct trie_child_entry_f, node->children_count); if (!children) return -ENOMEM; } /* post-order recursion */ for (i = 0; i < node->children_count; i++) { int64_t child_off; child_off = trie_store_nodes(trie, node->children[i].child); if (child_off < 0) { free(children); return child_off; } children[i].c = node->children[i].c; children[i].child_off = htole64(child_off); } /* write node */ node_off = ftello(trie->f); if (fwrite(&n, sizeof(struct trie_node_f), 1, trie->f) != 1) log_error("Failed to write sizeof struct trie_node_f to n in %s\n", "[udevadm-hwdb.c:trie_store_nodes]"); trie->nodes_count++; /* append children array */ if (node->children_count) { if (fwrite(children, sizeof(struct trie_child_entry_f), node->children_count, trie->f) != node->children_count) log_error("Failed to write children_count in %s\n", "[udevadm-hwdb.c:trie_store_nodes]"); trie->children_count += node->children_count; free(children); } /* append values array */ for (i = 0; i < node->values_count; i++) { struct trie_value_entry_f v = { .key_off = htole64(trie->strings_off + node->values[i].key_off), .value_off = htole64(trie->strings_off + node->values[i].value_off), }; if (fwrite(&v, sizeof(struct trie_value_entry_f), 1, trie->f) != 1) log_error("Failed to write sizeof trie_value_entry_f to v in %s\n", "[udevadm-hwdb.c:trie_store_nodes]"); trie->values_count++; } return node_off; } static int trie_store(struct trie *trie, const char *filename) { struct trie_f t = { .trie = trie, }; char *filename_tmp; int64_t pos; int64_t root_off; int64_t size; struct trie_header_f h = { .signature = HWDB_SIG, .tool_version = htole64(atoi(VERSION)), .header_size = htole64(sizeof(struct trie_header_f)), .node_size = htole64(sizeof(struct trie_node_f)), .child_entry_size = htole64(sizeof(struct trie_child_entry_f)), .value_entry_size = htole64(sizeof(struct trie_value_entry_f)), }; int err; /* calculate size of header, nodes, children entries, value entries */ t.strings_off = sizeof(struct trie_header_f); trie_store_nodes_size(&t, trie->root); err = fopen_temporary(filename , &t.f, &filename_tmp); if (err < 0) return err; fchmod(fileno(t.f), 0444); /* write nodes */ fseeko(t.f, sizeof(struct trie_header_f), SEEK_SET); root_off = trie_store_nodes(&t, trie->root); h.nodes_root_off = htole64(root_off); pos = ftello(t.f); h.nodes_len = htole64(pos - sizeof(struct trie_header_f)); /* write string buffer */ if (fwrite(trie->strings->buf, trie->strings->len, 1, t.f) != 1) log_error("Failed to write into trie->strings->buf in %s\n", "[udevadm-hwdb.c:trie_store]"); h.strings_len = htole64(trie->strings->len); /* write header */ size = ftello(t.f); h.file_size = htole64(size); fseeko(t.f, 0, SEEK_SET); if (fwrite(&h, sizeof(struct trie_header_f), 1, t.f) != 1) log_error("Failed to write into h in %s\n", "[udevadm-hwdb.c:trie_store]"); err = ferror(t.f); if (err) err = -errno; fclose(t.f); if (err < 0 || rename(filename_tmp, filename) < 0) { unlink(filename_tmp); goto out; } log_debug("=== trie on-disk ===\n"); log_debug("size: %8llu bytes\n", (unsigned long long)size); log_debug("header: %8zu bytes\n", sizeof(struct trie_header_f)); log_debug("nodes: %8llu bytes (%8llu)\n", (unsigned long long)t.nodes_count * sizeof(struct trie_node_f), (unsigned long long)t.nodes_count); log_debug("child pointers: %8llu bytes (%8llu)\n", (unsigned long long)t.children_count * sizeof(struct trie_child_entry_f), (unsigned long long)t.children_count); log_debug("value pointers: %8llu bytes (%8llu)\n", (unsigned long long)t.values_count * sizeof(struct trie_value_entry_f), (unsigned long long)t.values_count); log_debug("string store: %8llu bytes\n", (unsigned long long)trie->strings->len); log_debug("strings start: %8llu\n", (unsigned long long) t.strings_off); out: free(filename_tmp); return err; } static int insert_data(struct trie *trie, struct udev_list *match_list, char *line, const char *filename) { char *value; struct udev_list_entry *entry; value = strchr(line, '='); if (!value) { log_error("Error, key/value pair expected but got '%s' in '%s':\n", line, filename); return -EINVAL; } value[0] = '\0'; value++; if (line[0] == '\0' || value[0] == '\0') { log_error("Error, empty key or value '%s' in '%s':\n", line, filename); return -EINVAL; } udev_list_entry_foreach(entry, udev_list_get_entry(match_list)) trie_insert(trie, trie->root, udev_list_entry_get_name(entry), line, value); return 0; } static int import_file(struct udev *udev, struct trie *trie, const char *filename) { enum { HW_MATCH, HW_DATA, HW_NONE, } state = HW_NONE; FILE *f; char line[LINE_MAX]; struct udev_list match_list; udev_list_init(udev, &match_list, false); f = fopen(filename, "re"); if (f == NULL) return -errno; while (fgets(line, sizeof(line), f)) { size_t len; char *pos; /* comment line */ if (line[0] == '#') continue; /* strip trailing comment */ pos = strchr(line, '#'); if (pos) pos[0] = '\0'; /* strip trailing whitespace */ len = strlen(line); while (len > 0 && isspace(line[len-1])) len--; line[len] = '\0'; switch (state) { case HW_NONE: if (len == 0) break; if (line[0] == ' ') { log_error("Error, MATCH expected but got '%s' in '%s':\n", line, filename); break; } /* start of record, first match */ state = HW_MATCH; udev_list_entry_add(&match_list, line, NULL); break; case HW_MATCH: if (len == 0) { log_error("Error, DATA expected but got empty line in '%s':\n", filename); state = HW_NONE; udev_list_cleanup(&match_list); break; } /* another match */ if (line[0] != ' ') { udev_list_entry_add(&match_list, line, NULL); break; } /* first data */ state = HW_DATA; insert_data(trie, &match_list, line, filename); break; case HW_DATA: /* end of record */ if (len == 0) { state = HW_NONE; udev_list_cleanup(&match_list); break; } if (line[0] != ' ') { log_error("Error, DATA expected but got '%s' in '%s':\n", line, filename); state = HW_NONE; udev_list_cleanup(&match_list); break; } insert_data(trie, &match_list, line, filename); break; }; } fclose(f); udev_list_cleanup(&match_list); return 0; } static void help(void) { printf("Usage: udevadm hwdb OPTIONS\n" " --update update the hardware database\n" " --test=<modalias> query database and print result\n" " --root=<path> alternative root path in the filesystem\n" " --help\n\n"); }
static int node_permissions_apply(struct udev_device *dev, bool apply, mode_t mode, uid_t uid, gid_t gid, struct udev_list *seclabel_list) { const char *devnode = udev_device_get_devnode(dev); dev_t devnum = udev_device_get_devnum(dev); struct stat stats; struct udev_list_entry *entry; int err = 0; if (streq(udev_device_get_subsystem(dev), "block")) mode |= S_IFBLK; else mode |= S_IFCHR; if (lstat(devnode, &stats) != 0) { err = -errno; log_debug_errno(errno, "can not stat() node '%s' (%m)", devnode); goto out; } if (((stats.st_mode & S_IFMT) != (mode & S_IFMT)) || (stats.st_rdev != devnum)) { err = -EEXIST; log_debug("found node '%s' with non-matching devnum %s, skip handling", udev_device_get_devnode(dev), udev_device_get_id_filename(dev)); goto out; } if (apply) { bool selinux = false; bool smack = false; if ((stats.st_mode & 0777) != (mode & 0777) || stats.st_uid != uid || stats.st_gid != gid) { log_debug("set permissions %s, %#o, uid=%u, gid=%u", devnode, mode, uid, gid); err = chmod(devnode, mode); if (err < 0) log_warning_errno(errno, "setting mode of %s to %#o failed: %m", devnode, mode); err = chown(devnode, uid, gid); if (err < 0) log_warning_errno(errno, "setting owner of %s to uid=%u, gid=%u failed: %m", devnode, uid, gid); } else { log_debug("preserve permissions %s, %#o, uid=%u, gid=%u", devnode, mode, uid, gid); } /* apply SECLABEL{$module}=$label */ udev_list_entry_foreach(entry, udev_list_get_entry(seclabel_list)) { const char *name, *label; int r; name = udev_list_entry_get_name(entry); label = udev_list_entry_get_value(entry); if (streq(name, "selinux")) { selinux = true; r = mac_selinux_apply(devnode, label); if (r < 0) log_error_errno(r, "SECLABEL: failed to set SELinux label '%s': %m", label); else log_debug("SECLABEL: set SELinux label '%s'", label); } else if (streq(name, "smack")) { smack = true; r = mac_smack_apply(devnode, label); if (r < 0) log_error_errno(r, "SECLABEL: failed to set SMACK label '%s': %m", label); else log_debug("SECLABEL: set SMACK label '%s'", label); } else log_error("SECLABEL: unknown subsystem, ignoring '%s'='%s'", name, label); } /* set the defaults */ if (!selinux) mac_selinux_fix(devnode, true, false); if (!smack) mac_smack_apply(devnode, NULL); } /* always update timestamp when we re-use the node, like on media change events */ utimensat(AT_FDCWD, devnode, NULL, 0); out: return err; }
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; }
static int adm_monitor(struct udev *udev, int argc, char *argv[]) { struct sigaction act = {}; sigset_t mask; bool prop = false; bool print_kernel = false; bool print_udev = false; _cleanup_udev_list_cleanup_ struct udev_list subsystem_match_list; _cleanup_udev_list_cleanup_ struct udev_list tag_match_list; _cleanup_udev_monitor_unref_ struct udev_monitor *udev_monitor = NULL; _cleanup_udev_monitor_unref_ struct udev_monitor *kernel_monitor = NULL; _cleanup_close_ int fd_ep = -1; int fd_kernel = -1, fd_udev = -1; struct epoll_event ep_kernel, ep_udev; int c; static const struct option options[] = { { "property", no_argument, NULL, 'p' }, { "environment", no_argument, NULL, 'e' }, /* alias for -p */ { "kernel", no_argument, NULL, 'k' }, { "udev", no_argument, NULL, 'u' }, { "subsystem-match", required_argument, NULL, 's' }, { "tag-match", required_argument, NULL, 't' }, { "help", no_argument, NULL, 'h' }, {} }; udev_list_init(udev, &subsystem_match_list, true); udev_list_init(udev, &tag_match_list, true); while ((c = getopt_long(argc, argv, "pekus:t:h", options, NULL)) >= 0) switch (c) { case 'p': case 'e': prop = true; break; case 'k': print_kernel = true; break; case 'u': print_udev = true; break; case 's': { char subsys[UTIL_NAME_SIZE]; char *devtype; strscpy(subsys, sizeof(subsys), optarg); devtype = strchr(subsys, '/'); if (devtype != NULL) { devtype[0] = '\0'; devtype++; } udev_list_entry_add(&subsystem_match_list, subsys, devtype); break; } case 't': udev_list_entry_add(&tag_match_list, optarg, NULL); break; case 'h': help(); return 0; default: return 1; } if (!print_kernel && !print_udev) { print_kernel = true; print_udev = true; } /* set signal handlers */ act.sa_handler = sig_handler; act.sa_flags = SA_RESTART; sigaction(SIGINT, &act, NULL); sigaction(SIGTERM, &act, NULL); sigemptyset(&mask); sigaddset(&mask, SIGINT); sigaddset(&mask, SIGTERM); sigprocmask(SIG_UNBLOCK, &mask, NULL); /* Callers are expecting to see events as they happen: Line buffering */ setlinebuf(stdout); fd_ep = epoll_create1(EPOLL_CLOEXEC); if (fd_ep < 0) { log_error_errno(errno, "error creating epoll fd: %m"); return 1; } printf("monitor will print the received events for:\n"); if (print_udev) { struct udev_list_entry *entry; udev_monitor = udev_monitor_new_from_netlink(udev, "udev"); if (udev_monitor == NULL) { fprintf(stderr, "error: unable to create netlink socket\n"); return 1; } udev_monitor_set_receive_buffer_size(udev_monitor, 128*1024*1024); fd_udev = udev_monitor_get_fd(udev_monitor); udev_list_entry_foreach(entry, udev_list_get_entry(&subsystem_match_list)) { const char *subsys = udev_list_entry_get_name(entry); const char *devtype = udev_list_entry_get_value(entry); if (udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, subsys, devtype) < 0) fprintf(stderr, "error: unable to apply subsystem filter '%s'\n", subsys); } udev_list_entry_foreach(entry, udev_list_get_entry(&tag_match_list)) { const char *tag = udev_list_entry_get_name(entry); if (udev_monitor_filter_add_match_tag(udev_monitor, tag) < 0) fprintf(stderr, "error: unable to apply tag filter '%s'\n", tag); } if (udev_monitor_enable_receiving(udev_monitor) < 0) { fprintf(stderr, "error: unable to subscribe to udev events\n"); return 2; } memzero(&ep_udev, sizeof(struct epoll_event)); ep_udev.events = EPOLLIN; ep_udev.data.fd = fd_udev; if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_udev, &ep_udev) < 0) { log_error_errno(errno, "fail to add fd to epoll: %m"); return 2; } printf("UDEV - the event which udev sends out after rule processing\n"); } if (print_kernel) { struct udev_list_entry *entry; kernel_monitor = udev_monitor_new_from_netlink(udev, "kernel"); if (kernel_monitor == NULL) { fprintf(stderr, "error: unable to create netlink socket\n"); return 3; } udev_monitor_set_receive_buffer_size(kernel_monitor, 128*1024*1024); fd_kernel = udev_monitor_get_fd(kernel_monitor); udev_list_entry_foreach(entry, udev_list_get_entry(&subsystem_match_list)) { const char *subsys = udev_list_entry_get_name(entry); if (udev_monitor_filter_add_match_subsystem_devtype(kernel_monitor, subsys, NULL) < 0) fprintf(stderr, "error: unable to apply subsystem filter '%s'\n", subsys); } if (udev_monitor_enable_receiving(kernel_monitor) < 0) { fprintf(stderr, "error: unable to subscribe to kernel events\n"); return 4; } memzero(&ep_kernel, sizeof(struct epoll_event)); ep_kernel.events = EPOLLIN; ep_kernel.data.fd = fd_kernel; if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_kernel, &ep_kernel) < 0) { log_error_errno(errno, "fail to add fd to epoll: %m"); return 5; } printf("KERNEL - the kernel uevent\n"); } printf("\n"); while (!udev_exit) { int fdcount; struct epoll_event ev[4]; int i; fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), -1); if (fdcount < 0) { if (errno != EINTR) fprintf(stderr, "error receiving uevent message: %m\n"); continue; } for (i = 0; i < fdcount; i++) { if (ev[i].data.fd == fd_kernel && ev[i].events & EPOLLIN) { struct udev_device *device; device = udev_monitor_receive_device(kernel_monitor); if (device == NULL) continue; print_device(device, "KERNEL", prop); udev_device_unref(device); } else if (ev[i].data.fd == fd_udev && ev[i].events & EPOLLIN) { struct udev_device *device; device = udev_monitor_receive_device(udev_monitor); if (device == NULL) continue; print_device(device, "UDEV", prop); udev_device_unref(device); } } } return 0; }
struct udev_list_entry *udev_get_properties_list_entry(struct udev *udev) { return udev_list_get_entry(&udev->properties_list); }