kernel_random.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. //go:build !windows
  14. // +build !windows
  15. package procfs
  16. import (
  17. "os"
  18. "github.com/prometheus/procfs/internal/util"
  19. )
  20. // KernelRandom contains information about to the kernel's random number generator.
  21. type KernelRandom struct {
  22. // EntropyAvaliable gives the available entropy, in bits.
  23. EntropyAvaliable *uint64
  24. // PoolSize gives the size of the entropy pool, in bits.
  25. PoolSize *uint64
  26. // URandomMinReseedSeconds is the number of seconds after which the DRNG will be reseeded.
  27. URandomMinReseedSeconds *uint64
  28. // WriteWakeupThreshold the number of bits of entropy below which we wake up processes
  29. // that do a select(2) or poll(2) for write access to /dev/random.
  30. WriteWakeupThreshold *uint64
  31. // ReadWakeupThreshold is the number of bits of entropy required for waking up processes that sleep
  32. // waiting for entropy from /dev/random.
  33. ReadWakeupThreshold *uint64
  34. }
  35. // KernelRandom returns values from /proc/sys/kernel/random.
  36. func (fs FS) KernelRandom() (KernelRandom, error) {
  37. random := KernelRandom{}
  38. for file, p := range map[string]**uint64{
  39. "entropy_avail": &random.EntropyAvaliable,
  40. "poolsize": &random.PoolSize,
  41. "urandom_min_reseed_secs": &random.URandomMinReseedSeconds,
  42. "write_wakeup_threshold": &random.WriteWakeupThreshold,
  43. "read_wakeup_threshold": &random.ReadWakeupThreshold,
  44. } {
  45. val, err := util.ReadUintFromFile(fs.proc.Path("sys", "kernel", "random", file))
  46. if os.IsNotExist(err) {
  47. continue
  48. }
  49. if err != nil {
  50. return random, err
  51. }
  52. *p = &val
  53. }
  54. return random, nil
  55. }