mvpel/core — pull request #1 by mvpel (2012-11-14 03:40:39+00:00)
Title: Implement a new $(sys.uptime) variable (Mantis #1134), plus .systime and .sysday

This commit grew from discussion in a pull request by @nyetsche from April 2012: https://github.com/cfengine/core/pull/46 and Mantis 1134 from May 2012: https://cfengine.com/bugtracker/view.php?id=1134

As implemented, the $(sys.uptime) variable contains the number of minutes the system has been up and running. It uses one of various techniques depending on the OS platform (determined at compile time, not run) to find the system uptime:

* sysinfo() for Linux platforms
* kstat() for Solaris platforms
* pstat_getproc() on the init process for HP-UX
* sysctl() for BSD platforms

Any platform which doesn't have one of these methods available will default to a stat(/proc/1) to try to get the create time of the init process, which falls within a few seconds of the boot time of the system. (Even though Linux and Solaris have /proc, this method is not used on those platforms even as a fallback.)

If any of these approaches fail, the routine falls back to running the "/usr/bin/uptime" or "/bin/uptime" command, and its output is parsed into days, hours, and minutes with a PCRE regexp, and converted to a usable time for the variable. Thanks to @tzz Ted Zlatanov for the suggestion: https://github.com/cfengine/core/pull/46#issuecomment-10066632

This popen() approach is unlikely to be used very frequently - probably only if the /proc filesystem isn't mounted - but it's a decent backstop to give a system the best possible chance to set $(sys.uptime) successfully.

Two other closely related variables are introduced: $(sys.systime) and $(sys.sysday). The former is simply the result of the time() system call, which is useful in manipulating timestamps in HP-UX TCB password databases, for example, and the latter is that value divided by 86,400 - the number of seconds in a day - expressed as an integer. This is, of course, useful in the /etc/shadow file to indicate the password change date.

Three new documents are added in docs/reference/vars to describe the $(sys.uptime), $(sys.systime), and $(sys.sysday) variables.