Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@redhat.com>
To: Joel Brobecker <brobecker@adacore.com>
Cc: gdb-patches@sourceware.org
Subject: Re: RFC: close-on-exec internal file descriptors
Date: Mon, 06 May 2013 18:53:00 -0000	[thread overview]
Message-ID: <874negym65.fsf@fleche.redhat.com> (raw)
In-Reply-To: <20130501144722.GA10034@adacore.com> (Joel Brobecker's message of	"Wed, 1 May 2013 18:47:22 +0400")

>>>>> "Joel" == Joel Brobecker <brobecker@adacore.com> writes:

Joel> Unfortunately, this is causing problems on Darwin and HP/UX,
Joel> because both platforms use pipes to synchronize parent and
Joel> child during the fork/exec. See for instance darwin-nat.c:

Sorry Joel.
I did not even consider this possibility.

Joel> I don't think there is a way of opening a FD with the intention
Joel> of protecting them from the close-at-exec, is there? So, the solution
Joel> would seem to add an interface to setup the protection, as well
Joel> as a function to remove that protection (necessary when we close
Joel> the fd).

Joel> What do you think?

Could you try the appended?  I can't even build parts of it, so...

I think it is pretty ugly, since it is a layer on top of the
kernel-level cloexec flags.  However, that is another discussion, from
the earlier reviews.

Tom

diff --git a/gdb/common/filestuff.c b/gdb/common/filestuff.c
index 68f66ca..e7af3a5 100644
--- a/gdb/common/filestuff.c
+++ b/gdb/common/filestuff.c
@@ -177,6 +177,33 @@ notice_open_fds (void)
   fdwalk (do_mark_open_fd, NULL);
 }
 
+/* See filestuff.h.  */
+
+void
+mark_fd_no_cloexec (int fd)
+{
+  do_mark_open_fd (NULL, fd);
+}
+
+/* See filestuff.h.  */
+
+void
+unmark_fd_no_cloexec (int fd)
+{
+  int i, val;
+
+  for (i = 0; VEC_iterate (int, open_fds, i, val); ++i)
+    {
+      if (fd == val)
+	{
+	  VEC_unordered_remove (int, open_fds, i);
+	  return;
+	}
+    }
+
+  gdb_assert_not_reached (_("fd not found in open_fds"));
+}
+
 /* Helper function for close_most_fds that closes the file descriptor
    if appropriate.  */
 
diff --git a/gdb/common/filestuff.h b/gdb/common/filestuff.h
index 0db33f0..b162a0c 100644
--- a/gdb/common/filestuff.h
+++ b/gdb/common/filestuff.h
@@ -24,6 +24,16 @@
 
 extern void notice_open_fds (void);
 
+/* Mark a file descriptor as inheritable across an exec.  */
+
+extern void mark_fd_no_cloexec (int fd);
+
+/* Mark a file descriptor as no longer being inheritable across an
+   exec.  This is only meaningful when FD was previously passed to
+   mark_fd_no_cloexec.  */
+
+extern void unmark_fd_no_cloexec (int fd);
+
 /* Close all open file descriptors other than those marked by
    'notice_open_fds', and stdin, stdout, and stderr.  Errors that
    occur while closing are ignored.  */
diff --git a/gdb/darwin-nat.c b/gdb/darwin-nat.c
index acdbf36..b7a8b35 100644
--- a/gdb/darwin-nat.c
+++ b/gdb/darwin-nat.c
@@ -1516,6 +1516,9 @@ darwin_pre_ptrace (void)
       ptrace_fds[1] = -1;
       error (_("unable to create a pipe: %s"), safe_strerror (errno));
     }
+
+  mark_fd_no_cloexec (ptrace_fds[0]);
+  mark_fd_no_cloexec (ptrace_fds[1]);
 }
 
 static void
@@ -1533,6 +1536,9 @@ darwin_ptrace_him (int pid)
   close (ptrace_fds[0]);
   close (ptrace_fds[1]);
 
+  unmark_fd_no_cloexec (ptrace_fds[0]);
+  unmark_fd_no_cloexec (ptrace_fds[1]);
+
   darwin_init_thread_list (inf);
 
   startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
diff --git a/gdb/inf-ttrace.c b/gdb/inf-ttrace.c
index 642e520..f39df49 100644
--- a/gdb/inf-ttrace.c
+++ b/gdb/inf-ttrace.c
@@ -558,6 +558,11 @@ do_cleanup_pfds (void *dummy)
   close (inf_ttrace_pfd1[1]);
   close (inf_ttrace_pfd2[0]);
   close (inf_ttrace_pfd2[1]);
+
+  unmark_fd_no_cloexec (inf_ttrace_pfd1[0]);
+  unmark_fd_no_cloexec (inf_ttrace_pfd1[1]);
+  unmark_fd_no_cloexec (inf_ttrace_pfd2[0]);
+  unmark_fd_no_cloexec (inf_ttrace_pfd2[1]);
 }
 
 static void
@@ -572,6 +577,11 @@ inf_ttrace_prepare (void)
       close (inf_ttrace_pfd2[0]);
       perror_with_name (("pipe"));
     }
+
+  mark_fd_no_cloexec (inf_ttrace_pfd1[0]);
+  mark_fd_no_cloexec (inf_ttrace_pfd1[1]);
+  mark_fd_no_cloexec (inf_ttrace_pfd2[0]);
+  mark_fd_no_cloexec (inf_ttrace_pfd2[1]);
 }
 
 /* Prepare to be traced.  */


  reply	other threads:[~2013-05-06 18:53 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-18 17:45 Tom Tromey
2012-12-18 22:12 ` Andreas Schwab
2012-12-19 14:53   ` Tom Tromey
2013-01-03 16:17     ` Tom Tromey
2013-04-22 23:55       ` Tom Tromey
2013-04-23  1:51         ` Tom Tromey
2013-05-01 14:47         ` Joel Brobecker
2013-05-06 18:53           ` Tom Tromey [this message]
2013-05-07  6:46             ` Joel Brobecker
2013-05-10 17:00               ` Tom Tromey
2013-04-23 15:45 ` Yao Qi
2013-04-24  2:45   ` Tom Tromey
2013-04-24 20:35     ` Jan Kratochvil

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=874negym65.fsf@fleche.redhat.com \
    --to=tromey@redhat.com \
    --cc=brobecker@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox