summaryrefslogtreecommitdiff
path: root/src/libsystemd/sd-login
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2014-05-19 09:33:20 +0900
committerLennart Poettering <lennart@poettering.net>2014-05-19 09:33:20 +0900
commita077b666cb883213fd85be21b518c2de54cb70b1 (patch)
treeced168d59b0cc21e8932b08a40a928db5708e012 /src/libsystemd/sd-login
parent952d32609f9bceee444fa2690afb4d28539b4b92 (diff)
sd-login: add C API to query primary session of a user
Diffstat (limited to 'src/libsystemd/sd-login')
-rw-r--r--src/libsystemd/sd-login/sd-login.c44
1 files changed, 38 insertions, 6 deletions
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;