1 // Copyright 2017 The BoringSSL Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <openssl/rand.h>
16 
17 #include <stdlib.h>
18 
19 #include "../fipsmodule/rand/internal.h"
20 #include "../internal.h"
21 
22 
23 // g_buffering_enabled is one if fork-unsafe buffering has been enabled and zero
24 // otherwise.
25 static CRYPTO_atomic_u32 g_buffering_enabled;
26 
27 #if !defined(OPENSSL_WINDOWS)
RAND_enable_fork_unsafe_buffering(int fd)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_atomic_store_u32(&g_buffering_enabled, 1);
35 }
36 
RAND_disable_fork_unsafe_buffering(void)37 void RAND_disable_fork_unsafe_buffering(void) {
38   CRYPTO_atomic_store_u32(&g_buffering_enabled, 0);
39 }
40 #endif
41 
rand_fork_unsafe_buffering_enabled(void)42 int rand_fork_unsafe_buffering_enabled(void) {
43   return CRYPTO_atomic_load_u32(&g_buffering_enabled) != 0;
44 }
45