From d71f5b1217986a18e8ddaef3191afddfe731b2d7 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Fri, 24 Feb 2023 00:48:10 +0800 Subject: [PATCH] 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 b69fa137821..b62c35ae740 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 802009b7100..013167f0979 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 d483889fd18..a01855d5830 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 85dd086e2b9..fefc05667b1 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); -- 2.47.3