diff --git a/host/host_linux.go b/host/host_linux.go index 8f6e02f..098d086 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -109,6 +109,7 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) { if err != nil { return 0, err } + statFile := "stat" if system == "lxc" && role == "guest" { // if lxc, /proc/uptime is used. @@ -123,20 +124,35 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) { if err != nil { return 0, err } - for _, line := range lines { - if strings.HasPrefix(line, "btime") { - f := strings.Fields(line) - if len(f) != 2 { - return 0, fmt.Errorf("wrong btime format") - } - b, err := strconv.ParseInt(f[1], 10, 64) - if err != nil { - return 0, err + + if statFile == "stat" { + for _, line := range lines { + if strings.HasPrefix(line, "btime") { + f := strings.Fields(line) + if len(f) != 2 { + return 0, fmt.Errorf("wrong btime format") + } + b, err := strconv.ParseInt(f[1], 10, 64) + if err != nil { + return 0, err + } + t = uint64(b) + atomic.StoreUint64(&cachedBootTime, t) + return t, nil } - t = uint64(b) - atomic.StoreUint64(&cachedBootTime, t) - return t, nil } + } else if statFile == "uptime" { + if len(lines) != 1 { + return 0, fmt.Errorf("wrong uptime format") + } + f := strings.Fields(lines[0]) + b, err := strconv.ParseInt(f[0], 10, 64) + if err != nil { + return 0, err + } + t = uint64(time.Now().Unix()) - uint64(b) + atomic.StoreUint64(&cachedBootTime, t) + return t, nil } return 0, fmt.Errorf("could not find btime")