sd-login: add C API to query primary session of a user

This commit is contained in:
Lennart Poettering 2014-05-19 09:33:20 +09:00
parent 952d32609f
commit a077b666cb
3 changed files with 48 additions and 8 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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. */