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