Download this file
| 1 | package creds |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "os" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | // BootTime is when this machine last booted (/proc/stat's btime; the |
| 12 | // test build's fake Mac worker and the tests). |
| 13 | func BootTime() (time.Time, error) { |
| 14 | raw, err := os.ReadFile("/proc/stat") |
| 15 | if err != nil { |
| 16 | return time.Time{}, err |
| 17 | } |
| 18 | for _, line := range strings.Split(string(raw), "\n") { |
| 19 | if v, ok := strings.CutPrefix(line, "btime "); ok { |
| 20 | n, err := strconv.ParseInt(strings.TrimSpace(v), 10, 64) |
| 21 | if err != nil { |
| 22 | return time.Time{}, err |
| 23 | } |
| 24 | return time.Unix(n, 0), nil |
| 25 | } |
| 26 | } |
| 27 | return time.Time{}, errors.New("creds: no boot time") |
| 28 | } |
| 29 | |
| 30 | // BootID names this boot (/proc/sys/kernel/random/boot_id): a new value |
| 31 | // after every boot, whatever the clock did. |
| 32 | func BootID() (string, error) { |
| 33 | raw, err := os.ReadFile("/proc/sys/kernel/random/boot_id") |
| 34 | if id := strings.TrimSpace(string(raw)); err == nil && id != "" { |
| 35 | return id, nil |
| 36 | } |
| 37 | return "", errors.Join(errors.New("creds: no boot id"), err) |
| 38 | } |
| 39 | |