Uptime (SunOS): improves detection via kstat

This commit is contained in:
Carter Li
2026-10-25 00:13:54 +08:00
parent bfe74b8437
commit 197d1c6e5d
3 changed files with 35 additions and 12 deletions
+5
View File
@@ -1,3 +1,8 @@
# Unreleased
Bugfixes:
* Improved uptime detection on SunOS via kstat. (#2674, Uptime, SunOS)
# 2.70.0
Changes:
+29 -11
View File
@@ -1,19 +1,37 @@
#include "uptime.h"
#include "common/debug.h"
#include "common/time.h"
#include <utmpx.h>
#include <kstat.h>
static inline void kstatFreeWrap(kstat_ctl_t** pkc) {
assert(pkc);
if (*pkc) {
kstat_close(*pkc);
}
}
const char* ffDetectUptime(FFUptimeResult* result) {
struct utmpx* ut;
setutxent();
while (nullptr != (ut = getutxent())) {
if (ut->ut_type == BOOT_TIME) {
result->bootTime = (uint64_t) ut->ut_tv.tv_sec * 1000 + (uint64_t) ut->ut_tv.tv_usec / 1000000;
result->uptime = ffTimeGetNow() - result->bootTime;
break;
}
[[gnu::cleanup(kstatFreeWrap)]] kstat_ctl_t* kc = kstat_open();
if (!kc) {
FF_DEBUG("kstat_open() failed");
return "kstat_open() failed";
}
endutxent();
kstat_t* ks = kstat_lookup(kc, "unix", 0, "system_misc");
if (!ks || kstat_read(kc, ks, nullptr) < 0) {
FF_DEBUG("kstat_lookup(unix:0:system_misc) failed");
return "kstat_lookup(unix:0:system_misc) failed";
}
kstat_named_t* bootTime = (kstat_named_t*) kstat_data_lookup(ks, "boot_time");
if (!bootTime) {
FF_DEBUG("kstat_data_lookup(boot_time) failed");
return "kstat_data_lookup(boot_time) failed";
}
result->bootTime = (uint64_t) bootTime->value.ui32 * 1000;
result->uptime = ffTimeGetNow() - result->bootTime;
return nullptr;
}
+1 -1
View File
@@ -60,7 +60,7 @@ void ffGenerateUptimeJsonConfig(FFUptimeOptions* options, yyjson_mut_doc* doc, y
}
bool ffGenerateUptimeJsonResult([[maybe_unused]] FFUptimeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
FFUptimeResult result;
FFUptimeResult result = {};
const char* error = ffDetectUptime(&result);
if (error) {