From b391ab0fb9d45c1ef00bd12f25d240801db62cbf Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Mon, 18 Feb 2013 18:20:00 +0000 Subject: [PATCH 3/5] sd_uid_get_display: add This can be used to get a best-effort X11 display, suitable for passing to user-bus-activated services. Signed-off-by: Simon McVittie --- Makefile.am | 1 + man/sd_uid_get_state.xml | 22 ++++++++++++++++++++++ src/login/libsystemd-login.sym | 1 + src/login/sd-login.c | 26 ++++++++++++++++++++++++++ src/systemd/sd-login.h | 3 +++ 5 files changed, 53 insertions(+) diff --git a/Makefile.am b/Makefile.am index 10934eb..62a91aa 100644 --- a/Makefile.am +++ b/Makefile.am @@ -896,6 +896,7 @@ libsystemd_core_la_LIBADD = \ libsystemd-audit.la \ libsystemd-id128-internal.la \ libsystemd-daemon.la \ + libsystemd-login-internal.la \ libudev.la \ $(LIBWRAP_LIBS) \ $(PAM_LIBS) \ diff --git a/man/sd_uid_get_state.xml b/man/sd_uid_get_state.xml index cc8fc0f..6cfc6b7 100644 --- a/man/sd_uid_get_state.xml +++ b/man/sd_uid_get_state.xml @@ -47,6 +47,7 @@ sd_uid_is_on_seat sd_uid_get_sessions sd_uid_get_seats + sd_uid_get_display Determine login state of a specific Unix user ID @@ -80,6 +81,12 @@ int require_active char*** seats + + + int sd_uid_get_display + uid_t uid + char** display + @@ -147,6 +154,15 @@ with no attached seat and hence the number of entries in the returned array may differ from the one returned by sd_uid_get_sessions(). + + sd_uid_get_display() may + be used to determine an X11 display for the + specified user. If they have more than one, an arbitrary one + is returned. The display returned through the + display needs to be + freed by the caller with the libc + free3 + function. @@ -162,6 +178,11 @@ number of entries in the returned arrays. On failure, these calls return a negative errno-style error code. + + On success, + sd_uid_get_display() + returns 0. On failure, it returns a negative errno-style + error code. @@ -169,6 +190,7 @@ The sd_uid_get_state(), sd_uid_is_on_seat(), + sd_uid_get_display(), sd_uid_get_sessions(), and sd_uid_get_seats() interfaces are available as shared library, which can be compiled and diff --git a/src/login/libsystemd-login.sym b/src/login/libsystemd-login.sym index 272b0e2..7a53332 100644 --- a/src/login/libsystemd-login.sym +++ b/src/login/libsystemd-login.sym @@ -57,4 +57,5 @@ global: LIBSYSTEMD_LOGIN_198 { global: sd_session_get_tty; + sd_uid_get_display; } LIBSYSTEMD_LOGIN_186; diff --git a/src/login/sd-login.c b/src/login/sd-login.c index 8867e8c..2214a87 100644 --- a/src/login/sd-login.c +++ b/src/login/sd-login.c @@ -165,6 +165,32 @@ _public_ int sd_uid_get_state(uid_t uid, char**state) { return 0; } +_public_ int sd_uid_get_display(uid_t uid, char **display) { + char *p, *s = NULL; + int r; + + if (!display) + return -EINVAL; + + if (asprintf(&p, "/run/systemd/users/%lu", (unsigned long) uid) < 0) + return -ENOMEM; + + r = parse_env_file(p, NEWLINE, "DISPLAY", &s, NULL); + free(p); + + if (r < 0) { + free(s); + return r; + } else if (isempty(s)) { + free(s); + return -ENOENT; + } + + r = sd_session_get_display(s, display); + free(s); + return r; +} + _public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat) { char *p, *w, *t, *state, *s = NULL; size_t l; diff --git a/src/systemd/sd-login.h b/src/systemd/sd-login.h index 3746b45..dcc8ab3 100644 --- a/src/systemd/sd-login.h +++ b/src/systemd/sd-login.h @@ -82,6 +82,9 @@ int sd_uid_get_sessions(uid_t uid, int require_active, char ***sessions); * just return number of seats.*/ int sd_uid_get_seats(uid_t uid, int require_active, char ***seats); +/* If uid has a session with an X11 display, get it. */ +int sd_uid_get_display(uid_t uid, char **display); + /* Return 1 if the session is a active. */ int sd_session_is_active(const char *session); -- 1.7.10.4