]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-device-monitor: split passes_filter() into two parts
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 20 Feb 2021 22:53:31 +0000 (07:53 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 2 Apr 2021 06:09:03 +0000 (15:09 +0900)
src/libsystemd/sd-device/device-monitor.c

index 3948e862a9f81be01cda8e70e90f4c07b09b78c8..d072171bebda20c67b4f739b8c4e37cbf2746671 100644 (file)
@@ -342,49 +342,62 @@ static sd_device_monitor *device_monitor_free(sd_device_monitor *m) {
 
 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_device_monitor, sd_device_monitor, device_monitor_free);
 
-static int passes_filter(sd_device_monitor *m, sd_device *device) {
-        const char *tag, *subsystem, *devtype, *s, *d = NULL;
+static int check_subsystem_filter(sd_device_monitor *m, sd_device *device) {
+        const char *s, *subsystem, *d, *devtype = NULL;
         int r;
 
         assert(m);
         assert(device);
 
         if (hashmap_isempty(m->subsystem_filter))
-                goto tag;
+                return true;
 
-        r = sd_device_get_subsystem(device, &s);
+        r = sd_device_get_subsystem(device, &subsystem);
         if (r < 0)
                 return r;
 
-        r = sd_device_get_devtype(device, &d);
+        r = sd_device_get_devtype(device, &devtype);
         if (r < 0 && r != -ENOENT)
                 return r;
 
-        HASHMAP_FOREACH_KEY(devtype, subsystem, m->subsystem_filter) {
+        HASHMAP_FOREACH_KEY(d, s, m->subsystem_filter) {
                 if (!streq(s, subsystem))
                         continue;
 
-                if (!devtype)
-                        goto tag;
+                if (!d || streq_ptr(d, devtype))
+                        return true;
+        }
 
-                if (!d)
-                        continue;
+        return false;
+}
 
-                if (streq(d, devtype))
-                        goto tag;
-        }
+static bool check_tag_filter(sd_device_monitor *m, sd_device *device) {
+        const char *tag;
 
-        return 0;
+        assert(m);
+        assert(device);
 
-tag:
         if (set_isempty(m->tag_filter))
-                return 1;
+                return true;
 
         SET_FOREACH(tag, m->tag_filter)
                 if (sd_device_has_tag(device, tag) > 0)
-                        return 1;
+                        return true;
 
-        return 0;
+        return false;
+}
+
+static int passes_filter(sd_device_monitor *m, sd_device *device) {
+        int r;
+
+        assert(m);
+        assert(device);
+
+        r = check_subsystem_filter(m, device);
+        if (r <= 0)
+                return r;
+
+        return check_tag_filter(m, device);
 }
 
 int device_monitor_receive_device(sd_device_monitor *m, sd_device **ret) {