bfd/
[deliverable/binutils-gdb.git] / gdb / inflow.c
index f7bf7d12e3038a237c8f83204425cda508c31cc2..6a775316c84b8a7a69e5b5f504ea823241c41f59 100644 (file)
@@ -98,6 +98,24 @@ static const char *inferior_thisrun_terminal;
 
 int terminal_is_ours;
 
+#ifdef PROCESS_GROUP_TYPE
+static PROCESS_GROUP_TYPE
+gdb_getpgrp (void)
+{
+  int process_group = -1;
+#ifdef HAVE_TERMIOS
+  process_group = tcgetpgrp (0);
+#endif
+#ifdef HAVE_TERMIO
+  process_group = getpgrp ();
+#endif
+#ifdef HAVE_SGTTY
+  ioctl (0, TIOCGPGRP, &process_group);
+#endif
+  return process_group;
+}
+#endif
+
 enum
   {
     yes, no, have_not_checked
@@ -132,14 +150,8 @@ gdb_has_a_terminal (void)
          if (our_ttystate != NULL)
            {
              gdb_has_a_terminal_flag = yes;
-#ifdef HAVE_TERMIOS
-             our_process_group = tcgetpgrp (0);
-#endif
-#ifdef HAVE_TERMIO
-             our_process_group = getpgrp ();
-#endif
-#ifdef HAVE_SGTTY
-             ioctl (0, TIOCGPGRP, &our_process_group);
+#ifdef PROCESS_GROUP_TYPE
+             our_process_group = gdb_getpgrp ();
 #endif
            }
        }
@@ -339,14 +351,13 @@ terminal_ours_1 (int output_only)
       if (inferior_ttystate)
        xfree (inferior_ttystate);
       inferior_ttystate = serial_get_tty_state (stdin_serial);
-#ifdef HAVE_TERMIOS
-      inferior_process_group = tcgetpgrp (0);
-#endif
-#ifdef HAVE_TERMIO
-      inferior_process_group = getpgrp ();
-#endif
-#ifdef HAVE_SGTTY
-      ioctl (0, TIOCGPGRP, &inferior_process_group);
+
+#ifdef PROCESS_GROUP_TYPE
+      if (!attach_flag)
+       /* If setpgrp failed in terminal_inferior, this would give us
+          our process group instead of the inferior's.  See
+          terminal_inferior for details.  */
+       inferior_process_group = gdb_getpgrp ();
 #endif
 
       /* Here we used to set ICANON in our ttystate, but I believe this
@@ -557,6 +568,16 @@ new_tty (void)
       close (2);
       dup (tty);
     }
+
+#ifdef TIOCSCTTY
+  /* Make tty our new controlling terminal.  */
+  if (ioctl (tty, TIOCSCTTY, 0) == -1)
+    /* Mention GDB in warning because it will appear in the inferior's
+       terminal instead of GDB's.  */
+    warning ("GDB: Failed to set controlling terminal: %s",
+            safe_strerror (errno));
+#endif
+
   if (tty > 2)
     close (tty);
 #endif /* !go32 && !win32 */
@@ -683,6 +704,33 @@ clear_sigio_trap (void)
 #endif /* No SIGIO.  */
 \f
 
+/* Create a new session if the inferior will run in a different tty.
+   A session is UNIX's way of grouping processes that share a controlling
+   terminal, so a new one is needed if the inferior terminal will be
+   different from GDB's.
+
+   Returns the session id of the new session, 0 if no session was created
+   or -1 if an error occurred.  */
+pid_t
+create_tty_session (void)
+{
+#ifdef HAVE_SETSID
+  pid_t ret;
+
+  if (!job_control || inferior_thisrun_terminal == 0)
+    return 0;
+
+  ret = setsid ();
+  if (ret == -1)
+    warning ("Failed to create new terminal session: setsid: %s",
+            safe_strerror (errno));
+
+  return ret;
+#else
+  return 0;
+#endif /* HAVE_SETSID */
+}
+
 /* This is here because this is where we figure out whether we (probably)
    have job control.  Just using job_control only does part of it because
    setpgid or setpgrp might not exist on a system without job control.
This page took 0.025091 seconds and 4 git commands to generate.