udev_get_sys_path
udev_get_dev_path
udev_device_new_from_syspath
+udev_device_new_from_devnum
udev_device_get_parent
udev_device_ref
udev_device_unref
return udev_device;
}
+struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
+{
+ char path[UTIL_PATH_SIZE];
+
+ snprintf(path, sizeof(path), "%s/dev/%s/%u:%u",
+ udev_get_sys_path(udev),
+ type == 'b' ? "block" : "char",
+ major(devnum), minor(devnum));
+ if (util_resolve_sys_link(udev, path, sizeof(path)) < 0)
+ return NULL;
+
+ return udev_device_new_from_syspath(udev, path);
+}
+
static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
{
struct udev_device *udev_device_parent = NULL;
int util_resolve_sys_link(struct udev *udev, char *syspath, size_t size)
{
+ struct stat statbuf;
char link_target[UTIL_PATH_SIZE];
int len;
int i;
int back;
+ if (lstat(syspath, &statbuf) < 0)
+ return -1;
+ if (!S_ISLNK(statbuf.st_mode))
+ return -1;
len = readlink(syspath, link_target, sizeof(link_target));
if (len <= 0)
return -1;
struct udev_device;
extern struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath);
+extern struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum);
extern struct udev_device *udev_device_get_parent(struct udev_device *udev_device);
extern struct udev_device *udev_device_ref(struct udev_device *udev_device);
extern void udev_device_unref(struct udev_device *udev_device);
#include <string.h>
#include <getopt.h>
#include <syslog.h>
+#include <fcntl.h>
#include <sys/select.h>
#include "libudev.h"
return 0;
}
+static int test_device_devnum(struct udev *udev)
+{
+ dev_t devnum = makedev(1, 3);
+ struct udev_device *device;
+
+ printf("looking up device: %u:%u\n", major(devnum), minor(devnum));
+ device = udev_device_new_from_devnum(udev, 'c', devnum);
+ if (device == NULL)
+ return -1;
+ print_device(device);
+ udev_device_unref(device);
+ return 0;
+}
+
static int devices_enum_cb(struct udev_device *device, void *data)
{
printf("device: '%s' (%s) '%s'\n",
}
test_device(udev, syspath);
+ test_device_devnum(udev);
test_device_parents(udev, syspath);
test_enumerate(udev, subsystem);
test_monitor(udev, socket);