forkunsafe.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /* Copyright (c) 2017, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/rand.h>
  15. #include <stdlib.h>
  16. #include "../fipsmodule/rand/internal.h"
  17. // g_buffering_enabled is true if fork-unsafe buffering has been enabled.
  18. static int g_buffering_enabled = 0;
  19. // g_lock protects |g_buffering_enabled|.
  20. static struct CRYPTO_STATIC_MUTEX g_lock = CRYPTO_STATIC_MUTEX_INIT;
  21. #if !defined(OPENSSL_WINDOWS)
  22. void RAND_enable_fork_unsafe_buffering(int fd) {
  23. // We no longer support setting the file-descriptor with this function.
  24. if (fd != -1) {
  25. abort();
  26. }
  27. CRYPTO_STATIC_MUTEX_lock_write(&g_lock);
  28. g_buffering_enabled = 1;
  29. CRYPTO_STATIC_MUTEX_unlock_write(&g_lock);
  30. }
  31. #endif
  32. int rand_fork_unsafe_buffering_enabled(void) {
  33. CRYPTO_STATIC_MUTEX_lock_read(&g_lock);
  34. const int ret = g_buffering_enabled;
  35. CRYPTO_STATIC_MUTEX_unlock_read(&g_lock);
  36. return ret;
  37. }