Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc-cloned / deps / grpc / third_party / boringssl / crypto / rand_extra / forkunsafe.c
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 #if !defined(OPENSSL_WINDOWS)
29 void RAND_enable_fork_unsafe_buffering(int fd) {
30   // We no longer support setting the file-descriptor with this function.
31   if (fd != -1) {
32     abort();
33   }
34
35   CRYPTO_STATIC_MUTEX_lock_write(&g_lock);
36   g_buffering_enabled = 1;
37   CRYPTO_STATIC_MUTEX_unlock_write(&g_lock);
38 }
39 #endif
40
41 int rand_fork_unsafe_buffering_enabled(void) {
42   CRYPTO_STATIC_MUTEX_lock_read(&g_lock);
43   const int ret = g_buffering_enabled;
44   CRYPTO_STATIC_MUTEX_unlock_read(&g_lock);
45   return ret;
46 }