From 34cc43d282f68f93b2f9c5be19d0c4a6beb7d39d Mon Sep 17 00:00:00 2001 From: Aman Gupta Karmani Date: Fri, 18 Nov 2022 19:17:18 -0800 Subject: [PATCH] [android][host] fix Info() failure due to forbidden /proc/stat and /proc/uptime (#1361) * [android][host] fallback to sysinfo() syscall for uptime with Android O, /proc/{stat,uptime} both return permission denied --- internal/common/common_linux.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/internal/common/common_linux.go b/internal/common/common_linux.go index 41916de3b..3b4de8e53 100644 --- a/internal/common/common_linux.go +++ b/internal/common/common_linux.go @@ -12,6 +12,7 @@ import ( "strconv" "strings" "sync" + "syscall" "time" ) @@ -68,6 +69,17 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) { filename := HostProc(statFile) lines, err := ReadLines(filename) + if os.IsPermission(err) { + var info syscall.Sysinfo_t + err := syscall.Sysinfo(&info) + if err != nil { + return 0, err + } + + currentTime := time.Now().UnixNano() / int64(time.Second) + t := currentTime - int64(info.Uptime) + return uint64(t), nil + } if err != nil { return 0, err }