Merge pull request #26574 from YHNdnzj/sd-login-new-interface

sd-login: add two interfaces for retriving session info
This commit is contained in:
Yu Watanabe
2023-02-24 09:20:52 +09:00
committed by GitHub
4 changed files with 69 additions and 0 deletions
+26
View File
@@ -21,7 +21,9 @@
<refname>sd_session_is_remote</refname>
<refname>sd_session_get_state</refname>
<refname>sd_session_get_uid</refname>
<refname>sd_session_get_username</refname>
<refname>sd_session_get_seat</refname>
<refname>sd_session_get_start_time</refname>
<refname>sd_session_get_service</refname>
<refname>sd_session_get_type</refname>
<refname>sd_session_get_class</refname>
@@ -60,12 +62,24 @@
<paramdef>uid_t *<parameter>uid</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>int <function>sd_session_get_username</function></funcdef>
<paramdef>const char *<parameter>session</parameter></paramdef>
<paramdef>char **<parameter>username</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>int <function>sd_session_get_seat</function></funcdef>
<paramdef>const char *<parameter>session</parameter></paramdef>
<paramdef>char **<parameter>seat</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>int <function>sd_session_get_start_time</function></funcdef>
<paramdef>const char *<parameter>session</parameter></paramdef>
<paramdef>uint64_t *<parameter>usec</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>int <function>sd_session_get_service</function></funcdef>
<paramdef>const char *<parameter>session</parameter></paramdef>
@@ -155,6 +169,13 @@
determine the user identifier of the Unix user the session
identified by the specified session identifier belongs to.</para>
<para><function>sd_session_get_username()</function> 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
<citerefentry project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
call after use.</para>
<para><function>sd_session_get_seat()</function> 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 @@
<citerefentry project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
call after use.</para>
<para><function>sd_session_get_start_time()</function> may be used to
determine the start time of the session identified by the specified
session identifier belongs to.</para>
<para><function>sd_session_get_service()</function> 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,
<function>sd_session_get_state()</function>,
<function>sd_session_get_uid()</function>,
<function>sd_session_get_username()</function>,
<function>sd_session_get_seat()</function>,
<function>sd_session_get_service()</function>,
<function>sd_session_get_type()</function>,
+6
View File
@@ -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;
+31
View File
@@ -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);
}
+6
View File
@@ -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);