xref: /dflybsd-src/contrib/gdb-7/gdb/inflow.c (revision 5796c8dc12c637f18a1740c26afd8d40ffa9b719)
1*5796c8dcSSimon Schubert /* Low level interface to ptrace, for GDB when running under Unix.
2*5796c8dcSSimon Schubert    Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3*5796c8dcSSimon Schubert    1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4*5796c8dcSSimon Schubert    2009 Free Software Foundation, Inc.
5*5796c8dcSSimon Schubert 
6*5796c8dcSSimon Schubert    This file is part of GDB.
7*5796c8dcSSimon Schubert 
8*5796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
9*5796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
10*5796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
11*5796c8dcSSimon Schubert    (at your option) any later version.
12*5796c8dcSSimon Schubert 
13*5796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
14*5796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
15*5796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*5796c8dcSSimon Schubert    GNU General Public License for more details.
17*5796c8dcSSimon Schubert 
18*5796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
19*5796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20*5796c8dcSSimon Schubert 
21*5796c8dcSSimon Schubert #include "defs.h"
22*5796c8dcSSimon Schubert #include "frame.h"
23*5796c8dcSSimon Schubert #include "inferior.h"
24*5796c8dcSSimon Schubert #include "command.h"
25*5796c8dcSSimon Schubert #include "serial.h"
26*5796c8dcSSimon Schubert #include "terminal.h"
27*5796c8dcSSimon Schubert #include "target.h"
28*5796c8dcSSimon Schubert #include "gdbthread.h"
29*5796c8dcSSimon Schubert #include "observer.h"
30*5796c8dcSSimon Schubert 
31*5796c8dcSSimon Schubert #include "gdb_string.h"
32*5796c8dcSSimon Schubert #include <signal.h>
33*5796c8dcSSimon Schubert #include <fcntl.h>
34*5796c8dcSSimon Schubert #include "gdb_select.h"
35*5796c8dcSSimon Schubert 
36*5796c8dcSSimon Schubert #include "inflow.h"
37*5796c8dcSSimon Schubert 
38*5796c8dcSSimon Schubert #ifdef HAVE_SYS_IOCTL_H
39*5796c8dcSSimon Schubert #include <sys/ioctl.h>
40*5796c8dcSSimon Schubert #endif
41*5796c8dcSSimon Schubert 
42*5796c8dcSSimon Schubert #ifndef O_NOCTTY
43*5796c8dcSSimon Schubert #define O_NOCTTY 0
44*5796c8dcSSimon Schubert #endif
45*5796c8dcSSimon Schubert 
46*5796c8dcSSimon Schubert #if defined (SIGIO) && defined (FASYNC) && defined (FD_SET) && defined (F_SETOWN)
47*5796c8dcSSimon Schubert static void handle_sigio (int);
48*5796c8dcSSimon Schubert #endif
49*5796c8dcSSimon Schubert 
50*5796c8dcSSimon Schubert extern void _initialize_inflow (void);
51*5796c8dcSSimon Schubert 
52*5796c8dcSSimon Schubert static void pass_signal (int);
53*5796c8dcSSimon Schubert 
54*5796c8dcSSimon Schubert static void terminal_ours_1 (int);
55*5796c8dcSSimon Schubert 
56*5796c8dcSSimon Schubert /* Record terminal status separately for debugger and inferior.  */
57*5796c8dcSSimon Schubert 
58*5796c8dcSSimon Schubert static struct serial *stdin_serial;
59*5796c8dcSSimon Schubert 
60*5796c8dcSSimon Schubert /* Terminal related info we need to keep track of.  Each inferior
61*5796c8dcSSimon Schubert    holds an instance of this structure --- we save it whenever the
62*5796c8dcSSimon Schubert    corresponding inferior stops, and restore it to the foreground
63*5796c8dcSSimon Schubert    inferior when it resumes.  */
64*5796c8dcSSimon Schubert struct terminal_info
65*5796c8dcSSimon Schubert {
66*5796c8dcSSimon Schubert   /* The name of the tty (from the `tty' command) that we gave to the
67*5796c8dcSSimon Schubert      inferior when it was started.  */
68*5796c8dcSSimon Schubert   char *run_terminal;
69*5796c8dcSSimon Schubert 
70*5796c8dcSSimon Schubert   /* TTY state.  We save it whenever the inferior stops, and restore
71*5796c8dcSSimon Schubert      it when it resumes.  */
72*5796c8dcSSimon Schubert   serial_ttystate ttystate;
73*5796c8dcSSimon Schubert 
74*5796c8dcSSimon Schubert #ifdef PROCESS_GROUP_TYPE
75*5796c8dcSSimon Schubert   /* Process group.  Saved and restored just like ttystate.  */
76*5796c8dcSSimon Schubert   PROCESS_GROUP_TYPE process_group;
77*5796c8dcSSimon Schubert #endif
78*5796c8dcSSimon Schubert 
79*5796c8dcSSimon Schubert   /* fcntl flags.  Saved and restored just like ttystate.  */
80*5796c8dcSSimon Schubert   int tflags;
81*5796c8dcSSimon Schubert };
82*5796c8dcSSimon Schubert 
83*5796c8dcSSimon Schubert /* Our own tty state, which we restore every time we need to deal with
84*5796c8dcSSimon Schubert    the terminal.  This is only set once, when GDB first starts.  The
85*5796c8dcSSimon Schubert    settings of flags which readline saves and restores and
86*5796c8dcSSimon Schubert    unimportant.  */
87*5796c8dcSSimon Schubert static struct terminal_info our_terminal_info;
88*5796c8dcSSimon Schubert 
89*5796c8dcSSimon Schubert #ifdef PROCESS_GROUP_TYPE
90*5796c8dcSSimon Schubert 
91*5796c8dcSSimon Schubert /* Return the process group of the current inferior.  */
92*5796c8dcSSimon Schubert 
93*5796c8dcSSimon Schubert PROCESS_GROUP_TYPE
94*5796c8dcSSimon Schubert inferior_process_group (void)
95*5796c8dcSSimon Schubert {
96*5796c8dcSSimon Schubert   return current_inferior ()->terminal_info->process_group;
97*5796c8dcSSimon Schubert }
98*5796c8dcSSimon Schubert #endif
99*5796c8dcSSimon Schubert 
100*5796c8dcSSimon Schubert /* While the inferior is running, we want SIGINT and SIGQUIT to go to the
101*5796c8dcSSimon Schubert    inferior only.  If we have job control, that takes care of it.  If not,
102*5796c8dcSSimon Schubert    we save our handlers in these two variables and set SIGINT and SIGQUIT
103*5796c8dcSSimon Schubert    to SIG_IGN.  */
104*5796c8dcSSimon Schubert 
105*5796c8dcSSimon Schubert static void (*sigint_ours) ();
106*5796c8dcSSimon Schubert static void (*sigquit_ours) ();
107*5796c8dcSSimon Schubert 
108*5796c8dcSSimon Schubert /* The name of the tty (from the `tty' command) that we're giving to
109*5796c8dcSSimon Schubert    the inferior when starting it up.  This is only (and should only
110*5796c8dcSSimon Schubert    be) used as a transient global by new_tty_prefork,
111*5796c8dcSSimon Schubert    create_tty_session, new_tty and new_tty_postfork, all called from
112*5796c8dcSSimon Schubert    fork_inferior, while forking a new child.  */
113*5796c8dcSSimon Schubert static const char *inferior_thisrun_terminal;
114*5796c8dcSSimon Schubert 
115*5796c8dcSSimon Schubert /* Nonzero if our terminal settings are in effect.  Zero if the
116*5796c8dcSSimon Schubert    inferior's settings are in effect.  Ignored if !gdb_has_a_terminal
117*5796c8dcSSimon Schubert    ().  */
118*5796c8dcSSimon Schubert 
119*5796c8dcSSimon Schubert int terminal_is_ours;
120*5796c8dcSSimon Schubert 
121*5796c8dcSSimon Schubert #ifdef PROCESS_GROUP_TYPE
122*5796c8dcSSimon Schubert static PROCESS_GROUP_TYPE
123*5796c8dcSSimon Schubert gdb_getpgrp (void)
124*5796c8dcSSimon Schubert {
125*5796c8dcSSimon Schubert   int process_group = -1;
126*5796c8dcSSimon Schubert #ifdef HAVE_TERMIOS
127*5796c8dcSSimon Schubert   process_group = tcgetpgrp (0);
128*5796c8dcSSimon Schubert #endif
129*5796c8dcSSimon Schubert #ifdef HAVE_TERMIO
130*5796c8dcSSimon Schubert   process_group = getpgrp ();
131*5796c8dcSSimon Schubert #endif
132*5796c8dcSSimon Schubert #ifdef HAVE_SGTTY
133*5796c8dcSSimon Schubert   ioctl (0, TIOCGPGRP, &process_group);
134*5796c8dcSSimon Schubert #endif
135*5796c8dcSSimon Schubert   return process_group;
136*5796c8dcSSimon Schubert }
137*5796c8dcSSimon Schubert #endif
138*5796c8dcSSimon Schubert 
139*5796c8dcSSimon Schubert enum
140*5796c8dcSSimon Schubert   {
141*5796c8dcSSimon Schubert     yes, no, have_not_checked
142*5796c8dcSSimon Schubert   }
143*5796c8dcSSimon Schubert gdb_has_a_terminal_flag = have_not_checked;
144*5796c8dcSSimon Schubert 
145*5796c8dcSSimon Schubert /* Does GDB have a terminal (on stdin)?  */
146*5796c8dcSSimon Schubert int
147*5796c8dcSSimon Schubert gdb_has_a_terminal (void)
148*5796c8dcSSimon Schubert {
149*5796c8dcSSimon Schubert   switch (gdb_has_a_terminal_flag)
150*5796c8dcSSimon Schubert     {
151*5796c8dcSSimon Schubert     case yes:
152*5796c8dcSSimon Schubert       return 1;
153*5796c8dcSSimon Schubert     case no:
154*5796c8dcSSimon Schubert       return 0;
155*5796c8dcSSimon Schubert     case have_not_checked:
156*5796c8dcSSimon Schubert       /* Get all the current tty settings (including whether we have a
157*5796c8dcSSimon Schubert          tty at all!).  Can't do this in _initialize_inflow because
158*5796c8dcSSimon Schubert          serial_fdopen() won't work until the serial_ops_list is
159*5796c8dcSSimon Schubert          initialized.  */
160*5796c8dcSSimon Schubert 
161*5796c8dcSSimon Schubert #ifdef F_GETFL
162*5796c8dcSSimon Schubert       our_terminal_info.tflags = fcntl (0, F_GETFL, 0);
163*5796c8dcSSimon Schubert #endif
164*5796c8dcSSimon Schubert 
165*5796c8dcSSimon Schubert       gdb_has_a_terminal_flag = no;
166*5796c8dcSSimon Schubert       if (stdin_serial != NULL)
167*5796c8dcSSimon Schubert 	{
168*5796c8dcSSimon Schubert 	  our_terminal_info.ttystate = serial_get_tty_state (stdin_serial);
169*5796c8dcSSimon Schubert 
170*5796c8dcSSimon Schubert 	  if (our_terminal_info.ttystate != NULL)
171*5796c8dcSSimon Schubert 	    {
172*5796c8dcSSimon Schubert 	      gdb_has_a_terminal_flag = yes;
173*5796c8dcSSimon Schubert #ifdef PROCESS_GROUP_TYPE
174*5796c8dcSSimon Schubert 	      our_terminal_info.process_group = gdb_getpgrp ();
175*5796c8dcSSimon Schubert #endif
176*5796c8dcSSimon Schubert 	    }
177*5796c8dcSSimon Schubert 	}
178*5796c8dcSSimon Schubert 
179*5796c8dcSSimon Schubert       return gdb_has_a_terminal_flag == yes;
180*5796c8dcSSimon Schubert     default:
181*5796c8dcSSimon Schubert       /* "Can't happen".  */
182*5796c8dcSSimon Schubert       return 0;
183*5796c8dcSSimon Schubert     }
184*5796c8dcSSimon Schubert }
185*5796c8dcSSimon Schubert 
186*5796c8dcSSimon Schubert /* Macro for printing errors from ioctl operations */
187*5796c8dcSSimon Schubert 
188*5796c8dcSSimon Schubert #define	OOPSY(what)	\
189*5796c8dcSSimon Schubert   if (result == -1)	\
190*5796c8dcSSimon Schubert     fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \
191*5796c8dcSSimon Schubert 	    what, safe_strerror (errno))
192*5796c8dcSSimon Schubert 
193*5796c8dcSSimon Schubert static void terminal_ours_1 (int);
194*5796c8dcSSimon Schubert 
195*5796c8dcSSimon Schubert /* Initialize the terminal settings we record for the inferior,
196*5796c8dcSSimon Schubert    before we actually run the inferior.  */
197*5796c8dcSSimon Schubert 
198*5796c8dcSSimon Schubert void
199*5796c8dcSSimon Schubert terminal_init_inferior_with_pgrp (int pgrp)
200*5796c8dcSSimon Schubert {
201*5796c8dcSSimon Schubert   if (gdb_has_a_terminal ())
202*5796c8dcSSimon Schubert     {
203*5796c8dcSSimon Schubert       struct inferior *inf = current_inferior ();
204*5796c8dcSSimon Schubert 
205*5796c8dcSSimon Schubert       /* We could just as well copy our_ttystate (if we felt like
206*5796c8dcSSimon Schubert          adding a new function serial_copy_tty_state()).  */
207*5796c8dcSSimon Schubert       xfree (inf->terminal_info->ttystate);
208*5796c8dcSSimon Schubert       inf->terminal_info->ttystate
209*5796c8dcSSimon Schubert 	= serial_get_tty_state (stdin_serial);
210*5796c8dcSSimon Schubert 
211*5796c8dcSSimon Schubert #ifdef PROCESS_GROUP_TYPE
212*5796c8dcSSimon Schubert       inf->terminal_info->process_group = pgrp;
213*5796c8dcSSimon Schubert #endif
214*5796c8dcSSimon Schubert 
215*5796c8dcSSimon Schubert       /* Make sure that next time we call terminal_inferior (which will be
216*5796c8dcSSimon Schubert          before the program runs, as it needs to be), we install the new
217*5796c8dcSSimon Schubert          process group.  */
218*5796c8dcSSimon Schubert       terminal_is_ours = 1;
219*5796c8dcSSimon Schubert     }
220*5796c8dcSSimon Schubert }
221*5796c8dcSSimon Schubert 
222*5796c8dcSSimon Schubert /* Save the terminal settings again.  This is necessary for the TUI
223*5796c8dcSSimon Schubert    when it switches to TUI or non-TUI mode;  curses changes the terminal
224*5796c8dcSSimon Schubert    and gdb must be able to restore it correctly.  */
225*5796c8dcSSimon Schubert 
226*5796c8dcSSimon Schubert void
227*5796c8dcSSimon Schubert terminal_save_ours (void)
228*5796c8dcSSimon Schubert {
229*5796c8dcSSimon Schubert   if (gdb_has_a_terminal ())
230*5796c8dcSSimon Schubert     {
231*5796c8dcSSimon Schubert       /* We could just as well copy our_ttystate (if we felt like adding
232*5796c8dcSSimon Schubert          a new function serial_copy_tty_state).  */
233*5796c8dcSSimon Schubert       xfree (our_terminal_info.ttystate);
234*5796c8dcSSimon Schubert       our_terminal_info.ttystate = serial_get_tty_state (stdin_serial);
235*5796c8dcSSimon Schubert     }
236*5796c8dcSSimon Schubert }
237*5796c8dcSSimon Schubert 
238*5796c8dcSSimon Schubert void
239*5796c8dcSSimon Schubert terminal_init_inferior (void)
240*5796c8dcSSimon Schubert {
241*5796c8dcSSimon Schubert #ifdef PROCESS_GROUP_TYPE
242*5796c8dcSSimon Schubert   /* This is for Lynx, and should be cleaned up by having Lynx be a separate
243*5796c8dcSSimon Schubert      debugging target with a version of target_terminal_init_inferior which
244*5796c8dcSSimon Schubert      passes in the process group to a generic routine which does all the work
245*5796c8dcSSimon Schubert      (and the non-threaded child_terminal_init_inferior can just pass in
246*5796c8dcSSimon Schubert      inferior_ptid to the same routine).  */
247*5796c8dcSSimon Schubert   /* We assume INFERIOR_PID is also the child's process group.  */
248*5796c8dcSSimon Schubert   terminal_init_inferior_with_pgrp (PIDGET (inferior_ptid));
249*5796c8dcSSimon Schubert #endif /* PROCESS_GROUP_TYPE */
250*5796c8dcSSimon Schubert }
251*5796c8dcSSimon Schubert 
252*5796c8dcSSimon Schubert /* Put the inferior's terminal settings into effect.
253*5796c8dcSSimon Schubert    This is preparation for starting or resuming the inferior.  */
254*5796c8dcSSimon Schubert 
255*5796c8dcSSimon Schubert void
256*5796c8dcSSimon Schubert terminal_inferior (void)
257*5796c8dcSSimon Schubert {
258*5796c8dcSSimon Schubert   struct inferior *inf;
259*5796c8dcSSimon Schubert 
260*5796c8dcSSimon Schubert   if (!terminal_is_ours)
261*5796c8dcSSimon Schubert     return;
262*5796c8dcSSimon Schubert 
263*5796c8dcSSimon Schubert   inf = current_inferior ();
264*5796c8dcSSimon Schubert 
265*5796c8dcSSimon Schubert   if (gdb_has_a_terminal ()
266*5796c8dcSSimon Schubert       && inf->terminal_info->ttystate != NULL
267*5796c8dcSSimon Schubert       && inf->terminal_info->run_terminal == NULL)
268*5796c8dcSSimon Schubert     {
269*5796c8dcSSimon Schubert       int result;
270*5796c8dcSSimon Schubert 
271*5796c8dcSSimon Schubert #ifdef F_GETFL
272*5796c8dcSSimon Schubert       /* Is there a reason this is being done twice?  It happens both
273*5796c8dcSSimon Schubert          places we use F_SETFL, so I'm inclined to think perhaps there
274*5796c8dcSSimon Schubert          is some reason, however perverse.  Perhaps not though...  */
275*5796c8dcSSimon Schubert       result = fcntl (0, F_SETFL, inf->terminal_info->tflags);
276*5796c8dcSSimon Schubert       result = fcntl (0, F_SETFL, inf->terminal_info->tflags);
277*5796c8dcSSimon Schubert       OOPSY ("fcntl F_SETFL");
278*5796c8dcSSimon Schubert #endif
279*5796c8dcSSimon Schubert 
280*5796c8dcSSimon Schubert       /* Because we were careful to not change in or out of raw mode in
281*5796c8dcSSimon Schubert          terminal_ours, we will not change in our out of raw mode with
282*5796c8dcSSimon Schubert          this call, so we don't flush any input.  */
283*5796c8dcSSimon Schubert       result = serial_set_tty_state (stdin_serial,
284*5796c8dcSSimon Schubert 				     inf->terminal_info->ttystate);
285*5796c8dcSSimon Schubert       OOPSY ("setting tty state");
286*5796c8dcSSimon Schubert 
287*5796c8dcSSimon Schubert       if (!job_control)
288*5796c8dcSSimon Schubert 	{
289*5796c8dcSSimon Schubert 	  sigint_ours = (void (*)()) signal (SIGINT, SIG_IGN);
290*5796c8dcSSimon Schubert #ifdef SIGQUIT
291*5796c8dcSSimon Schubert 	  sigquit_ours = (void (*)()) signal (SIGQUIT, SIG_IGN);
292*5796c8dcSSimon Schubert #endif
293*5796c8dcSSimon Schubert 	}
294*5796c8dcSSimon Schubert 
295*5796c8dcSSimon Schubert       /* If attach_flag is set, we don't know whether we are sharing a
296*5796c8dcSSimon Schubert          terminal with the inferior or not.  (attaching a process
297*5796c8dcSSimon Schubert          without a terminal is one case where we do not; attaching a
298*5796c8dcSSimon Schubert          process which we ran from the same shell as GDB via `&' is
299*5796c8dcSSimon Schubert          one case where we do, I think (but perhaps this is not
300*5796c8dcSSimon Schubert          `sharing' in the sense that we need to save and restore tty
301*5796c8dcSSimon Schubert          state)).  I don't know if there is any way to tell whether we
302*5796c8dcSSimon Schubert          are sharing a terminal.  So what we do is to go through all
303*5796c8dcSSimon Schubert          the saving and restoring of the tty state, but ignore errors
304*5796c8dcSSimon Schubert          setting the process group, which will happen if we are not
305*5796c8dcSSimon Schubert          sharing a terminal).  */
306*5796c8dcSSimon Schubert 
307*5796c8dcSSimon Schubert       if (job_control)
308*5796c8dcSSimon Schubert 	{
309*5796c8dcSSimon Schubert #ifdef HAVE_TERMIOS
310*5796c8dcSSimon Schubert 	  result = tcsetpgrp (0, inf->terminal_info->process_group);
311*5796c8dcSSimon Schubert 	  if (!inf->attach_flag)
312*5796c8dcSSimon Schubert 	    OOPSY ("tcsetpgrp");
313*5796c8dcSSimon Schubert #endif
314*5796c8dcSSimon Schubert 
315*5796c8dcSSimon Schubert #ifdef HAVE_SGTTY
316*5796c8dcSSimon Schubert 	  result = ioctl (0, TIOCSPGRP, &inf->terminal_info->process_group);
317*5796c8dcSSimon Schubert 	  if (!inf->attach_flag)
318*5796c8dcSSimon Schubert 	    OOPSY ("TIOCSPGRP");
319*5796c8dcSSimon Schubert #endif
320*5796c8dcSSimon Schubert 	}
321*5796c8dcSSimon Schubert 
322*5796c8dcSSimon Schubert     }
323*5796c8dcSSimon Schubert   terminal_is_ours = 0;
324*5796c8dcSSimon Schubert }
325*5796c8dcSSimon Schubert 
326*5796c8dcSSimon Schubert /* Put some of our terminal settings into effect,
327*5796c8dcSSimon Schubert    enough to get proper results from our output,
328*5796c8dcSSimon Schubert    but do not change into or out of RAW mode
329*5796c8dcSSimon Schubert    so that no input is discarded.
330*5796c8dcSSimon Schubert 
331*5796c8dcSSimon Schubert    After doing this, either terminal_ours or terminal_inferior
332*5796c8dcSSimon Schubert    should be called to get back to a normal state of affairs.  */
333*5796c8dcSSimon Schubert 
334*5796c8dcSSimon Schubert void
335*5796c8dcSSimon Schubert terminal_ours_for_output (void)
336*5796c8dcSSimon Schubert {
337*5796c8dcSSimon Schubert   terminal_ours_1 (1);
338*5796c8dcSSimon Schubert }
339*5796c8dcSSimon Schubert 
340*5796c8dcSSimon Schubert /* Put our terminal settings into effect.
341*5796c8dcSSimon Schubert    First record the inferior's terminal settings
342*5796c8dcSSimon Schubert    so they can be restored properly later.  */
343*5796c8dcSSimon Schubert 
344*5796c8dcSSimon Schubert void
345*5796c8dcSSimon Schubert terminal_ours (void)
346*5796c8dcSSimon Schubert {
347*5796c8dcSSimon Schubert   terminal_ours_1 (0);
348*5796c8dcSSimon Schubert }
349*5796c8dcSSimon Schubert 
350*5796c8dcSSimon Schubert /* output_only is not used, and should not be used unless we introduce
351*5796c8dcSSimon Schubert    separate terminal_is_ours and terminal_is_ours_for_output
352*5796c8dcSSimon Schubert    flags.  */
353*5796c8dcSSimon Schubert 
354*5796c8dcSSimon Schubert static void
355*5796c8dcSSimon Schubert terminal_ours_1 (int output_only)
356*5796c8dcSSimon Schubert {
357*5796c8dcSSimon Schubert   struct inferior *inf;
358*5796c8dcSSimon Schubert 
359*5796c8dcSSimon Schubert   if (terminal_is_ours)
360*5796c8dcSSimon Schubert     return;
361*5796c8dcSSimon Schubert 
362*5796c8dcSSimon Schubert   terminal_is_ours = 1;
363*5796c8dcSSimon Schubert 
364*5796c8dcSSimon Schubert   /* Checking inferior->run_terminal is necessary so that
365*5796c8dcSSimon Schubert      if GDB is running in the background, it won't block trying
366*5796c8dcSSimon Schubert      to do the ioctl()'s below.  Checking gdb_has_a_terminal
367*5796c8dcSSimon Schubert      avoids attempting all the ioctl's when running in batch.  */
368*5796c8dcSSimon Schubert 
369*5796c8dcSSimon Schubert   inf = current_inferior ();
370*5796c8dcSSimon Schubert 
371*5796c8dcSSimon Schubert   if (inf->terminal_info->run_terminal != NULL || gdb_has_a_terminal () == 0)
372*5796c8dcSSimon Schubert     return;
373*5796c8dcSSimon Schubert 
374*5796c8dcSSimon Schubert     {
375*5796c8dcSSimon Schubert #ifdef SIGTTOU
376*5796c8dcSSimon Schubert       /* Ignore this signal since it will happen when we try to set the
377*5796c8dcSSimon Schubert          pgrp.  */
378*5796c8dcSSimon Schubert       void (*osigttou) () = NULL;
379*5796c8dcSSimon Schubert #endif
380*5796c8dcSSimon Schubert       int result;
381*5796c8dcSSimon Schubert 
382*5796c8dcSSimon Schubert #ifdef SIGTTOU
383*5796c8dcSSimon Schubert       if (job_control)
384*5796c8dcSSimon Schubert 	osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN);
385*5796c8dcSSimon Schubert #endif
386*5796c8dcSSimon Schubert 
387*5796c8dcSSimon Schubert       xfree (inf->terminal_info->ttystate);
388*5796c8dcSSimon Schubert       inf->terminal_info->ttystate = serial_get_tty_state (stdin_serial);
389*5796c8dcSSimon Schubert 
390*5796c8dcSSimon Schubert #ifdef PROCESS_GROUP_TYPE
391*5796c8dcSSimon Schubert       if (!inf->attach_flag)
392*5796c8dcSSimon Schubert 	/* If setpgrp failed in terminal_inferior, this would give us
393*5796c8dcSSimon Schubert 	   our process group instead of the inferior's.  See
394*5796c8dcSSimon Schubert 	   terminal_inferior for details.  */
395*5796c8dcSSimon Schubert 	inf->terminal_info->process_group = gdb_getpgrp ();
396*5796c8dcSSimon Schubert #endif
397*5796c8dcSSimon Schubert 
398*5796c8dcSSimon Schubert       /* Here we used to set ICANON in our ttystate, but I believe this
399*5796c8dcSSimon Schubert          was an artifact from before when we used readline.  Readline sets
400*5796c8dcSSimon Schubert          the tty state when it needs to.
401*5796c8dcSSimon Schubert          FIXME-maybe: However, query() expects non-raw mode and doesn't
402*5796c8dcSSimon Schubert          use readline.  Maybe query should use readline (on the other hand,
403*5796c8dcSSimon Schubert          this only matters for HAVE_SGTTY, not termio or termios, I think).  */
404*5796c8dcSSimon Schubert 
405*5796c8dcSSimon Schubert       /* Set tty state to our_ttystate.  We don't change in our out of raw
406*5796c8dcSSimon Schubert          mode, to avoid flushing input.  We need to do the same thing
407*5796c8dcSSimon Schubert          regardless of output_only, because we don't have separate
408*5796c8dcSSimon Schubert          terminal_is_ours and terminal_is_ours_for_output flags.  It's OK,
409*5796c8dcSSimon Schubert          though, since readline will deal with raw mode when/if it needs to.
410*5796c8dcSSimon Schubert        */
411*5796c8dcSSimon Schubert 
412*5796c8dcSSimon Schubert       serial_noflush_set_tty_state (stdin_serial, our_terminal_info.ttystate,
413*5796c8dcSSimon Schubert 				    inf->terminal_info->ttystate);
414*5796c8dcSSimon Schubert 
415*5796c8dcSSimon Schubert       if (job_control)
416*5796c8dcSSimon Schubert 	{
417*5796c8dcSSimon Schubert #ifdef HAVE_TERMIOS
418*5796c8dcSSimon Schubert 	  result = tcsetpgrp (0, our_terminal_info.process_group);
419*5796c8dcSSimon Schubert #if 0
420*5796c8dcSSimon Schubert 	  /* This fails on Ultrix with EINVAL if you run the testsuite
421*5796c8dcSSimon Schubert 	     in the background with nohup, and then log out.  GDB never
422*5796c8dcSSimon Schubert 	     used to check for an error here, so perhaps there are other
423*5796c8dcSSimon Schubert 	     such situations as well.  */
424*5796c8dcSSimon Schubert 	  if (result == -1)
425*5796c8dcSSimon Schubert 	    fprintf_unfiltered (gdb_stderr, "[tcsetpgrp failed in terminal_ours: %s]\n",
426*5796c8dcSSimon Schubert 				safe_strerror (errno));
427*5796c8dcSSimon Schubert #endif
428*5796c8dcSSimon Schubert #endif /* termios */
429*5796c8dcSSimon Schubert 
430*5796c8dcSSimon Schubert #ifdef HAVE_SGTTY
431*5796c8dcSSimon Schubert 	  result = ioctl (0, TIOCSPGRP, &our_terminal_info.process_group);
432*5796c8dcSSimon Schubert #endif
433*5796c8dcSSimon Schubert 	}
434*5796c8dcSSimon Schubert 
435*5796c8dcSSimon Schubert #ifdef SIGTTOU
436*5796c8dcSSimon Schubert       if (job_control)
437*5796c8dcSSimon Schubert 	signal (SIGTTOU, osigttou);
438*5796c8dcSSimon Schubert #endif
439*5796c8dcSSimon Schubert 
440*5796c8dcSSimon Schubert       if (!job_control)
441*5796c8dcSSimon Schubert 	{
442*5796c8dcSSimon Schubert 	  signal (SIGINT, sigint_ours);
443*5796c8dcSSimon Schubert #ifdef SIGQUIT
444*5796c8dcSSimon Schubert 	  signal (SIGQUIT, sigquit_ours);
445*5796c8dcSSimon Schubert #endif
446*5796c8dcSSimon Schubert 	}
447*5796c8dcSSimon Schubert 
448*5796c8dcSSimon Schubert #ifdef F_GETFL
449*5796c8dcSSimon Schubert       inf->terminal_info->tflags = fcntl (0, F_GETFL, 0);
450*5796c8dcSSimon Schubert 
451*5796c8dcSSimon Schubert       /* Is there a reason this is being done twice?  It happens both
452*5796c8dcSSimon Schubert          places we use F_SETFL, so I'm inclined to think perhaps there
453*5796c8dcSSimon Schubert          is some reason, however perverse.  Perhaps not though...  */
454*5796c8dcSSimon Schubert       result = fcntl (0, F_SETFL, our_terminal_info.tflags);
455*5796c8dcSSimon Schubert       result = fcntl (0, F_SETFL, our_terminal_info.tflags);
456*5796c8dcSSimon Schubert #endif
457*5796c8dcSSimon Schubert     }
458*5796c8dcSSimon Schubert }
459*5796c8dcSSimon Schubert 
460*5796c8dcSSimon Schubert /* This is a "new_inferior" observer.  It's business is to allocate
461*5796c8dcSSimon Schubert    the TERMINAL_INFO member of the inferior structure.  This field is
462*5796c8dcSSimon Schubert    private to inflow.c, and its type is opaque to the rest of GDB.
463*5796c8dcSSimon Schubert    PID is the target pid of the inferior that has just been added to
464*5796c8dcSSimon Schubert    the inferior list.  */
465*5796c8dcSSimon Schubert 
466*5796c8dcSSimon Schubert static void
467*5796c8dcSSimon Schubert inflow_new_inferior (int pid)
468*5796c8dcSSimon Schubert {
469*5796c8dcSSimon Schubert   struct inferior *inf = find_inferior_pid (pid);
470*5796c8dcSSimon Schubert 
471*5796c8dcSSimon Schubert   inf->terminal_info = XZALLOC (struct terminal_info);
472*5796c8dcSSimon Schubert }
473*5796c8dcSSimon Schubert 
474*5796c8dcSSimon Schubert /* This is a "inferior_exit" observer.  Releases the TERMINAL_INFO member
475*5796c8dcSSimon Schubert    of the inferior structure.  This field is private to inflow.c, and
476*5796c8dcSSimon Schubert    its type is opaque to the rest of GDB.  PID is the target pid of
477*5796c8dcSSimon Schubert    the inferior that is about to be removed from the inferior
478*5796c8dcSSimon Schubert    list.  */
479*5796c8dcSSimon Schubert 
480*5796c8dcSSimon Schubert static void
481*5796c8dcSSimon Schubert inflow_inferior_exit (int pid)
482*5796c8dcSSimon Schubert {
483*5796c8dcSSimon Schubert   struct inferior *inf = find_inferior_pid (pid);
484*5796c8dcSSimon Schubert 
485*5796c8dcSSimon Schubert   xfree (inf->terminal_info->run_terminal);
486*5796c8dcSSimon Schubert   xfree (inf->terminal_info);
487*5796c8dcSSimon Schubert   inf->terminal_info = NULL;
488*5796c8dcSSimon Schubert }
489*5796c8dcSSimon Schubert 
490*5796c8dcSSimon Schubert void
491*5796c8dcSSimon Schubert copy_terminal_info (struct inferior *to, struct inferior *from)
492*5796c8dcSSimon Schubert {
493*5796c8dcSSimon Schubert   *to->terminal_info = *from->terminal_info;
494*5796c8dcSSimon Schubert   if (from->terminal_info->run_terminal)
495*5796c8dcSSimon Schubert     to->terminal_info->run_terminal
496*5796c8dcSSimon Schubert       = xstrdup (from->terminal_info->run_terminal);
497*5796c8dcSSimon Schubert }
498*5796c8dcSSimon Schubert 
499*5796c8dcSSimon Schubert void
500*5796c8dcSSimon Schubert term_info (char *arg, int from_tty)
501*5796c8dcSSimon Schubert {
502*5796c8dcSSimon Schubert   target_terminal_info (arg, from_tty);
503*5796c8dcSSimon Schubert }
504*5796c8dcSSimon Schubert 
505*5796c8dcSSimon Schubert void
506*5796c8dcSSimon Schubert child_terminal_info (char *args, int from_tty)
507*5796c8dcSSimon Schubert {
508*5796c8dcSSimon Schubert   struct inferior *inf;
509*5796c8dcSSimon Schubert 
510*5796c8dcSSimon Schubert   if (!gdb_has_a_terminal ())
511*5796c8dcSSimon Schubert     {
512*5796c8dcSSimon Schubert       printf_filtered (_("This GDB does not control a terminal.\n"));
513*5796c8dcSSimon Schubert       return;
514*5796c8dcSSimon Schubert     }
515*5796c8dcSSimon Schubert 
516*5796c8dcSSimon Schubert   if (ptid_equal (inferior_ptid, null_ptid))
517*5796c8dcSSimon Schubert     return;
518*5796c8dcSSimon Schubert 
519*5796c8dcSSimon Schubert   inf = current_inferior ();
520*5796c8dcSSimon Schubert 
521*5796c8dcSSimon Schubert   printf_filtered (_("Inferior's terminal status (currently saved by GDB):\n"));
522*5796c8dcSSimon Schubert 
523*5796c8dcSSimon Schubert   /* First the fcntl flags.  */
524*5796c8dcSSimon Schubert   {
525*5796c8dcSSimon Schubert     int flags;
526*5796c8dcSSimon Schubert 
527*5796c8dcSSimon Schubert     flags = inf->terminal_info->tflags;
528*5796c8dcSSimon Schubert 
529*5796c8dcSSimon Schubert     printf_filtered ("File descriptor flags = ");
530*5796c8dcSSimon Schubert 
531*5796c8dcSSimon Schubert #ifndef O_ACCMODE
532*5796c8dcSSimon Schubert #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
533*5796c8dcSSimon Schubert #endif
534*5796c8dcSSimon Schubert     /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
535*5796c8dcSSimon Schubert     switch (flags & (O_ACCMODE))
536*5796c8dcSSimon Schubert       {
537*5796c8dcSSimon Schubert       case O_RDONLY:
538*5796c8dcSSimon Schubert 	printf_filtered ("O_RDONLY");
539*5796c8dcSSimon Schubert 	break;
540*5796c8dcSSimon Schubert       case O_WRONLY:
541*5796c8dcSSimon Schubert 	printf_filtered ("O_WRONLY");
542*5796c8dcSSimon Schubert 	break;
543*5796c8dcSSimon Schubert       case O_RDWR:
544*5796c8dcSSimon Schubert 	printf_filtered ("O_RDWR");
545*5796c8dcSSimon Schubert 	break;
546*5796c8dcSSimon Schubert       }
547*5796c8dcSSimon Schubert     flags &= ~(O_ACCMODE);
548*5796c8dcSSimon Schubert 
549*5796c8dcSSimon Schubert #ifdef O_NONBLOCK
550*5796c8dcSSimon Schubert     if (flags & O_NONBLOCK)
551*5796c8dcSSimon Schubert       printf_filtered (" | O_NONBLOCK");
552*5796c8dcSSimon Schubert     flags &= ~O_NONBLOCK;
553*5796c8dcSSimon Schubert #endif
554*5796c8dcSSimon Schubert 
555*5796c8dcSSimon Schubert #if defined (O_NDELAY)
556*5796c8dcSSimon Schubert     /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will
557*5796c8dcSSimon Schubert        print it as O_NONBLOCK, which is good cause that is what POSIX
558*5796c8dcSSimon Schubert        has, and the flag will already be cleared by the time we get here.  */
559*5796c8dcSSimon Schubert     if (flags & O_NDELAY)
560*5796c8dcSSimon Schubert       printf_filtered (" | O_NDELAY");
561*5796c8dcSSimon Schubert     flags &= ~O_NDELAY;
562*5796c8dcSSimon Schubert #endif
563*5796c8dcSSimon Schubert 
564*5796c8dcSSimon Schubert     if (flags & O_APPEND)
565*5796c8dcSSimon Schubert       printf_filtered (" | O_APPEND");
566*5796c8dcSSimon Schubert     flags &= ~O_APPEND;
567*5796c8dcSSimon Schubert 
568*5796c8dcSSimon Schubert #if defined (O_BINARY)
569*5796c8dcSSimon Schubert     if (flags & O_BINARY)
570*5796c8dcSSimon Schubert       printf_filtered (" | O_BINARY");
571*5796c8dcSSimon Schubert     flags &= ~O_BINARY;
572*5796c8dcSSimon Schubert #endif
573*5796c8dcSSimon Schubert 
574*5796c8dcSSimon Schubert     if (flags)
575*5796c8dcSSimon Schubert       printf_filtered (" | 0x%x", flags);
576*5796c8dcSSimon Schubert     printf_filtered ("\n");
577*5796c8dcSSimon Schubert   }
578*5796c8dcSSimon Schubert 
579*5796c8dcSSimon Schubert #ifdef PROCESS_GROUP_TYPE
580*5796c8dcSSimon Schubert   printf_filtered ("Process group = %d\n",
581*5796c8dcSSimon Schubert 		   (int) inf->terminal_info->process_group);
582*5796c8dcSSimon Schubert #endif
583*5796c8dcSSimon Schubert 
584*5796c8dcSSimon Schubert   serial_print_tty_state (stdin_serial,
585*5796c8dcSSimon Schubert 			  inf->terminal_info->ttystate,
586*5796c8dcSSimon Schubert 			  gdb_stdout);
587*5796c8dcSSimon Schubert }
588*5796c8dcSSimon Schubert 
589*5796c8dcSSimon Schubert /* NEW_TTY_PREFORK is called before forking a new child process,
590*5796c8dcSSimon Schubert    so we can record the state of ttys in the child to be formed.
591*5796c8dcSSimon Schubert    TTYNAME is null if we are to share the terminal with gdb;
592*5796c8dcSSimon Schubert    or points to a string containing the name of the desired tty.
593*5796c8dcSSimon Schubert 
594*5796c8dcSSimon Schubert    NEW_TTY is called in new child processes under Unix, which will
595*5796c8dcSSimon Schubert    become debugger target processes.  This actually switches to
596*5796c8dcSSimon Schubert    the terminal specified in the NEW_TTY_PREFORK call.  */
597*5796c8dcSSimon Schubert 
598*5796c8dcSSimon Schubert void
599*5796c8dcSSimon Schubert new_tty_prefork (const char *ttyname)
600*5796c8dcSSimon Schubert {
601*5796c8dcSSimon Schubert   /* Save the name for later, for determining whether we and the child
602*5796c8dcSSimon Schubert      are sharing a tty.  */
603*5796c8dcSSimon Schubert   inferior_thisrun_terminal = ttyname;
604*5796c8dcSSimon Schubert }
605*5796c8dcSSimon Schubert 
606*5796c8dcSSimon Schubert 
607*5796c8dcSSimon Schubert /* If RESULT, assumed to be the return value from a system call, is
608*5796c8dcSSimon Schubert    negative, print the error message indicated by errno and exit.
609*5796c8dcSSimon Schubert    MSG should identify the operation that failed.  */
610*5796c8dcSSimon Schubert static void
611*5796c8dcSSimon Schubert check_syscall (const char *msg, int result)
612*5796c8dcSSimon Schubert {
613*5796c8dcSSimon Schubert   if (result < 0)
614*5796c8dcSSimon Schubert     {
615*5796c8dcSSimon Schubert       print_sys_errmsg (msg, errno);
616*5796c8dcSSimon Schubert       _exit (1);
617*5796c8dcSSimon Schubert     }
618*5796c8dcSSimon Schubert }
619*5796c8dcSSimon Schubert 
620*5796c8dcSSimon Schubert void
621*5796c8dcSSimon Schubert new_tty (void)
622*5796c8dcSSimon Schubert {
623*5796c8dcSSimon Schubert   int tty;
624*5796c8dcSSimon Schubert 
625*5796c8dcSSimon Schubert   if (inferior_thisrun_terminal == 0)
626*5796c8dcSSimon Schubert     return;
627*5796c8dcSSimon Schubert #if !defined(__GO32__) && !defined(_WIN32)
628*5796c8dcSSimon Schubert #ifdef TIOCNOTTY
629*5796c8dcSSimon Schubert   /* Disconnect the child process from our controlling terminal.  On some
630*5796c8dcSSimon Schubert      systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
631*5796c8dcSSimon Schubert      ignore SIGTTOU. */
632*5796c8dcSSimon Schubert   tty = open ("/dev/tty", O_RDWR);
633*5796c8dcSSimon Schubert   if (tty > 0)
634*5796c8dcSSimon Schubert     {
635*5796c8dcSSimon Schubert       void (*osigttou) ();
636*5796c8dcSSimon Schubert 
637*5796c8dcSSimon Schubert       osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN);
638*5796c8dcSSimon Schubert       ioctl (tty, TIOCNOTTY, 0);
639*5796c8dcSSimon Schubert       close (tty);
640*5796c8dcSSimon Schubert       signal (SIGTTOU, osigttou);
641*5796c8dcSSimon Schubert     }
642*5796c8dcSSimon Schubert #endif
643*5796c8dcSSimon Schubert 
644*5796c8dcSSimon Schubert   /* Now open the specified new terminal.  */
645*5796c8dcSSimon Schubert   tty = open (inferior_thisrun_terminal, O_RDWR | O_NOCTTY);
646*5796c8dcSSimon Schubert   check_syscall (inferior_thisrun_terminal, tty);
647*5796c8dcSSimon Schubert 
648*5796c8dcSSimon Schubert   /* Avoid use of dup2; doesn't exist on all systems.  */
649*5796c8dcSSimon Schubert   if (tty != 0)
650*5796c8dcSSimon Schubert     {
651*5796c8dcSSimon Schubert       close (0);
652*5796c8dcSSimon Schubert       check_syscall ("dup'ing tty into fd 0", dup (tty));
653*5796c8dcSSimon Schubert     }
654*5796c8dcSSimon Schubert   if (tty != 1)
655*5796c8dcSSimon Schubert     {
656*5796c8dcSSimon Schubert       close (1);
657*5796c8dcSSimon Schubert       check_syscall ("dup'ing tty into fd 1", dup (tty));
658*5796c8dcSSimon Schubert     }
659*5796c8dcSSimon Schubert   if (tty != 2)
660*5796c8dcSSimon Schubert     {
661*5796c8dcSSimon Schubert       close (2);
662*5796c8dcSSimon Schubert       check_syscall ("dup'ing tty into fd 2", dup (tty));
663*5796c8dcSSimon Schubert     }
664*5796c8dcSSimon Schubert 
665*5796c8dcSSimon Schubert #ifdef TIOCSCTTY
666*5796c8dcSSimon Schubert   /* Make tty our new controlling terminal.  */
667*5796c8dcSSimon Schubert   if (ioctl (tty, TIOCSCTTY, 0) == -1)
668*5796c8dcSSimon Schubert     /* Mention GDB in warning because it will appear in the inferior's
669*5796c8dcSSimon Schubert        terminal instead of GDB's.  */
670*5796c8dcSSimon Schubert     warning ("GDB: Failed to set controlling terminal: %s",
671*5796c8dcSSimon Schubert 	     safe_strerror (errno));
672*5796c8dcSSimon Schubert #endif
673*5796c8dcSSimon Schubert 
674*5796c8dcSSimon Schubert   if (tty > 2)
675*5796c8dcSSimon Schubert     close (tty);
676*5796c8dcSSimon Schubert #endif /* !go32 && !win32 */
677*5796c8dcSSimon Schubert }
678*5796c8dcSSimon Schubert 
679*5796c8dcSSimon Schubert /* NEW_TTY_POSTFORK is called after forking a new child process, and
680*5796c8dcSSimon Schubert    adding it to the inferior table, to store the TTYNAME being used by
681*5796c8dcSSimon Schubert    the child, or null if it sharing the terminal with gdb.  */
682*5796c8dcSSimon Schubert 
683*5796c8dcSSimon Schubert void
684*5796c8dcSSimon Schubert new_tty_postfork (void)
685*5796c8dcSSimon Schubert {
686*5796c8dcSSimon Schubert   /* Save the name for later, for determining whether we and the child
687*5796c8dcSSimon Schubert      are sharing a tty.  */
688*5796c8dcSSimon Schubert 
689*5796c8dcSSimon Schubert   if (inferior_thisrun_terminal)
690*5796c8dcSSimon Schubert     current_inferior ()->terminal_info->run_terminal
691*5796c8dcSSimon Schubert       = xstrdup (inferior_thisrun_terminal);
692*5796c8dcSSimon Schubert 
693*5796c8dcSSimon Schubert   inferior_thisrun_terminal = NULL;
694*5796c8dcSSimon Schubert }
695*5796c8dcSSimon Schubert 
696*5796c8dcSSimon Schubert 
697*5796c8dcSSimon Schubert /* Call set_sigint_trap when you need to pass a signal on to an attached
698*5796c8dcSSimon Schubert    process when handling SIGINT */
699*5796c8dcSSimon Schubert 
700*5796c8dcSSimon Schubert static void
701*5796c8dcSSimon Schubert pass_signal (int signo)
702*5796c8dcSSimon Schubert {
703*5796c8dcSSimon Schubert #ifndef _WIN32
704*5796c8dcSSimon Schubert   kill (PIDGET (inferior_ptid), SIGINT);
705*5796c8dcSSimon Schubert #endif
706*5796c8dcSSimon Schubert }
707*5796c8dcSSimon Schubert 
708*5796c8dcSSimon Schubert static void (*osig) ();
709*5796c8dcSSimon Schubert static int osig_set;
710*5796c8dcSSimon Schubert 
711*5796c8dcSSimon Schubert void
712*5796c8dcSSimon Schubert set_sigint_trap (void)
713*5796c8dcSSimon Schubert {
714*5796c8dcSSimon Schubert   struct inferior *inf = current_inferior ();
715*5796c8dcSSimon Schubert   if (inf->attach_flag || inf->terminal_info->run_terminal)
716*5796c8dcSSimon Schubert     {
717*5796c8dcSSimon Schubert       osig = (void (*)()) signal (SIGINT, pass_signal);
718*5796c8dcSSimon Schubert       osig_set = 1;
719*5796c8dcSSimon Schubert     }
720*5796c8dcSSimon Schubert   else
721*5796c8dcSSimon Schubert     osig_set = 0;
722*5796c8dcSSimon Schubert }
723*5796c8dcSSimon Schubert 
724*5796c8dcSSimon Schubert void
725*5796c8dcSSimon Schubert clear_sigint_trap (void)
726*5796c8dcSSimon Schubert {
727*5796c8dcSSimon Schubert   if (osig_set)
728*5796c8dcSSimon Schubert     {
729*5796c8dcSSimon Schubert       signal (SIGINT, osig);
730*5796c8dcSSimon Schubert       osig_set = 0;
731*5796c8dcSSimon Schubert     }
732*5796c8dcSSimon Schubert }
733*5796c8dcSSimon Schubert 
734*5796c8dcSSimon Schubert 
735*5796c8dcSSimon Schubert /* Create a new session if the inferior will run in a different tty.
736*5796c8dcSSimon Schubert    A session is UNIX's way of grouping processes that share a controlling
737*5796c8dcSSimon Schubert    terminal, so a new one is needed if the inferior terminal will be
738*5796c8dcSSimon Schubert    different from GDB's.
739*5796c8dcSSimon Schubert 
740*5796c8dcSSimon Schubert    Returns the session id of the new session, 0 if no session was created
741*5796c8dcSSimon Schubert    or -1 if an error occurred.  */
742*5796c8dcSSimon Schubert pid_t
743*5796c8dcSSimon Schubert create_tty_session (void)
744*5796c8dcSSimon Schubert {
745*5796c8dcSSimon Schubert #ifdef HAVE_SETSID
746*5796c8dcSSimon Schubert   pid_t ret;
747*5796c8dcSSimon Schubert 
748*5796c8dcSSimon Schubert   if (!job_control || inferior_thisrun_terminal == 0)
749*5796c8dcSSimon Schubert     return 0;
750*5796c8dcSSimon Schubert 
751*5796c8dcSSimon Schubert   ret = setsid ();
752*5796c8dcSSimon Schubert   if (ret == -1)
753*5796c8dcSSimon Schubert     warning ("Failed to create new terminal session: setsid: %s",
754*5796c8dcSSimon Schubert 	     safe_strerror (errno));
755*5796c8dcSSimon Schubert 
756*5796c8dcSSimon Schubert   return ret;
757*5796c8dcSSimon Schubert #else
758*5796c8dcSSimon Schubert   return 0;
759*5796c8dcSSimon Schubert #endif /* HAVE_SETSID */
760*5796c8dcSSimon Schubert }
761*5796c8dcSSimon Schubert 
762*5796c8dcSSimon Schubert /* This is here because this is where we figure out whether we (probably)
763*5796c8dcSSimon Schubert    have job control.  Just using job_control only does part of it because
764*5796c8dcSSimon Schubert    setpgid or setpgrp might not exist on a system without job control.
765*5796c8dcSSimon Schubert    It might be considered misplaced (on the other hand, process groups and
766*5796c8dcSSimon Schubert    job control are closely related to ttys).
767*5796c8dcSSimon Schubert 
768*5796c8dcSSimon Schubert    For a more clean implementation, in libiberty, put a setpgid which merely
769*5796c8dcSSimon Schubert    calls setpgrp and a setpgrp which does nothing (any system with job control
770*5796c8dcSSimon Schubert    will have one or the other).  */
771*5796c8dcSSimon Schubert int
772*5796c8dcSSimon Schubert gdb_setpgid (void)
773*5796c8dcSSimon Schubert {
774*5796c8dcSSimon Schubert   int retval = 0;
775*5796c8dcSSimon Schubert 
776*5796c8dcSSimon Schubert   if (job_control)
777*5796c8dcSSimon Schubert     {
778*5796c8dcSSimon Schubert #if defined (HAVE_TERMIOS) || defined (TIOCGPGRP)
779*5796c8dcSSimon Schubert #ifdef HAVE_SETPGID
780*5796c8dcSSimon Schubert       /* The call setpgid (0, 0) is supposed to work and mean the same
781*5796c8dcSSimon Schubert          thing as this, but on Ultrix 4.2A it fails with EPERM (and
782*5796c8dcSSimon Schubert          setpgid (getpid (), getpid ()) succeeds).  */
783*5796c8dcSSimon Schubert       retval = setpgid (getpid (), getpid ());
784*5796c8dcSSimon Schubert #else
785*5796c8dcSSimon Schubert #ifdef HAVE_SETPGRP
786*5796c8dcSSimon Schubert #ifdef SETPGRP_VOID
787*5796c8dcSSimon Schubert       retval = setpgrp ();
788*5796c8dcSSimon Schubert #else
789*5796c8dcSSimon Schubert       retval = setpgrp (getpid (), getpid ());
790*5796c8dcSSimon Schubert #endif
791*5796c8dcSSimon Schubert #endif /* HAVE_SETPGRP */
792*5796c8dcSSimon Schubert #endif /* HAVE_SETPGID */
793*5796c8dcSSimon Schubert #endif /* defined (HAVE_TERMIOS) || defined (TIOCGPGRP) */
794*5796c8dcSSimon Schubert     }
795*5796c8dcSSimon Schubert 
796*5796c8dcSSimon Schubert   return retval;
797*5796c8dcSSimon Schubert }
798*5796c8dcSSimon Schubert 
799*5796c8dcSSimon Schubert /* Get all the current tty settings (including whether we have a
800*5796c8dcSSimon Schubert    tty at all!).  We can't do this in _initialize_inflow because
801*5796c8dcSSimon Schubert    serial_fdopen() won't work until the serial_ops_list is
802*5796c8dcSSimon Schubert    initialized, but we don't want to do it lazily either, so
803*5796c8dcSSimon Schubert    that we can guarantee stdin_serial is opened if there is
804*5796c8dcSSimon Schubert    a terminal.  */
805*5796c8dcSSimon Schubert void
806*5796c8dcSSimon Schubert initialize_stdin_serial (void)
807*5796c8dcSSimon Schubert {
808*5796c8dcSSimon Schubert   stdin_serial = serial_fdopen (0);
809*5796c8dcSSimon Schubert }
810*5796c8dcSSimon Schubert 
811*5796c8dcSSimon Schubert void
812*5796c8dcSSimon Schubert _initialize_inflow (void)
813*5796c8dcSSimon Schubert {
814*5796c8dcSSimon Schubert   add_info ("terminal", term_info,
815*5796c8dcSSimon Schubert 	    _("Print inferior's saved terminal status."));
816*5796c8dcSSimon Schubert 
817*5796c8dcSSimon Schubert   terminal_is_ours = 1;
818*5796c8dcSSimon Schubert 
819*5796c8dcSSimon Schubert   /* OK, figure out whether we have job control.  If neither termios nor
820*5796c8dcSSimon Schubert      sgtty (i.e. termio or go32), leave job_control 0.  */
821*5796c8dcSSimon Schubert 
822*5796c8dcSSimon Schubert #if defined (HAVE_TERMIOS)
823*5796c8dcSSimon Schubert   /* Do all systems with termios have the POSIX way of identifying job
824*5796c8dcSSimon Schubert      control?  I hope so.  */
825*5796c8dcSSimon Schubert #ifdef _POSIX_JOB_CONTROL
826*5796c8dcSSimon Schubert   job_control = 1;
827*5796c8dcSSimon Schubert #else
828*5796c8dcSSimon Schubert #ifdef _SC_JOB_CONTROL
829*5796c8dcSSimon Schubert   job_control = sysconf (_SC_JOB_CONTROL);
830*5796c8dcSSimon Schubert #else
831*5796c8dcSSimon Schubert   job_control = 0;		/* have to assume the worst */
832*5796c8dcSSimon Schubert #endif /* _SC_JOB_CONTROL */
833*5796c8dcSSimon Schubert #endif /* _POSIX_JOB_CONTROL */
834*5796c8dcSSimon Schubert #endif /* HAVE_TERMIOS */
835*5796c8dcSSimon Schubert 
836*5796c8dcSSimon Schubert #ifdef HAVE_SGTTY
837*5796c8dcSSimon Schubert #ifdef TIOCGPGRP
838*5796c8dcSSimon Schubert   job_control = 1;
839*5796c8dcSSimon Schubert #else
840*5796c8dcSSimon Schubert   job_control = 0;
841*5796c8dcSSimon Schubert #endif /* TIOCGPGRP */
842*5796c8dcSSimon Schubert #endif /* sgtty */
843*5796c8dcSSimon Schubert 
844*5796c8dcSSimon Schubert   observer_attach_new_inferior (inflow_new_inferior);
845*5796c8dcSSimon Schubert   observer_attach_inferior_exit (inflow_inferior_exit);
846*5796c8dcSSimon Schubert }
847