Skip to content

Commit

Permalink
[android][host] fallback to sysinfo() syscall for uptime
Browse files Browse the repository at this point in the history
with Android O, /proc/{stat,uptime} both return permission denied
  • Loading branch information
tmm1 committed Oct 14, 2022
1 parent 400a453 commit 42f7a7c
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions internal/common/common_linux.go
Expand Up @@ -6,13 +6,13 @@ package common
import (
"context"
"fmt"
"golang.org/x/sys/unix"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)

func DoSysctrl(mib string) ([]string, error) {
Expand Down Expand Up @@ -68,7 +68,17 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) {

filename := HostProc(statFile)
lines, err := ReadLines(filename)
if err != nil {
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 - info.Uptime
return uint64(t), nil
} else if err != nil {
return 0, err
}

Expand Down

0 comments on commit 42f7a7c

Please sign in to comment.