summaryrefslogtreecommitdiff
path: root/udev/lib/test-libudev.c
diff options
context:
space:
mode:
authorKay Sievers <kay.sievers@vrfy.org>2008-08-28 23:05:01 +0200
committerKay Sievers <kay.sievers@vrfy.org>2008-08-28 23:05:01 +0200
commitba6929f6690a72485ac3c6b7610ffbd5ab319be7 (patch)
tree0c6988f9530bfe5ce88004c79ac106a80dd83ce6 /udev/lib/test-libudev.c
parent530fc1f526ee04360f6937812b31cc1df526e505 (diff)
libudev: add udev event monitor API
Diffstat (limited to 'udev/lib/test-libudev.c')
-rw-r--r--udev/lib/test-libudev.c139
1 files changed, 108 insertions, 31 deletions
diff --git a/udev/lib/test-libudev.c b/udev/lib/test-libudev.c
index e86c660e64..3c38600192 100644
--- a/udev/lib/test-libudev.c
+++ b/udev/lib/test-libudev.c
@@ -21,6 +21,11 @@
#include <stdio.h>
#include <stdarg.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/select.h>
+
#include "libudev.h"
static void log_fn(struct udev *udev,
@@ -31,9 +36,49 @@ static void log_fn(struct udev *udev,
vprintf(format, args);
}
-static int devlinks_cb(struct udev_device *udev_device, const char *value, void *data)
+static int print_devlinks_cb(struct udev_device *udev_device, const char *value, void *data)
{
- printf("link: %s\n", value);
+ printf("link: '%s'\n", value);
+ return 0;
+}
+
+static int print_properties_cb(struct udev_device *udev_device, const char *key, const char *value, void *data)
+{
+ printf("property: '%s=%s'\n", key, value);
+ return 0;
+}
+
+static void print_device(struct udev_device *device)
+{
+ const char *str;
+ int count;
+
+ printf("*** device: %p ***\n", device);
+ str = udev_device_get_devpath(device);
+ printf("devpath: '%s'\n", str);
+ str = udev_device_get_subsystem(device);
+ printf("subsystem: '%s'\n", str);
+ str = udev_device_get_devname(device);
+ printf("devname: '%s'\n", str);
+ count = udev_device_get_devlinks(device, print_devlinks_cb, NULL);
+ printf("found %i links\n", count);
+ count = udev_device_get_properties(device, print_properties_cb, NULL);
+ printf("found %i properties\n", count);
+ printf("\n");
+}
+
+static int test_device(struct udev *udev, const char *devpath)
+{
+ struct udev_device *device;
+
+ printf("looking at device: %s\n", devpath);
+ device = udev_device_new_from_devpath(udev, devpath);
+ if (device == NULL) {
+ printf("no device\n");
+ return -1;
+ }
+ print_device(device);
+ udev_device_unref(device);
return 0;
}
@@ -41,28 +86,79 @@ static int devices_enum_cb(struct udev *udev,
const char *devpath, const char *subsystem, const char *name,
void *data)
{
- printf("device: %s (%s) %s\n", devpath, subsystem, name);
+ printf("device: '%s' (%s) '%s'\n", devpath, subsystem, name);
return 0;
}
-static int properties_cb(struct udev_device *udev_device, const char *key, const char *value, void *data)
+static int test_enumerate(struct udev *udev, const char *subsystem)
{
- printf("property: %s=%s\n", key, value);
+ int count;
+
+ count = udev_devices_enumerate(udev, subsystem, devices_enum_cb, NULL);
+ printf("found %i devices\n\n", count);
+ return count;
+}
+
+static int test_monitor(struct udev *udev, const char *socket_path)
+{
+ struct udev_monitor *udev_monitor;
+ fd_set readfds;
+ int fd;
+
+ udev_monitor = udev_monitor_new_from_socket(udev, socket_path);
+ if (udev_monitor == NULL) {
+ printf("no socket\n");
+ return -1;
+ }
+
+ fd = udev_monitor_get_fd(udev_monitor);
+ FD_ZERO(&readfds);
+
+ while (1) {
+ struct udev_device *device;
+ int fdcount;
+
+ FD_SET(STDIN_FILENO, &readfds);
+ FD_SET(fd, &readfds);
+
+ printf("waiting for events on %s, press ENTER to exit\n", socket_path);
+ fdcount = select(fd+1, &readfds, NULL, NULL, NULL);
+ printf("select fd count: %i\n", fdcount);
+
+ if (FD_ISSET(fd, &readfds)) {
+ device = udev_monitor_get_device(udev_monitor);
+ if (device == NULL) {
+ printf("no device from socket\n");
+ continue;
+ }
+ print_device(device);
+ udev_device_unref(device);
+ }
+
+ if (FD_ISSET(STDIN_FILENO, &readfds)) {
+ printf("exiting loop\n");
+ break;
+ }
+ }
+
+ udev_monitor_unref(udev_monitor);
return 0;
}
int main(int argc, char *argv[], char *envp[])
{
struct udev *udev;
- struct udev_device *device;
- const char *str;
const char *devpath = "/devices/virtual/mem/null";
const char *subsystem = NULL;
+ const char *socket = "@/org/kernel/udev/monitor";
+ const char *str;
if (argv[1] != NULL) {
devpath = argv[1];
if (argv[2] != NULL)
subsystem = argv[2];
+ if (argv[3] != NULL)
+ socket = argv[3];
}
udev = udev_new();
@@ -75,32 +171,13 @@ int main(int argc, char *argv[], char *envp[])
printf("set log: %p\n", log_fn);
str = udev_get_sys_path(udev);
- printf("sys_path: %s\n", str);
+ printf("sys_path: '%s'\n", str);
str = udev_get_dev_path(udev);
- printf("dev_path: %s\n", str);
-
- printf("looking at device: %s\n", devpath);
- device = udev_device_new_from_devpath(udev, devpath);
- printf("device: %p\n", device);
- if (device == NULL) {
- printf("no device\n");
- return 1;
- }
- str = udev_device_get_devpath(device);
- printf("devpath: %s\n", str);
- str = udev_device_get_subsystem(device);
- printf("subsystem: %s\n", str);
- str = udev_device_get_devname(device);
- printf("devname: %s\n", str);
- udev_device_get_devlinks(device, devlinks_cb, NULL);
- udev_device_get_properties(device, properties_cb, NULL);
- udev_device_unref(device);
+ printf("dev_path: '%s'\n", str);
- if (subsystem == NULL)
- printf("enumerating devices from all subsystems\n");
- else
- printf("enumerating devices from subsystem: %s\n", subsystem);
- udev_devices_enumerate(udev, subsystem, devices_enum_cb, NULL);
+ test_device(udev, devpath);
+ test_enumerate(udev, subsystem);
+ test_monitor(udev, socket);
udev_unref(udev);
return 0;