-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUsersRoster.cpp
63 lines (53 loc) · 1.4 KB
/
UsersRoster.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* LoggedUsers.cpp
*
* Created on: 15/lug/2013
* Author: Stefano Ceccherini
*/
#include "UsersRoster.h"
#include "Support.h"
#include <cstring>
#include <ctime>
#include <set>
#include <utmpx.h>
#include <iostream>
bool
operator<(const user_entry& A, const user_entry& B)
{
// TODO: Also compare domain
return A.login.compare(B.login);
}
UsersRoster::UsersRoster()
{
std::set<user_entry> userSet;
setutxent();
struct utmpx* record = NULL;
while ((record = getutxent()) != NULL) {
if (record->ut_type == USER_PROCESS) {
user_entry entry;
std::string line = record->ut_user;
size_t domainSeparatorPos = line.find("@");
if (domainSeparatorPos == std::string::npos)
entry.login = record->ut_user;
else {
entry.login = line.substr(0, domainSeparatorPos);
entry.logindomain = line.substr(domainSeparatorPos + 1, -1);
}
time_t loginTime = record->ut_tv.tv_sec;
entry.logintime = loginTime;
struct tm timeInfoStruct;
struct tm* timeinfo = localtime_r(&loginTime, &timeInfoStruct);
char timeString[64];
strftime(timeString, sizeof(timeString), "%a %b %d %R", timeinfo);
entry.logintimestring = timeString;
userSet.insert(entry);
}
}
endutxent();
// Only insert one entry for user, otherwise GLPI rejects the inventory
std::set<user_entry>::const_iterator i;
for (i = userSet.begin(); i != userSet.end(); i++) {
fItems.push_back(*i);
}
Rewind();
}