entropy.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* See LICENSE file for copyright and license details. */
  2. #include "../components_config.h"
  3. #include "../lib/util.h"
  4. #ifdef __linux__
  5. # include <err.h>
  6. # include <fcntl.h>
  7. # include <stdint.h>
  8. # include <stdio.h>
  9. # include "../aslstatus.h"
  10. # define ENTROPY_AVAIL "/proc/sys/kernel/random/entropy_avail"
  11. void
  12. entropy(char* out,
  13. const char __unused* _a,
  14. uint32_t __unused _i,
  15. static_data_t* static_data)
  16. {
  17. size_t readed;
  18. int* fd = static_data->data;
  19. char buf[INT_STR_SIZE];
  20. /* if `static_data` contain only `fd`
  21. * then you can use `fd_cleanup` function from util.h */
  22. if (!static_data->cleanup) static_data->cleanup = fd_cleanup;
  23. if (*fd > 0) {
  24. /* if file already opened then rewind */
  25. if (!fd_rewind(*fd)) ERRRET(out);
  26. } else {
  27. /* open file */
  28. if (!eopen(*fd, ENTROPY_AVAIL, O_RDONLY | O_CLOEXEC))
  29. ERRRET(out);
  30. }
  31. /* read from file and save size of readed data */
  32. if (!eread_ret(readed, *fd, WITH_LEN(buf))) ERRRET(out);
  33. buf[--readed /* '\n' at the end */] = '\0';
  34. bprintf(out, "%s", buf);
  35. }
  36. #else
  37. # include <string.h>
  38. # ifndef ENTROPY_INFINITY
  39. # define ENTROPY_INFINITY "\xe2\x88\x9e"
  40. # endif
  41. void
  42. entropy(char *out,
  43. const char __unused *_a,
  44. uint32_t __unused _i,
  45. void __unused *_p)
  46. {
  47. bprintf(out, "%s", ENTROPY_INFINITY);
  48. }
  49. #endif