From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 9295 invoked by alias); 7 May 2013 06:46:10 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 9277 invoked by uid 89); 7 May 2013 06:46:08 -0000 X-Spam-SWARE-Status: No, score=-2.0 required=5.0 tests=AWL,BAYES_00,RCVD_IN_HOSTKARMA_NO autolearn=ham version=3.3.1 Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Tue, 07 May 2013 06:46:07 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 18B752ED85; Tue, 7 May 2013 02:46:06 -0400 (EDT) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id G90frajmEBvT; Tue, 7 May 2013 02:46:06 -0400 (EDT) Received: from joel.gnat.com (localhost.localdomain [127.0.0.1]) by rock.gnat.com (Postfix) with ESMTP id 977E22ED7E; Tue, 7 May 2013 02:46:05 -0400 (EDT) Received: by joel.gnat.com (Postfix, from userid 1000) id B363FC2AF9; Tue, 7 May 2013 10:45:59 +0400 (RET) Date: Tue, 07 May 2013 06:46:00 -0000 From: Joel Brobecker To: Tom Tromey Cc: gdb-patches@sourceware.org Subject: Re: RFC: close-on-exec internal file descriptors Message-ID: <20130507064559.GI5278@adacore.com> References: <874njjs1aa.fsf@fleche.redhat.com> <87fw32p00e.fsf@fleche.redhat.com> <874niy6y35.fsf@fleche.redhat.com> <87d2tmsey0.fsf@fleche.redhat.com> <20130501144722.GA10034@adacore.com> <874negym65.fsf@fleche.redhat.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="uh9ZiVrAOUUm9fzH" Content-Disposition: inline In-Reply-To: <874negym65.fsf@fleche.redhat.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-Virus-Found: No X-SW-Source: 2013-05/txt/msg00175.txt.bz2 --uh9ZiVrAOUUm9fzH Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-length: 597 > Sorry Joel. > I did not even consider this possibility. No worries at all! > 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. Yeah, hard to see an alternative based on the decisions made. Seems OK to me. I tested the patches, and they look good to me, except for the fact that we were missing the #include of filestuff.h. Attached is patch that builds and tests fine on x86_64-darwin and ia64-hpux... -- Joel --uh9ZiVrAOUUm9fzH Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-Do-not-close-pipes-used-to-communicate-with-child-du.patch" Content-length: 3701 >From 7118b53028599be9b5d2957bd1676c48ca87970b Mon Sep 17 00:00:00 2001 From: Joel Brobecker Date: Tue, 7 May 2013 02:33:02 -0400 Subject: [PATCH] Do not close pipes used to communicate with child during inferior fork --- gdb/common/filestuff.c | 27 +++++++++++++++++++++++++++ gdb/common/filestuff.h | 10 ++++++++++ gdb/darwin-nat.c | 7 +++++++ gdb/inf-ttrace.c | 11 +++++++++++ 4 files changed, 55 insertions(+), 0 deletions(-) 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..a2da924 100644 --- a/gdb/darwin-nat.c +++ b/gdb/darwin-nat.c @@ -66,6 +66,7 @@ #include #include "darwin-nat.h" +#include "common/filestuff.h" /* Quick overview. Darwin kernel is Mach + BSD derived kernel. Note that they share the @@ -1516,6 +1517,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 +1537,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 96b473d..2ecf482 100644 --- a/gdb/inf-ttrace.c +++ b/gdb/inf-ttrace.c @@ -38,6 +38,7 @@ #include "inf-child.h" #include "inf-ttrace.h" +#include "common/filestuff.h" @@ -558,6 +559,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 +578,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. */ -- 1.7.0.4 --uh9ZiVrAOUUm9fzH--