[go: up one dir, main page]

File: uptime.c

package info (click to toggle)
cctools 1%3A7.15.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 40,008 kB
  • sloc: ansic: 117,215; python: 30,569; cpp: 20,301; sh: 13,834; perl: 4,056; xml: 3,688; makefile: 1,502
file content (53 lines) | stat: -rw-r--r-- 1,234 bytes parent folder | download | duplicates (2)
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: */