diff options
author | jb <jb@138bc75d-0d04-0410-961f-82ee72b054a4> | 2013-11-10 20:34:29 +0000 |
---|---|---|
committer | jb <jb@138bc75d-0d04-0410-961f-82ee72b054a4> | 2013-11-10 20:34:29 +0000 |
commit | 544db64f1b123e87d643578cd9362d4ef847cee4 (patch) | |
tree | 25502b48dbfb346acf48c054900baac6f9b389c0 /libgfortran/io | |
parent | cc8feb1cf8566f3fc4ca14f6e509e2eb518a2c75 (diff) | |
download | gcc-544db64f1b123e87d643578cd9362d4ef847cee4.tar.gz |
Set close-on-exec flag when opening files.
2013-11-10 Janne Blomqvist <jb@gcc.gnu.org>
* configure.ac: Check presence of mkostemp.
* io/unix.c (set_close_on_exec): New function.
(tempfile_open): Use mkostemp and O_CLOEXEC if available, fallback
to calling set_close_on_exec.
(regular_file): Add O_CLOEXEC to flags if defined.
(open_external): Call set_close_on_exec if O_CLOEXEC is not
defined.
* config.h.in: Regenerated.
* configure: Regenerated.
* Makefile.in: Regenerated.
* aclocal.m4: Regenerated.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@204654 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libgfortran/io')
-rw-r--r-- | libgfortran/io/unix.c | 43 |
1 files changed, 37 insertions, 6 deletions
diff --git a/libgfortran/io/unix.c b/libgfortran/io/unix.c index dd2715b6b6d..8a84ae4eef3 100644 --- a/libgfortran/io/unix.c +++ b/libgfortran/io/unix.c @@ -1070,6 +1070,20 @@ unpack_filename (char *cstring, const char *fstring, int len) } +/* Set the close-on-exec flag for an existing fd, if the system + supports such. */ + +static void __attribute__ ((unused)) +set_close_on_exec (int fd __attribute__ ((unused))) +{ + /* Mingw does not define F_SETFD. */ +#if defined(F_SETFD) && defined(FD_CLOEXEC) + if (fd >= 0) + fcntl(fd, F_SETFD, FD_CLOEXEC); +#endif +} + + /* Helper function for tempfile(). Tries to open a temporary file in the directory specified by tempdir. If successful, the file name is stored in fname and the descriptor returned. Returns -1 on @@ -1109,7 +1123,12 @@ tempfile_open (const char *tempdir, char **fname) mode_mask = umask (S_IXUSR | S_IRWXG | S_IRWXO); #endif +#if defined(HAVE_MKOSTEMP) && defined(O_CLOEXEC) + fd = mkostemp (template, O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC); +#else fd = mkstemp (template); + set_close_on_exec (fd); +#endif #ifdef HAVE_UMASK (void) umask (mode_mask); @@ -1119,6 +1138,13 @@ tempfile_open (const char *tempdir, char **fname) fd = -1; int count = 0; size_t slashlen = strlen (slash); + int flags = O_RDWR | O_CREAT | O_EXCL; +#if defined(HAVE_CRLF) && defined(O_BINARY) + flags |= O_BINARY; +#endif +#ifdef O_CLOEXEC + flags |= O_CLOEXEC; +#endif do { snprintf (template, tempdirlen + 23, "%s%sgfortrantmpaaaXXXXXX", @@ -1142,14 +1168,12 @@ tempfile_open (const char *tempdir, char **fname) continue; } -#if defined(HAVE_CRLF) && defined(O_BINARY) - fd = open (template, O_RDWR | O_CREAT | O_EXCL | O_BINARY, - S_IRUSR | S_IWUSR); -#else - fd = open (template, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); -#endif + fd = open (template, flags, S_IRUSR | S_IWUSR); } while (fd == -1 && errno == EEXIST); +#ifndef O_CLOEXEC + set_close_on_exec (fd); +#endif #endif /* HAVE_MKSTEMP */ *fname = template; @@ -1323,6 +1347,10 @@ regular_file (st_parameter_open *opp, unit_flags *flags) crflag |= O_BINARY; #endif +#ifdef O_CLOEXEC + crflag |= O_CLOEXEC; +#endif + mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; fd = open (path, rwflag | crflag, mode); if (flags->action != ACTION_UNSPECIFIED) @@ -1386,6 +1414,9 @@ open_external (st_parameter_open *opp, unit_flags *flags) /* regular_file resets flags->action if it is ACTION_UNSPECIFIED and * if it succeeds */ fd = regular_file (opp, flags); +#ifndef O_CLOEXEC + set_close_on_exec (fd); +#endif } if (fd < 0) |