sysctl utils

This commit is contained in:
Linus Dierheimer
2022-09-19 21:31:50 +02:00
parent 49cdacd5a9
commit 2a61d29be9
10 changed files with 88 additions and 74 deletions
+1
View File
@@ -243,6 +243,7 @@ set(LIBFASTFETCH_SRC
if(APPLE)
list(APPEND LIBFASTFETCH_SRC
src/common/sysctl.c
src/util/apple/cfdict_helpers.c
src/util/apple/osascript.m
src/detection/host/host_apple.c
-35
View File
@@ -358,38 +358,3 @@ void ffSettingsGetAndroidProperty(const char* propName, FFstrbuf* result) {
result->chars[result->length] = '\0';
}
#endif //__ANDROID__
#ifdef __APPLE__
#include <sys/sysctl.h>
void ffSettingsGetAppleProperty(const char* propName, FFstrbuf* result)
{
size_t neededLength;
if(sysctlbyname(propName, NULL, &neededLength, NULL, 0) != 0 || neededLength == 0)
return;
ffStrbufEnsureFree(result, (uint32_t) neededLength);
if(sysctlbyname(propName, result->chars + result->length, &neededLength, NULL, 0) == 0)
result->length += (uint32_t) neededLength;
result->chars[result->length] = '\0';
}
int ffSettingsGetAppleInt(const char* propName, int defaultValue)
{
int result;
size_t neededLength = sizeof(result);
if(sysctlbyname(propName, &result, &neededLength, NULL, 0) != 0)
return defaultValue;
return result;
}
int64_t ffSettingsGetAppleInt64(const char* propName, int64_t defaultValue)
{
int64_t result;
size_t neededLength = sizeof(result);
if(sysctlbyname(propName, &result, &neededLength, NULL, 0) != 0)
return defaultValue;
return result;
}
#endif //__APPLE__
-6
View File
@@ -36,10 +36,4 @@ int ffSettingsGetSQLite3Int(const FFinstance* instance, const char* dbPath, cons
void ffSettingsGetAndroidProperty(const char* propName, FFstrbuf* result);
#endif
#ifdef __APPLE__
void ffSettingsGetAppleProperty(const char* propName, FFstrbuf* result);
int ffSettingsGetAppleInt(const char* propName, int defaultValue);
int64_t ffSettingsGetAppleInt64(const char* propName, int64_t defaultValue);
#endif
#endif
+53
View File
@@ -0,0 +1,53 @@
#include "sysctl.h"
#include <sys/sysctl.h>
void ffSysctlGetString(const char* propName, FFstrbuf* result)
{
size_t neededLength;
if(sysctlbyname(propName, NULL, &neededLength, NULL, 0) != 0 || neededLength == 0)
return;
ffStrbufEnsureFree(result, (uint32_t) neededLength);
if(sysctlbyname(propName, result->chars + result->length, &neededLength, NULL, 0) == 0)
result->length += (uint32_t) neededLength;
result->chars[result->length] = '\0';
}
int ffSysctlGetInt(const char* propName, int defaultValue)
{
int result;
size_t neededLength = sizeof(result);
if(sysctlbyname(propName, &result, &neededLength, NULL, 0) != 0)
return defaultValue;
return result;
}
int64_t ffSysctlGetInt64(const char* propName, int64_t defaultValue)
{
int64_t result;
size_t neededLength = sizeof(result);
if(sysctlbyname(propName, &result, &neededLength, NULL, 0) != 0)
return defaultValue;
return result;
}
void* ffSysctlGetData(int* request, int requestLength, size_t* resultLength)
{
if(sysctl(request, requestLength, NULL, &length, NULL, 0) != 0)
return NULL;
void* data = malloc(*length);
if(data == NULL)
return NULL;
if(sysctl(request, requestLength, data, length, NULL, 0) != 0)
{
free(data);
return NULL;
}
return data;
}
+13
View File
@@ -0,0 +1,13 @@
#pragma once
#ifndef FF_INCLUDED_common_sysctl
#define FF_INCLUDED_common_sysctl
#include "fastfetch.h"
void ffSysctlGetString(const char* propName, FFstrbuf* result);
int ffSysctlGetInt(const char* propName, int defaultValue);
int64_t ffSysctlGetInt64(const char* propName, int64_t defaultValue);
void* ffSysctlGetData(int* request, int requestLength, size_t* resultLength);
#endif
+10 -10
View File
@@ -1,9 +1,9 @@
#include "cpu.h"
#include "common/settings.h"
#include "common/sysctl.h"
static double getFrequency(const char* propName)
{
double herz = (double) ffSettingsGetAppleInt64(propName, 0);
double herz = (double) ffSysctlGetInt64(propName, 0);
if(herz <= 0.0)
return herz;
@@ -15,22 +15,22 @@ static double getFrequency(const char* propName)
void ffDetectCPUImpl(FFCPUResult* cpu)
{
ffStrbufInit(&cpu->name);
ffSettingsGetAppleProperty("machdep.cpu.brand_string", &cpu->name);
ffSysctlGetString("machdep.cpu.brand_string", &cpu->name);
ffStrbufInit(&cpu->vendor);
ffSettingsGetAppleProperty("machdep.cpu.vendor", &cpu->vendor);
ffSysctlGetString("machdep.cpu.vendor", &cpu->vendor);
cpu->coresPhysical = (uint16_t) ffSettingsGetAppleInt("hw.physicalcpu_max", 1);
cpu->coresPhysical = (uint16_t) ffSysctlGetInt("hw.physicalcpu_max", 1);
if(cpu->coresPhysical == 1)
cpu->coresPhysical = (uint16_t) ffSettingsGetAppleInt("hw.physicalcpu", 1);
cpu->coresPhysical = (uint16_t) ffSysctlGetInt("hw.physicalcpu", 1);
cpu->coresLogical = (uint16_t) ffSettingsGetAppleInt("hw.logicalcpu_max", 1);
cpu->coresLogical = (uint16_t) ffSysctlGetInt("hw.logicalcpu_max", 1);
if(cpu->coresLogical == 1)
cpu->coresLogical = (uint16_t) ffSettingsGetAppleInt("hw.ncpu", 1);
cpu->coresLogical = (uint16_t) ffSysctlGetInt("hw.ncpu", 1);
cpu->coresOnline = (uint16_t) ffSettingsGetAppleInt("hw.logicalcpu", 1);
cpu->coresOnline = (uint16_t) ffSysctlGetInt("hw.logicalcpu", 1);
if(cpu->coresOnline == 1)
cpu->coresOnline = (uint16_t) ffSettingsGetAppleInt("hw.activecpu", 1);
cpu->coresOnline = (uint16_t) ffSysctlGetInt("hw.activecpu", 1);
cpu->frequencyMin = getFrequency("hw.cpufrequency_min");
cpu->frequencyMax = getFrequency("hw.cpufrequency_max");
@@ -1,10 +1,9 @@
#include "displayserver.h"
#include "common/library.h"
#include "common/sysctl.h"
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <sys/sysctl.h>
#include <ApplicationServices/ApplicationServices.h>
//Resolution code heavily inspired by displayplacer <3
@@ -60,19 +59,10 @@ static void detectWM(FFDisplayServerResult* ds)
u_int requestLength = sizeof(request) / sizeof(*request);
size_t length = 0;
if(sysctl(request, requestLength, NULL, &length, NULL, 0) != 0)
return;
struct kinfo_proc* processes = malloc(length);
struct kinfo_proc* processes = ffSysctlGetData(request, requestLength, &length);
if(processes == NULL)
return;
if(sysctl(request, requestLength, processes, &length, NULL, 0) != 0)
{
free(processes);
return;
}
for(size_t i = 0; i < length / sizeof(struct kinfo_proc); i++)
{
const char* comm = processes[i].kp_proc.p_comm;
+2 -2
View File
@@ -1,5 +1,5 @@
#include "host.h"
#include "common/settings.h"
#include "common/sysctl.h"
void ffDetectHostImpl(FFHostResult* host)
{
@@ -20,5 +20,5 @@ void ffDetectHostImpl(FFHostResult* host)
ffStrbufInitA(&host->chassisVendor, 0);
ffStrbufInitA(&host->chassisVersion, 0);
ffSettingsGetAppleProperty("hw.model", &host->productName);
ffSysctlGetString("hw.model", &host->productName);
}
+3 -4
View File
@@ -1,17 +1,16 @@
#include "memory.h"
#include "common/settings.h"
#include "common/systcl.h"
#include <string.h>
#include <mach/mach.h>
#include <sys/sysctl.h>
void ffDetectMemoryImpl(FFMemoryResult* memory)
{
memset(memory, 0, sizeof(FFMemoryResult));
memory->ram.bytesTotal = (uint64_t) ffSettingsGetAppleInt64("hw.memsize", 0);
memory->ram.bytesTotal = (uint64_t) ffSysctlGetInt64("hw.memsize", 0);
uint32_t pagesize = (uint32_t) ffSettingsGetAppleInt("hw.pagesize", 0);
uint32_t pagesize = (uint32_t) ffSysctlGetInt("hw.pagesize", 0);
if(pagesize == 0)
return;
+4 -5
View File
@@ -1,9 +1,8 @@
#include "os.h"
#include "common/properties.h"
#include "common/settings.h"
#include "common/sysctl.h"
#include <stdlib.h>
#include <sys/sysctl.h>
typedef enum PListKey
{
@@ -83,11 +82,11 @@ void ffDetectOSImpl(FFOSResult* os, const FFinstance* instance)
ffStrbufAppendS(&os->id, "macos");
if(os->version.length == 0)
ffSettingsGetAppleProperty("kern.osproductversion", &os->version);
ffSysctlGetString("kern.osproductversion", &os->version);
//TODO map version to pretty name
ffStrbufAppend(&os->prettyName, &os->name);
ffStrbufAppend(&os->versionID, &os->version);
ffSettingsGetAppleProperty("kern.ostype", &os->systemName);
ffSettingsGetAppleProperty("hw.machine", &os->architecture);
ffSysctlGetString("kern.ostype", &os->systemName);
ffSysctlGetString("hw.machine", &os->architecture);
}