sd-login: place inner part of sd_login_monitor_new() by a table

This commit is contained in:
Lennart Poettering
2025-06-04 17:58:43 +02:00
parent eac4a028df
commit e2af482f0a
+14 -27
View 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;
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, "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;
good = true;
}
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;