From dacea9ff6be55f1b115c13b1ab530812d0d66879 Mon Sep 17 00:00:00 2001 From: Kay Sievers Date: Thu, 23 Apr 2009 03:24:38 +0200 Subject: [PATCH] libudev: monitor - switch to filter_add_match_subsystem_devtype() --- udev/lib/exported_symbols | 2 +- udev/lib/libudev-monitor.c | 58 +++++++++++++++++++++++++++----------- udev/lib/libudev.h | 2 +- udev/lib/test-libudev.c | 6 ++-- udev/udevadm-monitor.c | 2 +- 5 files changed, 47 insertions(+), 23 deletions(-) diff --git a/udev/lib/exported_symbols b/udev/lib/exported_symbols index 23cd8fbe..a700f98a 100644 --- a/udev/lib/exported_symbols +++ b/udev/lib/exported_symbols @@ -56,7 +56,7 @@ udev_monitor_unref udev_monitor_get_udev udev_monitor_get_fd udev_monitor_receive_device -udev_monitor_filter_add_match_subsystem +udev_monitor_filter_add_match_subsystem_devtype udev_queue_new udev_queue_ref udev_queue_unref diff --git a/udev/lib/libudev-monitor.c b/udev/lib/libudev-monitor.c index a0f93546..d8b375c7 100644 --- a/udev/lib/libudev-monitor.c +++ b/udev/lib/libudev-monitor.c @@ -56,11 +56,12 @@ struct udev_monitor_netlink_header { unsigned short properties_off; unsigned short properties_len; /* - * hashes of some common device propertie strings to filter with socket filters in + * hashes of some common device properties strings to filter with socket filters in * the client used in the kernel from socket filter rules; needs to be stored in * network order */ unsigned int filter_subsystem; + unsigned int filter_devtype; }; static struct udev_monitor *udev_monitor_new(struct udev *udev) @@ -236,23 +237,34 @@ static int filter_apply(struct udev_monitor *udev_monitor) memset(ins, 0x00, sizeof(ins)); i = 0; - /* load magic in accu */ + /* load magic in A */ bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, magic)); /* jump if magic matches */ bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, UDEV_MONITOR_MAGIC, 1, 0); /* wrong magic, drop packet */ bpf_stmt(ins, &i, BPF_RET|BPF_K, 0); - /* load filter_subsystem value in accu */ - bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_subsystem)); /* add all subsystem match values */ udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) { - const char *subsys = udev_list_entry_get_name(list_entry); unsigned int hash; - hash = util_string_hash32(subsys); - /* jump if value does not match */ - bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1); + /* load filter_subsystem value in A */ + bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_subsystem)); + hash = util_string_hash32(udev_list_entry_get_name(list_entry)); + if (udev_list_entry_get_value(list_entry) == NULL) { + /* jump if subsystem does not match */ + bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1); + } else { + /* jump if subsystem does not match */ + bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 3); + + /* load filter_devtype value in A */ + bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_devtype)); + /* jump if value does not match */ + hash = util_string_hash32(udev_list_entry_get_value(list_entry)); + bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1); + } + /* matched, pass packet */ bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff); @@ -374,20 +386,29 @@ int udev_monitor_get_fd(struct udev_monitor *udev_monitor) static int passes_filter(struct udev_monitor *udev_monitor, struct udev_device *udev_device) { struct udev_list_entry *list_entry; - int pass = 0; if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL) return 1; udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) { - const char *subsys = udev_device_get_subsystem(udev_device); + const char *subsys = udev_list_entry_get_name(list_entry); + const char *dsubsys = udev_device_get_subsystem(udev_device); + const char *devtype; + const char *ddevtype; - if (strcmp(udev_list_entry_get_name(list_entry), subsys) == 0) { - pass= 1; - break; - } + if (strcmp(dsubsys, subsys) != 0) + continue; + + devtype = udev_list_entry_get_value(list_entry); + if (devtype == NULL) + return 1; + ddevtype = udev_device_get_devtype(udev_device); + if (ddevtype == NULL) + continue; + if (strcmp(ddevtype, devtype) == 0) + return 1; } - return pass; + return 0; } /** @@ -648,6 +669,9 @@ int udev_monitor_send_device(struct udev_monitor *udev_monitor, struct udev_devi nlh.magic = htonl(UDEV_MONITOR_MAGIC); val = udev_device_get_subsystem(udev_device); nlh.filter_subsystem = htonl(util_string_hash32(val)); + val = udev_device_get_devtype(udev_device); + if (val != NULL) + nlh.filter_devtype = htonl(util_string_hash32(val)); iov[0].iov_base = &nlh; iov[0].iov_len = sizeof(struct udev_monitor_netlink_header); @@ -672,14 +696,14 @@ int udev_monitor_send_device(struct udev_monitor *udev_monitor, struct udev_devi return count; } -int udev_monitor_filter_add_match_subsystem(struct udev_monitor *udev_monitor, const char *subsystem) +int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor, const char *subsystem, const char *devtype) { if (udev_monitor == NULL) return -EINVAL; if (subsystem == NULL) return 0; if (udev_list_entry_add(udev_monitor->udev, - &udev_monitor->filter_subsystem_list, subsystem, NULL, 1, 0) == NULL) + &udev_monitor->filter_subsystem_list, subsystem, devtype, 0, 0) == NULL) return -ENOMEM; return 0; } diff --git a/udev/lib/libudev.h b/udev/lib/libudev.h index 4094b3a8..e366d768 100644 --- a/udev/lib/libudev.h +++ b/udev/lib/libudev.h @@ -84,7 +84,7 @@ extern void udev_monitor_unref(struct udev_monitor *udev_monitor); extern struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor); extern int udev_monitor_get_fd(struct udev_monitor *udev_monitor); extern struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor); -extern int udev_monitor_filter_add_match_subsystem(struct udev_monitor *udev_monitor, const char *subsystem); +extern int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor, const char *subsystem, const char *devtype); /* sys enumeration */ struct udev_enumerate; diff --git a/udev/lib/test-libudev.c b/udev/lib/test-libudev.c index be25d611..db08d484 100644 --- a/udev/lib/test-libudev.c +++ b/udev/lib/test-libudev.c @@ -228,9 +228,9 @@ static int test_monitor(struct udev *udev) printf("no socket\n"); return -1; } - if (udev_monitor_filter_add_match_subsystem(udev_monitor, "block") < 0 || - udev_monitor_filter_add_match_subsystem(udev_monitor, "tty") < 0 || - udev_monitor_filter_add_match_subsystem(udev_monitor, "usb") < 0) { + if (udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "block", NULL) < 0 || + udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "tty", NULL) < 0 || + udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", "usb_device") < 0) { printf("filter failed\n"); return -1; } diff --git a/udev/udevadm-monitor.c b/udev/udevadm-monitor.c index 92215b67..10b2f2e7 100644 --- a/udev/udevadm-monitor.c +++ b/udev/udevadm-monitor.c @@ -143,7 +143,7 @@ int udevadm_monitor(struct udev *udev, int argc, char *argv[]) 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(udev_monitor, subsys) < 0) + if (udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, subsys, NULL) < 0) fprintf(stderr, "error: unable to apply subsystem filter '%s'\n", subsys); } -- 2.39.5