1
2
3
4
5 package oshi.driver.unix.solaris;
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.unix.SolarisLibc;
19 import oshi.jna.platform.unix.SolarisLibc.SolarisUtmpx;
20 import oshi.software.os.OSSession;
21
22
23
24
25 @ThreadSafe
26 public final class Who {
27
28 private static final SolarisLibc LIBC = SolarisLibc.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 SolarisUtmpx ut;
41
42 LIBC.setutxent();
43 try {
44
45 while ((ut = LIBC.getutxent()) != null) {
46 if (ut.ut_type == USER_PROCESS || ut.ut_type == LOGIN_PROCESS) {
47 String user = Native.toString(ut.ut_user, StandardCharsets.US_ASCII);
48 String device = Native.toString(ut.ut_line, StandardCharsets.US_ASCII);
49 String host = Native.toString(ut.ut_host, StandardCharsets.US_ASCII);
50 long loginTime = ut.ut_tv.tv_sec.longValue() * 1000L + ut.ut_tv.tv_usec.longValue() / 1000L;
51
52 if (!isSessionValid(user, device, loginTime)) {
53 return oshi.driver.unix.Who.queryWho();
54 }
55 whoList.add(new OSSession(user, device, loginTime, host));
56 }
57 }
58 } finally {
59
60 LIBC.endutxent();
61 }
62 return whoList;
63 }
64 }