/** * Oshi (https://github.com/oshi/oshi) * * Copyright (c) 2010 - 2017 The Oshi Project Team * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Maintainers: * dblock[at]dblock[dot]org * widdis[at]gmail[dot]com * enrico.bianchi[at]gmail[dot]com * * Contributors: * https://github.com/oshi/oshi/graphs/contributors */ package oshi.util.platform.unix.freebsd; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.sun.jna.Memory; import com.sun.jna.Native; import com.sun.jna.Pointer; import com.sun.jna.Structure; import com.sun.jna.ptr.IntByReference; import oshi.jna.platform.unix.freebsd.Libc; /** * Provides access to sysctl calls on FreeBSD * * @author widdis[at]gmail[dot]com */ public class BsdSysctlUtil { private static final Logger LOG = LoggerFactory.getLogger(BsdSysctlUtil.class); private static final String SYSCTL_FAIL = "Failed syctl call: {}, Error code: {}"; private BsdSysctlUtil() { } /** * Executes a sysctl call with an int result * * @param name * name of the sysctl * @param def * default int value * @return The int result of the call if successful; the default otherwise */ public static int sysctl(String name, int def) { IntByReference size = new IntByReference(Libc.INT_SIZE); Pointer p = new Memory(size.getValue()); if (0 != Libc.INSTANCE.sysctlbyname(name, p, size, null, 0)) { LOG.error("Failed sysctl call: {}, Error code: {}", name, Native.getLastError()); return def; } return p.getInt(0); } /** * Executes a sysctl call with a long result * * @param name * name of the sysctl * @param def * default long value * @return The long result of the call if successful; the default otherwise */ public static long sysctl(String name, long def) { IntByReference size = new IntByReference(Libc.UINT64_SIZE); Pointer p = new Memory(size.getValue()); if (0 != Libc.INSTANCE.sysctlbyname(name, p, size, null, 0)) { LOG.error(SYSCTL_FAIL, name, Native.getLastError()); return def; } return p.getLong(0); } /** * Executes a sysctl call with a String result * * @param name * name of the sysctl * @param def * default String value * @return The String result of the call if successful; the default * otherwise */ public static String sysctl(String name, String def) { // Call first time with null pointer to get value of size IntByReference size = new IntByReference(); if (0 != Libc.INSTANCE.sysctlbyname(name, null, size, null, 0)) { LOG.error(SYSCTL_FAIL, name, Native.getLastError()); return def; } // Add 1 to size for null terminated string Pointer p = new Memory(size.getValue() + 1); if (0 != Libc.INSTANCE.sysctlbyname(name, p, size, null, 0)) { LOG.error(SYSCTL_FAIL, name, Native.getLastError()); return def; } return p.getString(0); } /** * Executes a sysctl call with a Structure result * * @param name * name of the sysctl * @param struct * structure for the result * @return True if structure is successfuly populated, false otherwise */ public static boolean sysctl(String name, Structure struct) { if (0 != Libc.INSTANCE.sysctlbyname(name, struct.getPointer(), new IntByReference(struct.size()), null, 0)) { LOG.error(SYSCTL_FAIL, name, Native.getLastError()); return false; } struct.read(); return true; } }