]> git.ipfire.org Git - thirdparty/util-linux.git/blame - lib/boottime.c
lib/boottime: add a new file
[thirdparty/util-linux.git] / lib / boottime.c
CommitLineData
2c5484f7
KZ
1
2#include <time.h>
3#include <sys/sysinfo.h>
4#include <sys/time.h>
5
6#include "c.h"
7#include "nls.h"
8#include "boottime.h"
9
10int get_boot_time(struct timeval *boot_time)
11{
12 struct timespec hires_uptime;
13 struct timeval lores_uptime, now;
14 struct sysinfo info;
15
16 if (gettimeofday(&now, NULL) != 0) {
17 warn(_("gettimeofday failed"));
18 return -errno;
19 }
20#ifdef CLOCK_BOOTTIME
21 if (clock_gettime(CLOCK_BOOTTIME, &hires_uptime) == 0) {
22 TIMESPEC_TO_TIMEVAL(&lores_uptime, &hires_uptime);
23 timersub(&now, &lores_uptime, boot_time);
24 return 0;
25 }
26#endif
27 /* fallback */
28 if (sysinfo(&info) != 0)
29 warn(_("sysinfo failed"));
30
31 boot_time->tv_sec = now.tv_sec - info.uptime;
32 boot_time->tv_usec = 0;
33 return 0;
34}