From: Yu Watanabe Date: Fri, 24 Feb 2023 00:20:52 +0000 (+0900) Subject: Merge pull request #26574 from YHNdnzj/sd-login-new-interface X-Git-Tag: v254-rc1~1165 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9cddd367077f8841b5f84786d113ebc4556b06d3;hp=c73676dcbbe3032c75d893d43ac309998c5f23e5;p=thirdparty%2Fsystemd.git Merge pull request #26574 from YHNdnzj/sd-login-new-interface sd-login: add two interfaces for retriving session info --- diff --git a/man/sd_session_is_active.xml b/man/sd_session_is_active.xml index b69fa137821..716d8e162e2 100644 --- a/man/sd_session_is_active.xml +++ b/man/sd_session_is_active.xml @@ -21,7 +21,9 @@ sd_session_is_remote sd_session_get_state 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 @@ -60,12 +62,24 @@ uid_t *uid + + int sd_session_get_username + const char *session + char **username + + int sd_session_get_seat const char *session char **seat + + int sd_session_get_start_time + const char *session + uint64_t *usec + + int sd_session_get_service const char *session @@ -155,6 +169,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 @@ -164,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 @@ -252,6 +277,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 802009b7100..8b5edabb75e 100644 --- a/src/libsystemd/libsystemd.sym +++ b/src/libsystemd/libsystemd.sym @@ -812,3 +812,9 @@ global: sd_pidfd_get_user_unit; sd_journal_get_seqnum; } LIBSYSTEMD_252; + +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 d483889fd18..12cb4e67922 100644 --- a/src/libsystemd/sd-login/sd-login.c +++ b/src/libsystemd/sd-login/sd-login.c @@ -742,10 +742,41 @@ 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); } +_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 85dd086e2b9..526af34d376 100644 --- a/src/systemd/sd-login.h +++ b/src/systemd/sd-login.h @@ -157,9 +157,15 @@ 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); +/* 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);