From: Yu Watanabe Date: Sun, 21 Feb 2021 00:33:04 +0000 (+0900) Subject: sd-device-monitor: introduce sd_device_monitor_filter_add_match_sysattr() X-Git-Tag: v249-rc1~483^2~6 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d9b030b673576ca217ad1a1844b9996b32309eec;p=thirdparty%2Fsystemd.git sd-device-monitor: introduce sd_device_monitor_filter_add_match_sysattr() --- diff --git a/src/libsystemd/libsystemd.sym b/src/libsystemd/libsystemd.sym index 74eff7fcf08..0ac00081ae1 100644 --- a/src/libsystemd/libsystemd.sym +++ b/src/libsystemd/libsystemd.sym @@ -751,3 +751,8 @@ global: sd_device_new_from_stat_rdev; sd_device_trigger; } LIBSYSTEMD_247; + +LIBSYSTEMD_249 { +global: + sd_device_monitor_filter_add_match_sysattr; +} LIBSYSTEMD_248; diff --git a/src/libsystemd/sd-device/device-monitor.c b/src/libsystemd/sd-device/device-monitor.c index d072171bebd..b12687cc2b5 100644 --- a/src/libsystemd/sd-device/device-monitor.c +++ b/src/libsystemd/sd-device/device-monitor.c @@ -37,6 +37,8 @@ struct sd_device_monitor { Hashmap *subsystem_filter; Set *tag_filter; + Hashmap *match_sysattr_filter; + Hashmap *nomatch_sysattr_filter; bool filter_uptodate; sd_event *event; @@ -336,6 +338,8 @@ static sd_device_monitor *device_monitor_free(sd_device_monitor *m) { hashmap_free(m->subsystem_filter); set_free(m->tag_filter); + hashmap_free(m->match_sysattr_filter); + hashmap_free(m->nomatch_sysattr_filter); return mfree(m); } @@ -397,7 +401,10 @@ static int passes_filter(sd_device_monitor *m, sd_device *device) { if (r <= 0) return r; - return check_tag_filter(m, device); + if (!check_tag_filter(m, device)) + return false; + + return device_match_sysattr(device, m->match_sysattr_filter, m->nomatch_sysattr_filter); } int device_monitor_receive_device(sd_device_monitor *m, sd_device **ret) { @@ -760,6 +767,21 @@ _public_ int sd_device_monitor_filter_add_match_tag(sd_device_monitor *m, const return r; } +_public_ int sd_device_monitor_filter_add_match_sysattr(sd_device_monitor *m, const char *sysattr, const char *value, int match) { + Hashmap **hashmap; + + assert_return(m, -EINVAL); + assert_return(sysattr, -EINVAL); + + if (match) + hashmap = &m->match_sysattr_filter; + else + hashmap = &m->nomatch_sysattr_filter; + + /* TODO: unset m->filter_uptodate on success when we support this filter on BPF. */ + return hashmap_put_strdup_full(hashmap, &trivial_hash_ops_free_free, sysattr, value); +} + _public_ int sd_device_monitor_filter_remove(sd_device_monitor *m) { static const struct sock_fprog filter = { 0, NULL }; @@ -767,6 +789,8 @@ _public_ int sd_device_monitor_filter_remove(sd_device_monitor *m) { m->subsystem_filter = hashmap_free(m->subsystem_filter); m->tag_filter = set_free(m->tag_filter); + m->match_sysattr_filter = hashmap_free(m->match_sysattr_filter); + m->nomatch_sysattr_filter = hashmap_free(m->nomatch_sysattr_filter); if (setsockopt(m->sock, SOL_SOCKET, SO_DETACH_FILTER, &filter, sizeof(filter)) < 0) return -errno; diff --git a/src/systemd/sd-device.h b/src/systemd/sd-device.h index 310fcaa278a..d7f78d82708 100644 --- a/src/systemd/sd-device.h +++ b/src/systemd/sd-device.h @@ -136,6 +136,7 @@ int sd_device_monitor_stop(sd_device_monitor *m); int sd_device_monitor_filter_add_match_subsystem_devtype(sd_device_monitor *m, const char *subsystem, const char *devtype); int sd_device_monitor_filter_add_match_tag(sd_device_monitor *m, const char *tag); +int sd_device_monitor_filter_add_match_sysattr(sd_device_monitor *m, const char *sysattr, const char *value, int match); int sd_device_monitor_filter_update(sd_device_monitor *m); int sd_device_monitor_filter_remove(sd_device_monitor *m);