2017-09-17 17:45:03 +02:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2018-03-28 19:46:27 +02:00
|
|
|
#include <errno.h>
|
2018-03-20 00:48:10 +01:00
|
|
|
#include <stdio.h>
|
2018-03-28 19:46:27 +02:00
|
|
|
#include <string.h>
|
2018-03-21 12:21:37 +01:00
|
|
|
#if defined(__linux__)
|
2017-09-17 16:18:17 +02:00
|
|
|
#include <sys/sysinfo.h>
|
2018-03-21 12:21:37 +01:00
|
|
|
#elif defined(__OpenBSD__)
|
2018-03-20 00:48:10 +01:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
2017-09-17 16:18:17 +02:00
|
|
|
|
2017-09-24 15:33:01 +02:00
|
|
|
#include "../util.h"
|
2017-09-17 16:18:17 +02:00
|
|
|
|
2018-04-29 20:07:09 +02:00
|
|
|
#if defined(__linux__)
|
2017-09-17 16:18:17 +02:00
|
|
|
const char *
|
|
|
|
uptime(void)
|
|
|
|
{
|
2018-03-20 00:48:10 +01:00
|
|
|
int h;
|
|
|
|
int m;
|
|
|
|
int uptime = 0;
|
2017-09-17 16:18:17 +02:00
|
|
|
struct sysinfo info;
|
|
|
|
|
|
|
|
sysinfo(&info);
|
2018-03-20 00:48:10 +01:00
|
|
|
uptime = info.uptime;
|
2018-04-29 20:07:09 +02:00
|
|
|
|
|
|
|
h = uptime / 3600;
|
|
|
|
m = (uptime - h * 3600) / 60;
|
|
|
|
|
|
|
|
return bprintf("%dh %dm", h, m);
|
|
|
|
}
|
2018-03-21 12:21:37 +01:00
|
|
|
#elif defined(__OpenBSD__)
|
2018-04-29 20:07:09 +02:00
|
|
|
const char *
|
|
|
|
uptime(void)
|
|
|
|
{
|
|
|
|
int h;
|
|
|
|
int m;
|
|
|
|
int uptime = 0;
|
|
|
|
|
2018-03-20 00:48:10 +01:00
|
|
|
int mib[2];
|
|
|
|
size_t size;
|
|
|
|
time_t now;
|
|
|
|
struct timeval boottime;
|
|
|
|
|
|
|
|
time(&now);
|
|
|
|
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_BOOTTIME;
|
|
|
|
|
|
|
|
size = sizeof(boottime);
|
|
|
|
|
|
|
|
if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
|
|
|
|
uptime = now - boottime.tv_sec;
|
2018-03-28 19:46:27 +02:00
|
|
|
else {
|
|
|
|
fprintf(stderr, "sysctl 'KERN_BOOTTIME': %s\n", strerror(errno));
|
2018-03-20 00:48:10 +01:00
|
|
|
return NULL;
|
2018-03-28 19:46:27 +02:00
|
|
|
}
|
2018-04-29 20:07:09 +02:00
|
|
|
|
2018-03-20 00:48:10 +01:00
|
|
|
h = uptime / 3600;
|
|
|
|
m = (uptime - h * 3600) / 60;
|
2017-09-17 16:18:17 +02:00
|
|
|
|
|
|
|
return bprintf("%dh %dm", h, m);
|
|
|
|
}
|
2018-04-29 20:07:09 +02:00
|
|
|
#endif
|