Merge branch 'vendor/GDB' into gdb7
[dragonfly.git] / contrib / gdb-7 / 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 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 #if defined (SIGIO) && defined (FASYNC) && defined (FD_SET) && defined (F_SETOWN)
47 static void handle_sigio (int);
48 #endif
49
50 extern void _initialize_inflow (void);
51
52 static void pass_signal (int);
53
54 static void terminal_ours_1 (int);
55 \f
56 /* Record terminal status separately for debugger and inferior.  */
57
58 static struct serial *stdin_serial;
59
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.  */
64 struct terminal_info
65 {
66   /* The name of the tty (from the `tty' command) that we gave to the
67      inferior when it was started.  */
68   char *run_terminal;
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
78
79   /* fcntl flags.  Saved and restored just like ttystate.  */
80   int tflags;
81 };
82
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.  */
87 static struct terminal_info our_terminal_info;
88
89 #ifdef PROCESS_GROUP_TYPE
90
91 /* Return the process group of the current inferior.  */
92
93 PROCESS_GROUP_TYPE
94 inferior_process_group (void)
95 {
96   return current_inferior ()->terminal_info->process_group;
97 }
98 #endif
99
100 /* While the inferior is running, we want SIGINT and SIGQUIT to go to the
101    inferior only.  If we have job control, that takes care of it.  If not,
102    we save our handlers in these two variables and set SIGINT and SIGQUIT
103    to SIG_IGN.  */
104
105 static void (*sigint_ours) ();
106 static void (*sigquit_ours) ();
107
108 /* The name of the tty (from the `tty' command) that we're giving to
109    the inferior when starting it up.  This is only (and should only
110    be) used as a transient global by new_tty_prefork,
111    create_tty_session, new_tty and new_tty_postfork, all called from
112    fork_inferior, while forking a new child.  */
113 static const char *inferior_thisrun_terminal;
114
115 /* Nonzero if our terminal settings are in effect.  Zero if the
116    inferior's settings are in effect.  Ignored if !gdb_has_a_terminal
117    ().  */
118
119 int terminal_is_ours;
120
121 #ifdef PROCESS_GROUP_TYPE
122 static PROCESS_GROUP_TYPE
123 gdb_getpgrp (void)
124 {
125   int process_group = -1;
126 #ifdef HAVE_TERMIOS
127   process_group = tcgetpgrp (0);
128 #endif
129 #ifdef HAVE_TERMIO
130   process_group = getpgrp ();
131 #endif
132 #ifdef HAVE_SGTTY
133   ioctl (0, TIOCGPGRP, &process_group);
134 #endif
135   return process_group;
136 }
137 #endif
138
139 enum
140   {
141     yes, no, have_not_checked
142   }
143 gdb_has_a_terminal_flag = have_not_checked;
144
145 /* Does GDB have a terminal (on stdin)?  */
146 int
147 gdb_has_a_terminal (void)
148 {
149   switch (gdb_has_a_terminal_flag)
150     {
151     case yes:
152       return 1;
153     case no:
154       return 0;
155     case have_not_checked:
156       /* Get all the current tty settings (including whether we have a
157          tty at all!).  Can't do this in _initialize_inflow because
158          serial_fdopen() won't work until the serial_ops_list is
159          initialized.  */
160
161 #ifdef F_GETFL
162       our_terminal_info.tflags = fcntl (0, F_GETFL, 0);
163 #endif
164
165       gdb_has_a_terminal_flag = no;
166       if (stdin_serial != NULL)
167         {
168           our_terminal_info.ttystate = serial_get_tty_state (stdin_serial);
169
170           if (our_terminal_info.ttystate != NULL)
171             {
172               gdb_has_a_terminal_flag = yes;
173 #ifdef PROCESS_GROUP_TYPE
174               our_terminal_info.process_group = gdb_getpgrp ();
175 #endif
176             }
177         }
178
179       return gdb_has_a_terminal_flag == yes;
180     default:
181       /* "Can't happen".  */
182       return 0;
183     }
184 }
185
186 /* Macro for printing errors from ioctl operations */
187
188 #define OOPSY(what)     \
189   if (result == -1)     \
190     fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \
191             what, safe_strerror (errno))
192
193 static void terminal_ours_1 (int);
194
195 /* Initialize the terminal settings we record for the inferior,
196    before we actually run the inferior.  */
197
198 void
199 terminal_init_inferior_with_pgrp (int pgrp)
200 {
201   if (gdb_has_a_terminal ())
202     {
203       struct inferior *inf = current_inferior ();
204
205       /* We could just as well copy our_ttystate (if we felt like
206          adding a new function serial_copy_tty_state()).  */
207       xfree (inf->terminal_info->ttystate);
208       inf->terminal_info->ttystate
209         = serial_get_tty_state (stdin_serial);
210
211 #ifdef PROCESS_GROUP_TYPE
212       inf->terminal_info->process_group = pgrp;
213 #endif
214
215       /* Make sure that next time we call terminal_inferior (which will be
216          before the program runs, as it needs to be), we install the new
217          process group.  */
218       terminal_is_ours = 1;
219     }
220 }
221
222 /* Save the terminal settings again.  This is necessary for the TUI
223    when it switches to TUI or non-TUI mode;  curses changes the terminal
224    and gdb must be able to restore it correctly.  */
225
226 void
227 terminal_save_ours (void)
228 {
229   if (gdb_has_a_terminal ())
230     {
231       /* We could just as well copy our_ttystate (if we felt like adding
232          a new function serial_copy_tty_state).  */
233       xfree (our_terminal_info.ttystate);
234       our_terminal_info.ttystate = serial_get_tty_state (stdin_serial);
235     }
236 }
237
238 void
239 terminal_init_inferior (void)
240 {
241 #ifdef PROCESS_GROUP_TYPE
242   /* This is for Lynx, and should be cleaned up by having Lynx be a separate
243      debugging target with a version of target_terminal_init_inferior which
244      passes in the process group to a generic routine which does all the work
245      (and the non-threaded child_terminal_init_inferior can just pass in
246      inferior_ptid to the same routine).  */
247   /* We assume INFERIOR_PID is also the child's process group.  */
248   terminal_init_inferior_with_pgrp (PIDGET (inferior_ptid));
249 #endif /* PROCESS_GROUP_TYPE */
250 }
251
252 /* Put the inferior's terminal settings into effect.
253    This is preparation for starting or resuming the inferior.  */
254
255 void
256 terminal_inferior (void)
257 {
258   struct inferior *inf;
259
260   if (!terminal_is_ours)
261     return;
262
263   inf = current_inferior ();
264
265   if (gdb_has_a_terminal ()
266       && inf->terminal_info->ttystate != NULL
267       && inf->terminal_info->run_terminal == NULL)
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, inf->terminal_info->tflags);
276       result = fcntl (0, F_SETFL, inf->terminal_info->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                                      inf->terminal_info->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, inf->terminal_info->process_group);
311           if (!inf->attach_flag)
312             OOPSY ("tcsetpgrp");
313 #endif
314
315 #ifdef HAVE_SGTTY
316           result = ioctl (0, TIOCSPGRP, &inf->terminal_info->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
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
371   if (inf->terminal_info->run_terminal != NULL || gdb_has_a_terminal () == 0)
372     return;
373
374     {
375 #ifdef SIGTTOU
376       /* Ignore this signal since it will happen when we try to set the
377          pgrp.  */
378       void (*osigttou) () = NULL;
379 #endif
380       int result;
381
382 #ifdef SIGTTOU
383       if (job_control)
384         osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN);
385 #endif
386
387       xfree (inf->terminal_info->ttystate);
388       inf->terminal_info->ttystate = serial_get_tty_state (stdin_serial);
389
390 #ifdef PROCESS_GROUP_TYPE
391       if (!inf->attach_flag)
392         /* If setpgrp failed in terminal_inferior, this would give us
393            our process group instead of the inferior's.  See
394            terminal_inferior for details.  */
395         inf->terminal_info->process_group = gdb_getpgrp ();
396 #endif
397
398       /* Here we used to set ICANON in our ttystate, but I believe this
399          was an artifact from before when we used readline.  Readline sets
400          the tty state when it needs to.
401          FIXME-maybe: However, query() expects non-raw mode and doesn't
402          use readline.  Maybe query should use readline (on the other hand,
403          this only matters for HAVE_SGTTY, not termio or termios, I think).  */
404
405       /* Set tty state to our_ttystate.  We don't change in our out of raw
406          mode, to avoid flushing input.  We need to do the same thing
407          regardless of output_only, because we don't have separate
408          terminal_is_ours and terminal_is_ours_for_output flags.  It's OK,
409          though, since readline will deal with raw mode when/if it needs to.
410        */
411
412       serial_noflush_set_tty_state (stdin_serial, our_terminal_info.ttystate,
413                                     inf->terminal_info->ttystate);
414
415       if (job_control)
416         {
417 #ifdef HAVE_TERMIOS
418           result = tcsetpgrp (0, our_terminal_info.process_group);
419 #if 0
420           /* This fails on Ultrix with EINVAL if you run the testsuite
421              in the background with nohup, and then log out.  GDB never
422              used to check for an error here, so perhaps there are other
423              such situations as well.  */
424           if (result == -1)
425             fprintf_unfiltered (gdb_stderr, "[tcsetpgrp failed in terminal_ours: %s]\n",
426                                 safe_strerror (errno));
427 #endif
428 #endif /* termios */
429
430 #ifdef HAVE_SGTTY
431           result = ioctl (0, TIOCSPGRP, &our_terminal_info.process_group);
432 #endif
433         }
434
435 #ifdef SIGTTOU
436       if (job_control)
437         signal (SIGTTOU, osigttou);
438 #endif
439
440       if (!job_control)
441         {
442           signal (SIGINT, sigint_ours);
443 #ifdef SIGQUIT
444           signal (SIGQUIT, sigquit_ours);
445 #endif
446         }
447
448 #ifdef F_GETFL
449       inf->terminal_info->tflags = fcntl (0, F_GETFL, 0);
450
451       /* Is there a reason this is being done twice?  It happens both
452          places we use F_SETFL, so I'm inclined to think perhaps there
453          is some reason, however perverse.  Perhaps not though...  */
454       result = fcntl (0, F_SETFL, our_terminal_info.tflags);
455       result = fcntl (0, F_SETFL, our_terminal_info.tflags);
456 #endif
457     }
458 }
459
460 /* This is a "new_inferior" observer.  It's business is to allocate
461    the TERMINAL_INFO member of the inferior structure.  This field is
462    private to inflow.c, and its type is opaque to the rest of GDB.
463    PID is the target pid of the inferior that has just been added to
464    the inferior list.  */
465
466 static void
467 inflow_new_inferior (int pid)
468 {
469   struct inferior *inf = find_inferior_pid (pid);
470
471   inf->terminal_info = XZALLOC (struct terminal_info);
472 }
473
474 /* This is a "inferior_exit" observer.  Releases the TERMINAL_INFO member
475    of the inferior structure.  This field is private to inflow.c, and
476    its type is opaque to the rest of GDB.  PID is the target pid of
477    the inferior that is about to be removed from the inferior
478    list.  */
479
480 static void
481 inflow_inferior_exit (int pid)
482 {
483   struct inferior *inf = find_inferior_pid (pid);
484
485   xfree (inf->terminal_info->run_terminal);
486   xfree (inf->terminal_info);
487   inf->terminal_info = NULL;
488 }
489
490 void
491 copy_terminal_info (struct inferior *to, struct inferior *from)
492 {
493   *to->terminal_info = *from->terminal_info;
494   if (from->terminal_info->run_terminal)
495     to->terminal_info->run_terminal
496       = xstrdup (from->terminal_info->run_terminal);
497 }
498
499 void
500 term_info (char *arg, int from_tty)
501 {
502   target_terminal_info (arg, from_tty);
503 }
504
505 void
506 child_terminal_info (char *args, int from_tty)
507 {
508   struct inferior *inf;
509
510   if (!gdb_has_a_terminal ())
511     {
512       printf_filtered (_("This GDB does not control a terminal.\n"));
513       return;
514     }
515
516   if (ptid_equal (inferior_ptid, null_ptid))
517     return;
518
519   inf = current_inferior ();
520
521   printf_filtered (_("Inferior's terminal status (currently saved by GDB):\n"));
522
523   /* First the fcntl flags.  */
524   {
525     int flags;
526
527     flags = inf->terminal_info->tflags;
528
529     printf_filtered ("File descriptor flags = ");
530
531 #ifndef O_ACCMODE
532 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
533 #endif
534     /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
535     switch (flags & (O_ACCMODE))
536       {
537       case O_RDONLY:
538         printf_filtered ("O_RDONLY");
539         break;
540       case O_WRONLY:
541         printf_filtered ("O_WRONLY");
542         break;
543       case O_RDWR:
544         printf_filtered ("O_RDWR");
545         break;
546       }
547     flags &= ~(O_ACCMODE);
548
549 #ifdef O_NONBLOCK
550     if (flags & O_NONBLOCK)
551       printf_filtered (" | O_NONBLOCK");
552     flags &= ~O_NONBLOCK;
553 #endif
554
555 #if defined (O_NDELAY)
556     /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will
557        print it as O_NONBLOCK, which is good cause that is what POSIX
558        has, and the flag will already be cleared by the time we get here.  */
559     if (flags & O_NDELAY)
560       printf_filtered (" | O_NDELAY");
561     flags &= ~O_NDELAY;
562 #endif
563
564     if (flags & O_APPEND)
565       printf_filtered (" | O_APPEND");
566     flags &= ~O_APPEND;
567
568 #if defined (O_BINARY)
569     if (flags & O_BINARY)
570       printf_filtered (" | O_BINARY");
571     flags &= ~O_BINARY;
572 #endif
573
574     if (flags)
575       printf_filtered (" | 0x%x", flags);
576     printf_filtered ("\n");
577   }
578
579 #ifdef PROCESS_GROUP_TYPE
580   printf_filtered ("Process group = %d\n",
581                    (int) inf->terminal_info->process_group);
582 #endif
583
584   serial_print_tty_state (stdin_serial,
585                           inf->terminal_info->ttystate,
586                           gdb_stdout);
587 }
588 \f
589 /* NEW_TTY_PREFORK is called before forking a new child process,
590    so we can record the state of ttys in the child to be formed.
591    TTYNAME is null if we are to share the terminal with gdb;
592    or points to a string containing the name of the desired tty.
593
594    NEW_TTY is called in new child processes under Unix, which will
595    become debugger target processes.  This actually switches to
596    the terminal specified in the NEW_TTY_PREFORK call.  */
597
598 void
599 new_tty_prefork (const char *ttyname)
600 {
601   /* Save the name for later, for determining whether we and the child
602      are sharing a tty.  */
603   inferior_thisrun_terminal = ttyname;
604 }
605
606
607 /* If RESULT, assumed to be the return value from a system call, is
608    negative, print the error message indicated by errno and exit.
609    MSG should identify the operation that failed.  */
610 static void
611 check_syscall (const char *msg, int result)
612 {
613   if (result < 0)
614     {
615       print_sys_errmsg (msg, errno);
616       _exit (1);
617     }
618 }
619
620 void
621 new_tty (void)
622 {
623   int tty;
624
625   if (inferior_thisrun_terminal == 0)
626     return;
627 #if !defined(__GO32__) && !defined(_WIN32)
628 #ifdef TIOCNOTTY
629   /* Disconnect the child process from our controlling terminal.  On some
630      systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
631      ignore SIGTTOU. */
632   tty = open ("/dev/tty", O_RDWR);
633   if (tty > 0)
634     {
635       void (*osigttou) ();
636
637       osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN);
638       ioctl (tty, TIOCNOTTY, 0);
639       close (tty);
640       signal (SIGTTOU, osigttou);
641     }
642 #endif
643
644   /* Now open the specified new terminal.  */
645   tty = open (inferior_thisrun_terminal, O_RDWR | O_NOCTTY);
646   check_syscall (inferior_thisrun_terminal, tty);
647
648   /* Avoid use of dup2; doesn't exist on all systems.  */
649   if (tty != 0)
650     {
651       close (0);
652       check_syscall ("dup'ing tty into fd 0", dup (tty));
653     }
654   if (tty != 1)
655     {
656       close (1);
657       check_syscall ("dup'ing tty into fd 1", dup (tty));
658     }
659   if (tty != 2)
660     {
661       close (2);
662       check_syscall ("dup'ing tty into fd 2", dup (tty));
663     }
664
665 #ifdef TIOCSCTTY
666   /* Make tty our new controlling terminal.  */
667   if (ioctl (tty, TIOCSCTTY, 0) == -1)
668     /* Mention GDB in warning because it will appear in the inferior's
669        terminal instead of GDB's.  */
670     warning ("GDB: Failed to set controlling terminal: %s",
671              safe_strerror (errno));
672 #endif
673
674   if (tty > 2)
675     close (tty);
676 #endif /* !go32 && !win32 */
677 }
678
679 /* NEW_TTY_POSTFORK is called after forking a new child process, and
680    adding it to the inferior table, to store the TTYNAME being used by
681    the child, or null if it sharing the terminal with gdb.  */
682
683 void
684 new_tty_postfork (void)
685 {
686   /* Save the name for later, for determining whether we and the child
687      are sharing a tty.  */
688
689   if (inferior_thisrun_terminal)
690     current_inferior ()->terminal_info->run_terminal
691       = xstrdup (inferior_thisrun_terminal);
692
693   inferior_thisrun_terminal = NULL;
694 }
695
696 \f
697 /* Call set_sigint_trap when you need to pass a signal on to an attached
698    process when handling SIGINT */
699
700 static void
701 pass_signal (int signo)
702 {
703 #ifndef _WIN32
704   kill (PIDGET (inferior_ptid), SIGINT);
705 #endif
706 }
707
708 static void (*osig) ();
709 static int osig_set;
710
711 void
712 set_sigint_trap (void)
713 {
714   struct inferior *inf = current_inferior ();
715   if (inf->attach_flag || inf->terminal_info->run_terminal)
716     {
717       osig = (void (*)()) signal (SIGINT, pass_signal);
718       osig_set = 1;
719     }
720   else
721     osig_set = 0;
722 }
723
724 void
725 clear_sigint_trap (void)
726 {
727   if (osig_set)
728     {
729       signal (SIGINT, osig);
730       osig_set = 0;
731     }
732 }
733 \f
734
735 /* Create a new session if the inferior will run in a different tty.
736    A session is UNIX's way of grouping processes that share a controlling
737    terminal, so a new one is needed if the inferior terminal will be
738    different from GDB's.
739
740    Returns the session id of the new session, 0 if no session was created
741    or -1 if an error occurred.  */
742 pid_t
743 create_tty_session (void)
744 {
745 #ifdef HAVE_SETSID
746   pid_t ret;
747
748   if (!job_control || inferior_thisrun_terminal == 0)
749     return 0;
750
751   ret = setsid ();
752   if (ret == -1)
753     warning ("Failed to create new terminal session: setsid: %s",
754              safe_strerror (errno));
755
756   return ret;
757 #else
758   return 0;
759 #endif /* HAVE_SETSID */
760 }
761
762 /* This is here because this is where we figure out whether we (probably)
763    have job control.  Just using job_control only does part of it because
764    setpgid or setpgrp might not exist on a system without job control.
765    It might be considered misplaced (on the other hand, process groups and
766    job control are closely related to ttys).
767
768    For a more clean implementation, in libiberty, put a setpgid which merely
769    calls setpgrp and a setpgrp which does nothing (any system with job control
770    will have one or the other).  */
771 int
772 gdb_setpgid (void)
773 {
774   int retval = 0;
775
776   if (job_control)
777     {
778 #if defined (HAVE_TERMIOS) || defined (TIOCGPGRP)
779 #ifdef HAVE_SETPGID
780       /* The call setpgid (0, 0) is supposed to work and mean the same
781          thing as this, but on Ultrix 4.2A it fails with EPERM (and
782          setpgid (getpid (), getpid ()) succeeds).  */
783       retval = setpgid (getpid (), getpid ());
784 #else
785 #ifdef HAVE_SETPGRP
786 #ifdef SETPGRP_VOID 
787       retval = setpgrp ();
788 #else
789       retval = setpgrp (getpid (), getpid ());
790 #endif
791 #endif /* HAVE_SETPGRP */
792 #endif /* HAVE_SETPGID */
793 #endif /* defined (HAVE_TERMIOS) || defined (TIOCGPGRP) */
794     }
795
796   return retval;
797 }
798
799 /* Get all the current tty settings (including whether we have a
800    tty at all!).  We can't do this in _initialize_inflow because
801    serial_fdopen() won't work until the serial_ops_list is
802    initialized, but we don't want to do it lazily either, so
803    that we can guarantee stdin_serial is opened if there is
804    a terminal.  */
805 void
806 initialize_stdin_serial (void)
807 {
808   stdin_serial = serial_fdopen (0);
809 }
810
811 void
812 _initialize_inflow (void)
813 {
814   add_info ("terminal", term_info,
815             _("Print inferior's saved terminal status."));
816
817   terminal_is_ours = 1;
818
819   /* OK, figure out whether we have job control.  If neither termios nor
820      sgtty (i.e. termio or go32), leave job_control 0.  */
821
822 #if defined (HAVE_TERMIOS)
823   /* Do all systems with termios have the POSIX way of identifying job
824      control?  I hope so.  */
825 #ifdef _POSIX_JOB_CONTROL
826   job_control = 1;
827 #else
828 #ifdef _SC_JOB_CONTROL
829   job_control = sysconf (_SC_JOB_CONTROL);
830 #else
831   job_control = 0;              /* have to assume the worst */
832 #endif /* _SC_JOB_CONTROL */
833 #endif /* _POSIX_JOB_CONTROL */
834 #endif /* HAVE_TERMIOS */
835
836 #ifdef HAVE_SGTTY
837 #ifdef TIOCGPGRP
838   job_control = 1;
839 #else
840   job_control = 0;
841 #endif /* TIOCGPGRP */
842 #endif /* sgtty */
843
844   observer_attach_new_inferior (inflow_new_inferior);
845   observer_attach_inferior_exit (inflow_inferior_exit);
846 }