libmisc: implement active_sessions_count()
Implement `active_sessions_count()` in `utmp.c` and `logind.c`. Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
This commit is contained in:
committed by
Serge Hallyn
parent
1f368e1c18
commit
ede0665a5a
@@ -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 *);
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user