Debian Codegraph @

contrib/mod_snmp/uptime.c

raw symbols SCIP
1 /*
2 * ProFTPD - mod_snmp uptime
3 * Copyright (c) 2012-2016 TJ Saunders
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, TJ Saunders and other respective copyright holders
20 * give permission to link this program with OpenSSL, and distribute the
21 * resulting executable, without including the source code for OpenSSL in the
22 * source distribution.
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 /* No sysinfo(3), no sysctl(3) */
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