| 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 |
#include "mod_snmp.h" |
| 26 |
#include "uptime.h" |
| 27 |
|
| 28 |
#ifdef HAVE_SYS_SYSCTL_H |
| 29 |
# include <sys/sysctl.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
#ifdef HAVE_SYS_SYSINFO_H |
| 33 |
# include <sys/sysinfo.h> |
| 34 |
#endif |
| 35 |
|
| 36 |
#ifdef HAVE_SYSINFO |
| 37 |
static int snmp_uptime_via_sysinfo(struct timeval *tv) { |
| 38 |
int res; |
| 39 |
struct sysinfo info; |
| 40 |
|
| 41 |
res = sysinfo(&info); |
| 42 |
if (res < 0) { |
| 43 |
return -1; |
| 44 |
} |
| 45 |
|
| 46 |
tv->tv_sec = info.uptime; |
| 47 |
tv->tv_usec = 0; |
| 48 |
|
| 49 |
return res; |
| 50 |
} |
| 51 |
|
| 52 |
#elif HAVE_SYSCTL |
| 53 |
static int snmp_uptime_via_sysctl(struct timeval *tv) { |
| 54 |
int res; |
| 55 |
int mib[2]; |
| 56 |
struct timeval boot_tv; |
| 57 |
size_t boot_tvlen; |
| 58 |
|
| 59 |
mib[0] = CTL_KERN; |
| 60 |
mib[1] = KERN_BOOTTIME; |
| 61 |
boot_tvlen = sizeof(boot_tv); |
| 62 |
|
| 63 |
res = sysctl(mib, 2, &boot_tv, &boot_tvlen, NULL, 0); |
| 64 |
if (res < 0) { |
| 65 |
return -1; |
| 66 |
} |
| 67 |
|
| 68 |
tv->tv_sec = boot_tv.tv_sec; |
| 69 |
tv->tv_usec = boot_tv.tv_usec; |
| 70 |
|
| 71 |
return res; |
| 72 |
} |
| 73 |
#endif |
| 74 |
|
| 75 |
int snmp_uptime_get(pool *p, struct timeval *tv) { |
| 76 |
int res; |
| 77 |
|
| 78 |
if (p == NULL || |
| 79 |
tv == NULL) { |
| 80 |
errno = EINVAL; |
| 81 |
return -1; |
| 82 |
} |
| 83 |
|
| 84 |
#ifdef HAVE_SYSINFO |
| 85 |
res = snmp_uptime_via_sysinfo(tv); |
| 86 |
|
| 87 |
#elif HAVE_SYSCTL |
| 88 |
res = snmp_uptime_via_sysctl(tv); |
| 89 |
|
| 90 |
#else |
| 91 |
errno = ENOSYS; |
| 92 |
res = -1; |
| 93 |
#endif |
| 94 |
|
| 95 |
return res; |
| 96 |
} |
| 97 |
|