[systemd-commits] src/libsystemd src/systemd
Lennart Poettering
lennart at kemper.freedesktop.org
Sun May 18 17:33:37 PDT 2014
src/libsystemd/libsystemd.sym.m4 | 7 +++++
src/libsystemd/sd-login/sd-login.c | 44 +++++++++++++++++++++++++++++++------
src/systemd/sd-login.h | 5 +++-
3 files changed, 48 insertions(+), 8 deletions(-)
New commits:
commit a077b666cb883213fd85be21b518c2de54cb70b1
Author: Lennart Poettering <lennart at poettering.net>
Date: Mon May 19 09:33:20 2014 +0900
sd-login: add C API to query primary session of a user
diff --git a/src/libsystemd/libsystemd.sym.m4 b/src/libsystemd/libsystemd.sym.m4
index 7545ae4..61952d7 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 e7eea19..375a538 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 776733a..dad459a 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. */
More information about the systemd-commits
mailing list