package creds import ( "errors" "os" "strconv" "strings" "time" ) // BootTime is when this machine last booted (/proc/stat's btime; the // test build's fake Mac worker and the tests). func BootTime() (time.Time, error) { raw, err := os.ReadFile("/proc/stat") if err != nil { return time.Time{}, err } for _, line := range strings.Split(string(raw), "\n") { if v, ok := strings.CutPrefix(line, "btime "); ok { n, err := strconv.ParseInt(strings.TrimSpace(v), 10, 64) if err != nil { return time.Time{}, err } return time.Unix(n, 0), nil } } return time.Time{}, errors.New("creds: no boot time") } // BootID names this boot (/proc/sys/kernel/random/boot_id): a new value // after every boot, whatever the clock did. func BootID() (string, error) { raw, err := os.ReadFile("/proc/sys/kernel/random/boot_id") if id := strings.TrimSpace(string(raw)); err == nil && id != "" { return id, nil } return "", errors.Join(errors.New("creds: no boot id"), err) }