From: Lennart Poettering Date: Wed, 24 Apr 2013 20:54:55 +0000 (-0300) Subject: login: allow watching virtual machines with sd_get_machine_names() X-Git-Tag: v203~87 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=commitdiff_plain;h=e10375f2c0f5dd0dc8508f3ca165eb8aa63c64fb login: allow watching virtual machines with sd_get_machine_names() --- diff --git a/man/sd_login_monitor_new.xml b/man/sd_login_monitor_new.xml index b187ad0db..26af0ea24 100644 --- a/man/sd_login_monitor_new.xml +++ b/man/sd_login_monitor_new.xml @@ -50,7 +50,7 @@ sd_login_monitor_get_events sd_login_monitor_get_timeout sd_login_monitor - Monitor login sessions, seats and users + Monitor login sessions, seats, users and virtual machines/containers @@ -96,23 +96,26 @@ Description sd_login_monitor_new() may - be used to monitor login sessions, users and seats. Via - a monitor object a file descriptor can be integrated - into an application defined event loop which is woken - up each time a user logs in, logs out or a seat is - added or removed, or a session, user, or seat changes - state otherwise. The first parameter takes a string - which can be seat (to get + be used to monitor login sessions, users, seats and + virtual machines/containers. Via a monitor object a + file descriptor can be integrated into an application + defined event loop which is woken up each time a user + logs in, logs out or a seat is added or removed, or a + session, user, seat or virtual machine/container + changes state otherwise. The first parameter takes a + string which can be seat (to get only notifications about seats being added, removed or changed), session (to get only notifications about sessions being created or removed - or changed) or uid (to get only + or changed), uid (to get only notifications when a user changes state in respect to - logins). If notifications shall be generated in all - these conditions, NULL may be passed. Note that in the - future additional categories may be defined. The - second parameter returns a monitor object and needs to - be freed with the + logins) or machine (to get only + notifications when a virtual machine or container is + started or stopped). If notifications shall be + generated in all these conditions, NULL may be + passed. Note that in the future additional categories + may be defined. The second parameter returns a monitor + object and needs to be freed with the sd_login_monitor_unref() call after use. diff --git a/src/login/sd-login.c b/src/login/sd-login.c index 35deb85f2..bc8cd8ae8 100644 --- a/src/login/sd-login.c +++ b/src/login/sd-login.c @@ -677,6 +677,27 @@ _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) { good = true; } + if (!category || streq(category, "machine")) { + _cleanup_free_ char *md = NULL, *p = NULL; + int r; + + r = cg_get_machine_path(&md); + if (r < 0) + return r; + + r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, md, NULL, &p); + if (r < 0) + return r; + + k = inotify_add_watch(fd, p, IN_MOVED_TO|IN_CREATE|IN_DELETE); + if (k < 0) { + close_nointr_nofail(fd); + return -errno; + } + + good = true; + } + if (!good) { close_nointr(fd); return -EINVAL; diff --git a/src/systemd/sd-login.h b/src/systemd/sd-login.h index 241503941..4855e327a 100644 --- a/src/systemd/sd-login.h +++ b/src/systemd/sd-login.h @@ -157,7 +157,8 @@ int sd_get_machine_names(char ***machines); typedef struct sd_login_monitor sd_login_monitor; /* Create a new monitor. Category must be NULL, "seat", "session", - * "uid" to get monitor events for the specific category (or all). */ + * "uid", "machine" to get monitor events for the specific category + * (or all). */ int sd_login_monitor_new(const char *category, sd_login_monitor** ret); /* Destroys the passed monitor. Returns NULL. */