1
2
3
4
5 package oshi.driver.mac;
6
7 import static oshi.jna.platform.unix.CLibrary.LOGIN_PROCESS;
8 import static oshi.jna.platform.unix.CLibrary.USER_PROCESS;
9 import static oshi.util.Util.isSessionValid;
10
11 import java.nio.charset.StandardCharsets;
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import com.sun.jna.Native;
16
17 import oshi.annotation.concurrent.ThreadSafe;
18 import oshi.jna.platform.mac.SystemB;
19 import oshi.jna.platform.mac.SystemB.MacUtmpx;
20 import oshi.software.os.OSSession;
21
22
23
24
25 @ThreadSafe
26 public final class Who {
27
28 private static final SystemB SYS = SystemB.INSTANCE;
29
30 private Who() {
31 }
32
33
34
35
36
37
38 public static synchronized List<OSSession> queryUtxent() {
39 List<OSSession> whoList = new ArrayList<>();
40 MacUtmpx ut;
41
42 SYS.setutxent();
43 try {
44 while ((ut = SYS.getutxent()) != null) {
45 if (ut.ut_type == USER_PROCESS || ut.ut_type == LOGIN_PROCESS) {
46 String user = Native.toString(ut.ut_user, StandardCharsets.US_ASCII);
47 String device = Native.toString(ut.ut_line, StandardCharsets.US_ASCII);
48 String host = Native.toString(ut.ut_host, StandardCharsets.US_ASCII);
49 long loginTime = ut.ut_tv.tv_sec.longValue() * 1000L + ut.ut_tv.tv_usec / 1000L;
50
51 if (!isSessionValid(user, device, loginTime)) {
52 return oshi.driver.unix.Who.queryWho();
53 }
54 whoList.add(new OSSession(user, device, loginTime, host));
55 }
56 }
57 } finally {
58
59 SYS.endutxent();
60 }
61 return whoList;
62 }
63 }