summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorY. T. Chung <zonyitoo@gmail.com>2017-07-20 15:02:23 (GMT)
committerDavid Goldblatt <davidtgoldblatt@gmail.com>2017-07-20 21:13:33 (GMT)
commit0975b88dfd3a890f469c8c282a5140013af85ab2 (patch)
tree147ac0ea173848299cba7ed803961726f400263e /src
parentfb6787a78c3a1e3a4868520d0531fc2ebdda21d8 (diff)
downloadjemalloc-0975b88dfd3a890f469c8c282a5140013af85ab2.zip
jemalloc-0975b88dfd3a890f469c8c282a5140013af85ab2.tar.gz
jemalloc-0975b88dfd3a890f469c8c282a5140013af85ab2.tar.bz2
Fall back to FD_CLOEXEC when O_CLOEXEC is unavailable.
Older Linux systems don't have O_CLOEXEC. If that's the case, we fcntl immediately after open, to minimize the length of the racy period in which an operation in another thread can leak a file descriptor to a child.
Diffstat (limited to 'src')
-rw-r--r--src/pages.c27
-rw-r--r--src/prof.c6
2 files changed, 28 insertions, 5 deletions
diff --git a/src/pages.c b/src/pages.c
index fec64dd..0883647 100644
--- a/src/pages.c
+++ b/src/pages.c
@@ -353,14 +353,31 @@ os_overcommits_proc(void) {
ssize_t nread;
#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_open)
- fd = (int)syscall(SYS_open, "/proc/sys/vm/overcommit_memory", O_RDONLY |
- O_CLOEXEC);
+ #if defined(O_CLOEXEC)
+ fd = (int)syscall(SYS_open, "/proc/sys/vm/overcommit_memory", O_RDONLY |
+ O_CLOEXEC);
+ #else
+ fd = (int)syscall(SYS_open, "/proc/sys/vm/overcommit_memory", O_RDONLY);
+ fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
+ #endif
#elif defined(JEMALLOC_USE_SYSCALL) && defined(SYS_openat)
- fd = (int)syscall(SYS_openat,
- AT_FDCWD, "/proc/sys/vm/overcommit_memory", O_RDONLY | O_CLOEXEC);
+ #if defined(O_CLOEXEC)
+ fd = (int)syscall(SYS_openat,
+ AT_FDCWD, "/proc/sys/vm/overcommit_memory", O_RDONLY | O_CLOEXEC);
+ #else
+ fd = (int)syscall(SYS_openat,
+ AT_FDCWD, "/proc/sys/vm/overcommit_memory", O_RDONLY);
+ fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
+ #endif
#else
- fd = open("/proc/sys/vm/overcommit_memory", O_RDONLY | O_CLOEXEC);
+ #if defined(O_CLOEXEC)
+ fd = open("/proc/sys/vm/overcommit_memory", O_RDONLY | O_CLOEXEC);
+ #else
+ fd = open("/proc/sys/vm/overcommit_memory", O_RDONLY);
+ fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
+ #endif
#endif
+
if (fd == -1) {
return false; /* Error. */
}
diff --git a/src/prof.c b/src/prof.c
index 975722c..a1ca9e2 100644
--- a/src/prof.c
+++ b/src/prof.c
@@ -1409,7 +1409,13 @@ prof_open_maps(const char *format, ...) {
va_start(ap, format);
malloc_vsnprintf(filename, sizeof(filename), format, ap);
va_end(ap);
+
+#if defined(O_CLOEXEC)
mfd = open(filename, O_RDONLY | O_CLOEXEC);
+#else
+ mfd = open(filename, O_RDONLY);
+ fcntl(mfd, F_SETFD, fcntl(mfd, F_GETFD) | FD_CLOEXEC);
+#endif
return mfd;
}