Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

needs_restarting: Fix boot time derivation for systems without RTC #468

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Fix boot time derivation for systems with no rtc
  • Loading branch information
Todd Lewis committed Nov 23, 2022
commit b8132e1e04fb44c9ef8ab7dbcea2d2b3b920be2c
24 changes: 23 additions & 1 deletion plugins/needs_restarting.py
Expand Up @@ -34,6 +34,7 @@
import os
import re
import stat
import time


# For which package updates we should recommend a reboot
Expand Down Expand Up @@ -199,7 +200,28 @@ def __init__(self):

@staticmethod
def get_boot_time():
return int(os.stat('/proc/1').st_mtime)
"""
We have two sources from which to derive the boot time. These values vary
depending on containerization, existence of a Real Time Clock, etc.
For our purposes we want the latest derived value.
- st_mtime of /proc/1
Reflects the time the first process was run after booting
This works for all known cases except machines without
a RTC - they awake at the start of the epoch.
- /proc/uptime
Seconds field of /proc/uptime subtracted from the current time
Works for machines without RTC iff the current time is reasonably correct.
Does not work on containers which share their kernel with the
host - there the host kernel uptime is returned
"""

proc_1_boot_time = int(os.stat('/proc/1').st_mtime)
if os.path.isfile('/proc/uptime'):
with open('/proc/uptime', 'rb') as f:
uptime = f.readline().strip().split()[0].strip()
proc_uptime_boot_time = int(time.time() - float(uptime))
return max(proc_1_boot_time, proc_uptime_boot_time)
return proc_1_boot_time

@staticmethod
def get_sc_clk_tck():
Expand Down