Class LinuxOperatingSystem

  • All Implemented Interfaces:
    OperatingSystem

    @ThreadSafe
    public class LinuxOperatingSystem
    extends AbstractOperatingSystem
    Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically packaged in a Linux distribution.
    • Field Detail

      • LOG

        private static final org.slf4j.Logger LOG
      • LSB_RELEASE_A_LOG

        private static final java.lang.String LSB_RELEASE_A_LOG
        See Also:
        Constant Field Values
      • FILENAME_PROPERTIES

        private static final java.lang.String FILENAME_PROPERTIES
        See Also:
        Constant Field Values
      • HAS_UDEV

        public static final boolean HAS_UDEV
        This static field identifies if the udev library can be loaded.
      • HAS_GETTID

        public static final boolean HAS_GETTID
        This static field identifies if the gettid function is in the c library.
      • HAS_SYSCALL_GETTID

        public static final boolean HAS_SYSCALL_GETTID
        This static field identifies if the syscall for gettid returns sane results.
      • USER_HZ

        private static final long USER_HZ
        Jiffies per second, used for process time counters.
      • PAGE_SIZE

        private static final long PAGE_SIZE
      • OS_NAME

        private static final java.lang.String OS_NAME
        OS Name for manufacturer
      • BOOTTIME

        static final long BOOTTIME
      • PPID_INDEX

        private static final int[] PPID_INDEX
    • Constructor Detail

      • LinuxOperatingSystem

        public LinuxOperatingSystem()

        Constructor for LinuxOperatingSystem.

    • Method Detail

      • queryBitness

        protected int queryBitness​(int jvmBitness)
        Description copied from class: AbstractOperatingSystem
        Backup OS-specific query to determine bitness if previous checks fail
        Specified by:
        queryBitness in class AbstractOperatingSystem
        Parameters:
        jvmBitness - The bitness of the JVM
        Returns:
        The operating system bitness
      • getSessions

        public java.util.List<OSSession> getSessions()
        Description copied from interface: OperatingSystem
        Gets 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-standard who command, and may internally employ reentrant code on some platforms. Users may opt to use this command-line variant by default using the oshi.os.unix.whoCommand configuration property.

        Returns:
        A list of OSSession objects representing logged-in users
      • getProcess

        public OSProcess getProcess​(int pid)
        Description copied from interface: OperatingSystem
        Gets information on a currently running process
        Parameters:
        pid - A process ID
        Returns:
        An OSProcess object for the specified process id if it is running; null otherwise
      • queryProcessList

        private java.util.List<OSProcess> queryProcessList​(java.util.Set<java.lang.Integer> descendantPids)
      • getParentPidsFromProcFiles

        private static java.util.Map<java.lang.Integer,​java.lang.Integer> getParentPidsFromProcFiles​(java.io.File[] pidFiles)
      • getParentPidFromProcFile

        private static int getParentPidFromProcFile​(int pid)
      • getProcessId

        public int getProcessId()
        Description copied from interface: OperatingSystem
        Gets the current process ID (PID).
        Returns:
        the Process ID of the current process
      • getProcessCount

        public int getProcessCount()
        Description copied from interface: OperatingSystem
        Get the number of processes currently running
        Returns:
        The number of processes running
      • getThreadId

        public int getThreadId()
        Description copied from interface: OperatingSystem
        Makes a best effort to get the current thread ID (TID). May not be useful in a multithreaded environment. The thread ID returned may have been short lived and no longer exist.

        Thread IDs on macOS are not correlated with any other Operating System output.

        Returns:
        the Thread ID of the current thread if known, 0 otherwise.
      • getCurrentThread

        public OSThread getCurrentThread()
        Description copied from interface: OperatingSystem
        Makes a best effort to get the current thread. May not be useful in a multithreaded environment. The thread returned may have been short lived and no longer exist.

        On macOS, returns the oldest thread in the calling process.

        Returns:
        the current thread if known; an invalid thread otherwise.
      • getThreadCount

        public int getThreadCount()
        Description copied from interface: OperatingSystem
        Get the number of threads currently running
        Returns:
        The number of threads running
      • getSystemUptime

        public long getSystemUptime()
        Description copied from interface: OperatingSystem
        Get the System up time (time since boot).
        Returns:
        Number of seconds since boot.
      • getSystemBootTime

        public long getSystemBootTime()
        Description copied from interface: OperatingSystem
        Get Unix time of boot.
        Returns:
        The approximate time at which the system booted, in seconds since the Unix epoch.
      • queryFamilyVersionCodenameFromReleaseFiles

        private static Triplet<java.lang.String,​java.lang.String,​java.lang.String> queryFamilyVersionCodenameFromReleaseFiles()
      • readOsRelease

        private static Triplet<java.lang.String,​java.lang.String,​java.lang.String> readOsRelease()
        Attempts to read /etc/os-release
        Returns:
        a triplet with the parsed family, versionID and codeName if file successfully read and NAME= found, null otherwise
      • execLsbRelease

        private static Triplet<java.lang.String,​java.lang.String,​java.lang.String> execLsbRelease()
        Attempts to execute `lsb_release -a`
        Returns:
        a triplet with the parsed family, versionID and codeName if the command successfully executed and Distributor ID: or Description: found, null otherwise
      • readLsbRelease

        private static Triplet<java.lang.String,​java.lang.String,​java.lang.String> readLsbRelease()
        Attempts to read /etc/lsb-release
        Returns:
        a triplet with the parsed family, versionID and codeName if file successfully read and and DISTRIB_ID or DISTRIB_DESCRIPTION, null otherwise
      • readDistribRelease

        private static Triplet<java.lang.String,​java.lang.String,​java.lang.String> readDistribRelease​(java.lang.String filename)
        Attempts to read /etc/distrib-release (for some value of distrib)
        Parameters:
        filename - The /etc/distrib-release file
        Returns:
        a triplet with the parsed family, versionID and codeName if file successfully read and " release " or " VERSION " found, null otherwise
      • parseRelease

        private static Triplet<java.lang.String,​java.lang.String,​java.lang.String> parseRelease​(java.lang.String line,
                                                                                                            java.lang.String splitLine)
        Helper method to parse version description line style
        Parameters:
        line - a String of the form "Distributor release x.x (Codename)"
        splitLine - A regex to split on, e.g. " release "
        Returns:
        a triplet with the parsed family, versionID and codeName
      • getReleaseFilename

        protected static java.lang.String getReleaseFilename()
        Looks for a collection of possible distrib-release filenames
        Returns:
        The first valid matching filename
      • filenameToFamily

        private static java.lang.String filenameToFamily​(java.lang.String name)
        Converts a portion of a filename (e.g. the 'redhat' in /etc/redhat-release) to a mixed case string representing the family (e.g., Red Hat)
        Parameters:
        name - Stripped version of filename after removing /etc and -release
        Returns:
        Mixed case family
      • getServices

        public java.util.List<OSService> getServices()
        Description copied from interface: OperatingSystem
        Gets the all services on the system. The definition of what is a service is platform-dependent.
        Returns:
        An array of OSService objects
      • getHz

        public static long getHz()
        Gets Jiffies per second, useful for converting ticks to milliseconds and vice versa.
        Returns:
        Jiffies per second.
      • getPageSize

        public static long getPageSize()
        Gets Page Size, for converting memory stats from pages to bytes
        Returns:
        Page Size