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