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 15 #include <openssl/rand.h> 16 17 #include <stdlib.h> 18 19 #include "../fipsmodule/rand/internal.h" 20 21 22 /* g_buffering_enabled is true if fork-unsafe buffering has been enabled. */ 23 static int g_buffering_enabled = 0; 24 25 /* g_lock protects |g_buffering_enabled|. */ 26 static struct CRYPTO_STATIC_MUTEX g_lock = CRYPTO_STATIC_MUTEX_INIT; 27 28 void RAND_enable_fork_unsafe_buffering(int fd) { 29 /* We no longer support setting the file-descriptor with this function. */ 30 if (fd != -1) { 31 abort(); 32 } 33 34 CRYPTO_STATIC_MUTEX_lock_write(&g_lock); 35 g_buffering_enabled = 1; 36 CRYPTO_STATIC_MUTEX_unlock_write(&g_lock); 37 } 38 39 int rand_fork_unsafe_buffering_enabled(void) { 40 CRYPTO_STATIC_MUTEX_lock_read(&g_lock); 41 const int ret = g_buffering_enabled; 42 CRYPTO_STATIC_MUTEX_unlock_read(&g_lock); 43 return ret; 44 } 45