lwe_glibc_compat.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Functions that bundled libraries of the portable release call but older systems don't have.
  2. //
  3. // glibc: libraries retargeted by retarget_glibc_versions.py can't bind to libc's own copy and resolve to this
  4. // unversioned one instead (see the script).
  5. //
  6. // Others are forwarded to the system's own copy when it has one. Libraries referencing them get this one as a
  7. // dependency (package_release.sh).
  8. #define _GNU_SOURCE
  9. #include <dlfcn.h>
  10. #include <errno.h>
  11. #include <stddef.h>
  12. #include <stdint.h>
  13. #include <string.h>
  14. #include <sys/random.h>
  15. // glibc 2.36
  16. void arc4random_buf (void* buffer, size_t size) {
  17. unsigned char* out = buffer;
  18. while (size > 0) {
  19. ssize_t got = getrandom (out, size, 0);
  20. if (got < 0) {
  21. if (errno == EINTR)
  22. continue;
  23. // no fallback: getrandom exists since Linux 3.17
  24. __builtin_trap ();
  25. }
  26. out += got;
  27. size -= got;
  28. }
  29. }
  30. uint32_t arc4random (void) {
  31. uint32_t value;
  32. arc4random_buf (&value, sizeof (value));
  33. return value;
  34. }
  35. uint32_t arc4random_uniform (uint32_t upper) {
  36. if (upper < 2)
  37. return 0;
  38. // rejection sampling to avoid the modulo bias
  39. uint32_t min = -upper % upper;
  40. uint32_t value;
  41. do {
  42. value = arc4random ();
  43. } while (value < min);
  44. return value % upper;
  45. }
  46. // libpipewire 0.3.50, called by mpv (Ubuntu 22.04 has 0.3.48). The older call fills the start of the same struct
  47. int pw_stream_get_time_n (void* stream, void* time, size_t size) {
  48. static int (*own) (void*, void*, size_t);
  49. static int (*old) (void*, void*);
  50. if (!own && !old) {
  51. own = dlsym (RTLD_NEXT, "pw_stream_get_time_n");
  52. old = dlsym (RTLD_DEFAULT, "pw_stream_get_time");
  53. }
  54. if (own)
  55. return own (stream, time, size);
  56. if (!old)
  57. return -ENOSYS;
  58. memset (time, 0, size);
  59. return old (stream, time);
  60. }