oshi.software.os.unix.freebsd.FreeBsdOperatingSystem Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2016-2023 The OSHI Project Contributors
* SPDX-License-Identifier: MIT
*/
package oshi.software.os.unix.freebsd;
import static oshi.software.os.OSService.State.RUNNING;
import static oshi.software.os.OSService.State.STOPPED;
import static oshi.software.os.OperatingSystem.ProcessFiltering.VALID_PROCESS;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.jna.ptr.NativeLongByReference;
import oshi.annotation.concurrent.ThreadSafe;
import oshi.driver.unix.freebsd.Who;
import oshi.jna.platform.unix.FreeBsdLibc;
import oshi.jna.platform.unix.FreeBsdLibc.Timeval;
import oshi.software.common.AbstractOperatingSystem;
import oshi.software.os.FileSystem;
import oshi.software.os.InternetProtocolStats;
import oshi.software.os.NetworkParams;
import oshi.software.os.OSProcess;
import oshi.software.os.OSService;
import oshi.software.os.OSSession;
import oshi.software.os.OSThread;
import oshi.util.ExecutingCommand;
import oshi.util.ParseUtil;
import oshi.util.platform.unix.freebsd.BsdSysctlUtil;
import oshi.util.tuples.Pair;
/**
* FreeBSD is a free and open-source Unix-like operating system descended from the Berkeley Software Distribution (BSD),
* which was based on Research Unix. The first version of FreeBSD was released in 1993. In 2005, FreeBSD was the most
* popular open-source BSD operating system, accounting for more than three-quarters of all installed simply,
* permissively licensed BSD systems.
*/
@ThreadSafe
public class FreeBsdOperatingSystem extends AbstractOperatingSystem {
private static final Logger LOG = LoggerFactory.getLogger(FreeBsdOperatingSystem.class);
private static final long BOOTTIME = querySystemBootTime();
/*
* Package-private for use by FreeBsdOSProcess
*/
enum PsKeywords {
STATE, PID, PPID, USER, UID, GROUP, GID, NLWP, PRI, VSZ, RSS, ETIMES, SYSTIME, TIME, COMM, MAJFLT, MINFLT,
NVCSW, NIVCSW, ARGS; // ARGS must always be last
}
static final String PS_COMMAND_ARGS = Arrays.stream(PsKeywords.values()).map(Enum::name)
.map(name -> name.toLowerCase(Locale.ROOT)).collect(Collectors.joining(","));
@Override
public String queryManufacturer() {
return "Unix/BSD";
}
@Override
public Pair queryFamilyVersionInfo() {
String family = BsdSysctlUtil.sysctl("kern.ostype", "FreeBSD");
String version = BsdSysctlUtil.sysctl("kern.osrelease", "");
String versionInfo = BsdSysctlUtil.sysctl("kern.version", "");
String buildNumber = versionInfo.split(":")[0].replace(family, "").replace(version, "").trim();
return new Pair<>(family, new OSVersionInfo(version, null, buildNumber));
}
@Override
protected int queryBitness(int jvmBitness) {
if (jvmBitness < 64 && ExecutingCommand.getFirstAnswer("uname -m").indexOf("64") == -1) {
return jvmBitness;
}
return 64;
}
@Override
public FileSystem getFileSystem() {
return new FreeBsdFileSystem();
}
@Override
public InternetProtocolStats getInternetProtocolStats() {
return new FreeBsdInternetProtocolStats();
}
@Override
public List getSessions() {
return USE_WHO_COMMAND ? super.getSessions() : Who.queryUtxent();
}
@Override
public List queryAllProcesses() {
return getProcessListFromPS(-1);
}
@Override
public List queryChildProcesses(int parentPid) {
List allProcs = queryAllProcesses();
Set descendantPids = getChildrenOrDescendants(allProcs, parentPid, false);
return allProcs.stream().filter(p -> descendantPids.contains(p.getProcessID())).collect(Collectors.toList());
}
@Override
public List queryDescendantProcesses(int parentPid) {
List allProcs = queryAllProcesses();
Set descendantPids = getChildrenOrDescendants(allProcs, parentPid, true);
return allProcs.stream().filter(p -> descendantPids.contains(p.getProcessID())).collect(Collectors.toList());
}
@Override
public OSProcess getProcess(int pid) {
List procs = getProcessListFromPS(pid);
if (procs.isEmpty()) {
return null;
}
return procs.get(0);
}
private List getProcessListFromPS(int pid) {
String psCommand = "ps -awwxo " + PS_COMMAND_ARGS;
if (pid >= 0) {
psCommand += " -p " + pid;
}
Predicate