summaryrefslogtreecommitdiffstats
path: root/src/detection/uptime/uptime_linux.c
blob: 75c7768a86623f4b5dc411643c98a9bb4c260f3d (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
28
29
30
31
32
33
34
35
36
37
38
#include "uptime.h"
#include "common/time.h"
#include "common/io.h"

#include <inttypes.h>

const char* ffDetectUptime(FFUptimeResult* result) {
#ifndef __ANDROID__ // cat: /proc/uptime: Permission denied

    // #620
    char buf[64];
    ssize_t nRead = ffReadFileData("/proc/uptime", ARRAY_SIZE(buf) - 1, buf);
    if (nRead > 0) {
        buf[nRead] = '\0';

        char* err = NULL;
        double sec = strtod(buf, &err);
        if (err != buf) {
            result->uptime = (uint64_t) (sec * 1000);
            result->bootTime = ffTimeGetNow() - result->uptime;
            return NULL;
        }
    }

#endif
#ifndef __GNU__
    struct timespec uptime;
    if (clock_gettime(CLOCK_BOOTTIME, &uptime) != 0) {
        return "clock_gettime(CLOCK_BOOTTIME) failed";
    }

    result->uptime = (uint64_t) uptime.tv_sec * 1000 + (uint64_t) uptime.tv_nsec / 1000000;
    result->bootTime = ffTimeGetNow() - result->uptime;
    return NULL;
#else
    return "read(/proc/uptime) failed";
#endif
}