Skip to content

Commit

Permalink
[android][host] fix Info() failure due to forbidden /proc/stat and /p…
Browse files Browse the repository at this point in the history
…roc/uptime (#1361)

* [android][host] fallback to sysinfo() syscall for uptime

with Android O, /proc/{stat,uptime} both return permission denied
  • Loading branch information
tmm1 committed Nov 19, 2022
1 parent d3ea877 commit 34cc43d
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions internal/common/common_linux.go
Expand Up @@ -12,6 +12,7 @@ import (
"strconv"
"strings"
"sync"
"syscall"
"time"
)

Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 34cc43d

Please sign in to comment.