From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3168 invoked by alias); 12 Mar 2008 14:13:03 -0000 Received: (qmail 3157 invoked by uid 22791); 12 Mar 2008 14:13:02 -0000 X-Spam-Check-By: sourceware.org Received: from igw2.br.ibm.com (HELO igw2.br.ibm.com) (32.104.18.25) by sourceware.org (qpsmtpd/0.31) with ESMTP; Wed, 12 Mar 2008 14:12:34 +0000 Received: from mailhub3.br.ibm.com (mailhub3 [9.18.232.110]) by igw2.br.ibm.com (Postfix) with ESMTP id 8BA9D17F5FE for ; Wed, 12 Mar 2008 11:04:22 -0300 (BRST) Received: from d24av01.br.ibm.com (d24av01.br.ibm.com [9.18.232.46]) by mailhub3.br.ibm.com (8.13.8/8.13.8/NCO v8.7) with ESMTP id m2CECQsV3055774 for ; Wed, 12 Mar 2008 11:12:26 -0300 Received: from d24av01.br.ibm.com (loopback [127.0.0.1]) by d24av01.br.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id m2CECPFs001788 for ; Wed, 12 Mar 2008 11:12:25 -0300 Received: from [9.18.238.95] (dyn531828.br.ibm.com [9.18.238.95]) by d24av01.br.ibm.com (8.12.11.20060308/8.12.11) with ESMTP id m2CECPeD001777; Wed, 12 Mar 2008 11:12:25 -0300 Subject: Re: [RFA] Fix GDB's handling of the inferior controlling terminal. From: Thiago Jung Bauermann To: Daniel Jacobowitz Cc: gdb-patches In-Reply-To: <20080312123958.GA9349@caradoc.them.org> References: <1205170916.18153.202.camel@localhost.localdomain> <20080312123958.GA9349@caradoc.them.org> Content-Type: multipart/mixed; boundary="=-p6eiBROxyFtu4Qd/EOHZ" Date: Wed, 12 Mar 2008 14:13:00 -0000 Message-Id: <1205331145.6643.32.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.12.3 X-IsSubscribed: yes 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 X-SW-Source: 2008-03/txt/msg00126.txt.bz2 --=-p6eiBROxyFtu4Qd/EOHZ Content-Type: text/plain Content-Transfer-Encoding: 7bit Content-length: 354 On Wed, 2008-03-12 at 08:39 -0400, Daniel Jacobowitz wrote: > On Mon, Mar 10, 2008 at 02:41:56PM -0300, Thiago Jung Bauermann wrote: > This is OK, thanks. > > > + ret = setsid(); > > Space there before paren. Thanks! Committed with the change. Attached is the final patch. -- []'s Thiago Jung Bauermann Software Engineer IBM Linux Technology Center --=-p6eiBROxyFtu4Qd/EOHZ Content-Disposition: attachment; filename=controlling-tty-fix.diff Content-Type: text/x-patch; name=controlling-tty-fix.diff; charset=utf-8 Content-Transfer-Encoding: 7bit Content-length: 4951 2008-03-10 Thiago Jung Bauermann * configure.ac (AC_CHECK_FUNCS): Add check for setsid. * config.in, configure: Regenerate. * fork-child.c (fork_inferior): Call create_tty_session. * inflow.c (new_tty): Set controlling terminal with TIOCSCTTY. (create_tty_session): New function. * terminal.h: Declare create_tty_session. === modified file 'gdb/config.in' Index: gdb/config.in =================================================================== --- gdb/config.in.orig 2008-01-08 17:36:25.000000000 -0200 +++ gdb/config.in 2008-03-12 11:05:33.000000000 -0300 @@ -278,6 +278,9 @@ /* Define to 1 if you have the `setpgrp' function. */ #undef HAVE_SETPGRP +/* Define to 1 if you have the `setsid' function. */ +#undef HAVE_SETSID + /* Define to 1 if you have the header file. */ #undef HAVE_SGTTY_H Index: gdb/configure =================================================================== --- gdb/configure.orig 2008-03-12 11:04:38.000000000 -0300 +++ gdb/configure 2008-03-12 11:05:34.000000000 -0300 @@ -18503,7 +18503,8 @@ done -for ac_func in setpgid setpgrp + +for ac_func in setpgid setpgrp setsid do as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh` echo "$as_me:$LINENO: checking for $ac_func" >&5 Index: gdb/configure.ac =================================================================== --- gdb/configure.ac.orig 2008-03-12 11:04:38.000000000 -0300 +++ gdb/configure.ac 2008-03-12 11:05:34.000000000 -0300 @@ -603,7 +603,7 @@ AC_CHECK_FUNCS(getuid getgid) AC_CHECK_FUNCS(poll) AC_CHECK_FUNCS(pread64) AC_CHECK_FUNCS(sbrk) -AC_CHECK_FUNCS(setpgid setpgrp) +AC_CHECK_FUNCS(setpgid setpgrp setsid) AC_CHECK_FUNCS(sigaction sigprocmask sigsetmask) AC_CHECK_FUNCS(socketpair) AC_CHECK_FUNCS(syscall) Index: gdb/fork-child.c =================================================================== --- gdb/fork-child.c.orig 2008-03-12 11:04:38.000000000 -0300 +++ gdb/fork-child.c 2008-03-12 11:05:34.000000000 -0300 @@ -303,10 +303,16 @@ fork_inferior (char *exec_file_arg, char if (debug_fork) sleep (debug_fork); - /* Run inferior in a separate process group. */ - debug_setpgrp = gdb_setpgid (); - if (debug_setpgrp == -1) - perror ("setpgrp failed in child"); + /* Create a new session for the inferior process, if necessary. + It will also place the inferior in a separate process group. */ + if (create_tty_session () <= 0) + { + /* No session was created, but we still want to run the inferior + in a separate process group. */ + debug_setpgrp = gdb_setpgid (); + if (debug_setpgrp == -1) + perror ("setpgrp failed in child"); + } /* Ask the tty subsystem to switch to the one we specified earlier (or to share the current terminal, if none was Index: gdb/inflow.c =================================================================== --- gdb/inflow.c.orig 2008-01-03 19:27:04.000000000 -0200 +++ gdb/inflow.c 2008-03-12 11:08:55.000000000 -0300 @@ -557,6 +557,16 @@ new_tty (void) close (2); dup (tty); } + +#ifdef TIOCSCTTY + /* Make tty our new controlling terminal. */ + if (ioctl (tty, TIOCSCTTY, 0) == -1) + /* Mention GDB in warning because it will appear in the inferior's + terminal instead of GDB's. */ + warning ("GDB: Failed to set controlling terminal: %s", + safe_strerror (errno)); +#endif + if (tty > 2) close (tty); #endif /* !go32 && !win32 */ @@ -683,6 +693,33 @@ clear_sigio_trap (void) #endif /* No SIGIO. */ +/* Create a new session if the inferior will run in a different tty. + A session is UNIX's way of grouping processes that share a controlling + terminal, so a new one is needed if the inferior terminal will be + different from GDB's. + + Returns the session id of the new session, 0 if no session was created + or -1 if an error occurred. */ +pid_t +create_tty_session (void) +{ +#ifdef HAVE_SETSID + pid_t ret; + + if (!job_control || inferior_thisrun_terminal == 0) + return 0; + + ret = setsid (); + if (ret == -1) + warning ("Failed to create new terminal session: setsid: %s", + safe_strerror (errno)); + + return ret; +#else + return 0; +#endif /* HAVE_SETSID */ +} + /* This is here because this is where we figure out whether we (probably) have job control. Just using job_control only does part of it because setpgid or setpgrp might not exist on a system without job control. Index: gdb/terminal.h =================================================================== --- gdb/terminal.h.orig 2008-01-03 19:27:07.000000000 -0200 +++ gdb/terminal.h 2008-03-12 11:05:34.000000000 -0300 @@ -82,6 +82,8 @@ extern void new_tty (void); a given run of GDB. In inflow.c. */ extern int job_control; +extern int create_tty_session (void); + /* Set the process group of the caller to its own pid, or do nothing if we lack job control. */ extern int gdb_setpgid (void); --=-p6eiBROxyFtu4Qd/EOHZ--