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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
/*
Copyright (C) 2022 The University of Notre Dame
This software is distributed under the GNU General Public License.
See the file COPYING for details.
*/
#include "uptime.h"
#include "debug.h"
#if defined(CCTOOLS_OPSYS_DARWIN) || defined(CCTOOLS_OPSYS_FREEBSD)
#include <sys/sysctl.h>
#include <time.h>
#elif defined(CCTOOLS_OPSYS_LINUX)
#include <sys/sysinfo.h>
#endif
int uptime_get()
{
int uptime;
#if defined(CCTOOLS_OPSYS_DARWIN) || defined(CCTOOLS_OPSYS_FREEBSD)
struct timeval boottime;
size_t len = sizeof(boottime);
int mib[2] = {CTL_KERN, KERN_BOOTTIME};
if (sysctl(mib, 2, &boottime, &len, NULL, 0) < 0) {
uptime = -1;
}
time_t bsec = boottime.tv_sec;
time_t csec = time(NULL);
uptime = difftime(csec, bsec);
#elif defined(CCTOOLS_OPSYS_LINUX)
struct sysinfo info;
sysinfo(&info);
uptime = info.uptime;
#else
/*
Note that this is implemented as a text warning, since
system uptime detection is only used a few limited
cases and then only as a debugging tool.
*/
static int did_warning = 0;
if (!did_warning) {
debug(D_NOTICE, "uptime not implemented (yet) on this operating system");
did_warning = 1;
}
uptime = 0;
#endif
return uptime;
}
/* vim: set noexpandtab tabstop=8: */
|