1// Copyright 2020 The Prometheus Authors
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14// +build !windows
15
16package procfs
17
18import (
19	"os"
20
21	"github.com/prometheus/procfs/internal/util"
22)
23
24// KernelRandom contains information about to the kernel's random number generator.
25type KernelRandom struct {
26	// EntropyAvaliable gives the available entropy, in bits.
27	EntropyAvaliable *uint64
28	// PoolSize gives the size of the entropy pool, in bits.
29	PoolSize *uint64
30	// URandomMinReseedSeconds is the number of seconds after which the DRNG will be reseeded.
31	URandomMinReseedSeconds *uint64
32	// WriteWakeupThreshold the number of bits of entropy below which we wake up processes
33	// that do a select(2) or poll(2) for write access to /dev/random.
34	WriteWakeupThreshold *uint64
35	// ReadWakeupThreshold is the number of bits of entropy required for waking up processes that sleep
36	// waiting for entropy from /dev/random.
37	ReadWakeupThreshold *uint64
38}
39
40// KernelRandom returns values from /proc/sys/kernel/random.
41func (fs FS) KernelRandom() (KernelRandom, error) {
42	random := KernelRandom{}
43
44	for file, p := range map[string]**uint64{
45		"entropy_avail":           &random.EntropyAvaliable,
46		"poolsize":                &random.PoolSize,
47		"urandom_min_reseed_secs": &random.URandomMinReseedSeconds,
48		"write_wakeup_threshold":  &random.WriteWakeupThreshold,
49		"read_wakeup_threshold":   &random.ReadWakeupThreshold,
50	} {
51		val, err := util.ReadUintFromFile(fs.proc.Path("sys", "kernel", "random", file))
52		if os.IsNotExist(err) {
53			continue
54		}
55		if err != nil {
56			return random, err
57		}
58		*p = &val
59	}
60
61	return random, nil
62}
63