diff --git a/header_checks/meson.build b/header_checks/meson.build index ff5b21ee0c..4cc855e218 100644 --- a/header_checks/meson.build +++ b/header_checks/meson.build @@ -123,6 +123,20 @@ function_checks = [ ['dladdr', ['dlfcn.h'], ['dl'], '-D_GNU_SOURCE=1'] ] +open_cloexec = cc.run('''#include + #include + #include + int main(int argc, char **argv) { + int res = open(argv[0], O_RDONLY | O_CLOEXEC); + if (res < 0) return 1; + return 0; + } + ''', + name : 'open works with O_CLOEXEC') +if open_cloexec.compiled() and open_cloexec.returncode() == 0 + config_h.set10('HAVE_OPEN_CLOEXEC', true) +endif + strerror_r_char_p = cc.compiles('''#define _GNU_SOURCE #include int func (void) diff --git a/src/lib/eina/eina_file.c b/src/lib/eina/eina_file.c index cc53f65cce..068aa12e14 100644 --- a/src/lib/eina/eina_file.c +++ b/src/lib/eina/eina_file.c @@ -802,19 +802,28 @@ eina_file_open(const char *path, Eina_Bool shared) if (!filename) return NULL; if (shared) + { #ifdef HAVE_SHM_OPEN - fd = shm_open(filename, O_RDONLY, S_IRWXU | S_IRWXG | S_IRWXO); + fd = shm_open(filename, O_RDONLY, S_IRWXU | S_IRWXG | S_IRWXO); + if ((fd != -1) && (!eina_file_close_on_exec(fd, EINA_TRUE))) + goto on_error; #else - goto on_error; + goto on_error; #endif + } else - fd = open(filename, O_RDONLY, S_IRWXU | S_IRWXG | S_IRWXO); + { +#ifdef HAVE_OPEN_CLOEXEC + fd = open(filename, O_RDONLY, S_IRWXU | S_IRWXG | S_IRWXO | O_CLOEXEC); +#else + fd = open(filename, O_RDONLY, S_IRWXU | S_IRWXG | S_IRWXO); + if ((fd != -1) && (!eina_file_close_on_exec(fd, EINA_TRUE))) + goto on_error; +#endif + } if (fd < 0) goto on_error; - if (!eina_file_close_on_exec(fd, EINA_TRUE)) - goto on_error; - if (fstat(fd, &file_stat)) goto on_error; diff --git a/src/lib/eina/eina_mmap.c b/src/lib/eina/eina_mmap.c index 11d8804759..23386dac1d 100644 --- a/src/lib/eina/eina_mmap.c +++ b/src/lib/eina/eina_mmap.c @@ -198,10 +198,16 @@ eina_mmap_safety_enabled_set(Eina_Bool enabled) /* no zero page device - open it */ if (_eina_mmap_zero_fd < 0) { +#ifdef HAVE_OPEN_CLOEXEC + _eina_mmap_zero_fd = open("/dev/zero", O_RDWR | O_CLOEXEC); + /* if we don;'t have one - fail to set up mmap safety */ + if (_eina_mmap_zero_fd < 0) return EINA_FALSE; +#else _eina_mmap_zero_fd = open("/dev/zero", O_RDWR); /* if we don;'t have one - fail to set up mmap safety */ if (_eina_mmap_zero_fd < 0) return EINA_FALSE; eina_file_close_on_exec(_eina_mmap_zero_fd, EINA_TRUE); +#endif } /* set up signal handler for SIGBUS */ sa.sa_sigaction = _eina_mmap_safe_sigbus;