aboutsummaryrefslogtreecommitdiffstats
path: root/src/_cffi_src
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2019-01-23 19:51:32 -0500
committerPaul Kehrer <paul.l.kehrer@gmail.com>2019-01-23 18:51:32 -0600
commit908121176f037c618b1f774ab969ad7f67ea3020 (patch)
tree9eb10b6e5c8a3b3d9bc09c85fefd42e06ba7105b /src/_cffi_src
parent543226c1a00f9a775358d8028b462e99b1bdb60a (diff)
downloadcryptography-908121176f037c618b1f774ab969ad7f67ea3020.tar.gz
cryptography-908121176f037c618b1f774ab969ad7f67ea3020.tar.bz2
cryptography-908121176f037c618b1f774ab969ad7f67ea3020.zip
Use O_CLOEXEC when it's available (#4733)
* Use O_CLOEXEC when it's available * Don't have two vars with the same name * A normal person would be emberassed
Diffstat (limited to 'src/_cffi_src')
-rw-r--r--src/_cffi_src/openssl/src/osrandom_engine.c26
1 files changed, 16 insertions, 10 deletions
diff --git a/src/_cffi_src/openssl/src/osrandom_engine.c b/src/_cffi_src/openssl/src/osrandom_engine.c
index 697381c8..1a660f0b 100644
--- a/src/_cffi_src/openssl/src/osrandom_engine.c
+++ b/src/_cffi_src/openssl/src/osrandom_engine.c
@@ -94,7 +94,18 @@ static struct {
ino_t st_ino;
} urandom_cache = { -1 };
-static int set_cloexec(int fd) {
+static int open_cloexec(const char *path) {
+ int open_flags = O_RDONLY;
+#ifdef O_CLOEXEC
+ open_flags |= O_CLOEXEC;
+#endif
+
+ int fd = open(path, open_flags);
+ if (fd == -1) {
+ return -1;
+ }
+
+#ifndef O_CLOEXEC
int flags = fcntl(fd, F_GETFD);
if (flags == -1) {
return -1;
@@ -102,7 +113,8 @@ static int set_cloexec(int fd) {
if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) {
return -1;
}
- return 0;
+#endif
+ return fd;
}
#ifdef __linux__
@@ -114,13 +126,10 @@ static int set_cloexec(int fd) {
static int wait_on_devrandom(void) {
struct pollfd pfd = {};
int ret = 0;
- int random_fd = open("/dev/random", O_RDONLY);
+ int random_fd = open_cloexec("/dev/random");
if (random_fd < 0) {
return -1;
}
- if (set_cloexec(random_fd) < 0) {
- return -1;
- }
pfd.fd = random_fd;
pfd.events = POLLIN;
pfd.revents = 0;
@@ -154,13 +163,10 @@ static int dev_urandom_fd(void) {
}
#endif
- fd = open("/dev/urandom", O_RDONLY);
+ fd = open_cloexec("/dev/urandom");
if (fd < 0) {
goto error;
}
- if (set_cloexec(fd) < 0) {
- goto error;
- }
if (fstat(fd, &st)) {
goto error;
}