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