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