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