]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
libmisc: implement `active_sessions_count()`
authorIker Pedrosa <ipedrosa@redhat.com>
Wed, 19 Jul 2023 09:02:55 +0000 (11:02 +0200)
committerSerge Hallyn <serge@hallyn.com>
Wed, 2 Aug 2023 15:13:28 +0000 (10:13 -0500)
Implement `active_sessions_count()` in `utmp.c` and `logind.c`.

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
lib/prototypes.h
libmisc/logind.c
libmisc/utmp.c

index bb3b9391cf342b33613de3a4b8617e2dacf5a9ee..a1338341077102ede0bc1d20d86e696261c09b3d 100644 (file)
@@ -511,6 +511,18 @@ extern void record_failure(const char *failent_user,
                            const char *hostname);
 #endif /* ENABLE_LOGIND */
 
+/**
+ * @brief Number of active user sessions
+ *
+ * @param[in] name username
+ * @param[in] limit maximum number of active sessions
+ *
+ * @return number of active sessions.
+ *
+ */
+extern unsigned long active_sessions_count(const char *name,
+                                           unsigned long limit);
+
 /* valid.c */
 extern bool valid (const char *, const struct passwd *);
 
index ac1b3a141930ba41b6ea38bfd71854681c3cb36d..d4d217ceb6fe34e710810c8cc2789dfee797e550 100644 (file)
@@ -34,3 +34,18 @@ done:
     free (session);
     return ret;
 }
+
+unsigned long active_sessions_count(const char *name, unsigned long unused)
+{
+    struct passwd *pw;
+    unsigned long count = 0;
+
+    pw = prefix_getpwnam(name);
+    if (pw == NULL) {
+        return 0;
+    }
+
+    count = sd_uid_get_sessions(pw->pw_uid, 0, NULL);
+
+    return count;
+}
index 0a86b18e3db1ea233ca18c5eae59b6ceaa40c278..e5e79cffcf2eee5ddb57baafa06b536e7971bceb 100644 (file)
@@ -395,3 +395,30 @@ void record_failure(const char *failent_user,
                free (failent);
        }
 }
+
+unsigned long active_sessions_count(const char *name, unsigned long limit)
+{
+       struct utmp *ut;
+       unsigned long count = 0;
+
+       setutent ();
+       while ((ut = getutent ()))
+       {
+               if (USER_PROCESS != ut->ut_type) {
+                       continue;
+               }
+               if ('\0' == ut->ut_user[0]) {
+                       continue;
+               }
+               if (strncmp (name, ut->ut_user, sizeof (ut->ut_user)) != 0) {
+                       continue;
+               }
+               count++;
+               if (count > limit) {
+                       break;
+               }
+       }
+       endutent ();
+
+       return count;
+}