Adam Langley | 92f888e | 2017-04-11 15:06:47 -0700 | [diff] [blame] | 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 | |
Adam Langley | 7784104 | 2017-04-14 11:16:20 -0700 | [diff] [blame] | 19 | #include "../fipsmodule/rand/internal.h" |
Adam Langley | 92f888e | 2017-04-11 15:06:47 -0700 | [diff] [blame] | 20 | |
| 21 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 22 | // g_buffering_enabled is true if fork-unsafe buffering has been enabled. |
Adam Langley | 92f888e | 2017-04-11 15:06:47 -0700 | [diff] [blame] | 23 | static int g_buffering_enabled = 0; |
| 24 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 25 | // g_lock protects |g_buffering_enabled|. |
Adam Langley | 92f888e | 2017-04-11 15:06:47 -0700 | [diff] [blame] | 26 | static struct CRYPTO_STATIC_MUTEX g_lock = CRYPTO_STATIC_MUTEX_INIT; |
| 27 | |
David Benjamin | e2568c4 | 2017-08-16 15:25:27 -0400 | [diff] [blame] | 28 | #if !defined(OPENSSL_WINDOWS) |
Adam Langley | 92f888e | 2017-04-11 15:06:47 -0700 | [diff] [blame] | 29 | void RAND_enable_fork_unsafe_buffering(int fd) { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 30 | // We no longer support setting the file-descriptor with this function. |
Adam Langley | 92f888e | 2017-04-11 15:06:47 -0700 | [diff] [blame] | 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 | } |
David Benjamin | e2568c4 | 2017-08-16 15:25:27 -0400 | [diff] [blame] | 39 | #endif |
Adam Langley | 92f888e | 2017-04-11 15:06:47 -0700 | [diff] [blame] | 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 | } |