int dhcp_identifier_set_iaid(int ifindex, uint8_t *mac, size_t mac_len, void *_id) { #if 0 /* NM_IGNORED */ /* name is a pointer to memory in the udev_device struct, so must have the same scope */ _cleanup_udev_device_unref_ struct udev_device *device = NULL; #else /* NM_IGNORED */ char name_buf[IF_NAMESIZE]; #endif /* NM_IGNORED */ const char *name = NULL; uint64_t id; #if 0 /* NM_IGNORED */ if (detect_container() <= 0) { /* not in a container, udev will be around */ _cleanup_udev_unref_ struct udev *udev; char ifindex_str[2 + DECIMAL_STR_MAX(int)]; udev = udev_new(); if (!udev) return -ENOMEM; sprintf(ifindex_str, "n%d", ifindex); device = udev_device_new_from_device_id(udev, ifindex_str); if (device) { if (udev_device_get_is_initialized(device) <= 0) /* not yet ready */ return -EBUSY; name = net_get_name(device); } }
/* move any old watches directory out of the way, and then restore * the watches */ void udev_watch_restore(struct udev *udev) { char filename[UTIL_PATH_SIZE], oldname[UTIL_PATH_SIZE]; if (inotify_fd < 0) return; util_strscpyl(oldname, sizeof(oldname), udev_get_run_path(udev), "/watch.old", NULL); util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev), "/watch", NULL); if (rename(filename, oldname) == 0) { DIR *dir; struct dirent *ent; dir = opendir(oldname); if (dir == NULL) { err(udev, "unable to open old watches dir '%s', old watches will not be restored: %m", oldname); return; } for (ent = readdir(dir); ent != NULL; ent = readdir(dir)) { char device[UTIL_PATH_SIZE]; char *s; size_t l; ssize_t len; struct udev_device *dev; if (ent->d_name[0] == '.') continue; s = device; l = util_strpcpy(&s, sizeof(device), udev_get_sys_path(udev)); len = readlinkat(dirfd(dir), ent->d_name, s, l); if (len <= 0 || len == (ssize_t)l) goto unlink; s[len] = '\0'; dev = udev_device_new_from_device_id(udev, s); if (dev == NULL) goto unlink; info(udev, "restoring old watch on '%s'\n", udev_device_get_devnode(dev)); udev_watch_begin(udev, dev); udev_device_unref(dev); unlink: unlinkat(dirfd(dir), ent->d_name, 0); } closedir(dir); rmdir(oldname); } else if (errno != ENOENT) { err(udev, "unable to move watches dir '%s', old watches will not be restored: %m", filename); } }
/* find device node of device with highest priority */ static const char *link_find_prioritized(struct udev_device *dev, bool add, const char *stackdir, char *buf, size_t bufsize) { struct udev *udev = udev_device_get_udev(dev); DIR *dir; int priority = 0; const char *target = NULL; if (add) { priority = udev_device_get_devlink_priority(dev); util_strscpy(buf, bufsize, udev_device_get_devnode(dev)); target = buf; } dir = opendir(stackdir); if (dir == NULL) return target; for (;;) { struct udev_device *dev_db; struct dirent *dent; dent = readdir(dir); if (dent == NULL || dent->d_name[0] == '\0') break; if (dent->d_name[0] == '.') continue; info(udev, "found '%s' claiming '%s'\n", dent->d_name, stackdir); /* did we find ourself? */ if (strcmp(dent->d_name, udev_device_get_id_filename(dev)) == 0) continue; dev_db = udev_device_new_from_device_id(udev, dent->d_name); if (dev_db != NULL) { const char *devnode; devnode = udev_device_get_devnode(dev_db); if (devnode != NULL) { dbg(udev, "compare priority of '%s'(%i) > '%s'(%i)\n", target, priority, udev_device_get_devnode(dev_db), udev_device_get_devlink_priority(dev_db)); if (target == NULL || udev_device_get_devlink_priority(dev_db) > priority) { info(udev, "'%s' claims priority %i for '%s'\n", udev_device_get_syspath(dev_db), udev_device_get_devlink_priority(dev_db), stackdir); priority = udev_device_get_devlink_priority(dev_db); util_strscpy(buf, bufsize, devnode); target = buf; } } udev_device_unref(dev_db); } } closedir(dir); return target; }
/* find device node of device with highest priority */ static const char *link_find_prioritized(struct udev_device *dev, bool add, const char *stackdir, char *buf, size_t bufsize) { struct udev *udev = udev_device_get_udev(dev); DIR *dir; struct dirent *dent; int priority = 0; const char *target = NULL; if (add) { priority = udev_device_get_devlink_priority(dev); strscpy(buf, bufsize, udev_device_get_devnode(dev)); target = buf; } dir = opendir(stackdir); if (dir == NULL) return target; FOREACH_DIRENT_ALL(dent, dir, break) { struct udev_device *dev_db; if (dent->d_name[0] == '\0') break; if (dent->d_name[0] == '.') continue; log_debug("found '%s' claiming '%s'", dent->d_name, stackdir); /* did we find ourself? */ if (streq(dent->d_name, udev_device_get_id_filename(dev))) continue; dev_db = udev_device_new_from_device_id(udev, dent->d_name); if (dev_db != NULL) { const char *devnode; devnode = udev_device_get_devnode(dev_db); if (devnode != NULL) { if (target == NULL || udev_device_get_devlink_priority(dev_db) > priority) { log_debug("'%s' claims priority %i for '%s'", udev_device_get_syspath(dev_db), udev_device_get_devlink_priority(dev_db), stackdir); priority = udev_device_get_devlink_priority(dev_db); strscpy(buf, bufsize, devnode); target = buf; } } udev_device_unref(dev_db); } } closedir(dir); return target; }
struct udev_device *udev_watch_lookup(struct udev *udev, int wd) { char filename[UTIL_PATH_SIZE]; char device[UTIL_NAME_SIZE]; ssize_t len; if (inotify_fd < 0 || wd < 0) return NULL; snprintf(filename, sizeof(filename), "/run/udev/watch/%d", wd); len = readlink(filename, device, sizeof(device)); if (len <= 0 || (size_t)len == sizeof(device)) return NULL; device[len] = '\0'; return udev_device_new_from_device_id(udev, device); }
/* move any old watches directory out of the way, and then restore * the watches */ void udev_watch_restore(struct udev *udev) { if (inotify_fd < 0) return; if (rename("/run/udev/watch", "/run/udev/watch.old") == 0) { DIR *dir; struct dirent *ent; dir = opendir("/run/udev/watch.old"); if (dir == NULL) { log_error("unable to open old watches dir /run/udev/watch.old; old watches will not be restored: %m"); return; } for (ent = readdir(dir); ent != NULL; ent = readdir(dir)) { char device[UTIL_PATH_SIZE]; ssize_t len; struct udev_device *dev; if (ent->d_name[0] == '.') continue; len = readlinkat(dirfd(dir), ent->d_name, device, sizeof(device)); if (len <= 0 || len == (ssize_t)sizeof(device)) goto unlink; device[len] = '\0'; dev = udev_device_new_from_device_id(udev, device); if (dev == NULL) goto unlink; log_debug("restoring old watch on '%s'", udev_device_get_devnode(dev)); udev_watch_begin(udev, dev); udev_device_unref(dev); unlink: unlinkat(dirfd(dir), ent->d_name, 0); } closedir(dir); rmdir("/run/udev/watch.old"); } else if (errno != ENOENT) { log_error("unable to move watches dir /run/udev/watch; old watches will not be restored: %m"); } }
struct udev_device *udev_watch_lookup(struct udev *udev, int wd) { char filename[UTIL_PATH_SIZE]; char majmin[UTIL_PATH_SIZE]; char *s; size_t l; ssize_t len; if (inotify_fd < 0 || wd < 0) return NULL; snprintf(filename, sizeof(filename), "%s/watch/%d", udev_get_run_path(udev), wd); s = majmin; l = util_strpcpy(&s, sizeof(majmin), udev_get_sys_path(udev)); len = readlink(filename, s, l); if (len <= 0 || (size_t)len == l) return NULL; s[len] = '\0'; return udev_device_new_from_device_id(udev, s); }
static int list_links(int argc, char *argv[], void *userdata) { _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL; _cleanup_udev_unref_ struct udev *udev = NULL; _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL; _cleanup_free_ LinkInfo *links = NULL; int r, c, i; pager_open_if_enabled(); r = sd_rtnl_open(&rtnl, 0); if (r < 0) return log_error_errno(r, "Failed to connect to netlink: %m"); udev = udev_new(); if (!udev) return log_error_errno(errno, "Failed to connect to udev: %m"); r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0); if (r < 0) return rtnl_log_create_error(r); r = sd_rtnl_message_request_dump(req, true); if (r < 0) return rtnl_log_create_error(r); r = sd_rtnl_call(rtnl, req, 0, &reply); if (r < 0) return log_error_errno(r, "Failed to enumerate links: %m"); if (arg_legend) printf("%3s %-16s %-18s %-11s %-10s\n", "IDX", "LINK", "TYPE", "OPERATIONAL", "SETUP"); c = decode_and_sort_links(reply, &links); if (c < 0) return rtnl_log_parse_error(c); for (i = 0; i < c; i++) { _cleanup_free_ char *setup_state = NULL, *operational_state = NULL; _cleanup_udev_device_unref_ struct udev_device *d = NULL; const char *on_color_operational, *off_color_operational, *on_color_setup, *off_color_setup; char devid[2 + DECIMAL_STR_MAX(int)]; _cleanup_free_ char *t = NULL; sd_network_link_get_operational_state(links[i].ifindex, &operational_state); operational_state_to_color(operational_state, &on_color_operational, &off_color_operational); sd_network_link_get_setup_state(links[i].ifindex, &setup_state); setup_state_to_color(setup_state, &on_color_setup, &off_color_setup); sprintf(devid, "n%i", links[i].ifindex); d = udev_device_new_from_device_id(udev, devid); link_get_type_string(links[i].iftype, d, &t); printf("%3i %-16s %-18s %s%-11s%s %s%-10s%s\n", links[i].ifindex, links[i].name, strna(t), on_color_operational, strna(operational_state), off_color_operational, on_color_setup, strna(setup_state), off_color_setup); } if (arg_legend) printf("\n%i links listed.\n", c); return 0; }
static int builtin_hwdb(struct udev_device *dev, int argc, char *argv[], bool test) { static const struct option options[] = { { "filter", required_argument, NULL, 'f' }, { "device", required_argument, NULL, 'd' }, { "subsystem", required_argument, NULL, 's' }, { "lookup-prefix", required_argument, NULL, 'p' }, {} }; const char *filter = NULL; const char *device = NULL; const char *subsystem = NULL; const char *prefix = NULL; struct udev_device *srcdev; if (!hwdb) return EXIT_FAILURE; for (;;) { int option; option = getopt_long(argc, argv, "f:d:s:p:", options, NULL); if (option == -1) break; switch (option) { case 'f': filter = optarg; break; case 'd': device = optarg; break; case 's': subsystem = optarg; break; case 'p': prefix = optarg; break; } } /* query a specific key given as argument */ if (argv[optind]) { if (udev_builtin_hwdb_lookup(dev, prefix, argv[optind], filter, test) > 0) return EXIT_SUCCESS; return EXIT_FAILURE; } /* read data from another device than the device we will store the data */ if (device) { srcdev = udev_device_new_from_device_id(udev_device_get_udev(dev), device); if (!srcdev) return EXIT_FAILURE; } else srcdev = dev; if (udev_builtin_hwdb_search(dev, srcdev, subsystem, prefix, filter, test) > 0) return EXIT_SUCCESS; return EXIT_FAILURE; }
static void dev_kmsg_record(Server *s, const char *p, size_t l) { _cleanup_free_ char *message = NULL, *syslog_priority = NULL, *syslog_pid = NULL, *syslog_facility = NULL, *syslog_identifier = NULL, *source_time = NULL, *identifier = NULL, *pid = NULL; struct iovec iovec[N_IOVEC_META_FIELDS + 7 + N_IOVEC_KERNEL_FIELDS + 2 + N_IOVEC_UDEV_FIELDS]; char *kernel_device = NULL; unsigned long long usec; size_t n = 0, z = 0, j; int priority, r; char *e, *f, *k; uint64_t serial; size_t pl; assert(s); assert(p); if (l <= 0) return; e = memchr(p, ',', l); if (!e) return; *e = 0; r = safe_atoi(p, &priority); if (r < 0 || priority < 0 || priority > 999) return; if (s->forward_to_kmsg && (priority & LOG_FACMASK) != LOG_KERN) return; l -= (e - p) + 1; p = e + 1; e = memchr(p, ',', l); if (!e) return; *e = 0; r = safe_atou64(p, &serial); if (r < 0) return; if (s->kernel_seqnum) { /* We already read this one? */ if (serial < *s->kernel_seqnum) return; /* Did we lose any? */ if (serial > *s->kernel_seqnum) server_driver_message(s, 0, "MESSAGE_ID=" SD_MESSAGE_JOURNAL_MISSED_STR, LOG_MESSAGE("Missed %"PRIu64" kernel messages", serial - *s->kernel_seqnum), NULL); /* Make sure we never read this one again. Note that * we always store the next message serial we expect * here, simply because this makes handling the first * message with serial 0 easy. */ *s->kernel_seqnum = serial + 1; } l -= (e - p) + 1; p = e + 1; f = memchr(p, ';', l); if (!f) return; /* Kernel 3.6 has the flags field, kernel 3.5 lacks that */ e = memchr(p, ',', l); if (!e || f < e) e = f; *e = 0; r = safe_atollu(p, &usec); if (r < 0) return; l -= (f - p) + 1; p = f + 1; e = memchr(p, '\n', l); if (!e) return; *e = 0; pl = e - p; l -= (e - p) + 1; k = e + 1; for (j = 0; l > 0 && j < N_IOVEC_KERNEL_FIELDS; j++) { char *m; /* Metadata fields attached */ if (*k != ' ') break; k++, l--; e = memchr(k, '\n', l); if (!e) return; *e = 0; if (cunescape_length_with_prefix(k, e - k, "_KERNEL_", UNESCAPE_RELAX, &m) < 0) break; if (startswith(m, "_KERNEL_DEVICE=")) kernel_device = m + 15; iovec[n++] = IOVEC_MAKE_STRING(m); z++; l -= (e - k) + 1; k = e + 1; } if (kernel_device) { struct udev_device *ud; ud = udev_device_new_from_device_id(s->udev, kernel_device); if (ud) { const char *g; struct udev_list_entry *ll; char *b; g = udev_device_get_devnode(ud); if (g) { b = strappend("_UDEV_DEVNODE=", g); if (b) { iovec[n++] = IOVEC_MAKE_STRING(b); z++; } } g = udev_device_get_sysname(ud); if (g) { b = strappend("_UDEV_SYSNAME=", g); if (b) { iovec[n++] = IOVEC_MAKE_STRING(b); z++; } } j = 0; ll = udev_device_get_devlinks_list_entry(ud); udev_list_entry_foreach(ll, ll) { if (j > N_IOVEC_UDEV_FIELDS) break; g = udev_list_entry_get_name(ll); if (g) { b = strappend("_UDEV_DEVLINK=", g); if (b) { iovec[n++] = IOVEC_MAKE_STRING(b); z++; } } j++; } udev_device_unref(ud); } } if (asprintf(&source_time, "_SOURCE_MONOTONIC_TIMESTAMP=%llu", usec) >= 0) iovec[n++] = IOVEC_MAKE_STRING(source_time); iovec[n++] = IOVEC_MAKE_STRING("_TRANSPORT=kernel"); if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0) iovec[n++] = IOVEC_MAKE_STRING(syslog_priority); if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0) iovec[n++] = IOVEC_MAKE_STRING(syslog_facility); if ((priority & LOG_FACMASK) == LOG_KERN) iovec[n++] = IOVEC_MAKE_STRING("SYSLOG_IDENTIFIER=kernel"); else { pl -= syslog_parse_identifier((const char**) &p, &identifier, &pid); /* Avoid any messages we generated ourselves via * log_info() and friends. */ if (pid && is_us(pid)) goto finish; if (identifier) { syslog_identifier = strappend("SYSLOG_IDENTIFIER=", identifier); if (syslog_identifier) iovec[n++] = IOVEC_MAKE_STRING(syslog_identifier); } if (pid) { syslog_pid = strappend("SYSLOG_PID=", pid); if (syslog_pid) iovec[n++] = IOVEC_MAKE_STRING(syslog_pid); } } if (cunescape_length_with_prefix(p, pl, "MESSAGE=", UNESCAPE_RELAX, &message) >= 0) iovec[n++] = IOVEC_MAKE_STRING(message); server_dispatch_message(s, iovec, n, ELEMENTSOF(iovec), NULL, NULL, priority, 0); finish: for (j = 0; j < z; j++) free(iovec[j].iov_base); }