where uptime utility gets its info

If I run uptime utility in FreeBSD I can see the uptime of my system. However, where is this information collected from? Are there different places where FreeBSD writes it's information when it boots up? :stud
 
w gets it data from /proc and w is also uptime, see source code for yourself /usr/src/usr.bin/w/w.c
 
ok, I see. As I understand, clock_gettime is the system call, which asks for how long system has been up:

Code:
/*
	 * Print how long system has been up.
	 */
	if (clock_gettime(CLOCK_MONOTONIC, &tp) != -1) {
		uptime = tp.tv_sec;
		if (uptime > 60)
			uptime += 30;
		days = uptime / 86400;
		uptime %= 86400;
		hrs = uptime / 3600;
		uptime %= 3600;
		mins = uptime / 60;
		secs = uptime % 60;
		(void)printf(" up");
		if (days > 0)
			(void)printf(" %d day%s,", days, days > 1 ? "s" : "");
		if (hrs > 0 && mins > 0)
			(void)printf(" %2d:%02d,", hrs, mins);
		else if (hrs > 0)
			(void)printf(" %d hr%s,", hrs, hrs > 1 ? "s" : "");
		else if (mins > 0)
			(void)printf(" %d min%s,", mins, mins > 1 ? "s" : "");
		else
			(void)printf(" %d sec%s,", secs, secs > 1 ? "s" : "");
	}

It looks like there isn't a particular file where uptime is written in. In the CLOCK_GETTIME(2) there is said:

CLOCK_UPTIME which starts at zero when the kernel boots
and increments monotonically in SI seconds while the machine is running

So basically there is some sort of uptime_daemon working in the background, which counts seconds and starts from zero if the device is rebooted?:)
 
You will have to view the source for libc, in particular what the clock_gettime call does.
 
You get uptime the same way you get normal real time/date, by calling SYS_CLOCK_GETTIME syscall just with flag CLOCK_UPTIME (defined in time.h). There is no 'daemon' running and calculating uptime, kernel just knows time it booted and gets uptime from its clock.
 
Back
Top