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