gdb/copyright.py: Remove testsuite/gdb.base/step-line.{c,inp} special handling
[deliverable/binutils-gdb.git] / gdb / inflow.c
CommitLineData
c906108c 1/* Low level interface to ptrace, for GDB when running under Unix.
61baf725 2 Copyright (C) 1986-2017 Free Software Foundation, Inc.
c906108c 3
c5aa993b 4 This file is part of GDB.
c906108c 5
c5aa993b
JM
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
a9762ec7 8 the Free Software Foundation; either version 3 of the License, or
c5aa993b 9 (at your option) any later version.
c906108c 10
c5aa993b
JM
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
c906108c 15
c5aa993b 16 You should have received a copy of the GNU General Public License
a9762ec7 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
18
19#include "defs.h"
20#include "frame.h"
21#include "inferior.h"
22#include "command.h"
c906108c
SS
23#include "serial.h"
24#include "terminal.h"
25#include "target.h"
26#include "gdbthread.h"
7e1789f5 27#include "observer.h"
c906108c
SS
28#include <signal.h>
29#include <fcntl.h>
0ea3f30e 30#include "gdb_select.h"
c906108c 31
44270758 32#include "inflow.h"
7bfc9434 33#include "gdbcmd.h"
726e1356
PA
34#ifdef HAVE_TERMIOS_H
35#include <termios.h>
36#endif
15652511 37#include "job-control.h"
c906108c 38
c2d11a7d
JM
39#ifdef HAVE_SYS_IOCTL_H
40#include <sys/ioctl.h>
41#endif
42
f2acbe1c
MK
43#ifndef O_NOCTTY
44#define O_NOCTTY 0
45#endif
46
a14ed312 47static void pass_signal (int);
c906108c 48
d6b64346 49static void child_terminal_ours_1 (int);
c906108c
SS
50\f
51/* Record terminal status separately for debugger and inferior. */
52
819cc324 53static struct serial *stdin_serial;
c906108c 54
7e1789f5
PA
55/* Terminal related info we need to keep track of. Each inferior
56 holds an instance of this structure --- we save it whenever the
57 corresponding inferior stops, and restore it to the foreground
58 inferior when it resumes. */
59struct terminal_info
60{
61 /* The name of the tty (from the `tty' command) that we gave to the
62 inferior when it was started. */
191c4426 63 char *run_terminal;
7e1789f5
PA
64
65 /* TTY state. We save it whenever the inferior stops, and restore
66 it when it resumes. */
67 serial_ttystate ttystate;
68
726e1356 69#ifdef HAVE_TERMIOS_H
7e1789f5 70 /* Process group. Saved and restored just like ttystate. */
726e1356 71 pid_t process_group;
7e1789f5 72#endif
c906108c 73
7e1789f5
PA
74 /* fcntl flags. Saved and restored just like ttystate. */
75 int tflags;
76};
c906108c 77
7e1789f5 78/* Our own tty state, which we restore every time we need to deal with
d9de1fe3
PA
79 the terminal. This is set once, when GDB first starts, and then
80 whenever we enter/leave TUI mode (gdb_save_tty_state). The
81 settings of flags which readline saves and restores are
7e1789f5
PA
82 unimportant. */
83static struct terminal_info our_terminal_info;
c906108c 84
d9de1fe3
PA
85/* Snapshot of the initial tty state taken during initialization of
86 GDB, before readline/ncurses have had a chance to change it. This
87 is used as the initial tty state given to each new spawned
88 inferior. Unlike our_terminal_info, this is only ever set
89 once. */
6a06d660
PP
90static serial_ttystate initial_gdb_ttystate;
91
6c95b8df
PA
92static struct terminal_info *get_inflow_inferior_data (struct inferior *);
93
e2c33ac7
PA
94/* RAII class used to ignore SIGTTOU in a scope. */
95
96class scoped_ignore_sigttou
97{
98public:
99 scoped_ignore_sigttou ()
100 {
101#ifdef SIGTTOU
102 if (job_control)
103 m_osigttou = signal (SIGTTOU, SIG_IGN);
104#endif
105 }
106
107 ~scoped_ignore_sigttou ()
108 {
109#ifdef SIGTTOU
110 if (job_control)
111 signal (SIGTTOU, m_osigttou);
112#endif
113 }
114
115 DISABLE_COPY_AND_ASSIGN (scoped_ignore_sigttou);
116
117private:
118#ifdef SIGTTOU
119 sighandler_t m_osigttou = NULL;
120#endif
121};
122
726e1356 123#ifdef HAVE_TERMIOS_H
7e1789f5
PA
124
125/* Return the process group of the current inferior. */
126
726e1356 127pid_t
7e1789f5
PA
128inferior_process_group (void)
129{
6c95b8df 130 return get_inflow_inferior_data (current_inferior ())->process_group;
7e1789f5 131}
c906108c
SS
132#endif
133
134/* While the inferior is running, we want SIGINT and SIGQUIT to go to the
135 inferior only. If we have job control, that takes care of it. If not,
136 we save our handlers in these two variables and set SIGINT and SIGQUIT
137 to SIG_IGN. */
138
a40805d4
PA
139static sighandler_t sigint_ours;
140static sighandler_t sigquit_ours;
c906108c 141
7e1789f5
PA
142/* The name of the tty (from the `tty' command) that we're giving to
143 the inferior when starting it up. This is only (and should only
191c4426
PA
144 be) used as a transient global by new_tty_prefork,
145 create_tty_session, new_tty and new_tty_postfork, all called from
146 fork_inferior, while forking a new child. */
3cb3b8df 147static const char *inferior_thisrun_terminal;
c906108c
SS
148
149/* Nonzero if our terminal settings are in effect. Zero if the
150 inferior's settings are in effect. Ignored if !gdb_has_a_terminal
151 (). */
152
153int terminal_is_ours;
154
d9de1fe3 155/* See terminal.h. */
ea42d6f8 156
6a06d660
PP
157void
158set_initial_gdb_ttystate (void)
159{
d9de1fe3
PA
160 /* Note we can't do any of this in _initialize_inflow because at
161 that point stdin_serial has not been created yet. */
162
6a06d660 163 initial_gdb_ttystate = serial_get_tty_state (stdin_serial);
6a06d660 164
d9de1fe3 165 if (initial_gdb_ttystate != NULL)
c906108c 166 {
d9de1fe3
PA
167 our_terminal_info.ttystate
168 = serial_copy_tty_state (stdin_serial, initial_gdb_ttystate);
c906108c 169#ifdef F_GETFL
7e1789f5 170 our_terminal_info.tflags = fcntl (0, F_GETFL, 0);
c906108c 171#endif
726e1356
PA
172#ifdef HAVE_TERMIOS_H
173 our_terminal_info.process_group = tcgetpgrp (0);
807bddf3 174#endif
c906108c
SS
175 }
176}
177
d9de1fe3
PA
178/* Does GDB have a terminal (on stdin)? */
179
180static int
181gdb_has_a_terminal (void)
182{
183 return initial_gdb_ttystate != NULL;
184}
185
c906108c
SS
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
c906108c
SS
193/* Initialize the terminal settings we record for the inferior,
194 before we actually run the inferior. */
195
196void
556e5da5 197child_terminal_init (struct target_ops *self)
c906108c 198{
6f64ef53
PA
199 struct inferior *inf = current_inferior ();
200 struct terminal_info *tinfo = get_inflow_inferior_data (inf);
201
726e1356 202#ifdef HAVE_TERMIOS_H
6f64ef53
PA
203 /* Store the process group even without a terminal as it is used not
204 only to reset the tty foreground process group, but also to
556e5da5
PA
205 interrupt the inferior. A child we spawn should be a process
206 group leader (PGID==PID) at this point, though that may not be
207 true if we're attaching to an existing process. */
208 tinfo->process_group = inf->pid;
6f64ef53
PA
209#endif
210
c906108c
SS
211 if (gdb_has_a_terminal ())
212 {
6c95b8df 213 xfree (tinfo->ttystate);
1e182ce8 214 tinfo->ttystate = serial_copy_tty_state (stdin_serial,
6a06d660 215 initial_gdb_ttystate);
c906108c 216
c906108c 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
3278a9f5 229gdb_save_tty_state (void)
a790ad35
SC
230{
231 if (gdb_has_a_terminal ())
232 {
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 238/* Put the inferior's terminal settings into effect.
4d4ca2a1
DE
239 This is preparation for starting or resuming the inferior.
240
241 N.B. Targets that want to use this with async support must build that
242 support on top of this (e.g., the caller still needs to remove stdin
243 from the event loop). E.g., see linux_nat_terminal_inferior. */
c906108c
SS
244
245void
d6b64346 246child_terminal_inferior (struct target_ops *self)
c906108c 247{
7e1789f5 248 struct inferior *inf;
6c95b8df 249 struct terminal_info *tinfo;
7e1789f5
PA
250
251 if (!terminal_is_ours)
252 return;
253
254 inf = current_inferior ();
6c95b8df 255 tinfo = get_inflow_inferior_data (inf);
7e1789f5
PA
256
257 if (gdb_has_a_terminal ()
6c95b8df
PA
258 && tinfo->ttystate != NULL
259 && tinfo->run_terminal == NULL)
c906108c
SS
260 {
261 int result;
262
263#ifdef F_GETFL
6c95b8df 264 result = fcntl (0, F_SETFL, tinfo->tflags);
c906108c
SS
265 OOPSY ("fcntl F_SETFL");
266#endif
267
726e1356 268 result = serial_set_tty_state (stdin_serial, tinfo->ttystate);
c906108c
SS
269 OOPSY ("setting tty state");
270
271 if (!job_control)
272 {
a40805d4 273 sigint_ours = signal (SIGINT, SIG_IGN);
c906108c 274#ifdef SIGQUIT
a40805d4 275 sigquit_ours = signal (SIGQUIT, SIG_IGN);
c906108c
SS
276#endif
277 }
278
279 /* If attach_flag is set, we don't know whether we are sharing a
c5aa993b
JM
280 terminal with the inferior or not. (attaching a process
281 without a terminal is one case where we do not; attaching a
282 process which we ran from the same shell as GDB via `&' is
283 one case where we do, I think (but perhaps this is not
284 `sharing' in the sense that we need to save and restore tty
285 state)). I don't know if there is any way to tell whether we
286 are sharing a terminal. So what we do is to go through all
287 the saving and restoring of the tty state, but ignore errors
288 setting the process group, which will happen if we are not
289 sharing a terminal). */
c906108c
SS
290
291 if (job_control)
292 {
726e1356 293#ifdef HAVE_TERMIOS_H
6c95b8df 294 result = tcsetpgrp (0, tinfo->process_group);
181e7f93 295 if (!inf->attach_flag)
c906108c
SS
296 OOPSY ("tcsetpgrp");
297#endif
c906108c 298 }
c906108c
SS
299 }
300 terminal_is_ours = 0;
301}
302
303/* Put some of our terminal settings into effect,
304 enough to get proper results from our output,
305 but do not change into or out of RAW mode
306 so that no input is discarded.
307
308 After doing this, either terminal_ours or terminal_inferior
4d4ca2a1
DE
309 should be called to get back to a normal state of affairs.
310
311 N.B. The implementation is (currently) no different than
312 child_terminal_ours. See child_terminal_ours_1. */
c906108c
SS
313
314void
d6b64346 315child_terminal_ours_for_output (struct target_ops *self)
c906108c 316{
d6b64346 317 child_terminal_ours_1 (1);
c906108c
SS
318}
319
320/* Put our terminal settings into effect.
321 First record the inferior's terminal settings
4d4ca2a1
DE
322 so they can be restored properly later.
323
324 N.B. Targets that want to use this with async support must build that
325 support on top of this (e.g., the caller still needs to add stdin to the
326 event loop). E.g., see linux_nat_terminal_ours. */
c906108c
SS
327
328void
d6b64346 329child_terminal_ours (struct target_ops *self)
c906108c 330{
d6b64346 331 child_terminal_ours_1 (0);
c906108c
SS
332}
333
334/* output_only is not used, and should not be used unless we introduce
335 separate terminal_is_ours and terminal_is_ours_for_output
336 flags. */
337
338static void
d6b64346 339child_terminal_ours_1 (int output_only)
c906108c 340{
7e1789f5 341 struct inferior *inf;
6c95b8df 342 struct terminal_info *tinfo;
7e1789f5
PA
343
344 if (terminal_is_ours)
345 return;
346
d9d2d8b6
PA
347 terminal_is_ours = 1;
348
7e1789f5 349 /* Checking inferior->run_terminal is necessary so that
c906108c
SS
350 if GDB is running in the background, it won't block trying
351 to do the ioctl()'s below. Checking gdb_has_a_terminal
352 avoids attempting all the ioctl's when running in batch. */
7e1789f5
PA
353
354 inf = current_inferior ();
6c95b8df 355 tinfo = get_inflow_inferior_data (inf);
7e1789f5 356
6c95b8df 357 if (tinfo->run_terminal != NULL || gdb_has_a_terminal () == 0)
c906108c 358 return;
a579cd9a 359 else
c906108c 360 {
821fc4ae 361 int result ATTRIBUTE_UNUSED;
c906108c 362
e2c33ac7
PA
363 /* Ignore SIGTTOU since it will happen when we try to set the
364 terminal's pgrp. */
365 scoped_ignore_sigttou ignore_sigttou;
c906108c 366
6c95b8df
PA
367 xfree (tinfo->ttystate);
368 tinfo->ttystate = serial_get_tty_state (stdin_serial);
64a0ac84 369
726e1356 370#ifdef HAVE_TERMIOS_H
181e7f93 371 if (!inf->attach_flag)
726e1356 372 /* If tcsetpgrp failed in terminal_inferior, this would give us
64a0ac84
PA
373 our process group instead of the inferior's. See
374 terminal_inferior for details. */
726e1356 375 tinfo->process_group = tcgetpgrp (0);
49a834f9 376#endif
c906108c 377
726e1356
PA
378 /* Set tty state to our_ttystate. */
379 serial_set_tty_state (stdin_serial, our_terminal_info.ttystate);
c906108c
SS
380
381 if (job_control)
382 {
726e1356 383#ifdef HAVE_TERMIOS_H
7e1789f5 384 result = tcsetpgrp (0, our_terminal_info.process_group);
c906108c
SS
385#if 0
386 /* This fails on Ultrix with EINVAL if you run the testsuite
387 in the background with nohup, and then log out. GDB never
388 used to check for an error here, so perhaps there are other
389 such situations as well. */
390 if (result == -1)
3e43a32a 391 fprintf_unfiltered (gdb_stderr,
d6b64346 392 "[tcsetpgrp failed in child_terminal_ours: %s]\n",
dc672865 393 safe_strerror (errno));
c906108c
SS
394#endif
395#endif /* termios */
c906108c
SS
396 }
397
c906108c
SS
398 if (!job_control)
399 {
400 signal (SIGINT, sigint_ours);
401#ifdef SIGQUIT
402 signal (SIGQUIT, sigquit_ours);
403#endif
404 }
405
406#ifdef F_GETFL
6c95b8df 407 tinfo->tflags = fcntl (0, F_GETFL, 0);
7e1789f5 408 result = fcntl (0, F_SETFL, our_terminal_info.tflags);
c906108c 409#endif
c906108c
SS
410 }
411}
412
6c95b8df
PA
413/* Per-inferior data key. */
414static const struct inferior_data *inflow_inferior_data;
7e1789f5
PA
415
416static void
6c95b8df 417inflow_inferior_data_cleanup (struct inferior *inf, void *arg)
7e1789f5 418{
9a3c8263 419 struct terminal_info *info = (struct terminal_info *) arg;
6c95b8df 420
487ad57c
YQ
421 xfree (info->run_terminal);
422 xfree (info->ttystate);
423 xfree (info);
6c95b8df
PA
424}
425
426/* Get the current svr4 data. If none is found yet, add it now. This
427 function always returns a valid object. */
428
429static struct terminal_info *
430get_inflow_inferior_data (struct inferior *inf)
431{
432 struct terminal_info *info;
433
9a3c8263 434 info = (struct terminal_info *) inferior_data (inf, inflow_inferior_data);
6c95b8df
PA
435 if (info == NULL)
436 {
41bf6aca 437 info = XCNEW (struct terminal_info);
6c95b8df
PA
438 set_inferior_data (inf, inflow_inferior_data, info);
439 }
7e1789f5 440
6c95b8df 441 return info;
7e1789f5
PA
442}
443
444/* This is a "inferior_exit" observer. Releases the TERMINAL_INFO member
445 of the inferior structure. This field is private to inflow.c, and
446 its type is opaque to the rest of GDB. PID is the target pid of
447 the inferior that is about to be removed from the inferior
448 list. */
449
450static void
a79b8f6e 451inflow_inferior_exit (struct inferior *inf)
7e1789f5 452{
6c95b8df 453 struct terminal_info *info;
7e1789f5 454
9a3c8263 455 info = (struct terminal_info *) inferior_data (inf, inflow_inferior_data);
6c95b8df
PA
456 if (info != NULL)
457 {
458 xfree (info->run_terminal);
1e182ce8 459 xfree (info->ttystate);
6c95b8df
PA
460 xfree (info);
461 set_inferior_data (inf, inflow_inferior_data, NULL);
462 }
7e1789f5
PA
463}
464
191c4426
PA
465void
466copy_terminal_info (struct inferior *to, struct inferior *from)
467{
6c95b8df
PA
468 struct terminal_info *tinfo_to, *tinfo_from;
469
470 tinfo_to = get_inflow_inferior_data (to);
471 tinfo_from = get_inflow_inferior_data (from);
1e182ce8
UW
472
473 xfree (tinfo_to->run_terminal);
474 xfree (tinfo_to->ttystate);
475
6c95b8df 476 *tinfo_to = *tinfo_from;
1e182ce8 477
6c95b8df
PA
478 if (tinfo_from->run_terminal)
479 tinfo_to->run_terminal
480 = xstrdup (tinfo_from->run_terminal);
1e182ce8
UW
481
482 if (tinfo_from->ttystate)
483 tinfo_to->ttystate
484 = serial_copy_tty_state (stdin_serial, tinfo_from->ttystate);
191c4426
PA
485}
486
c906108c 487void
1d12d88f 488info_terminal_command (const char *arg, int from_tty)
c906108c 489{
223ffa71 490 target_terminal::info (arg, from_tty);
c906108c
SS
491}
492
c906108c 493void
0a4f40a2 494child_terminal_info (struct target_ops *self, const char *args, int from_tty)
c906108c 495{
7e1789f5 496 struct inferior *inf;
6c95b8df 497 struct terminal_info *tinfo;
7e1789f5 498
c906108c
SS
499 if (!gdb_has_a_terminal ())
500 {
a3f17187 501 printf_filtered (_("This GDB does not control a terminal.\n"));
c906108c
SS
502 return;
503 }
504
7e1789f5
PA
505 if (ptid_equal (inferior_ptid, null_ptid))
506 return;
507
508 inf = current_inferior ();
6c95b8df 509 tinfo = get_inflow_inferior_data (inf);
7e1789f5 510
3e43a32a
MS
511 printf_filtered (_("Inferior's terminal status "
512 "(currently saved by GDB):\n"));
c906108c
SS
513
514 /* First the fcntl flags. */
515 {
516 int flags;
c5aa993b 517
6c95b8df 518 flags = tinfo->tflags;
c906108c
SS
519
520 printf_filtered ("File descriptor flags = ");
521
522#ifndef O_ACCMODE
523#define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
524#endif
1777feb0 525 /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */
c906108c
SS
526 switch (flags & (O_ACCMODE))
527 {
c5aa993b
JM
528 case O_RDONLY:
529 printf_filtered ("O_RDONLY");
530 break;
531 case O_WRONLY:
532 printf_filtered ("O_WRONLY");
533 break;
534 case O_RDWR:
535 printf_filtered ("O_RDWR");
536 break;
c906108c
SS
537 }
538 flags &= ~(O_ACCMODE);
539
540#ifdef O_NONBLOCK
c5aa993b 541 if (flags & O_NONBLOCK)
c906108c
SS
542 printf_filtered (" | O_NONBLOCK");
543 flags &= ~O_NONBLOCK;
544#endif
c5aa993b 545
c906108c
SS
546#if defined (O_NDELAY)
547 /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will
548 print it as O_NONBLOCK, which is good cause that is what POSIX
549 has, and the flag will already be cleared by the time we get here. */
550 if (flags & O_NDELAY)
551 printf_filtered (" | O_NDELAY");
552 flags &= ~O_NDELAY;
553#endif
554
555 if (flags & O_APPEND)
556 printf_filtered (" | O_APPEND");
557 flags &= ~O_APPEND;
558
559#if defined (O_BINARY)
560 if (flags & O_BINARY)
561 printf_filtered (" | O_BINARY");
562 flags &= ~O_BINARY;
563#endif
564
565 if (flags)
566 printf_filtered (" | 0x%x", flags);
567 printf_filtered ("\n");
568 }
569
726e1356 570#ifdef HAVE_TERMIOS_H
6c95b8df 571 printf_filtered ("Process group = %d\n", (int) tinfo->process_group);
c906108c
SS
572#endif
573
6c95b8df 574 serial_print_tty_state (stdin_serial, tinfo->ttystate, gdb_stdout);
c906108c
SS
575}
576\f
577/* NEW_TTY_PREFORK is called before forking a new child process,
578 so we can record the state of ttys in the child to be formed.
579 TTYNAME is null if we are to share the terminal with gdb;
580 or points to a string containing the name of the desired tty.
581
582 NEW_TTY is called in new child processes under Unix, which will
583 become debugger target processes. This actually switches to
584 the terminal specified in the NEW_TTY_PREFORK call. */
585
586void
3cb3b8df 587new_tty_prefork (const char *ttyname)
c906108c
SS
588{
589 /* Save the name for later, for determining whether we and the child
590 are sharing a tty. */
591 inferior_thisrun_terminal = ttyname;
592}
593
e6d088ec 594#if !defined(__GO32__) && !defined(_WIN32)
bf1d7d9c
JB
595/* If RESULT, assumed to be the return value from a system call, is
596 negative, print the error message indicated by errno and exit.
597 MSG should identify the operation that failed. */
598static void
599check_syscall (const char *msg, int result)
600{
601 if (result < 0)
602 {
603 print_sys_errmsg (msg, errno);
604 _exit (1);
605 }
606}
e6d088ec 607#endif
bf1d7d9c 608
c906108c 609void
fba45db2 610new_tty (void)
c906108c 611{
52f0bd74 612 int tty;
c906108c
SS
613
614 if (inferior_thisrun_terminal == 0)
615 return;
616#if !defined(__GO32__) && !defined(_WIN32)
617#ifdef TIOCNOTTY
618 /* Disconnect the child process from our controlling terminal. On some
619 systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
1777feb0 620 ignore SIGTTOU. */
c5aa993b 621 tty = open ("/dev/tty", O_RDWR);
c906108c
SS
622 if (tty > 0)
623 {
e2c33ac7 624 scoped_ignore_sigttou ignore_sigttou;
c906108c 625
c5aa993b
JM
626 ioctl (tty, TIOCNOTTY, 0);
627 close (tty);
c906108c
SS
628 }
629#endif
630
631 /* Now open the specified new terminal. */
c5aa993b 632 tty = open (inferior_thisrun_terminal, O_RDWR | O_NOCTTY);
bf1d7d9c 633 check_syscall (inferior_thisrun_terminal, tty);
c906108c
SS
634
635 /* Avoid use of dup2; doesn't exist on all systems. */
636 if (tty != 0)
c5aa993b
JM
637 {
638 close (0);
bf1d7d9c 639 check_syscall ("dup'ing tty into fd 0", dup (tty));
c5aa993b 640 }
c906108c 641 if (tty != 1)
c5aa993b
JM
642 {
643 close (1);
bf1d7d9c 644 check_syscall ("dup'ing tty into fd 1", dup (tty));
c5aa993b 645 }
c906108c 646 if (tty != 2)
c5aa993b
JM
647 {
648 close (2);
bf1d7d9c 649 check_syscall ("dup'ing tty into fd 2", dup (tty));
c5aa993b 650 }
83116857
TJB
651
652#ifdef TIOCSCTTY
653 /* Make tty our new controlling terminal. */
654 if (ioctl (tty, TIOCSCTTY, 0) == -1)
655 /* Mention GDB in warning because it will appear in the inferior's
656 terminal instead of GDB's. */
a73c6dcd 657 warning (_("GDB: Failed to set controlling terminal: %s"),
83116857
TJB
658 safe_strerror (errno));
659#endif
660
c906108c 661 if (tty > 2)
c5aa993b
JM
662 close (tty);
663#endif /* !go32 && !win32 */
c906108c 664}
191c4426
PA
665
666/* NEW_TTY_POSTFORK is called after forking a new child process, and
667 adding it to the inferior table, to store the TTYNAME being used by
668 the child, or null if it sharing the terminal with gdb. */
669
670void
671new_tty_postfork (void)
672{
673 /* Save the name for later, for determining whether we and the child
674 are sharing a tty. */
675
676 if (inferior_thisrun_terminal)
6c95b8df
PA
677 {
678 struct inferior *inf = current_inferior ();
679 struct terminal_info *tinfo = get_inflow_inferior_data (inf);
680
681 tinfo->run_terminal = xstrdup (inferior_thisrun_terminal);
682 }
191c4426
PA
683
684 inferior_thisrun_terminal = NULL;
685}
686
c906108c
SS
687\f
688/* Call set_sigint_trap when you need to pass a signal on to an attached
1777feb0 689 process when handling SIGINT. */
c906108c 690
c906108c 691static void
fba45db2 692pass_signal (int signo)
c906108c
SS
693{
694#ifndef _WIN32
dfd4cc63 695 kill (ptid_get_pid (inferior_ptid), SIGINT);
c906108c
SS
696#endif
697}
698
a40805d4 699static sighandler_t osig;
7e1789f5 700static int osig_set;
c906108c
SS
701
702void
fba45db2 703set_sigint_trap (void)
c906108c 704{
181e7f93 705 struct inferior *inf = current_inferior ();
6c95b8df
PA
706 struct terminal_info *tinfo = get_inflow_inferior_data (inf);
707
708 if (inf->attach_flag || tinfo->run_terminal)
c906108c 709 {
a40805d4 710 osig = signal (SIGINT, pass_signal);
7e1789f5 711 osig_set = 1;
c906108c 712 }
7e1789f5
PA
713 else
714 osig_set = 0;
c906108c
SS
715}
716
717void
fba45db2 718clear_sigint_trap (void)
c906108c 719{
7e1789f5 720 if (osig_set)
c906108c
SS
721 {
722 signal (SIGINT, osig);
7e1789f5 723 osig_set = 0;
c906108c
SS
724 }
725}
726\f
c906108c 727
83116857
TJB
728/* Create a new session if the inferior will run in a different tty.
729 A session is UNIX's way of grouping processes that share a controlling
730 terminal, so a new one is needed if the inferior terminal will be
731 different from GDB's.
732
733 Returns the session id of the new session, 0 if no session was created
734 or -1 if an error occurred. */
735pid_t
736create_tty_session (void)
737{
738#ifdef HAVE_SETSID
739 pid_t ret;
740
741 if (!job_control || inferior_thisrun_terminal == 0)
742 return 0;
743
744 ret = setsid ();
745 if (ret == -1)
a73c6dcd 746 warning (_("Failed to create new terminal session: setsid: %s"),
83116857
TJB
747 safe_strerror (errno));
748
749 return ret;
750#else
751 return 0;
752#endif /* HAVE_SETSID */
753}
754
0ea3f30e
DJ
755/* Get all the current tty settings (including whether we have a
756 tty at all!). We can't do this in _initialize_inflow because
757 serial_fdopen() won't work until the serial_ops_list is
758 initialized, but we don't want to do it lazily either, so
759 that we can guarantee stdin_serial is opened if there is
760 a terminal. */
761void
762initialize_stdin_serial (void)
763{
764 stdin_serial = serial_fdopen (0);
765}
766
c906108c 767void
fba45db2 768_initialize_inflow (void)
c906108c 769{
11db9430 770 add_info ("terminal", info_terminal_command,
1bedd215 771 _("Print inferior's saved terminal status."));
c906108c 772
c906108c
SS
773 terminal_is_ours = 1;
774
15652511
SDJ
775 /* OK, figure out whether we have job control. */
776 have_job_control ();
7e1789f5 777
7e1789f5 778 observer_attach_inferior_exit (inflow_inferior_exit);
6c95b8df
PA
779
780 inflow_inferior_data
8e260fc0 781 = register_inferior_data_with_cleanup (NULL, inflow_inferior_data_cleanup);
c906108c 782}
This page took 1.476671 seconds and 4 git commands to generate.