From 1841e1a99d5d15c1e9e9f4ab6e942c840f213324 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Mon, 17 Jun 2024 01:01:21 +0800 Subject: [PATCH] SunOS: support CPU & packages detection --- CMakeLists.txt | 5 ++-- src/detection/cpu/cpu_sunos.c | 38 +++++++++++++++++++++++++ src/detection/packages/packages_sunos.c | 15 ++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/detection/cpu/cpu_sunos.c create mode 100644 src/detection/packages/packages_sunos.c diff --git a/CMakeLists.txt b/CMakeLists.txt index c301005b5..de2697ada 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -748,7 +748,7 @@ elseif(SunOS) src/detection/bootmgr/bootmgr_nosupport.c src/detection/brightness/brightness_nosupport.c src/detection/chassis/chassis_windows.c - src/detection/cpu/cpu_nosupport.c + src/detection/cpu/cpu_sunos.c src/detection/cpucache/cpucache_nosupport.c src/detection/cpuusage/cpuusage_nosupport.c src/detection/cursor/cursor_linux.c @@ -786,7 +786,7 @@ elseif(SunOS) src/detection/netio/netio_nosupport.c src/detection/opengl/opengl_linux.c src/detection/os/os_sunos.c - src/detection/packages/packages_nosupport.c + src/detection/packages/packages_sunos.c src/detection/poweradapter/poweradapter_nosupport.c src/detection/processes/processes_linux.c src/detection/gtk_qt/qt.c @@ -1128,6 +1128,7 @@ elseif(SunOS) target_link_libraries(libfastfetch PRIVATE "m" PRIVATE "socket" + PRIVATE "kstat" ) elseif(ANDROID) CHECK_LIBRARY_EXISTS(-l:libandroid-wordexp.a wordexp "" HAVE_LIBANDROID_WORDEXP_STATIC) diff --git a/src/detection/cpu/cpu_sunos.c b/src/detection/cpu/cpu_sunos.c new file mode 100644 index 000000000..716dc68b9 --- /dev/null +++ b/src/detection/cpu/cpu_sunos.c @@ -0,0 +1,38 @@ +#include "cpu.h" +#include + +const char* ffDetectCPUImpl(FF_MAYBE_UNUSED const FFCPUOptions* options, FFCPUResult* cpu) +{ + kstat_ctl_t* kc = kstat_open(); + if (!kc) + return "kstat_open() failed"; + + kstat_t* ks = kstat_lookup(kc, "cpu_info", -1, NULL); + if (!ks) + return "kstat_lookup() failed"; + + if (ks->ks_type != KSTAT_TYPE_NAMED) + return "Invalid ks_type found"; + + if (kstat_read(kc, ks, NULL) < 0) + return "kstat_read() failed"; + + { + kstat_named_t* kn = kstat_data_lookup(ks, "brand"); + ffStrbufSetNS(&cpu->name, KSTAT_NAMED_STR_BUFLEN(kn), KSTAT_NAMED_STR_PTR(kn)); + } + { + kstat_named_t* kn = kstat_data_lookup(ks, "vendor_id"); + ffStrbufSetNS(&cpu->vendor, KSTAT_NAMED_STR_BUFLEN(kn), KSTAT_NAMED_STR_PTR(kn)); + } + { + kstat_named_t* kn = kstat_data_lookup(ks, "clock_MHz"); + cpu->frequencyBase = kn->value.i32 / 1000.; + } + + cpu->coresOnline = 1; + while ((ks = kstat_lookup(kc, "cpu_info", ks->ks_instance + 1, NULL))) + cpu->coresOnline++; + + return NULL; +} diff --git a/src/detection/packages/packages_sunos.c b/src/detection/packages/packages_sunos.c new file mode 100644 index 000000000..a295f9ae8 --- /dev/null +++ b/src/detection/packages/packages_sunos.c @@ -0,0 +1,15 @@ +#include "packages.h" + +void ffDetectPackagesImpl(FFPackagesResult* result, FFPackagesOptions* options) +{ + if (!(options->disabled & FF_PACKAGES_FLAG_PKG_BIT)) + { + yyjson_doc* doc = yyjson_read_file(FASTFETCH_TARGET_DIR_ROOT "/var/pkg/state/installed/catalog.attrs", YYJSON_READ_NOFLAG, NULL, NULL); + if (doc) + { + yyjson_val* packageCount = yyjson_obj_get(yyjson_doc_get_root(doc), "package-count"); + if (packageCount) + result->pkg = (uint32_t) yyjson_get_uint(packageCount); + } + } +}