gdb/
[deliverable/binutils-gdb.git] / gdb / inflow.c
CommitLineData
c906108c 1/* Low level interface to ptrace, for GDB when running under Unix.
197e01b6 2 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
0fb0cc75
JB
3 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 2009 Free Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
c5aa993b 11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b 18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
20
21#include "defs.h"
22#include "frame.h"
23#include "inferior.h"
24#include "command.h"
c906108c
SS
25#include "serial.h"
26#include "terminal.h"
27#include "target.h"
28#include "gdbthread.h"
7e1789f5 29#include "observer.h"
c906108c
SS
30
31#include "gdb_string.h"
32#include <signal.h>
33#include <fcntl.h>
0ea3f30e 34#include "gdb_select.h"
c906108c 35
44270758 36#include "inflow.h"
c906108c 37
c2d11a7d
JM
38#ifdef HAVE_SYS_IOCTL_H
39#include <sys/ioctl.h>
40#endif
41
f2acbe1c
MK
42#ifndef O_NOCTTY
43#define O_NOCTTY 0
44#endif
45
c906108c 46#if defined (SIGIO) && defined (FASYNC) && defined (FD_SET) && defined (F_SETOWN)
a14ed312 47static void handle_sigio (int);
c906108c
SS
48#endif
49
a14ed312 50extern void _initialize_inflow (void);
392a587b 51
a14ed312 52static void pass_signal (int);
c906108c 53
a14ed312 54static void terminal_ours_1 (int);
c906108c
SS
55\f
56/* Record terminal status separately for debugger and inferior. */
57
819cc324 58static struct serial *stdin_serial;
c906108c 59
7e1789f5
PA
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. */
64struct terminal_info
65{
66 /* The name of the tty (from the `tty' command) that we gave to the
67 inferior when it was started. */
191c4426 68 char *run_terminal;
7e1789f5
PA
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
c906108c 78
7e1789f5
PA
79 /* fcntl flags. Saved and restored just like ttystate. */
80 int tflags;
81};
c906108c 82
7e1789f5
PA
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. */
87static struct terminal_info our_terminal_info;
c906108c
SS
88
89#ifdef PROCESS_GROUP_TYPE
7e1789f5
PA
90
91/* Return the process group of the current inferior. */
92
93PROCESS_GROUP_TYPE
94inferior_process_group (void)
95{
96 return current_inferior ()->terminal_info->process_group;
97}
c906108c
SS
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
105static void (*sigint_ours) ();
106static void (*sigquit_ours) ();
107
7e1789f5
PA
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
191c4426
PA
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. */
3cb3b8df 113static const char *inferior_thisrun_terminal;
c906108c
SS
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
119int terminal_is_ours;
120
64a0ac84
PA
121#ifdef PROCESS_GROUP_TYPE
122static PROCESS_GROUP_TYPE
123gdb_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
c5aa993b
JM
139enum
140 {
141 yes, no, have_not_checked
142 }
143gdb_has_a_terminal_flag = have_not_checked;
c906108c
SS
144
145/* Does GDB have a terminal (on stdin)? */
146int
fba45db2 147gdb_has_a_terminal (void)
c906108c
SS
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:
2cd58942
AC
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. */
c906108c
SS
160
161#ifdef F_GETFL
7e1789f5 162 our_terminal_info.tflags = fcntl (0, F_GETFL, 0);
c906108c
SS
163#endif
164
165 gdb_has_a_terminal_flag = no;
c906108c
SS
166 if (stdin_serial != NULL)
167 {
7e1789f5 168 our_terminal_info.ttystate = serial_get_tty_state (stdin_serial);
c906108c 169
7e1789f5 170 if (our_terminal_info.ttystate != NULL)
c906108c
SS
171 {
172 gdb_has_a_terminal_flag = yes;
807bddf3 173#ifdef PROCESS_GROUP_TYPE
7e1789f5 174 our_terminal_info.process_group = gdb_getpgrp ();
807bddf3 175#endif
c906108c
SS
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", \
dc672865 191 what, safe_strerror (errno))
c906108c 192
a14ed312 193static void terminal_ours_1 (int);
c906108c
SS
194
195/* Initialize the terminal settings we record for the inferior,
196 before we actually run the inferior. */
197
198void
fba45db2 199terminal_init_inferior_with_pgrp (int pgrp)
c906108c
SS
200{
201 if (gdb_has_a_terminal ())
202 {
7e1789f5
PA
203 struct inferior *inf = current_inferior ();
204
2cd58942
AC
205 /* We could just as well copy our_ttystate (if we felt like
206 adding a new function serial_copy_tty_state()). */
7e1789f5
PA
207 xfree (inf->terminal_info->ttystate);
208 inf->terminal_info->ttystate
209 = serial_get_tty_state (stdin_serial);
c906108c
SS
210
211#ifdef PROCESS_GROUP_TYPE
7e1789f5 212 inf->terminal_info->process_group = pgrp;
c906108c
SS
213#endif
214
215 /* Make sure that next time we call terminal_inferior (which will be
c5aa993b
JM
216 before the program runs, as it needs to be), we install the new
217 process group. */
c906108c
SS
218 terminal_is_ours = 1;
219 }
220}
221
a790ad35
SC
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
226void
7bda5e4a 227terminal_save_ours (void)
a790ad35
SC
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). */
7e1789f5
PA
233 xfree (our_terminal_info.ttystate);
234 our_terminal_info.ttystate = serial_get_tty_state (stdin_serial);
a790ad35
SC
235 }
236}
237
c906108c 238void
fba45db2 239terminal_init_inferior (void)
c906108c
SS
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
39f77062 246 inferior_ptid to the same routine). */
c906108c 247 /* We assume INFERIOR_PID is also the child's process group. */
39f77062 248 terminal_init_inferior_with_pgrp (PIDGET (inferior_ptid));
c906108c
SS
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
255void
fba45db2 256terminal_inferior (void)
c906108c 257{
7e1789f5
PA
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)
c906108c
SS
268 {
269 int result;
270
271#ifdef F_GETFL
272 /* Is there a reason this is being done twice? It happens both
c5aa993b
JM
273 places we use F_SETFL, so I'm inclined to think perhaps there
274 is some reason, however perverse. Perhaps not though... */
7e1789f5
PA
275 result = fcntl (0, F_SETFL, inf->terminal_info->tflags);
276 result = fcntl (0, F_SETFL, inf->terminal_info->tflags);
c906108c
SS
277 OOPSY ("fcntl F_SETFL");
278#endif
279
280 /* Because we were careful to not change in or out of raw mode in
c5aa993b
JM
281 terminal_ours, we will not change in our out of raw mode with
282 this call, so we don't flush any input. */
7e1789f5
PA
283 result = serial_set_tty_state (stdin_serial,
284 inf->terminal_info->ttystate);
c906108c
SS
285 OOPSY ("setting tty state");
286
287 if (!job_control)
288 {
c5aa993b 289 sigint_ours = (void (*)()) signal (SIGINT, SIG_IGN);
c906108c 290#ifdef SIGQUIT
c5aa993b 291 sigquit_ours = (void (*)()) signal (SIGQUIT, SIG_IGN);
c906108c
SS
292#endif
293 }
294
295 /* If attach_flag is set, we don't know whether we are sharing a
c5aa993b
JM
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). */
c906108c
SS
306
307 if (job_control)
308 {
309#ifdef HAVE_TERMIOS
7e1789f5 310 result = tcsetpgrp (0, inf->terminal_info->process_group);
181e7f93 311 if (!inf->attach_flag)
c906108c
SS
312 OOPSY ("tcsetpgrp");
313#endif
314
315#ifdef HAVE_SGTTY
7e1789f5 316 result = ioctl (0, TIOCSPGRP, &inf->terminal_info->process_group);
181e7f93 317 if (!inf->attach_flag)
c906108c
SS
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
334void
fba45db2 335terminal_ours_for_output (void)
c906108c
SS
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
344void
fba45db2 345terminal_ours (void)
c906108c
SS
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
354static void
fba45db2 355terminal_ours_1 (int output_only)
c906108c 356{
7e1789f5
PA
357 struct inferior *inf;
358
359 if (terminal_is_ours)
360 return;
361
d9d2d8b6
PA
362 terminal_is_ours = 1;
363
7e1789f5 364 /* Checking inferior->run_terminal is necessary so that
c906108c
SS
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. */
7e1789f5
PA
368
369 inf = current_inferior ();
370
371 if (inf->terminal_info->run_terminal != NULL || gdb_has_a_terminal () == 0)
c906108c
SS
372 return;
373
c906108c 374 {
4b69c284 375#ifdef SIGTTOU
c906108c 376 /* Ignore this signal since it will happen when we try to set the
c5aa993b 377 pgrp. */
4b69c284
AC
378 void (*osigttou) () = NULL;
379#endif
c906108c
SS
380 int result;
381
c906108c
SS
382#ifdef SIGTTOU
383 if (job_control)
c5aa993b 384 osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN);
c906108c
SS
385#endif
386
7e1789f5
PA
387 xfree (inf->terminal_info->ttystate);
388 inf->terminal_info->ttystate = serial_get_tty_state (stdin_serial);
64a0ac84 389
49a834f9 390#ifdef PROCESS_GROUP_TYPE
181e7f93 391 if (!inf->attach_flag)
64a0ac84
PA
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. */
7e1789f5 395 inf->terminal_info->process_group = gdb_getpgrp ();
49a834f9 396#endif
c906108c
SS
397
398 /* Here we used to set ICANON in our ttystate, but I believe this
c5aa993b
JM
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). */
c906108c
SS
404
405 /* Set tty state to our_ttystate. We don't change in our out of raw
c5aa993b
JM
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 */
c906108c 411
7e1789f5
PA
412 serial_noflush_set_tty_state (stdin_serial, our_terminal_info.ttystate,
413 inf->terminal_info->ttystate);
c906108c
SS
414
415 if (job_control)
416 {
417#ifdef HAVE_TERMIOS
7e1789f5 418 result = tcsetpgrp (0, our_terminal_info.process_group);
c906108c
SS
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",
dc672865 426 safe_strerror (errno));
c906108c
SS
427#endif
428#endif /* termios */
429
430#ifdef HAVE_SGTTY
7e1789f5 431 result = ioctl (0, TIOCSPGRP, &our_terminal_info.process_group);
c906108c
SS
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
7e1789f5 449 inf->terminal_info->tflags = fcntl (0, F_GETFL, 0);
c906108c
SS
450
451 /* Is there a reason this is being done twice? It happens both
c5aa993b
JM
452 places we use F_SETFL, so I'm inclined to think perhaps there
453 is some reason, however perverse. Perhaps not though... */
7e1789f5
PA
454 result = fcntl (0, F_SETFL, our_terminal_info.tflags);
455 result = fcntl (0, F_SETFL, our_terminal_info.tflags);
c906108c 456#endif
c906108c
SS
457 }
458}
459
7e1789f5
PA
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
466static void
467inflow_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
480static void
481inflow_inferior_exit (int pid)
482{
483 struct inferior *inf = find_inferior_pid (pid);
484
191c4426 485 xfree (inf->terminal_info->run_terminal);
7e1789f5
PA
486 xfree (inf->terminal_info);
487 inf->terminal_info = NULL;
488}
489
191c4426
PA
490void
491copy_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
c906108c 498void
fba45db2 499term_info (char *arg, int from_tty)
c906108c
SS
500{
501 target_terminal_info (arg, from_tty);
502}
503
c906108c 504void
fba45db2 505child_terminal_info (char *args, int from_tty)
c906108c 506{
7e1789f5
PA
507 struct inferior *inf;
508
c906108c
SS
509 if (!gdb_has_a_terminal ())
510 {
a3f17187 511 printf_filtered (_("This GDB does not control a terminal.\n"));
c906108c
SS
512 return;
513 }
514
7e1789f5
PA
515 if (ptid_equal (inferior_ptid, null_ptid))
516 return;
517
518 inf = current_inferior ();
519
a3f17187 520 printf_filtered (_("Inferior's terminal status (currently saved by GDB):\n"));
c906108c
SS
521
522 /* First the fcntl flags. */
523 {
524 int flags;
c5aa993b 525
7e1789f5 526 flags = inf->terminal_info->tflags;
c906108c
SS
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 {
c5aa993b
JM
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;
c906108c
SS
545 }
546 flags &= ~(O_ACCMODE);
547
548#ifdef O_NONBLOCK
c5aa993b 549 if (flags & O_NONBLOCK)
c906108c
SS
550 printf_filtered (" | O_NONBLOCK");
551 flags &= ~O_NONBLOCK;
552#endif
c5aa993b 553
c906108c
SS
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
2acceee2 579 printf_filtered ("Process group = %d\n",
7e1789f5 580 (int) inf->terminal_info->process_group);
c906108c
SS
581#endif
582
7e1789f5
PA
583 serial_print_tty_state (stdin_serial,
584 inf->terminal_info->ttystate,
585 gdb_stdout);
c906108c
SS
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
597void
3cb3b8df 598new_tty_prefork (const char *ttyname)
c906108c
SS
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
bf1d7d9c
JB
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. */
609static void
610check_syscall (const char *msg, int result)
611{
612 if (result < 0)
613 {
614 print_sys_errmsg (msg, errno);
615 _exit (1);
616 }
617}
618
c906108c 619void
fba45db2 620new_tty (void)
c906108c 621{
52f0bd74 622 int tty;
c906108c
SS
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. */
c5aa993b 631 tty = open ("/dev/tty", O_RDWR);
c906108c
SS
632 if (tty > 0)
633 {
634 void (*osigttou) ();
635
c5aa993b
JM
636 osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN);
637 ioctl (tty, TIOCNOTTY, 0);
638 close (tty);
639 signal (SIGTTOU, osigttou);
c906108c
SS
640 }
641#endif
642
643 /* Now open the specified new terminal. */
c5aa993b 644 tty = open (inferior_thisrun_terminal, O_RDWR | O_NOCTTY);
bf1d7d9c 645 check_syscall (inferior_thisrun_terminal, tty);
c906108c
SS
646
647 /* Avoid use of dup2; doesn't exist on all systems. */
648 if (tty != 0)
c5aa993b
JM
649 {
650 close (0);
bf1d7d9c 651 check_syscall ("dup'ing tty into fd 0", dup (tty));
c5aa993b 652 }
c906108c 653 if (tty != 1)
c5aa993b
JM
654 {
655 close (1);
bf1d7d9c 656 check_syscall ("dup'ing tty into fd 1", dup (tty));
c5aa993b 657 }
c906108c 658 if (tty != 2)
c5aa993b
JM
659 {
660 close (2);
bf1d7d9c 661 check_syscall ("dup'ing tty into fd 2", dup (tty));
c5aa993b 662 }
83116857
TJB
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
c906108c 673 if (tty > 2)
c5aa993b
JM
674 close (tty);
675#endif /* !go32 && !win32 */
c906108c 676}
191c4426
PA
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
682void
683new_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
c906108c
SS
695\f
696/* Call set_sigint_trap when you need to pass a signal on to an attached
697 process when handling SIGINT */
698
c906108c 699static void
fba45db2 700pass_signal (int signo)
c906108c
SS
701{
702#ifndef _WIN32
39f77062 703 kill (PIDGET (inferior_ptid), SIGINT);
c906108c
SS
704#endif
705}
706
c5aa993b 707static void (*osig) ();
7e1789f5 708static int osig_set;
c906108c
SS
709
710void
fba45db2 711set_sigint_trap (void)
c906108c 712{
181e7f93 713 struct inferior *inf = current_inferior ();
7e1789f5 714 if (inf->attach_flag || inf->terminal_info->run_terminal)
c906108c 715 {
c5aa993b 716 osig = (void (*)()) signal (SIGINT, pass_signal);
7e1789f5 717 osig_set = 1;
c906108c 718 }
7e1789f5
PA
719 else
720 osig_set = 0;
c906108c
SS
721}
722
723void
fba45db2 724clear_sigint_trap (void)
c906108c 725{
7e1789f5 726 if (osig_set)
c906108c
SS
727 {
728 signal (SIGINT, osig);
7e1789f5 729 osig_set = 0;
c906108c
SS
730 }
731}
732\f
c906108c 733
83116857
TJB
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. */
741pid_t
742create_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
c906108c
SS
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). */
770int
fba45db2 771gdb_setpgid (void)
c906108c
SS
772{
773 int retval = 0;
774
775 if (job_control)
776 {
0200359f
MK
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
c5aa993b 781 setpgid (getpid (), getpid ()) succeeds). */
c906108c
SS
782 retval = setpgid (getpid (), getpid ());
783#else
0200359f
MK
784#ifdef HAVE_SETPGRP
785#ifdef SETPGRP_VOID
c906108c
SS
786 retval = setpgrp ();
787#else
788 retval = setpgrp (getpid (), getpid ());
0200359f
MK
789#endif
790#endif /* HAVE_SETPGRP */
791#endif /* HAVE_SETPGID */
792#endif /* defined (HAVE_TERMIOS) || defined (TIOCGPGRP) */
c906108c 793 }
0200359f 794
c906108c
SS
795 return retval;
796}
797
0ea3f30e
DJ
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. */
804void
805initialize_stdin_serial (void)
806{
807 stdin_serial = serial_fdopen (0);
808}
809
c906108c 810void
fba45db2 811_initialize_inflow (void)
c906108c
SS
812{
813 add_info ("terminal", term_info,
1bedd215 814 _("Print inferior's saved terminal status."));
c906108c 815
c906108c
SS
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
c5aa993b
JM
830 job_control = 0; /* have to assume the worst */
831#endif /* _SC_JOB_CONTROL */
832#endif /* _POSIX_JOB_CONTROL */
833#endif /* HAVE_TERMIOS */
c906108c
SS
834
835#ifdef HAVE_SGTTY
836#ifdef TIOCGPGRP
837 job_control = 1;
838#else
839 job_control = 0;
840#endif /* TIOCGPGRP */
841#endif /* sgtty */
7e1789f5
PA
842
843 observer_attach_new_inferior (inflow_new_inferior);
844 observer_attach_inferior_exit (inflow_inferior_exit);
c906108c 845}
This page took 0.712861 seconds and 4 git commands to generate.