]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-login: place inner part of sd_login_monitor_new() by a table
authorLennart Poettering <lennart@poettering.net>
Wed, 4 Jun 2025 09:40:45 +0000 (11:40 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 4 Jun 2025 15:58:43 +0000 (17:58 +0200)
src/libsystemd/sd-login/sd-login.c

index 93ceedeb89c2ad48230bca532f561498799a7298..4d32687ff9e9f01a160aaa9057c710c4467da8eb 100644 (file)
@@ -1135,8 +1135,6 @@ static sd_login_monitor* FD_TO_MONITOR(int fd) {
 
 _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **ret) {
         _cleanup_close_ int fd = -EBADF;
-        bool good = false;
-        int k;
 
         assert_return(ret, -EINVAL);
 
@@ -1144,33 +1142,22 @@ _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **ret)
         if (fd < 0)
                 return -errno;
 
-        if (!category || streq(category, "seat")) {
-                k = inotify_add_watch(fd, "/run/systemd/seats/", IN_MOVED_TO|IN_DELETE);
-                if (k < 0)
-                        return -errno;
-
-                good = true;
-        }
-
-        if (!category || streq(category, "session")) {
-                k = inotify_add_watch(fd, "/run/systemd/sessions/", IN_MOVED_TO|IN_DELETE);
-                if (k < 0)
-                        return -errno;
-
-                good = true;
-        }
-
-        if (!category || streq(category, "uid")) {
-                k = inotify_add_watch(fd, "/run/systemd/users/", IN_MOVED_TO|IN_DELETE);
-                if (k < 0)
-                        return -errno;
+        static const struct {
+                const char *name;
+                const char *path;
+        } categories[] = {
+                { "seat",     "/run/systemd/seats/"    },
+                { "session",  "/run/systemd/sessions/" },
+                { "uid",      "/run/systemd/users/"    },
+                { "machine",  "/run/systemd/machines/" },
+        };
 
-                good = true;
-        }
+        bool good = false;
+        FOREACH_ELEMENT(c, categories) {
+                if (category && !streq(category, c->name))
+                        continue;
 
-        if (!category || streq(category, "machine")) {
-                k = inotify_add_watch(fd, "/run/systemd/machines/", IN_MOVED_TO|IN_DELETE);
-                if (k < 0)
+                if (inotify_add_watch(fd, c->path, IN_MOVED_TO|IN_DELETE) < 0)
                         return -errno;
 
                 good = true;