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