summaryrefslogtreecommitdiffstats
path: root/src/detection/uptime/uptime_bsd.c
blob: 46943b02908fbb58927155d8637b61aa5e77d5d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "uptime.h"
#include "common/time.h"

#include <sys/sysctl.h>
#include <sys/time.h>

const char* ffDetectUptime(FFUptimeResult* result) {
#if __NetBSD__
    struct timespec bootTime;
#else
    struct timeval bootTime;
#endif
    size_t bootTimeSize = sizeof(bootTime);
    if (sysctl(
            (int[]) { CTL_KERN, KERN_BOOTTIME }, 2, &bootTime, &bootTimeSize, NULL, 0) != 0) {
        return "sysctl({CTL_KERN, KERN_BOOTTIME}) failed";
    }

#if __NetBSD__
    result->bootTime = (uint64_t) bootTime.tv_sec * 1000 + (uint64_t) bootTime.tv_nsec / 1000000;
#else
    result->bootTime = (uint64_t) bootTime.tv_sec * 1000 + (uint64_t) bootTime.tv_usec / 1000;
#endif
    result->uptime = ffTimeGetNow() - result->bootTime;

    return NULL;
}