]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-device-monitor: introduce sd_device_monitor_filter_add_match_sysattr()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 21 Feb 2021 00:33:04 +0000 (09:33 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 2 Apr 2021 06:10:09 +0000 (15:10 +0900)
src/libsystemd/libsystemd.sym
src/libsystemd/sd-device/device-monitor.c
src/systemd/sd-device.h

index 74eff7fcf0822ab998ede1b69d975f793f9125a7..0ac00081ae1cdfbec75c55533f844dcb3003a751 100644 (file)
@@ -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;
index d072171bebda20c67b4f739b8c4e37cbf2746671..b12687cc2b5fe6ac6087a86c54cb202096f5e1fb 100644 (file)
@@ -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;
index 310fcaa278a62dae6396c16830e64d631c4679fa..d7f78d827087f22e0d8b332453cc6f5c4bdc9e91 100644 (file)
@@ -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);