From f274e136db89f184964d0a8cc74dffec51f2086f Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Wed, 27 May 2020 20:35:39 +0200 Subject: [PATCH] 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 916f9cf945..e637b5772c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2020-05-28 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-28 Bruno Haible getloadavg: Make more robust in multithreaded applications. diff --git a/lib/progreloc.c b/lib/progreloc.c index 45be1ca5a4..de00bf6333 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 4111665c33..7e21682ecd 100644 --- a/modules/relocatable-prog +++ b/modules/relocatable-prog @@ -20,6 +20,7 @@ progname canonicalize-lgpl xalloc xreadlink +open stdbool unistd memcmp -- 2.39.5