int main(void) { _cleanup_(manager_freep) Manager *manager = NULL; _cleanup_(sd_device_unrefp) sd_device *loopback = NULL; int ifindex, r; test_setup_logging(LOG_INFO); test_deserialize_in_addr(); test_deserialize_dhcp_routes(); test_address_equality(); test_dhcp_hostname_shorten_overlong(); assert_se(manager_new(&manager) >= 0); r = test_load_config(manager); if (r == -EPERM) return log_tests_skipped("Cannot load configuration"); assert_se(r == 0); assert_se(sd_device_new_from_syspath(&loopback, "/sys/class/net/lo") >= 0); assert_se(loopback); assert_se(sd_device_get_ifindex(loopback, &ifindex) >= 0); assert_se(ifindex == 1); test_network_get(manager, loopback); assert_se(manager_rtnl_enumerate_links(manager) >= 0); }
int find_device(const char *id, const char *prefix, sd_device **ret) { _cleanup_free_ char *buf = NULL; assert(id); assert(ret); if (prefix && !path_startswith(id, prefix)) { buf = path_join(NULL, prefix, id); if (!buf) return -ENOMEM; id = buf; } if (path_startswith(id, "/sys/")) return sd_device_new_from_syspath(ret, id); if (path_startswith(id, "/dev/")) { struct stat st; if (stat(id, &st) < 0) return -errno; return device_new_from_stat_rdev(ret, &st); } return -EINVAL; }
/** * udev_device_new_from_syspath: * @udev: udev library context * @syspath: sys device path including sys directory * * Create new udev device, and fill in information from the sys * device and the udev database entry. The syspath is the absolute * path to the device, including the sys mount point. * * The initial refcount is 1, and needs to be decremented to * release the resources of the udev device. * * Returns: a new udev device, or #NULL, if it does not exist **/ _public_ struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath) { struct udev_device *udev_device; int r; udev_device = udev_device_new(udev); if (!udev_device) return NULL; r = sd_device_new_from_syspath(&udev_device->device, syspath); if (r < 0) { errno = -r; udev_device_unref(udev_device); return NULL; } return udev_device; }