ohmu
·
2026-10-15
1#include "uptime.h"
2#include "common/debug.h"
3#include "common/time.h"
4
5#include <errno.h>
6#include <string.h>
7#include <sys/sysctl.h>
8#include <sys/time.h>
9
10const char* ffDetectUptime(FFUptimeResult* result) {
11#if __NetBSD__
12 struct timespec bootTime;
13#else
14 struct timeval bootTime;
15#endif
16 size_t bootTimeSize = sizeof(bootTime);
17 if (sysctl(
18 (int[]) { CTL_KERN, KERN_BOOTTIME }, 2, &bootTime, &bootTimeSize, nullptr, 0) != 0) {
19 FF_DEBUG("sysctl({CTL_KERN, KERN_BOOTTIME}) failed: %s", strerror(errno));
20 return "sysctl({CTL_KERN, KERN_BOOTTIME}) failed";
21 }
22
23#if __NetBSD__
24 result->bootTime = (uint64_t) bootTime.tv_sec * 1000 + (uint64_t) bootTime.tv_nsec / 1000000;
25#else
26 result->bootTime = (uint64_t) bootTime.tv_sec * 1000 + (uint64_t) bootTime.tv_usec / 1000;
27#endif
28 result->uptime = ffTimeGetNow() - result->bootTime;
29
30 return nullptr;
31}