From c4ef14dc2a0eaae6b93d41e5c82f50ee86e480a4 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Fri, 24 Feb 2023 01:45:50 +0800 Subject: [PATCH] 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 b62c35ae740..716d8e162e2 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 013167f0979..8b5edabb75e 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 a01855d5830..12cb4e67922 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 fefc05667b1..526af34d376 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);