Implement `active_sessions_count()` in `utmp.c` and `logind.c`.
Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
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 *);
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;
+}
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;
+}