diff mbox

Mark more file descriptors close-on-exec

Message ID 20180918224747.6396-1-tom@tromey.com
State New
Headers show

Commit Message

Tom Tromey Sept. 18, 2018, 10:47 p.m. UTC
I noticed a couple of spots in gdb that were opening files but not
marking the file descriptors as close-on-exec.  This patch fixes
these.

There are still a few more of these, but they are in code that I can't
compile, so I'd prefer not to touch.

gdb/ChangeLog
2018-09-18  Tom Tromey  <tom@tromey.com>

	* ctf.c (ctf_start): Use gdb_fopen_cloexec.
	* common/scoped_mmap.c (mmap_file): Use gdb_open_cloexec.
---
 gdb/ChangeLog            | 5 +++++
 gdb/common/scoped_mmap.c | 3 ++-
 gdb/ctf.c                | 7 +++++--
 3 files changed, 12 insertions(+), 3 deletions(-)

Comments

Joel Brobecker Sept. 19, 2018, 3:30 p.m. UTC | #1
Hi Tom,

On Tue, Sep 18, 2018 at 04:47:47PM -0600, Tom Tromey wrote:
> I noticed a couple of spots in gdb that were opening files but not
> marking the file descriptors as close-on-exec.  This patch fixes
> these.
> 
> There are still a few more of these, but they are in code that I can't
> compile, so I'd prefer not to touch.
> 
> gdb/ChangeLog
> 2018-09-18  Tom Tromey  <tom@tromey.com>
> 
> 	* ctf.c (ctf_start): Use gdb_fopen_cloexec.
> 	* common/scoped_mmap.c (mmap_file): Use gdb_open_cloexec.

Thanks for the patch, Tom. It looks good to me.

Which other locations did you notice that you couldn't compile?
Tom Tromey Sept. 19, 2018, 6:44 p.m. UTC | #2
>>>>> "Joel" == Joel Brobecker <brobecker@adacore.com> writes:

Joel> Which other locations did you notice that you couldn't compile?

I grep for '[^ \t]f*open ('.  Some of the hits should not be cloexec --
like the tty stuff.  But nto-procfs.c, proc-api.c, procfs.c,
spu-linux-nat.c, and windows-nat.c should probably all use it.

It's relatively unimportant because it is hard to see any bad behavior,
at least for some uses of open.  You have to have a python or guile
thread running that does an exec at the exact wrong instant.
Nevertheless it's good for gdb to remain clean this way.

Tom
diff mbox

Patch

diff --git a/gdb/common/scoped_mmap.c b/gdb/common/scoped_mmap.c
index 7eb729312f0..e1fffb9336c 100644
--- a/gdb/common/scoped_mmap.c
+++ b/gdb/common/scoped_mmap.c
@@ -20,13 +20,14 @@ 
 #include "defs.h"
 #include "scoped_mmap.h"
 #include "scoped_fd.h"
+#include "common/filestuff.h"
 
 #ifdef HAVE_SYS_MMAN_H
 
 scoped_mmap
 mmap_file (const char *filename)
 {
-  scoped_fd fd (open (filename, O_RDONLY));
+  scoped_fd fd (gdb_open_cloexec (filename, O_RDONLY, 0));
   if (fd.get () < 0)
     perror_with_name (("open"));
 
diff --git a/gdb/ctf.c b/gdb/ctf.c
index a156b1faf27..43bb18ecda1 100644
--- a/gdb/ctf.c
+++ b/gdb/ctf.c
@@ -31,6 +31,7 @@ 
 #include "tracefile.h"
 #include <ctype.h>
 #include <algorithm>
+#include "common/filestuff.h"
 
 /* The CTF target.  */
 
@@ -354,7 +355,8 @@  ctf_start (struct trace_file_writer *self, const char *dirname)
 
   std::string file_name = string_printf ("%s/%s", dirname, CTF_METADATA_NAME);
 
-  writer->tcs.metadata_fd = fopen (file_name.c_str (), "w");
+  writer->tcs.metadata_fd
+    = gdb_fopen_cloexec (file_name.c_str (), "w").release ();
   if (writer->tcs.metadata_fd == NULL)
     error (_("Unable to open file '%s' for saving trace data (%s)"),
 	   file_name.c_str (), safe_strerror (errno));
@@ -362,7 +364,8 @@  ctf_start (struct trace_file_writer *self, const char *dirname)
   ctf_save_metadata_header (&writer->tcs);
 
   file_name = string_printf ("%s/%s", dirname, CTF_DATASTREAM_NAME);
-  writer->tcs.datastream_fd = fopen (file_name.c_str (), "w");
+  writer->tcs.datastream_fd
+    = gdb_fopen_cloexec (file_name.c_str (), "w").release ();
   if (writer->tcs.datastream_fd == NULL)
     error (_("Unable to open file '%s' for saving trace data (%s)"),
 	   file_name.c_str (), safe_strerror (errno));