]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Merge pull request #26574 from YHNdnzj/sd-login-new-interface
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 24 Feb 2023 00:20:52 +0000 (09:20 +0900)
committerGitHub <noreply@github.com>
Fri, 24 Feb 2023 00:20:52 +0000 (09:20 +0900)
sd-login: add two interfaces for retriving session info

man/sd_session_is_active.xml
src/libsystemd/libsystemd.sym
src/libsystemd/sd-login/sd-login.c
src/systemd/sd-login.h

index b69fa137821ad932b698ad07880b45e1c93f5b61..716d8e162e24c4d31f18288f47165b1a3cf277b8 100644 (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>
         <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>
     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
     <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
     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>,
index 802009b710013132316a9cce19984dd9c5507d8e..8b5edabb75eb4749000381a89318cdb5bab4b3a5 100644 (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;
index d483889fd18531c8bc8d3dadd2fa65a6531d2041..12cb4e67922370bc3d1e4eb133580ad67f33b06a 100644 (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);
 }
index 85dd086e2b94e384887d849a166bc9256c89bf78..526af34d376dc2b2db377edb5f7bc6ef0d407bf4 100644 (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);