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