Class AbstractOperatingSystem
- All Implemented Interfaces:
OperatingSystem
- Direct Known Subclasses:
AixOperatingSystem, DragonFlyBsdOperatingSystem, FreeBsdOperatingSystemJNA, LinuxOperatingSystem, MacOperatingSystem, NetBsdOperatingSystem, OpenBsdOperatingSystem, SolarisOperatingSystem, WindowsOperatingSystem
-
Nested Class Summary
Nested classes/interfaces inherited from interface OperatingSystem
OperatingSystem.OSVersionInfo, OperatingSystem.ProcessFiltering, OperatingSystem.ProcessSortingModifier and TypeInterfaceDescriptionstatic classA class representing the Operating System version details.static final classConstants which may be used to filter Process lists inOperatingSystem.getProcesses(Predicate, Comparator, int),OperatingSystem.getChildProcesses(int, Predicate, Comparator, int), andOperatingSystem.getDescendantProcesses(int, Predicate, Comparator, int).static final classConstants which may be used to sort Process lists inOperatingSystem.getProcesses(Predicate, Comparator, int),OperatingSystem.getChildProcesses(int, Predicate, Comparator, int), andOperatingSystem.getDescendantProcesses(int, Predicate, Comparator, int). -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final booleanWhether to use the 'who' command for session queries. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintGets the bitness (32 or 64) of the operating system.getChildProcesses(int parentPid, Predicate<OSProcess> filter, Comparator<OSProcess> sort, int limit) Gets currently running child processes of provided parent PID, optionally filtering, sorting, and limited to the top "N".getChildrenOrDescendants(Collection<OSProcess> allProcs, int parentPid, boolean allDescendants) Utility method for subclasses to take a full process list as input and return the children or descendants of a particular process.getChildrenOrDescendants(Map<Integer, Integer> parentPidMap, int parentPid, boolean allDescendants) Utility method for subclasses to take a map of pid to parent as input and return the children or descendants of a particular process.getDescendantProcesses(int parentPid, Predicate<OSProcess> filter, Comparator<OSProcess> sort, int limit) Gets currently running processes of provided parent PID's descendants, including their children, the children's children, etc., optionally filtering, sorting, and limited to the top "N".getDesktopWindows(boolean visibleOnly) Gets windows on the operating system's GUI desktop.Get the Operating System family.Get the Operating System manufacturer.getProcesses(Predicate<OSProcess> filter, Comparator<OSProcess> sort, int limit) Gets currently running processes, optionally filtering, sorting, and limited to the top "N".Gets currently logged in users.Get Operating System version information.booleanDetermine whether the current process has elevated permissions such as sudo / AdministratorQueries all processes on the system.protected abstract intqueryBitness(int jvmBitness) Backup OS-specific query to determine bitness if previous checks failqueryChildProcesses(int parentPid) Queries the children of a process.queryDescendantProcesses(int parentPid) Queries the descendants of a process.protected abstract Pair<String, OperatingSystem.OSVersionInfo> Queries the OS family and version info.protected abstract StringQueries the OS manufacturer.toString()Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface OperatingSystem
getCgroupInfo, getCurrentProcess, getCurrentThread, getFileSystem, getInstalledApplications, getInternetProtocolStats, getNetworkParams, getProcess, getProcessCount, getProcesses, getProcesses, getProcessId, getServices, getSystemBootTime, getSystemUptime, getThreadCount, getThreadIdModifier and TypeMethodDescriptiondefault CgroupInfoRetrieves cgroup information for the current process.default OSProcessGets the current process.Makes a best effort to get the current thread.Instantiates aFileSystemobject.default List<ApplicationInfo> Retrieves a list of installed applications on the system.Instantiates aInternetProtocolStatsobject.Instantiates aNetworkParamsobject.getProcess(int pid) Gets information on a currently running processintGet the number of processes currently runningGets currently running processes.getProcesses(Collection<Integer> pids) Gets information on aCollectionof currently running processes.intGets the current process ID (PID).Gets the all services on the system.longGet Unix time of boot.longGet the System up time (time since boot).intGet the number of threads currently runningintMakes a best effort to get the current thread ID (TID).
-
Field Details
-
USE_WHO_COMMAND
protected static final boolean USE_WHO_COMMANDWhether to use the 'who' command for session queries.
-
-
Constructor Details
-
AbstractOperatingSystem
protected AbstractOperatingSystem()Default constructor.
-
-
Method Details
-
getManufacturer
Description copied from interface:OperatingSystemGet the Operating System manufacturer.- Specified by:
getManufacturerin interfaceOperatingSystem- Returns:
- the manufacturer
-
queryManufacturer
Queries the OS manufacturer.- Returns:
- the manufacturer string
-
getFamily
Description copied from interface:OperatingSystemGet the Operating System family.- Specified by:
getFamilyin interfaceOperatingSystem- Returns:
- the family
-
getVersionInfo
Description copied from interface:OperatingSystemGet Operating System version information.- Specified by:
getVersionInfoin interfaceOperatingSystem- Returns:
- version information
-
queryFamilyVersionInfo
Queries the OS family and version info.- Returns:
- a pair of family string and OS version info
-
getBitness
public int getBitness()Description copied from interface:OperatingSystemGets the bitness (32 or 64) of the operating system.- Specified by:
getBitnessin interfaceOperatingSystem- Returns:
- The number of bits supported by the operating system.
-
queryBitness
protected abstract int queryBitness(int jvmBitness) Backup OS-specific query to determine bitness if previous checks fail- Parameters:
jvmBitness- The bitness of the JVM- Returns:
- The operating system bitness
-
getProcesses
public List<OSProcess> getProcesses(Predicate<OSProcess> filter, Comparator<OSProcess> sort, int limit) Description copied from interface:OperatingSystemGets currently running processes, optionally filtering, sorting, and limited to the top "N".Example: get the top 10 processes by CPU usage:
List<OSProcess> topCpu = os.getProcesses(ProcessFiltering.VALID_PROCESS, ProcessSorting.CPU_DESC, 10);- Specified by:
getProcessesin interfaceOperatingSystem- Parameters:
filter- An optionalPredicatelimiting the results to the specified filter. Some common predicates are available inOperatingSystem.ProcessFiltering. May benullfor no filtering.sort- An optionalComparatorspecifying the sorting order. Some common comparators are available inOperatingSystem.ProcessSorting. May benullfor no sorting.limit- Max number of results to return, or 0 to return all results- Returns:
- A list of
OSProcessobjects, optionally filtered, sorted, and limited to the specified number.The list may contain processes with a state of
OSProcess.State.INVALIDif a process terminates during iteration.
-
queryAllProcesses
-
getChildProcesses
public List<OSProcess> getChildProcesses(int parentPid, Predicate<OSProcess> filter, Comparator<OSProcess> sort, int limit) Description copied from interface:OperatingSystemGets currently running child processes of provided parent PID, optionally filtering, sorting, and limited to the top "N".- Specified by:
getChildProcessesin interfaceOperatingSystem- Parameters:
parentPid- The Process ID whose children to list.filter- An optionalPredicatelimiting the results to the specified filter. Some common predicates are available inOperatingSystem.ProcessSorting. May benullfor no filtering.sort- An optionalComparatorspecifying the sorting order. Some common comparators are available inOperatingSystem.ProcessSorting. May benullfor no sorting.limit- Max number of results to return, or 0 to return all results- Returns:
- A list of
OSProcessobjects representing the currently running child processes of the provided PID, optionally filtered, sorted, and limited to the specified number.The list may contain processes with a state of
OSProcess.State.INVALIDif a process terminates during iteration.
-
queryChildProcesses
-
getDescendantProcesses
public List<OSProcess> getDescendantProcesses(int parentPid, Predicate<OSProcess> filter, Comparator<OSProcess> sort, int limit) Description copied from interface:OperatingSystemGets currently running processes of provided parent PID's descendants, including their children, the children's children, etc., optionally filtering, sorting, and limited to the top "N".- Specified by:
getDescendantProcessesin interfaceOperatingSystem- Parameters:
parentPid- The Process ID whose children to list.filter- An optionalPredicatelimiting the results to the specified filter. Some common predicates are available inOperatingSystem.ProcessSorting. May benullfor no filtering.sort- An optionalComparatorspecifying the sorting order. Some common comparators are available inOperatingSystem.ProcessSorting. May benullfor no sorting.limit- Max number of results to return, or 0 to return all results- Returns:
- A list of
OSProcessobjects representing the currently running descendant processes of the provided PID, optionally filtered, sorted, and limited to the specified number.The list may contain processes with a state of
OSProcess.State.INVALIDif a process terminates during iteration.
-
queryDescendantProcesses
-
getChildrenOrDescendants
protected static Set<Integer> getChildrenOrDescendants(Collection<OSProcess> allProcs, int parentPid, boolean allDescendants) Utility method for subclasses to take a full process list as input and return the children or descendants of a particular process. The process itself is also returned to more efficiently extract its start time for filtering- Parameters:
allProcs- A collection of all processesparentPid- The process ID whose children or descendants to returnallDescendants- If false, only gets immediate children of this process. If true, gets all descendants.- Returns:
- Set of children or descendants of parentPid
-
getChildrenOrDescendants
protected static Set<Integer> getChildrenOrDescendants(Map<Integer, Integer> parentPidMap, int parentPid, boolean allDescendants) Utility method for subclasses to take a map of pid to parent as input and return the children or descendants of a particular process.- Parameters:
parentPidMap- a map of all processes with processID as key and parentProcessID as valueparentPid- The process ID whose children or descendants to returnallDescendants- If false, only gets immediate children of this process. If true, gets all descendants.- Returns:
- Set of children or descendants of parentPid, including the parent
-
isElevated
public boolean isElevated()Description copied from interface:OperatingSystemDetermine whether the current process has elevated permissions such as sudo / Administrator- Specified by:
isElevatedin interfaceOperatingSystem- Returns:
- True if this process has elevated permissions
-
getSessions
Description copied from interface:OperatingSystemGets currently logged in users.On macOS, Linux, and Unix systems, the default implementation uses native code (see
man getutxent) that is not thread safe. OSHI's use of this code is synchronized and may be used in a multi-threaded environment without introducing any additional conflicts. Users should note, however, that other operating system code may access the same native code.The
Who#queryWho()method produces similar output parsing the output of the Posix-standardwhocommand, and may internally employ reentrant code on some platforms. Users may opt to use this command-line variant by default using theoshi.os.unix.whocommandconfiguration property.- Specified by:
getSessionsin interfaceOperatingSystem- Returns:
- A list of
OSSessionobjects representing logged-in users
-
getDesktopWindows
Description copied from interface:OperatingSystemGets windows on the operating system's GUI desktop.On Unix-like systems, reports X11 windows only, which may be limited to the current display and will not report windows used by other window managers.
While not a guarantee, a best effort is made to return windows in foreground-to-background order. This ordering may be used along with
OSDesktopWindow.getOrder()to (probably) determine the frontmost window.- Specified by:
getDesktopWindowsin interfaceOperatingSystem- Parameters:
visibleOnly- Whether to restrict the list to only windows visible to the user.This is a best effort attempt at a reasonable definition of visibility. Visible windows may be completely transparent.
- Returns:
- A list of
OSDesktopWindowobjects representing the desktop windows.
-
toString
-