>From 4dfe4949cb1b0ea23281b32f8a06c8f4aab2418f Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Wed, 27 May 2020 20:35:39 +0200 Subject: [PATCH 18/18] relocatable-prog: Make more robust in multithreaded applications. * lib/progreloc.c (O_CLOEXEC): Define fallback to 0 when use from module relocatable-prog-wrapper. (find_executable): Pass an O_CLOEXEC flag to open(). * modules/relocatable-prog (Depends-on): Add 'open'. --- ChangeLog | 8 ++++++++ lib/progreloc.c | 13 +++++++++---- modules/relocatable-prog | 1 + 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index e60de12..c35e2fd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2020-05-27 Bruno Haible + relocatable-prog: Make more robust in multithreaded applications. + * lib/progreloc.c (O_CLOEXEC): Define fallback to 0 when use from module + relocatable-prog-wrapper. + (find_executable): Pass an O_CLOEXEC flag to open(). + * modules/relocatable-prog (Depends-on): Add 'open'. + +2020-05-27 Bruno Haible + getloadavg: Make more robust in multithreaded applications. * lib/getloadavg.c (getloadavg): Pass an O_CLOEXEC flag to open(). Simplify use of O_CLOEXEC. diff --git a/lib/progreloc.c b/lib/progreloc.c index 45be1ca..de00bf6 100644 --- a/lib/progreloc.c +++ b/lib/progreloc.c @@ -70,6 +70,11 @@ # define O_EXEC O_RDONLY /* This is often close enough in older systems. */ #endif +#if defined IN_RELOCWRAPPER && (!defined O_CLOEXEC || GNULIB_defined_O_CLOEXEC) +# undef O_CLOEXEC +# define O_CLOEXEC 0 +#endif + /* Declare canonicalize_file_name. The included above may be the system's one, not the gnulib one. */ @@ -242,7 +247,7 @@ find_executable (const char *argv0) if (link != NULL && link[0] != '[') return link; if (executable_fd < 0) - executable_fd = open ("/proc/self/exe", O_EXEC, 0); + executable_fd = open ("/proc/self/exe", O_EXEC | O_CLOEXEC, 0); { char buf[6+10+5]; @@ -251,7 +256,7 @@ find_executable (const char *argv0) if (link != NULL && link[0] != '[') return link; if (executable_fd < 0) - executable_fd = open (buf, O_EXEC, 0); + executable_fd = open (buf, O_EXEC | O_CLOEXEC, 0); } } # endif @@ -298,7 +303,7 @@ find_executable (const char *argv0) the current directory. */ { char namebuf[4096]; - int fd = open ("/proc/self/execname", O_RDONLY, 0); + int fd = open ("/proc/self/execname", O_RDONLY | O_CLOEXEC, 0); if (fd >= 0) { size_t len = full_read (fd, namebuf, sizeof (namebuf)); @@ -321,7 +326,7 @@ find_executable (const char *argv0) if (link != NULL) return link; if (executable_fd < 0) - executable_fd = open ("/proc/self/exe", O_EXEC, 0); + executable_fd = open ("/proc/self/exe", O_EXEC | O_CLOEXEC, 0); } # endif # if HAVE_MACH_O_DYLD_H && HAVE__NSGETEXECUTABLEPATH diff --git a/modules/relocatable-prog b/modules/relocatable-prog index 4111665..7e21682 100644 --- a/modules/relocatable-prog +++ b/modules/relocatable-prog @@ -20,6 +20,7 @@ progname canonicalize-lgpl xalloc xreadlink +open stdbool unistd memcmp -- 2.7.4