From d71f5b1217986a18e8ddaef3191afddfe731b2d7 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Fri, 24 Feb 2023 00:48:10 +0800 Subject: [PATCH 1/2] sd-login: add sd_session_get_username --- man/sd_session_is_active.xml | 15 +++++++++++++++ src/libsystemd/libsystemd.sym | 5 +++++ src/libsystemd/sd-login/sd-login.c | 4 ++++ src/systemd/sd-login.h | 3 +++ 4 files changed, 27 insertions(+) diff --git a/man/sd_session_is_active.xml b/man/sd_session_is_active.xml index b69fa13782..b62c35ae74 100644 --- a/man/sd_session_is_active.xml +++ b/man/sd_session_is_active.xml @@ -21,6 +21,7 @@ sd_session_is_remote sd_session_get_state sd_session_get_uid + sd_session_get_username sd_session_get_seat sd_session_get_service sd_session_get_type @@ -60,6 +61,12 @@ uid_t *uid + + int sd_session_get_username + const char *session + char **username + + int sd_session_get_seat const char *session @@ -155,6 +162,13 @@ determine the user identifier of the Unix user the session identified by the specified session identifier belongs to. + sd_session_get_username() may be used to + determine the name of the Unix user the session identified by + the specified session identifier belongs to. The returned string + needs to be freed with the libc + free3 + call after use. + sd_session_get_seat() may be used to determine the seat identifier of the seat the session identified by the specified session identifier belongs to. Note that not all @@ -252,6 +266,7 @@ positive integer; if it fails, 0. On success, sd_session_get_state(), sd_session_get_uid(), + sd_session_get_username(), sd_session_get_seat(), sd_session_get_service(), sd_session_get_type(), diff --git a/src/libsystemd/libsystemd.sym b/src/libsystemd/libsystemd.sym index 802009b710..013167f097 100644 --- a/src/libsystemd/libsystemd.sym +++ b/src/libsystemd/libsystemd.sym @@ -812,3 +812,8 @@ global: sd_pidfd_get_user_unit; sd_journal_get_seqnum; } LIBSYSTEMD_252; + +LIBSYSTEMD_254 { +global: + sd_session_get_username; +} LIBSYSTEMD_253; diff --git a/src/libsystemd/sd-login/sd-login.c b/src/libsystemd/sd-login/sd-login.c index d483889fd1..a01855d583 100644 --- a/src/libsystemd/sd-login/sd-login.c +++ b/src/libsystemd/sd-login/sd-login.c @@ -742,6 +742,10 @@ static int session_get_string(const char *session, const char *field, char **val return 0; } +_public_ int sd_session_get_username(const char *session, char **username) { + return session_get_string(session, "USER", username); +} + _public_ int sd_session_get_seat(const char *session, char **seat) { return session_get_string(session, "SEAT", seat); } diff --git a/src/systemd/sd-login.h b/src/systemd/sd-login.h index 85dd086e2b..fefc05667b 100644 --- a/src/systemd/sd-login.h +++ b/src/systemd/sd-login.h @@ -157,6 +157,9 @@ int sd_session_get_state(const char *session, char **state); /* Determine user ID of session */ int sd_session_get_uid(const char *session, uid_t *uid); +/* Determine username of session */ +int sd_session_get_username(const char *session, char **username); + /* Determine seat of session */ int sd_session_get_seat(const char *session, char **seat); From c4ef14dc2a0eaae6b93d41e5c82f50ee86e480a4 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Fri, 24 Feb 2023 01:45:50 +0800 Subject: [PATCH 2/2] sd-login: add sd_session_get_start_time --- man/sd_session_is_active.xml | 11 +++++++++++ src/libsystemd/libsystemd.sym | 1 + src/libsystemd/sd-login/sd-login.c | 27 +++++++++++++++++++++++++++ src/systemd/sd-login.h | 3 +++ 4 files changed, 42 insertions(+) diff --git a/man/sd_session_is_active.xml b/man/sd_session_is_active.xml index b62c35ae74..716d8e162e 100644 --- a/man/sd_session_is_active.xml +++ b/man/sd_session_is_active.xml @@ -23,6 +23,7 @@ sd_session_get_uid sd_session_get_username sd_session_get_seat + sd_session_get_start_time sd_session_get_service sd_session_get_type sd_session_get_class @@ -73,6 +74,12 @@ char **seat + + int sd_session_get_start_time + const char *session + uint64_t *usec + + int sd_session_get_service const char *session @@ -178,6 +185,10 @@ free3 call after use. + sd_session_get_start_time() may be used to + determine the start time of the session identified by the specified + session identifier belongs to. + sd_session_get_service() may be used to determine the name of the service (as passed during PAM session setup) that registered the session identified by the specified diff --git a/src/libsystemd/libsystemd.sym b/src/libsystemd/libsystemd.sym index 013167f097..8b5edabb75 100644 --- a/src/libsystemd/libsystemd.sym +++ b/src/libsystemd/libsystemd.sym @@ -816,4 +816,5 @@ global: LIBSYSTEMD_254 { global: sd_session_get_username; + sd_session_get_start_time; } LIBSYSTEMD_253; diff --git a/src/libsystemd/sd-login/sd-login.c b/src/libsystemd/sd-login/sd-login.c index a01855d583..12cb4e6792 100644 --- a/src/libsystemd/sd-login/sd-login.c +++ b/src/libsystemd/sd-login/sd-login.c @@ -750,6 +750,33 @@ _public_ int sd_session_get_seat(const char *session, char **seat) { return session_get_string(session, "SEAT", seat); } +_public_ int sd_session_get_start_time(const char *session, uint64_t *usec) { + _cleanup_free_ char *p = NULL, *s = NULL; + usec_t t; + int r; + + assert_return(usec, -EINVAL); + + r = file_of_session(session, &p); + if (r < 0) + return r; + + r = parse_env_file(NULL, p, "REALTIME", &s); + if (r == -ENOENT) + return -ENXIO; + if (r < 0) + return r; + if (isempty(s)) + return -EIO; + + r = safe_atou64(s, &t); + if (r < 0) + return r; + + *usec = t; + return 0; +} + _public_ int sd_session_get_tty(const char *session, char **tty) { return session_get_string(session, "TTY", tty); } diff --git a/src/systemd/sd-login.h b/src/systemd/sd-login.h index fefc05667b..526af34d37 100644 --- a/src/systemd/sd-login.h +++ b/src/systemd/sd-login.h @@ -163,6 +163,9 @@ int sd_session_get_username(const char *session, char **username); /* Determine seat of session */ int sd_session_get_seat(const char *session, char **seat); +/* Determine the start time of session */ +int sd_session_get_start_time(const char *session, uint64_t *usec); + /* Determine the (PAM) service name this session was registered by. */ int sd_session_get_service(const char *session, char **service);