diff options
author | Lennart Poettering <lennart@poettering.net> | 2014-05-19 09:33:20 +0900 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2014-05-19 09:33:20 +0900 |
commit | a077b666cb883213fd85be21b518c2de54cb70b1 (patch) | |
tree | ced168d59b0cc21e8932b08a40a928db5708e012 | |
parent | 952d32609f9bceee444fa2690afb4d28539b4b92 (diff) |
sd-login: add C API to query primary session of a user
-rw-r--r-- | src/libsystemd/libsystemd.sym.m4 | 7 | ||||
-rw-r--r-- | src/libsystemd/sd-login/sd-login.c | 44 | ||||
-rw-r--r-- | src/systemd/sd-login.h | 5 |
3 files changed, 48 insertions, 8 deletions
diff --git a/src/libsystemd/libsystemd.sym.m4 b/src/libsystemd/libsystemd.sym.m4 index 7545ae4dc1..61952d7621 100644 --- a/src/libsystemd/libsystemd.sym.m4 +++ b/src/libsystemd/libsystemd.sym.m4 @@ -135,6 +135,11 @@ global: sd_peer_get_user_unit; sd_peer_get_machine_name; sd_peer_get_slice; +} LIBSYSTEMD_209; + +LIBSYSTEMD_213 { +global: + sd_uid_get_display; m4_ifdef(`ENABLE_KDBUS', /* sd-bus */ @@ -427,4 +432,4 @@ m4_ifdef(`ENABLE_KDBUS', sd_resolve_query_set_userdata; sd_resolve_query_get_resolve; ) -} LIBSYSTEMD_209; +} LIBSYSTEMD_211; diff --git a/src/libsystemd/sd-login/sd-login.c b/src/libsystemd/sd-login/sd-login.c index e7eea19580..375a5388aa 100644 --- a/src/libsystemd/sd-login/sd-login.c +++ b/src/libsystemd/sd-login/sd-login.c @@ -165,6 +165,15 @@ _public_ int sd_peer_get_slice(int fd, char **slice) { return cg_pid_get_slice(ucred.pid, slice); } +static int file_of_uid(uid_t uid, char **p) { + assert(p); + + if (asprintf(p, "/run/systemd/users/" UID_FMT, uid) < 0) + return -ENOMEM; + + return 0; +} + _public_ int sd_uid_get_state(uid_t uid, char**state) { _cleanup_free_ char *p = NULL; char *s = NULL; @@ -172,8 +181,9 @@ _public_ int sd_uid_get_state(uid_t uid, char**state) { assert_return(state, -EINVAL); - if (asprintf(&p, "/run/systemd/users/"UID_FMT, uid) < 0) - return -ENOMEM; + r = file_of_uid(uid, &p); + if (r < 0) + return r; r = parse_env_file(p, NEWLINE, "STATE", &s, NULL); if (r == -ENOENT) { @@ -192,6 +202,29 @@ _public_ int sd_uid_get_state(uid_t uid, char**state) { return 0; } +_public_ int sd_uid_get_display(uid_t uid, char **session) { + _cleanup_free_ char *p = NULL, *s = NULL; + int r; + + assert_return(session, -EINVAL); + + r = file_of_uid(uid, &p); + if (r < 0) + return r; + + r = parse_env_file(p, NEWLINE, "DISPLAY", &s, NULL); + if (r < 0) + return r; + + if (isempty(s)) + return -ENOENT; + + *session = s; + s = NULL; + + return 0; +} + _public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat) { char *w, *state; _cleanup_free_ char *t = NULL, *s = NULL, *p = NULL; @@ -231,8 +264,9 @@ static int uid_get_array(uid_t uid, const char *variable, char ***array) { char **a; int r; - if (asprintf(&p, "/run/systemd/users/"UID_FMT, uid) < 0) - return -ENOMEM; + r = file_of_uid(uid, &p); + if (r < 0) + return r; r = parse_env_file(p, NEWLINE, variable, &s, @@ -362,7 +396,6 @@ _public_ int sd_session_get_state(const char *session, char **state) { return r; r = parse_env_file(p, NEWLINE, "STATE", &s, NULL); - if (r < 0) return r; else if (!s) @@ -405,7 +438,6 @@ static int session_get_string(const char *session, const char *field, char **val return r; r = parse_env_file(p, NEWLINE, field, &s, NULL); - if (r < 0) return r; diff --git a/src/systemd/sd-login.h b/src/systemd/sd-login.h index 776733ad1d..dad459a7e8 100644 --- a/src/systemd/sd-login.h +++ b/src/systemd/sd-login.h @@ -103,7 +103,10 @@ int sd_peer_get_machine_name(int fd, char **machine); int sd_peer_get_slice(int fd, char **slice); /* Get state from UID. Possible states: offline, lingering, online, active, closing */ -int sd_uid_get_state(uid_t uid, char**state); +int sd_uid_get_state(uid_t uid, char **state); + +/* Return primary session of user, if there is any */ +int sd_uid_get_display(uid_t uid, char **session); /* Return 1 if UID has session on seat. If require_active is true, this will * look for active sessions only. */ |